Module clipboard

Module clipboard 

Source
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§

ClipboardError
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
P Defines if on_copy and on_pre_copy command handles are enabled in the context.
can_cut
P Defines if on_cut and on_pre_cut command handles are enabled in the context.
can_paste
P Defines if on_paste and on_pre_paste command handles are enabled in the context.
on_copy
P On copy command.
on_cut
P On cut command.
on_paste
P On paste command.
on_pre_copy
P Preview on_copy command.
on_pre_cut
P Preview on_cut command.
on_pre_paste
P Preview on_paste command.