This commit is contained in:
Jovi Hsu 2021-11-29 08:17:39 +08:00
parent 4d63056d07
commit da807ead3e
4 changed files with 31 additions and 33 deletions

View File

@ -1,3 +1,3 @@
pub mod page_home; pub mod page_home;
pub mod page_not_found; pub mod page_not_found;
pub mod page_remote; pub mod page_vnc;

View File

@ -14,7 +14,7 @@ impl Component for PageHome {
fn view(&self) -> Html { fn view(&self) -> Html {
html! { html! {
<crate::pages::page_remote::PageRemote/> <crate::pages::page_vnc::PageVnc/>
} }
} }

View File

@ -19,7 +19,7 @@ use crate::{
utils::WeakComponentLink, utils::WeakComponentLink,
}; };
pub struct PageRemote { pub struct PageVnc {
link: ComponentLink<Self>, link: ComponentLink<Self>,
target: (String, u16), target: (String, u16),
error_msg: String, error_msg: String,
@ -38,9 +38,9 @@ pub struct PageRemote {
} }
#[derive(Clone, PartialEq, Properties)] #[derive(Clone, PartialEq, Properties)]
pub struct RemoteProps {} pub struct VncProps {}
pub enum RemoteMsg { pub enum VncMsg {
Connect((String, u16)), Connect((String, u16)),
ConnectResp(Result<Value, anyhow::Error>), ConnectResp(Result<Value, anyhow::Error>),
Connected, Connected,
@ -53,12 +53,12 @@ pub enum RemoteMsg {
RequireFrame(u8), RequireFrame(u8),
} }
impl Component for PageRemote { impl Component for PageVnc {
type Message = RemoteMsg; type Message = VncMsg;
type Properties = RemoteProps; type Properties = VncProps;
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self { fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
PageRemote { PageVnc {
link, link,
target: (String::from(""), 0), target: (String::from(""), 0),
error_msg: String::from(""), error_msg: String::from(""),
@ -79,7 +79,7 @@ impl Component for PageRemote {
fn update(&mut self, msg: Self::Message) -> ShouldRender { fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg { match msg {
RemoteMsg::Connect(target) => { VncMsg::Connect(target) => {
self.target = target; self.target = target;
// ConsoleService::log(&self.target); // ConsoleService::log(&self.target);
let to_post = json!({ let to_post = json!({
@ -98,7 +98,7 @@ impl Component for PageRemote {
.callback(|response: Response<Json<Result<Value, anyhow::Error>>>| { .callback(|response: Response<Json<Result<Value, anyhow::Error>>>| {
// ConsoleService::error(&format!("{:?}", response)); // ConsoleService::error(&format!("{:?}", response));
let Json(data) = response.into_body(); let Json(data) = response.into_body();
RemoteMsg::ConnectResp(data) VncMsg::ConnectResp(data)
}); });
// 3. pass the request and callback to the fetch service // 3. pass the request and callback to the fetch service
let task = FetchService::fetch(request, callback).expect("failed to start request"); let task = FetchService::fetch(request, callback).expect("failed to start request");
@ -106,12 +106,12 @@ impl Component for PageRemote {
self.fetch_task = Some(task); self.fetch_task = Some(task);
true true
} }
RemoteMsg::ConnectResp(response) => { VncMsg::ConnectResp(response) => {
if let Ok(response) = response { if let Ok(response) = response {
self.error_msg = response["status"].to_string(); self.error_msg = response["status"].to_string();
if "\"success\"" == self.error_msg { if "\"success\"" == self.error_msg {
self.link.send_message(RemoteMsg::Connected); self.link.send_message(VncMsg::Connected);
} else { } else {
self.error_msg = response["message"].to_string(); self.error_msg = response["message"].to_string();
} }
@ -123,15 +123,15 @@ impl Component for PageRemote {
self.fetch_task = None; self.fetch_task = None;
true true
} }
RemoteMsg::Connected => { VncMsg::Connected => {
self.connected = true; self.connected = true;
true true
} }
RemoteMsg::Recv(v) => { VncMsg::Recv(v) => {
self.handler.do_input(v); self.handler.do_input(v);
self.protocal_out_handler() self.protocal_out_handler()
} }
RemoteMsg::Send(v) => { VncMsg::Send(v) => {
self.websocket self.websocket
.borrow() .borrow()
.as_ref() .as_ref()
@ -139,31 +139,31 @@ impl Component for PageRemote {
.send_message(WebsocketMsg::Send(Ok(v))); .send_message(WebsocketMsg::Send(Ok(v)));
false false
} }
RemoteMsg::UpdateUsername(username) => { VncMsg::UpdateUsername(username) => {
self.username = username; self.username = username;
true true
} }
RemoteMsg::UpdatePassword(password) => { VncMsg::UpdatePassword(password) => {
self.password = password; self.password = password;
true true
} }
RemoteMsg::SendCredential => { VncMsg::SendCredential => {
self.request_username = false; self.request_username = false;
self.request_password = false; self.request_password = false;
self.handler.set_credential(&self.username, &self.password); self.handler.set_credential(&self.username, &self.password);
self.protocal_out_handler() self.protocal_out_handler()
} }
RemoteMsg::RequireFrame(incremental) => { VncMsg::RequireFrame(incremental) => {
self.handler.require_frame(incremental); self.handler.require_frame(incremental);
if self.interval.is_none() { if self.interval.is_none() {
let link = self.link.clone(); let link = self.link.clone();
let tick = let tick =
Interval::new(20, move || link.send_message(RemoteMsg::RequireFrame(1))); Interval::new(20, move || link.send_message(VncMsg::RequireFrame(1)));
self.interval = Some(tick); self.interval = Some(tick);
} }
self.protocal_out_handler() self.protocal_out_handler()
} }
RemoteMsg::UpdateClipboard(clipboard) => { VncMsg::UpdateClipboard(clipboard) => {
if clipboard.len() > 0 { if clipboard.len() > 0 {
self.handler.set_clipboard(&clipboard); self.handler.set_clipboard(&clipboard);
self.protocal_out_handler() self.protocal_out_handler()
@ -180,7 +180,7 @@ impl Component for PageRemote {
fn view(&self) -> Html { fn view(&self) -> Html {
if !self.connected { if !self.connected {
let connect_remote = self.link.callback(RemoteMsg::Connect); let connect_remote = self.link.callback(VncMsg::Connect);
html! { html! {
<> <>
<components::host::Host onsubmit=connect_remote/> <components::host::Host onsubmit=connect_remote/>
@ -188,8 +188,8 @@ impl Component for PageRemote {
</> </>
} }
} else { } else {
let recv_msg = self.link.callback(RemoteMsg::Recv); let recv_msg = self.link.callback(VncMsg::Recv);
let clipboard_update = self.link.callback(RemoteMsg::UpdateClipboard); let clipboard_update = self.link.callback(VncMsg::UpdateClipboard);
let websocket = &self.websocket; let websocket = &self.websocket;
let clipboard = &self.clipboard; let clipboard = &self.clipboard;
html! { html! {
@ -218,7 +218,7 @@ impl Component for PageRemote {
} }
// impl PageRemote // impl PageRemote
impl PageRemote { impl PageVnc {
fn protocal_out_handler(&mut self) -> ShouldRender { fn protocal_out_handler(&mut self) -> ShouldRender {
let out = self.handler.get_output(); let out = self.handler.get_output();
let mut should_render = false; let mut should_render = false;
@ -236,7 +236,7 @@ impl PageRemote {
} }
ProtocalHandlerOutput::WsBuf(out) => { ProtocalHandlerOutput::WsBuf(out) => {
if out.len() > 0 { if out.len() > 0 {
self.link.send_message(RemoteMsg::Send(out)); self.link.send_message(VncMsg::Send(out));
} }
} }
ProtocalHandlerOutput::RequirePassword => { ProtocalHandlerOutput::RequirePassword => {
@ -274,7 +274,7 @@ impl PageRemote {
canvas.set_width(width as u32); canvas.set_width(width as u32);
canvas.set_height(height as u32); canvas.set_height(height as u32);
self.bind_mouse_and_key(&canvas); self.bind_mouse_and_key(&canvas);
self.link.send_message(RemoteMsg::RequireFrame(0)); self.link.send_message(VncMsg::RequireFrame(0));
let ctx = match &self.canvas_ctx { let ctx = match &self.canvas_ctx {
Some(ctx) => ctx, Some(ctx) => ctx,
None => { None => {
@ -305,7 +305,7 @@ impl PageRemote {
fn username_view(&self) -> Html { fn username_view(&self) -> Html {
if self.request_username { if self.request_username {
let update_username = self.link.callback(RemoteMsg::UpdateUsername); let update_username = self.link.callback(VncMsg::UpdateUsername);
html! { html! {
<> <>
<Input id="username" type_="text" placeholder="username" on_change={update_username}/> <Input id="username" type_="text" placeholder="username" on_change={update_username}/>
@ -319,7 +319,7 @@ impl PageRemote {
fn password_view(&self) -> Html { fn password_view(&self) -> Html {
if self.request_password { if self.request_password {
let update_password = self.link.callback(RemoteMsg::UpdatePassword); let update_password = self.link.callback(VncMsg::UpdatePassword);
html! { html! {
<> <>
<Input id="password" type_="password" placeholder="password" on_change={update_password}/> <Input id="password" type_="password" placeholder="password" on_change={update_password}/>
@ -333,7 +333,7 @@ impl PageRemote {
fn button_connect_view(&self) -> Html { fn button_connect_view(&self) -> Html {
if self.request_username || self.request_password { if self.request_username || self.request_password {
let send_credential = self.link.callback(|_| RemoteMsg::SendCredential); let send_credential = self.link.callback(|_| VncMsg::SendCredential);
html! { html! {
<> <>
<button type="submit" onclick={send_credential}>{"Connect"}</button> <button type="submit" onclick={send_credential}>{"Connect"}</button>

View File

@ -3,8 +3,6 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(non_upper_case_globals)] #![allow(non_upper_case_globals)]
// referring: // referring:
// https://github.com/AltF02/x11-rs/blob/master/src/keysym.rs // https://github.com/AltF02/x11-rs/blob/master/src/keysym.rs