From fc8db69d9e56e3cea71488bbefb5c57f021b179b Mon Sep 17 00:00:00 2001 From: dignow Date: Thu, 29 Jun 2023 13:47:55 +0800 Subject: [PATCH] refact, win, clipboard, notify callback, tmp commit Signed-off-by: dignow --- flutter/lib/common.dart | 12 +++++++- libs/clipboard/src/lib.rs | 37 +++++++++++++++++++++---- libs/clipboard/src/windows/wf_cliprdr.c | 2 +- src/flutter.rs | 33 ++++++++++++++++++++++ 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/flutter/lib/common.dart b/flutter/lib/common.dart index 67091db7a..3a4f1f638 100644 --- a/flutter/lib/common.dart +++ b/flutter/lib/common.dart @@ -998,6 +998,16 @@ Widget msgboxIcon(String type) { // title should be null Widget msgboxContent(String type, String title, String text) { + String translateText(String text) { + List words = text.split(' '); + if (words.isNotEmpty && words[0].endsWith('_tip')) { + words[0] = translate(words[0]); + return words.join(' '); + } else { + return translate(text); + } + } + return Row( children: [ msgboxIcon(type), @@ -1009,7 +1019,7 @@ Widget msgboxContent(String type, String title, String text) { translate(title), style: TextStyle(fontSize: 21), ).marginOnly(bottom: 10), - Text(translate(text), style: const TextStyle(fontSize: 15)), + Text(translateText(text), style: const TextStyle(fontSize: 15)), ], ), ), diff --git a/libs/clipboard/src/lib.rs b/libs/clipboard/src/lib.rs index 152dc0bcc..ee8f5644c 100644 --- a/libs/clipboard/src/lib.rs +++ b/libs/clipboard/src/lib.rs @@ -1,6 +1,6 @@ use cliprdr::*; use hbb_common::{ - allow_err, log, + allow_err, lazy_static, log, tokio::sync::{ mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}, Mutex as TokioMutex, @@ -19,6 +19,13 @@ pub mod context_send; pub use context_send::*; const ERR_CODE_SERVER_FUNCTION_NONE: u32 = 0x00000001; +const ERR_CODE_INVALID_PARAMETER: u32 = 0x00000002; + +pub type FnNotifyCallback = fn(r#type: u32, msg: &str, details: &str) -> u32; + +lazy_static::lazy_static! { + static ref NOTIFY_CALLBACK: Arc>> = Default::default(); +} #[derive(Debug, Serialize, Deserialize, Clone)] #[serde(tag = "t", content = "c")] @@ -462,11 +469,31 @@ pub fn create_cliprdr_context( )?) } -extern "C" fn notify_callback( - msg: *const NOTIFICATION_MESSAGE, -) -> UINT { +pub fn set_notify_callback(cb: FnNotifyCallback) { + NOTIFY_CALLBACK.lock().unwrap().replace(cb); +} + +extern "C" fn notify_callback(msg: *const NOTIFICATION_MESSAGE) -> UINT { log::debug!("notify_callback called"); - 0 + if let Some(cb) = NOTIFY_CALLBACK.lock().unwrap().as_ref() { + unsafe { + let msg = &*msg; + let details = if msg.details.is_null() { + Ok("") + } else { + CStr::from_ptr(msg.details as _).to_str() + }; + match (CStr::from_ptr(msg.msg as _).to_str(), details) { + (Ok(m), Ok(d)) => cb(msg.r#type, m, d), + _ => { + log::error!("notify_callback: failed to convert msg"); + ERR_CODE_INVALID_PARAMETER + } + } + } + } else { + ERR_CODE_SERVER_FUNCTION_NONE + } } extern "C" fn client_format_list( diff --git a/libs/clipboard/src/windows/wf_cliprdr.c b/libs/clipboard/src/windows/wf_cliprdr.c index b9939121d..407316d03 100644 --- a/libs/clipboard/src/windows/wf_cliprdr.c +++ b/libs/clipboard/src/windows/wf_cliprdr.c @@ -1493,7 +1493,7 @@ UINT wait_response_event(wfClipboard *clipboard, HANDLE event, void **data) { NOTIFICATION_MESSAGE msg; msg.type = 2; - msg.msg = "timeout waiting for response"; + msg.msg = "clipboard_wait_response_timeout_tip"; msg.details = NULL; clipboard->context->NotifyClipboardMsg(&msg); rc = ERROR_INTERNAL_ERROR; diff --git a/src/flutter.rs b/src/flutter.rs index 4008b2420..490a674fa 100644 --- a/src/flutter.rs +++ b/src/flutter.rs @@ -1089,6 +1089,39 @@ pub fn stop_global_event_stream(app_type: String) { let _ = GLOBAL_EVENT_STREAM.write().unwrap().remove(&app_type); } +fn msgbox_clipboard_(channel: &str, r#type: u32, msg: &str, details: &str) { + let msgtype = format!( + "{}-nocancel-nook-hasclose", + if r#type == 0 { + "info" + } else if r#type == 1 { + "warn" + } else { + "error" + } + ); + let text = format!("{} {}", msg, details); + if let Ok(event) = serde_json::ser::to_string(&HashMap::from([ + ("type", &msgtype as &str), + ("title", "clipboard"), + ("text", &text), + ("link", ""), + ("hasRetry", ""), + ])) { + push_global_event(channel, event); + } +} + +#[inline] +pub fn msgbox_clipboard_remote(r#type: u32, msg: &str, details: &str) { + msgbox_clipboard_(APP_TYPE_DESKTOP_REMOTE, r#type, msg, details); +} + +#[inline] +pub fn msgbox_clipboard_cm(r#type: u32, msg: &str, details: &str) { + msgbox_clipboard_(APP_TYPE_CM, r#type, msg, details); +} + #[no_mangle] unsafe extern "C" fn get_rgba() {}