The clipboard
Os::clipboard reads and writes the text on the system clipboard from a handler.
Reading and writing text
impl Os {
pub fn clipboard(&self) -> Clipboard<'_>;
}
impl Clipboard<'_> {
pub async fn read_text(&self) -> Result<Option<String>, Error>;
pub async fn write_text(&self, text: impl Into<String>) -> Result<(), Error>;
}Os::clipboard returns a Clipboard that borrows the Os. It is Copy, and a handler usually calls one method on it right away, as in state.os.clipboard().write_text(text).await. Both methods handle text only.
| Call | Returns |
|---|---|
read_text | Ok(Some(text)) with the clipboard's text |
read_text | Ok(None) when the clipboard holds no text |
write_text | Ok(()) once text is on the clipboard |
| either | Err whose kind() is ErrorKind::Clipboard when the system clipboard cannot be opened, read, or written |
The error is an Error, whose Display names the cause, and ErrorKind::Clipboard tells it apart from the other kinds. A clipboard without text is Ok(None) rather than an error, so a paste handler answers it with a message of its own.
Copy and paste buttons
The clipboard is read and written by the handler, in the application's process. The page's text reaches the handler as the action's argument, COPY.call_with(Editor::text()), and the clipboard's text reaches the page as the patch the handler returns. The page itself does not read or write the clipboard. The Os is reached the same way as for a dialog, from the router's state:
#[derive(Signals, Serialize, Deserialize)]
struct Editor {
text: String,
status: String,
}
#[derive(Clone)]
struct AppState {
os: Os,
}
hypersig::routes! {
fn actions(Router<AppState>);
action COPY: Action<Editor, String> = post "/copy" => copy;
action PASTE: Action<Editor> = post "/paste" => paste;
}
fn editor() -> Page<Editor> {
Page::new("Editor", Editor { text: String::new(), status: String::new() }).child(
div()
.child(textarea().bind(Editor::text()))
.child(button().on_click(COPY.call_with(Editor::text())).text("Copy"))
.child(button().on_click(PASTE.call()).text("Paste"))
.child(p().bind_text(Editor::status())),
)
}
/// Puts the text the page sent on the clipboard.
async fn copy(state: AppState, text: String) -> Patches<Editor> {
match state.os.clipboard().write_text(text).await {
Ok(()) => Editor::status().patch("Copied.".to_owned()).into(),
Err(error) => Editor::status().patch(format!("Not copied: {error}")).into(),
}
}
/// Replaces the page's text with the clipboard's.
async fn paste(state: AppState, (): ()) -> Patches<Editor> {
match state.os.clipboard().read_text().await {
Ok(Some(text)) => Editor::text().patch(text).into(),
Ok(None) => Editor::status().patch("The clipboard holds no text.".to_owned()).into(),
Err(error) => Editor::status().patch(format!("Not pasted: {error}")).into(),
}
}examples/notes has the same Copy button. In a test, FakeOs keeps a clipboard of its own: set_clipboard_text puts text on it as another program would, and clipboard_text returns what the handler wrote.
Text written on Linux
On Linux, under X11 and Wayland alike, the text on the clipboard is served by the program that wrote it, so another program reads it from that program when it pastes. hypersig_desktop keeps the clipboard open from the first call until the application closes, so text written once stays available for the whole run. Once the application has closed, that text may no longer be available to other programs.
Next
- Testing without a window: the fake clipboard of
FakeOs, read and written by a test. - Links to other sites:
Os::open_externaland what a window does with a link off the application.