Expand description
Clipboard service, commands and other types.
This module provides the CLIPBOARD service and clipboard related commands and command handlers.
The service does not implement the commands, widgets implement the commands and optionally use the service.
Note that the CLIPBOARD service uses the view-process the interact with the system clipboard, so it will only
work if a headed app or headless app with renderer is running.
§Text
The example below uses the service to copy text to the clipboard:
use zng::prelude::*;
let txt = var(Txt::from(""));
let copied = var(false);
Container! {
child = TextInput!(txt.clone());
child_spacing = 5;
child_end = Button! {
child = Text!(copied.map(|&c| if !c { "Copy" } else { "Copied!" }.into()));
on_click = async_hn!(txt, copied, |_| {
if zng::clipboard::CLIPBOARD.set_text(txt.get()).wait_rsp().await.is_ok() {
copied.set(true);
}
});
};
}The TextInput widget also implements the clipboard commands, the example below requests clipboard paste to the
text input, that widget uses the clipboard service to get the text.
use zng::prelude::*;
Container! {
child = TextInput! {
id = "input-1";
txt = var(Txt::from(""));
};
child_spacing = 4;
child_end = Button!(zng::clipboard::PASTE_CMD.scoped(WidgetId::named("input-1")));
}§File List
The example below modifies the paste button to paste file paths, the paths can be used to read or move the each file, in the example they are converted to a text list.
use zng::prelude::*;
Button! {
child = Text!("Paste");
on_click = hn!(|_| {
if let Ok(Some(f)) = zng::clipboard::CLIPBOARD.file_list() {
txt.modify(move |txt| {
let txt = txt.to_mut();
txt.clear();
for f in f {
use std::fmt::Write as _;
let _ = writeln!(txt, "{}", f.display());
}
});
}
});
}§Image
The example below pastes an image from the clipboard. The example also demonstrates how to separate the paste button from the paste action, the button only needs to know that the window handles the paste command, the window implements the paste by setting an image variable.
use zng::clipboard;
use zng::prelude::*;
let img_source = var(ImageSource::flood(layout::PxSize::splat(layout::Px(1)), colors::BLACK, None));
Window! {
child_top = Button!(clipboard::PASTE_CMD.scoped(WINDOW.id()));
child = Image!(img_source.clone());
clipboard::on_paste = hn!(|_| {
if let Ok(Some(img)) = clipboard::CLIPBOARD.image() {
img_source.set(img);
}
});
}§Full API
See zng_ext_clipboard for the full clipboard API.
Structs§
- CLIPBOARD
- Clipboard service.
Enums§
- Clipboard
Error - Error getting or setting the clipboard.
Statics§
- COPY_
CMD - Represents the clipboard copy action.
- CUT_CMD
- Represents the clipboard cut action.
- PASTE_
CMD - Represents the clipboard paste action.
Functions§
- can_
copy PDefines ifon_copyandon_pre_copycommand handles are enabled in the context.- can_cut
PDefines ifon_cutandon_pre_cutcommand handles are enabled in the context.- can_
paste PDefines ifon_pasteandon_pre_pastecommand handles are enabled in the context.- on_copy
POn copy command.- on_cut
POn cut command.- on_
paste POn paste command.- on_
pre_ copy PPreviewon_copycommand.- on_
pre_ cut PPreviewon_cutcommand.- on_
pre_ paste PPreviewon_pastecommand.