plugin_framework, show remote toolbar widget

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-04-23 20:53:51 +08:00
parent eb96b0d451
commit cbeebea7a5
10 changed files with 86 additions and 49 deletions

View File

@@ -1573,12 +1573,12 @@ pub fn plugin_feature_is_enabled() -> SyncReturn<bool> {
}
}
pub fn plugin_sync_ui() {
pub fn plugin_sync_ui(_sync_to: String) {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
if plugin_feature_is_enabled().0 {
crate::plugin::sync_ui();
crate::plugin::sync_ui(_sync_to);
}
}
}

View File

@@ -69,7 +69,8 @@ impl SharedConfig {
#[inline]
pub fn load(id: &str) {
let mut conf = hbb_common::config::load_path::<SharedConfig>(Self::path(id));
let conf = hbb_common::config::load_path::<HashMap<String, String>>(Self::path(id));
let mut conf = SharedConfig(conf);
if let Some(items) = CONFIG_SHARED_ITEMS.lock().unwrap().get(id) {
for item in items {
if !conf.contains_key(&item.key) {
@@ -116,7 +117,8 @@ impl PeerConfig {
#[inline]
pub fn load(id: &str, peer: &str) {
let mut conf = hbb_common::config::load_path::<PeerConfig>(Self::path(id, peer));
let conf = hbb_common::config::load_path::<HashMap<String, String>>(Self::path(id, peer));
let mut conf = PeerConfig(conf);
if let Some(items) = CONFIG_PEER_ITEMS.lock().unwrap().get(id) {
for item in items {
if !conf.contains_key(&item.key) {
@@ -124,16 +126,15 @@ impl PeerConfig {
}
}
}
match CONFIG_PEERS.lock().unwrap().get_mut(id) {
Some(peers) => {
peers.insert(peer.to_owned(), conf);
}
None => {
let mut peers = HashMap::new();
peers.insert(peer.to_owned(), conf);
CONFIG_PEERS.lock().unwrap().insert(id.to_owned(), peers);
}
if let Some(peers) = CONFIG_PEERS.lock().unwrap().get_mut(id) {
peers.insert(peer.to_owned(), conf);
return;
}
let mut peers = HashMap::new();
peers.insert(peer.to_owned(), conf);
CONFIG_PEERS.lock().unwrap().insert(id.to_owned(), peers);
}
#[inline]

View File

@@ -36,7 +36,6 @@ pub struct Location {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigItem {
pub key: String,
pub value: String,
pub default: String,
pub description: String,
}

View File

@@ -1,6 +1,6 @@
// to-do: Interdependence(This mod and crate::ipc) is not good practice here.
use crate::ipc::{connect, Connection, Data};
use hbb_common::{allow_err, bail, bytes, log, tokio, ResultType};
use hbb_common::{allow_err, log, tokio, ResultType};
use serde_derive::{Deserialize, Serialize};
#[cfg(not(windows))]
use std::{fs::File, io::prelude::*};

View File

@@ -224,10 +224,10 @@ fn load_plugin_path(path: &str) -> ResultType<()> {
(plugin.set_cb_msg)(callback_msg::callback_msg);
(plugin.set_cb_get_id)(get_local_peer_id as _);
// Ui may be not ready now, so we need to update again once ui is ready.
update_ui_plugin_desc(&desc);
update_ui_plugin_desc(&desc, None);
update_config(&desc);
// Ui may be not ready now, so we need to reload again once ui is ready.
reload_ui(&desc);
reload_ui(&desc, None);
let plugin_info = PluginInfo {
path: path.to_string(),
desc,
@@ -238,10 +238,10 @@ fn load_plugin_path(path: &str) -> ResultType<()> {
Ok(())
}
pub fn sync_ui() {
pub fn sync_ui(sync_to: String) {
for plugin in PLUGIN_INFO.read().unwrap().values() {
update_ui_plugin_desc(&plugin.desc);
reload_ui(&plugin.desc);
update_ui_plugin_desc(&plugin.desc, Some(&sync_to));
reload_ui(&plugin.desc, Some(&sync_to));
}
}
@@ -369,44 +369,70 @@ fn update_config(desc: &Desc) {
super::config::set_peer_items(desc.id(), &desc.config().peer);
}
fn reload_ui(desc: &Desc) {
fn reload_ui(desc: &Desc, sync_to: Option<&str>) {
for (location, ui) in desc.location().ui.iter() {
let v: Vec<&str> = location.split('|').collect();
// The first element is the "client" or "host".
// The second element is the "main", "remote", "cm", "file transfer", "port forward".
if v.len() >= 2 {
let available_channels = vec![
flutter::APP_TYPE_MAIN,
flutter::APP_TYPE_DESKTOP_REMOTE,
flutter::APP_TYPE_CM,
flutter::APP_TYPE_DESKTOP_FILE_TRANSFER,
flutter::APP_TYPE_DESKTOP_PORT_FORWARD,
];
if available_channels.contains(&v[1]) {
if let Ok(ui) = serde_json::to_string(&ui) {
let mut m = HashMap::new();
m.insert("name", MSG_TO_UI_TYPE_PLUGIN_RELOAD);
m.insert("id", desc.id());
m.insert("location", &location);
m.insert("ui", &ui);
flutter::push_global_event(v[1], serde_json::to_string(&m).unwrap());
if let Ok(ui) = serde_json::to_string(&ui) {
let make_event = |ui: &str| {
let mut m = HashMap::new();
m.insert("name", MSG_TO_UI_TYPE_PLUGIN_RELOAD);
m.insert("id", desc.id());
m.insert("location", &location);
// Do not depend on the "location" and plugin desc on the ui side.
// Send the ui field to ensure the ui is valid.
m.insert("ui", ui);
serde_json::to_string(&m).unwrap_or("".to_owned())
};
match sync_to {
Some(channel) => {
let _res = flutter::push_global_event(channel, make_event(&ui));
}
None => {
let v: Vec<&str> = location.split('|').collect();
// The first element is the "client" or "host".
// The second element is the "main", "remote", "cm", "file transfer", "port forward".
if v.len() >= 2 {
let available_channels = vec![
flutter::APP_TYPE_MAIN,
flutter::APP_TYPE_DESKTOP_REMOTE,
flutter::APP_TYPE_CM,
flutter::APP_TYPE_DESKTOP_FILE_TRANSFER,
flutter::APP_TYPE_DESKTOP_PORT_FORWARD,
];
if available_channels.contains(&v[1]) {
let _res = flutter::push_global_event(v[1], make_event(&ui));
}
}
}
}
}
}
}
fn update_ui_plugin_desc(desc: &Desc) {
fn update_ui_plugin_desc(desc: &Desc, sync_to: Option<&str>) {
// This function is rarely used. There's no need to care about serialization efficiency here.
if let Ok(desc_str) = serde_json::to_string(desc) {
let mut m = HashMap::new();
m.insert("name", MSG_TO_UI_TYPE_PLUGIN_DESC);
m.insert("desc", &desc_str);
flutter::push_global_event(flutter::APP_TYPE_MAIN, serde_json::to_string(&m).unwrap());
flutter::push_global_event(
flutter::APP_TYPE_DESKTOP_REMOTE,
serde_json::to_string(&m).unwrap(),
);
flutter::push_global_event(flutter::APP_TYPE_CM, serde_json::to_string(&m).unwrap());
let event = serde_json::to_string(&m).unwrap_or("".to_owned());
match sync_to {
Some(channel) => {
let _res = flutter::push_global_event(channel, serde_json::to_string(&m).unwrap());
}
None => {
let _res = flutter::push_global_event(
flutter::APP_TYPE_MAIN,
serde_json::to_string(&m).unwrap(),
);
let _res = flutter::push_global_event(
flutter::APP_TYPE_DESKTOP_REMOTE,
serde_json::to_string(&m).unwrap(),
);
let _res = flutter::push_global_event(
flutter::APP_TYPE_CM,
serde_json::to_string(&m).unwrap(),
);
}
}
}
}