From 1552402907ad88c4dd63cff45c0d68d7a5550a60 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Sun, 2 Jan 2022 21:56:04 +0800 Subject: [PATCH] working on fav --- libs/hbb_common/src/config.rs | 26 +++++++++++++++++ src/ui.rs | 54 +++++++++++++++++++++++++---------- src/ui/index.css | 2 +- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/libs/hbb_common/src/config.rs b/libs/hbb_common/src/config.rs index cf80cd49b..26b8d160f 100644 --- a/libs/hbb_common/src/config.rs +++ b/libs/hbb_common/src/config.rs @@ -688,6 +688,32 @@ impl PeerConfig { } } +#[derive(Debug, Default, Serialize, Deserialize, Clone)] +pub struct Fav { + #[serde(default)] + pub peers: Vec, +} + +impl Fav { + pub fn load() -> Fav { + let _ = CONFIG.read().unwrap(); // for lock + match confy::load_path(&Config::file_("_fav")) { + Ok(fav) => fav, + Err(err) => { + log::error!("Failed to load fav: {}", err); + Default::default() + } + } + } + + pub fn store(peers: Vec) { + let f = Fav { peers }; + if let Err(err) = confy::store_path(Config::file_("_fav"), f) { + log::error!("Failed to store fav: {}", err); + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ui.rs b/src/ui.rs index 355cf5b47..da7141284 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -8,7 +8,7 @@ use crate::common::SOFTWARE_UPDATE_URL; use crate::ipc; use hbb_common::{ allow_err, - config::{Config, PeerConfig, APP_NAME, ICON}, + config::{Config, Fav, PeerConfig, APP_NAME, ICON}, log, sleep, tokio::{self, time}, }; @@ -411,22 +411,43 @@ impl UI { v } + #[inline] + fn get_peer_value(id: String, p: PeerConfig) -> Value { + let values = vec![ + id, + p.info.username.clone(), + p.info.hostname.clone(), + p.info.platform.clone(), + p.options.get("alias").unwrap_or(&"".to_owned()).to_owned(), + ]; + Value::from_iter(values) + } + + fn get_peer(&self, id: String) -> Value { + let c = PeerConfig::load(&id); + Self::get_peer_value(id, c) + } + + fn get_fav(&self) -> Value { + Value::from_iter(Fav::load().peers) + } + + fn store_fav(&self, fav: Value) { + let mut tmp = vec![]; + fav.values().for_each(|v| { + if let Some(v) = v.as_string() { + if !v.is_empty() { + tmp.push(v); + } + } + }); + Fav::store(tmp); + } + fn get_recent_sessions(&mut self) -> Value { let peers: Vec = PeerConfig::peers() - .iter() - .map(|p| { - let values = vec![ - p.0.clone(), - p.2.info.username.clone(), - p.2.info.hostname.clone(), - p.2.info.platform.clone(), - p.2.options - .get("alias") - .unwrap_or(&"".to_owned()) - .to_owned(), - ]; - Value::from_iter(values) - }) + .drain(..) + .map(|p| Self::get_peer_value(p.0, p.2)) .collect(); Value::from_iter(peers) } @@ -599,6 +620,9 @@ impl sciter::EventHandler for UI { fn remove_peer(String); fn get_connect_status(); fn get_recent_sessions(); + fn get_peer(String); + fn get_fav(); + fn store_fav(Value); fn recent_sessions_updated(); fn get_icon(); fn get_msgbox(); diff --git a/src/ui/index.css b/src/ui/index.css index 69b3dae4c..16841777f 100644 --- a/src/ui/index.css +++ b/src/ui/index.css @@ -66,7 +66,7 @@ div.sessions-tab span.active { } div.search-id { - width: 200px; + width: 160px; padding: 0; position: relative; display: inline-block;