rustdesk/src/ui/cm.rs

160 lines
4.1 KiB
Rust
Raw Normal View History

#[cfg(target_os = "linux")]
use crate::ipc::start_pa;
use crate::ui_cm_interface::{start_ipc, ConnectionManager, InvokeUiCM};
use hbb_common::{allow_err, log};
use sciter::{make_args, Element, Value, HELEMENT};
use std::sync::Mutex;
use std::{ops::Deref, sync::Arc};
2021-03-29 15:59:14 +08:00
#[derive(Clone, Default)]
pub struct SciterHandler {
pub element: Arc<Mutex<Option<Element>>>,
2021-03-29 15:59:14 +08:00
}
impl InvokeUiCM for SciterHandler {
fn add_connection(&self, client: &crate::ui_cm_interface::Client) {
2021-03-29 15:59:14 +08:00
self.call(
"addConnection",
&make_args!(
client.id,
client.is_file_transfer,
client.port_forward.clone(),
client.peer_id.clone(),
client.name.clone(),
client.authorized,
client.keyboard,
client.clipboard,
client.audio,
client.file,
client.restart,
client.recording
2021-03-29 15:59:14 +08:00
),
);
}
fn remove_connection(&self, id: i32, close: bool) {
self.call("removeConnection", &make_args!(id, close));
if crate::ui_cm_interface::get_clients_length().eq(&0) {
crate::platform::quit_gui();
2022-05-02 00:02:41 +08:00
}
2021-03-29 15:59:14 +08:00
}
fn new_message(&self, id: i32, text: String) {
self.call("newMessage", &make_args!(id, text));
2021-03-29 15:59:14 +08:00
}
fn change_theme(&self, _dark: String) {
// TODO
}
fn change_language(&self) {
// TODO
}
fn show_elevation(&self, show: bool) {
self.call("showElevation", &make_args!(show));
}
}
2021-03-29 15:59:14 +08:00
impl SciterHandler {
#[inline]
fn call(&self, func: &str, args: &[Value]) {
if let Some(e) = self.element.lock().unwrap().as_ref() {
allow_err!(e.call_method(func, &super::value_crash_workaround(args)[..]));
2021-03-29 15:59:14 +08:00
}
}
}
2021-03-29 15:59:14 +08:00
pub struct SciterConnectionManager(ConnectionManager<SciterHandler>);
impl Deref for SciterConnectionManager {
type Target = ConnectionManager<SciterHandler>;
fn deref(&self) -> &Self::Target {
&self.0
2021-03-29 15:59:14 +08:00
}
}
2021-03-29 15:59:14 +08:00
impl SciterConnectionManager {
pub fn new() -> Self {
#[cfg(target_os = "linux")]
std::thread::spawn(start_pa);
let cm = ConnectionManager {
ui_handler: SciterHandler::default(),
};
let cloned = cm.clone();
std::thread::spawn(move || start_ipc(cloned));
SciterConnectionManager(cm)
2021-03-29 15:59:14 +08:00
}
fn get_icon(&mut self) -> String {
crate::get_icon()
2021-03-29 15:59:14 +08:00
}
fn check_click_time(&mut self, id: i32) {
crate::ui_cm_interface::check_click_time(id);
2021-03-29 15:59:14 +08:00
}
fn get_click_time(&self) -> f64 {
crate::ui_cm_interface::get_click_time() as _
2021-03-29 15:59:14 +08:00
}
fn switch_permission(&self, id: i32, name: String, enabled: bool) {
crate::ui_cm_interface::switch_permission(id, name, enabled);
2021-03-29 15:59:14 +08:00
}
fn close(&self, id: i32) {
crate::ui_cm_interface::close(id);
2021-03-29 15:59:14 +08:00
}
fn remove_disconnected_connection(&self, id: i32) {
crate::ui_cm_interface::remove(id);
}
fn quit(&self) {
crate::platform::quit_gui();
}
fn authorize(&self, id: i32) {
crate::ui_cm_interface::authorize(id);
}
fn send_msg(&self, id: i32, text: String) {
crate::ui_cm_interface::send_chat(id, text);
2021-03-29 15:59:14 +08:00
}
2021-12-25 16:45:22 +08:00
fn t(&self, name: String) -> String {
crate::client::translate(name)
}
fn can_elevate(&self) -> bool {
crate::ui_cm_interface::can_elevate()
}
fn elevate_portable(&self, id: i32) {
crate::ui_cm_interface::elevate_portable(id);
}
2021-03-29 15:59:14 +08:00
}
impl sciter::EventHandler for SciterConnectionManager {
2021-03-29 15:59:14 +08:00
fn attached(&mut self, root: HELEMENT) {
*self.ui_handler.element.lock().unwrap() = Some(Element::from(root));
2021-03-29 15:59:14 +08:00
}
sciter::dispatch_script_call! {
2021-12-25 16:45:22 +08:00
fn t(String);
2022-05-12 17:35:25 +08:00
fn check_click_time(i32);
fn get_click_time();
2021-03-29 15:59:14 +08:00
fn get_icon();
fn close(i32);
fn remove_disconnected_connection(i32);
fn quit();
2021-03-29 15:59:14 +08:00
fn authorize(i32);
fn switch_permission(i32, String, bool);
fn send_msg(i32, String);
fn can_elevate();
fn elevate_portable(i32);
2021-03-29 15:59:14 +08:00
}
}