1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Add Context::copy_image (#5533)

* Closes https://github.com/emilk/egui/issues/5424

This adds support for copying images to the system clipboard on native
and on web using `Context::copy_image`.
This commit is contained in:
Emil Ernerfeldt
2024-12-29 18:03:32 +01:00
committed by GitHub
parent e2c7e9e733
commit bf6ed3adfc
15 changed files with 285 additions and 41 deletions

View File

@@ -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(),

View File

@@ -0,0 +1,81 @@
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;
if !cfg!(target_arch = "wasm32") {
// These commands are not yet implemented on web
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!());
});
}
}

View File

@@ -1,3 +1,4 @@
mod clipboard_test;
mod cursor_test;
mod grid_test;
mod id_test;
@@ -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;

View File

@@ -1,7 +1,7 @@
use std::collections::HashMap;
use egui::{
emath::GuiRounding, epaint, lerp, pos2, vec2, widgets::color_picker::show_color, Align2,
emath::GuiRounding as _, epaint, lerp, pos2, vec2, widgets::color_picker::show_color, Align2,
Color32, FontId, Image, Mesh, Pos2, Rect, Response, Rgba, RichText, Sense, Shape, Stroke,
TextureHandle, TextureOptions, Ui, Vec2,
};