diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index aafe0cf5b93..5440fccf565 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -927,6 +927,19 @@ impl Context { self.output_mut(|o| o.open_url = Some(open_url)); } + /// Copy the given text to the system clipboard. + /// + /// Empty strings are ignored. + /// + /// Equivalent to: + /// ``` + /// # let ctx = egui::Context::default(); + /// ctx.output_mut(|o| o.copied_text = "Copy this".to_owned(); + /// ``` + pub fn copy_text(&self, text: String) { + self.output_mut(|o| o.copied_text = text); + } + /// Format the given shortcut in a human-readable way (e.g. `Ctrl+Shift+X`). /// /// Can be used to get the text for [`Button::shortcut_text`]. diff --git a/crates/egui/src/widgets/color_picker.rs b/crates/egui/src/widgets/color_picker.rs index fcb701f47fa..aed954793e7 100644 --- a/crates/egui/src/widgets/color_picker.rs +++ b/crates/egui/src/widgets/color_picker.rs @@ -234,9 +234,9 @@ fn color_text_ui(ui: &mut Ui, color: impl Into, alpha: Alpha) { if ui.button("📋").on_hover_text("Click to copy").clicked() { if alpha == Alpha::Opaque { - ui.output_mut(|o| o.copied_text = format!("{r}, {g}, {b}")); + ui.ctx().copy_text(format!("{r}, {g}, {b}")); } else { - ui.output_mut(|o| o.copied_text = format!("{r}, {g}, {b}, {a}")); + ui.ctx().copy_text(format!("{r}, {g}, {b}, {a}")); } } diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index e1969897718..f07beedea19 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -900,7 +900,7 @@ fn events( let copy_if_not_password = |ui: &Ui, text: String| { if !password { - ui.ctx().output_mut(|o| o.copied_text = text); + ui.ctx().copy_text(text); } }; diff --git a/crates/egui_demo_app/src/apps/http_app.rs b/crates/egui_demo_app/src/apps/http_app.rs index ef5c2987af3..90be24dd280 100644 --- a/crates/egui_demo_app/src/apps/http_app.rs +++ b/crates/egui_demo_app/src/apps/http_app.rs @@ -195,7 +195,7 @@ fn ui_resource(ui: &mut egui::Ui, resource: &Resource) { if let Some(text) = &text { let tooltip = "Click to copy the response body"; if ui.button("📋").on_hover_text(tooltip).clicked() { - ui.output_mut(|o| o.copied_text = text.clone()); + ui.ctx().copy_text(text.clone()); } ui.separator(); } diff --git a/crates/egui_demo_lib/src/demo/font_book.rs b/crates/egui_demo_lib/src/demo/font_book.rs index 2b4eebae019..4dda2d87b48 100644 --- a/crates/egui_demo_lib/src/demo/font_book.rs +++ b/crates/egui_demo_lib/src/demo/font_book.rs @@ -93,7 +93,7 @@ impl super::View for FontBook { }; if ui.add(button).on_hover_ui(tooltip_ui).clicked() { - ui.output_mut(|o| o.copied_text = chr.to_string()); + ui.ctx().copy_text(chr.to_string()); } } } diff --git a/examples/puffin_profiler/src/main.rs b/examples/puffin_profiler/src/main.rs index ab0007156cb..18b7f6e16f2 100644 --- a/examples/puffin_profiler/src/main.rs +++ b/examples/puffin_profiler/src/main.rs @@ -28,7 +28,7 @@ impl eframe::App for MyApp { ui.horizontal(|ui| { ui.monospace(cmd); if ui.small_button("📋").clicked() { - ui.output_mut(|o| o.copied_text = cmd.into()); + ui.ctx().copy_text(cmd.into()); } });