Skip to content

Commit

Permalink
Add a test of the clipboard features
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Dec 29, 2024
1 parent 7836cfc commit 528a0bf
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/egui_demo_lib/src/demo/demo_app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl Default for DemoGroups {
Box::<super::window_options::WindowOptions>::default(),
]),
tests: DemoGroup::new(vec![
Box::<super::tests::ClipboardTest>::default(),
Box::<super::tests::CursorTest>::default(),
Box::<super::tests::GridTest>::default(),
Box::<super::tests::IdTest>::default(),
Expand Down
78 changes: 78 additions & 0 deletions crates/egui_demo_lib/src/demo/tests/clipboard_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
pub struct ClipboardTest {
text: String,
}

impl Default for ClipboardTest {
fn default() -> Self {
Self {
text: "Example text you can copy-and-paste".to_owned(),
}
}
}

impl crate::Demo for ClipboardTest {
fn name(&self) -> &'static str {
"Clipboard Test"
}

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| {
use crate::View as _;
self.ui(ui);
});
}
}

impl crate::View for ClipboardTest {
fn ui(&mut self, ui: &mut egui::Ui) {
ui.label("egui integrates with the system clipboard.");
ui.label("Try copy-cut-pasting text in the text edit below.");

let text_edit_response = ui
.horizontal(|ui| {
let text_edit_response = ui.text_edit_singleline(&mut self.text);
if ui.button("📋").clicked() {
ui.ctx().copy_text(self.text.clone());
}
text_edit_response
})
.inner;

ui.horizontal(|ui| {
for (name, cmd) in [
("Copy", egui::ViewportCommand::RequestCopy),
("Cut", egui::ViewportCommand::RequestCut),
("Paste", egui::ViewportCommand::RequestPaste),
] {
if ui.button(name).clicked() {
// Next frame we should get a copy/cut/paste-event…
ui.ctx().send_viewport_cmd(cmd);

// …that should en up here:
text_edit_response.request_focus();
}
}
});

ui.separator();

ui.label("You can also copy images:");
ui.horizontal(|ui| {
let image_source = egui::include_image!("../../../data/icon.png");
let uri = image_source.uri().unwrap().to_owned();
ui.image(image_source);

if let Ok(egui::load::ImagePoll::Ready { image }) =
ui.ctx().try_load_image(&uri, Default::default())
{
if ui.button("📋").clicked() {
ui.ctx().copy_image((*image).clone());
}
}
});

ui.vertical_centered_justified(|ui| {
ui.add(crate::egui_github_link_file!());
});
}
}
2 changes: 2 additions & 0 deletions crates/egui_demo_lib/src/demo/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod clipboard_test;
mod cursor_test;
mod grid_test;
mod id_test;
Expand All @@ -7,6 +8,7 @@ mod layout_test;
mod manual_layout_test;
mod window_resize_test;

pub use clipboard_test::ClipboardTest;
pub use cursor_test::CursorTest;
pub use grid_test::GridTest;
pub use id_test::IdTest;
Expand Down

0 comments on commit 528a0bf

Please sign in to comment.