@@ -97,7 +97,7 @@ async fn loop_systray() -> Result<()> {
9797
9898 std:: thread:: sleep ( std:: time:: Duration :: from_secs ( update_interval) ) ;
9999
100- if let Ok ( mut updated_in_advance) = updated_in_advance. lock ( ) {
100+ if let Ok ( mut updated_in_advance) = updated_in_advance. try_lock ( ) {
101101 if std:: mem:: replace ( & mut * updated_in_advance, false ) {
102102 continue ;
103103 }
@@ -197,18 +197,19 @@ async fn loop_systray() -> Result<()> {
197197 // 如菜单ID可以格式化为u64,则菜单事件对应的是更新频率的设置
198198 if let Ok ( update_interval) = menu_id. trim ( ) . parse :: < u64 > ( ) {
199199 config. update_interval = update_interval;
200- write_ini_update_interval ( & ini_path, update_interval) ;
200+ write_ini_settings ( & ini_path, " update_interval" , update_interval . to_string ( ) ) ;
201201 if let Ok ( mut update_menu_event) = update_menu_event. lock ( ) {
202202 if let Err ( err) = proxy_menu. send_event ( TrayEvent :: ForwardUpdate ) {
203203 eprintln ! ( "{err}" )
204204 } else {
205205 * update_menu_event = true ;
206206 }
207207 }
208+ // 如菜单ID可以格式化为f64,则菜单事件对应的是设备低电量的设置
208209 } else if let Ok ( low_battery) = menu_id. trim ( ) . parse :: < f64 > ( ) {
209210 let low_battery = ( low_battery * 100.0 ) . floor ( ) . clamp ( 0.0 , 99.0 ) as u8 ;
210211 config. notify_low_battery = if low_battery == 0 { None } else { Some ( low_battery) } ;
211- write_ini_notify_low_battery ( & ini_path, low_battery) ;
212+ write_ini_settings ( & ini_path, "notify_low_battery" , low_battery. to_string ( ) ) ;
212213 if let Ok ( mut update_menu_event) = update_menu_event. lock ( ) {
213214 if let Err ( err) = proxy_menu. send_event ( TrayEvent :: ForwardUpdate ) {
214215 eprintln ! ( "{err}" ) ;
@@ -240,7 +241,41 @@ async fn loop_systray() -> Result<()> {
240241 } ,
241242 "show_disconnected_devices" => {
242243 config. show_disconnected_devices = !config. show_disconnected_devices ;
243- write_ini_show_disconnected ( & ini_path, config. show_disconnected_devices . to_string ( ) ) ;
244+ write_ini_settings (
245+ & ini_path,
246+ "show_disconnected_devices" ,
247+ config. show_disconnected_devices . to_string ( )
248+ ) ;
249+ if let Ok ( mut update_menu_event) = update_menu_event. lock ( ) {
250+ if let Err ( err) = proxy_menu. send_event ( TrayEvent :: ForwardUpdate ) {
251+ eprintln ! ( "{err}" )
252+ } else {
253+ * update_menu_event = true ;
254+ }
255+ }
256+ } ,
257+ "truncate_bluetooth_name" => {
258+ config. truncate_bluetooth_name = !config. truncate_bluetooth_name ;
259+ write_ini_settings (
260+ & ini_path,
261+ "truncate_bluetooth_name" ,
262+ config. truncate_bluetooth_name . to_string ( )
263+ ) ;
264+ if let Ok ( mut update_menu_event) = update_menu_event. lock ( ) {
265+ if let Err ( err) = proxy_menu. send_event ( TrayEvent :: ForwardUpdate ) {
266+ eprintln ! ( "{err}" )
267+ } else {
268+ * update_menu_event = true ;
269+ }
270+ }
271+ } ,
272+ "battery_prefix_name" => {
273+ config. battery_prefix_name = !config. battery_prefix_name ;
274+ write_ini_settings (
275+ & ini_path,
276+ "battery_prefix_name" ,
277+ config. battery_prefix_name . to_string ( )
278+ ) ;
244279 if let Ok ( mut update_menu_event) = update_menu_event. lock ( ) {
245280 if let Err ( err) = proxy_menu. send_event ( TrayEvent :: ForwardUpdate ) {
246281 eprintln ! ( "{err}" )
@@ -429,19 +464,32 @@ async fn get_bluetooth_tray_info(config: Arc<Mutex<Config>>) -> Result<(Vec<Stri
429464 . await
430465 . map_err ( |e| anyhow ! ( "Failed to get bluetooth devices info - {e}" ) ) ?;
431466 let show_disconnected_devices = config. lock ( ) . map_or ( false , |c| c. show_disconnected_devices ) ;
432- let ( tooltip, menu_devices) = convert_tray_info ( & bluetooth_devices_info, show_disconnected_devices) ;
467+ let truncate_bluetooth_name = config. lock ( ) . map_or ( false , |c| c. truncate_bluetooth_name ) ;
468+ let battery_prefix_name = config. lock ( ) . map_or ( false , |c| c. battery_prefix_name ) ;
469+ let ( tooltip, menu_devices) = convert_tray_info (
470+ & bluetooth_devices_info,
471+ show_disconnected_devices,
472+ truncate_bluetooth_name,
473+ battery_prefix_name,
474+ ) ;
433475 Ok ( ( tooltip, menu_devices, bluetooth_devices_info) )
434476}
435477
436478fn convert_tray_info (
437479 bluetooth_devices_info : & HashSet < BluetoothInfo > ,
438480 show_disconnected_devices : bool ,
481+ truncate_bluetooth_name : bool ,
482+ battery_prefix_name : bool ,
439483) -> ( Vec < String > , Vec < String > ) {
440484 bluetooth_devices_info. iter ( ) . fold ( ( Vec :: new ( ) , Vec :: new ( ) ) , |mut acc, blue_info| {
441- let name = truncate_with_ellipsis ( & blue_info. name , 10 ) ;
485+ let name = truncate_with_ellipsis ( truncate_bluetooth_name , & blue_info. name , 10 ) ;
442486 let battery = blue_info. battery ;
443- let status_icon = if blue_info. status { "🟢" } else { "🔴" } ; // { "[●]" } else { "[−]" }
444- let info = format ! ( "{status_icon}{battery:3}% - {name}" ) ;
487+ let status_icon = if blue_info. status { "🟢" } else { "🔴" } ;
488+ let info = if battery_prefix_name {
489+ format ! ( "{status_icon}{battery:3}% - {name}" )
490+ } else {
491+ format ! ( "{status_icon}{name} - {battery:3}%" )
492+ } ;
445493 match blue_info. status {
446494 true => {
447495 acc. 0 . insert ( 0 , info) ;
@@ -459,8 +507,8 @@ fn convert_tray_info(
459507 } )
460508}
461509
462- fn truncate_with_ellipsis ( s : & str , max_chars : usize ) -> String {
463- if s. chars ( ) . count ( ) > max_chars {
510+ fn truncate_with_ellipsis ( truncate_bluetooth_name : bool , s : & str , max_chars : usize ) -> String {
511+ if truncate_bluetooth_name && s. chars ( ) . count ( ) > max_chars {
464512 let mut result = s. chars ( ) . take ( max_chars) . collect :: < String > ( ) ;
465513 result. push_str ( "..." ) ;
466514 result
@@ -478,12 +526,28 @@ fn create_tray_menu(menu_devices: &Vec<String>, config: &Config) -> Result<Menu>
478526
479527 let menu_show_disconnected_devices = CheckMenuItem :: with_id (
480528 "show_disconnected_devices" ,
481- "Show Disconnected" ,
529+ "Show Disconnected Devices " ,
482530 true ,
483531 config. show_disconnected_devices ,
484532 None
485533 ) ;
486534
535+ let truncate_bluetooth_name = CheckMenuItem :: with_id (
536+ "truncate_bluetooth_name" ,
537+ "Truncate Device Name" ,
538+ true ,
539+ config. truncate_bluetooth_name ,
540+ None
541+ ) ;
542+
543+ let battery_prefix_name = CheckMenuItem :: with_id (
544+ "battery_prefix_name" ,
545+ "Battery Prefix Name" ,
546+ true ,
547+ config. battery_prefix_name ,
548+ None
549+ ) ;
550+
487551 let update_items = & [
488552 & CheckMenuItem :: with_id ( "15" , "15s" , true , config. update_interval == 15 , None ) as & dyn IsMenuItem ,
489553 & CheckMenuItem :: with_id ( "30" , "30s" , true , config. update_interval == 30 , None ) as & dyn IsMenuItem ,
@@ -510,6 +574,7 @@ fn create_tray_menu(menu_devices: &Vec<String>, config: &Config) -> Result<Menu>
510574 & CheckMenuItem :: with_id ( "0.25" , "25%" , true , low_battery. map_or ( false , |v| v == 25 ) , None ) as & dyn IsMenuItem ,
511575 ] ;
512576 let notify_low_battery = Submenu :: with_items ( "Low Battery" , true , low_battery_items) ?;
577+
513578 let notify_items = & [
514579 & CheckMenuItem :: with_id ( "notify_mute" , "Notify Silently" , true , config. notify_mute , None ) as & dyn IsMenuItem ,
515580 & notify_low_battery as & dyn IsMenuItem ,
@@ -518,7 +583,6 @@ fn create_tray_menu(menu_devices: &Vec<String>, config: &Config) -> Result<Menu>
518583 & CheckMenuItem :: with_id ( "notify_added_devices" , "New Devices" , true , config. notify_added_devices , None ) as & dyn IsMenuItem ,
519584 & CheckMenuItem :: with_id ( "notify_remove_devices" , "Remove Devices" , true , config. notify_remove_devices , None ) as & dyn IsMenuItem ,
520585 ] ;
521-
522586 let menu_notify = Submenu :: with_items ( "Notifications" , true , notify_items) ?;
523587
524588 let menu_about = PredefinedMenuItem :: about (
@@ -531,16 +595,23 @@ fn create_tray_menu(menu_devices: &Vec<String>, config: &Config) -> Result<Menu>
531595 ..Default :: default ( )
532596 } ) ) ;
533597
598+ let settings_items = & [
599+ & menu_show_disconnected_devices as & dyn IsMenuItem ,
600+ & truncate_bluetooth_name as & dyn IsMenuItem ,
601+ & battery_prefix_name as & dyn IsMenuItem ,
602+ & menu_update as & dyn IsMenuItem ,
603+ & menu_notify as & dyn IsMenuItem ,
604+ ] ;
605+
606+ let menu_setting = Submenu :: with_items ( "Settings" , true , settings_items) ?;
607+
534608 for text in menu_devices {
535609 let item = CheckMenuItem :: with_id ( text, text, true , false , None ) ;
536610 tray_menu. append ( & item) . map_err ( |_| anyhow ! ( "Failed to append 'Devices' to Tray Menu" ) ) ?;
537611 }
612+
538613 tray_menu. append ( & menu_separator) . context ( "Failed to apped 'Separator' to Tray Menu" ) ?;
539- tray_menu. append ( & menu_show_disconnected_devices) . context ( "Failed to apped 'Separator' to Tray Menu" ) ?;
540- tray_menu. append ( & menu_separator) . context ( "Failed to apped 'Separator' to Tray Menu" ) ?;
541- tray_menu. append ( & menu_update) . context ( "Failed to apped 'Update Interval' to Tray Menu" ) ?;
542- tray_menu. append ( & menu_separator) . context ( "Failed to apped 'Separator' to Tray Menu" ) ?;
543- tray_menu. append ( & menu_notify) . context ( "Failed to apped 'Update Interval' to Tray Menu" ) ?;
614+ tray_menu. append ( & menu_setting) . context ( "Failed to apped 'Update Interval' to Tray Menu" ) ?;
544615 tray_menu. append ( & menu_separator) . context ( "Failed to apped 'Separator' to Tray Menu" ) ?;
545616 tray_menu. append ( & menu_about) . context ( "Failed to apped 'About' to Tray Menu" ) ?;
546617 tray_menu. append ( & menu_separator) . context ( "Failed to apped 'Separator' to Tray Menu" ) ?;
0 commit comments