Skip to content

Commit

Permalink
Add Context::copy_text
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 24, 2023
1 parent 11975a2 commit 8cd1a5e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
13 changes: 13 additions & 0 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/widgets/color_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ fn color_text_ui(ui: &mut Ui, color: impl Into<Color32>, 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}"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_app/src/apps/http_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/font_book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/puffin_profiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});

Expand Down

0 comments on commit 8cd1a5e

Please sign in to comment.