From 6ac6a0a5224810c6f98b58d87ebf7871a2e8d15c Mon Sep 17 00:00:00 2001 From: Jovi Hsu Date: Sat, 27 Nov 2021 16:17:07 +0800 Subject: [PATCH] Add a simple clipboard --- frontend/src/components/clipboard.rs | 69 ++++++++++++++++++++++++ frontend/src/components/mod.rs | 1 + frontend/src/pages/page_remote.rs | 41 ++++++++++---- frontend/src/protocal/common.rs | 11 ++-- frontend/src/protocal/vnc/vnc.rs | 60 ++++++++++++++------- frontend/src/protocal/vnc/x11keyboard.rs | 6 +-- 6 files changed, 154 insertions(+), 34 deletions(-) create mode 100644 frontend/src/components/clipboard.rs diff --git a/frontend/src/components/clipboard.rs b/frontend/src/components/clipboard.rs new file mode 100644 index 0000000..3cb6110 --- /dev/null +++ b/frontend/src/components/clipboard.rs @@ -0,0 +1,69 @@ +use yew::prelude::*; + +use crate::utils::WeakComponentLink; + +pub enum ClipboardMsg { + UpdateClipboard(String), + SendClipboard, +} + +pub struct Clipboard { + link: ComponentLink, + onsubmit: Callback, + text: String, +} + +// Props +#[derive(Clone, PartialEq, Properties)] +pub struct ClipboardProps { + #[prop_or_default] + pub weak_link: WeakComponentLink, + pub onsubmit: Callback, +} + +impl Component for Clipboard { + type Message = ClipboardMsg; + type Properties = ClipboardProps; + + fn create(props: Self::Properties, link: ComponentLink) -> Self { + props.weak_link.borrow_mut().replace(link.clone()); + Clipboard { + link, + onsubmit: props.onsubmit, + text: String::new(), + } + } + + fn update(&mut self, msg: Self::Message) -> ShouldRender { + match msg { + ClipboardMsg::UpdateClipboard(text) => { + self.text = text; + } + ClipboardMsg::SendClipboard => { + self.onsubmit.emit(self.text.clone()); + self.text.clear(); + } + } + true + } + + fn change(&mut self, _props: Self::Properties) -> ShouldRender { + false + } + + fn view(&self) -> Html { + let update_clipboard = self.link.callback(|e: ChangeData| match e { + ChangeData::Value(v) => ClipboardMsg::UpdateClipboard(v), + _ => panic!("unexpected message"), + }); + let set_clipboard = self.link.callback(|_| ClipboardMsg::SendClipboard); + html! { + <> +