From f5dcfc455a15393a11734cb56fa02d706961d531 Mon Sep 17 00:00:00 2001 From: Ward from fusion-voyager-3 Date: Fri, 4 Oct 2024 19:03:12 +0300 Subject: [PATCH] fully implement sys tray --- locales/en_US.json | 1 + src/bin/gui/apt_update_page/mod.rs | 15 ++- src/bin/gui/build_ui/mod.rs | 127 +++++++++++++++++-------- src/bin/gui/flatpak_update_page/mod.rs | 14 +++ 4 files changed, 118 insertions(+), 39 deletions(-) diff --git a/locales/en_US.json b/locales/en_US.json index 48aa274..92ffa29 100644 --- a/locales/en_US.json +++ b/locales/en_US.json @@ -157,6 +157,7 @@ "flatpak_ref_install_dialog_cancel_label": "Cancel", "pikman_indicator_apt_count_item_label": "APT Updates: {NUM}", "pikman_indicator_flatpak_count_item_label": "Flatpak Updates: {NUM}", + "pikman_indicator_flatpak_item_label_calculating": "calculating", "pikman_indicator_open_item_label": "Open", "pikman_indicator_exit_item_label": "Exit" } \ No newline at end of file diff --git a/src/bin/gui/apt_update_page/mod.rs b/src/bin/gui/apt_update_page/mod.rs index dac7464..70fe805 100644 --- a/src/bin/gui/apt_update_page/mod.rs +++ b/src/bin/gui/apt_update_page/mod.rs @@ -32,7 +32,10 @@ pub fn apt_update_page( window: adw::ApplicationWindow, retry_signal_action: &SimpleAction, flatpak_retry_signal_action: &SimpleAction, - flatpak_ran_once: Rc> + flatpak_ran_once: Rc>, + update_sys_tray: &SimpleAction, + apt_update_count: &Rc>, + flatpak_update_count: &Rc>, ) -> gtk::Box { let (update_percent_sender, update_percent_receiver) = async_channel::unbounded::(); let update_percent_sender = update_percent_sender.clone(); @@ -42,6 +45,8 @@ pub fn apt_update_page( let (get_upgradable_sender, get_upgradable_receiver) = async_channel::unbounded(); let get_upgradable_sender = get_upgradable_sender.clone(); + (*apt_update_count.borrow_mut() = 0); + let excluded_updates_vec: Rc>> = Rc::new(RefCell::new(Vec::new())); thread::spawn(move || { @@ -385,6 +390,12 @@ pub fn apt_update_page( viewport_bin, #[strong] excluded_updates_vec, + #[strong] + update_sys_tray, + #[strong] + apt_update_count, + #[strong] + flatpak_update_count, async move { while let Ok(state) = get_upgradable_receiver.recv().await { viewport_bin.set_child(Some(&packages_viewport)); @@ -441,8 +452,10 @@ pub fn apt_update_page( ), ); packages_boxedlist.append(&apt_row); + (*apt_update_count.borrow_mut() += 1); if state.is_last { packages_boxedlist.set_sensitive(true); + update_sys_tray.activate(Some(&glib::Variant::array_from_fixed_array(&[*apt_update_count.borrow(),*flatpak_update_count.borrow()]))); } } } diff --git a/src/bin/gui/build_ui/mod.rs b/src/bin/gui/build_ui/mod.rs index eb73001..c3a0912 100644 --- a/src/bin/gui/build_ui/mod.rs +++ b/src/bin/gui/build_ui/mod.rs @@ -96,6 +96,9 @@ pub fn build_ui(app: &Application) { // Systray + let apt_update_count = Rc::new(RefCell::new(0)); + let flatpak_update_count = Rc::new(RefCell::new(0)); + let update_sys_tray = gio::SimpleAction::new("sys_tray", Some(glib::VariantTy::ARRAY)); let (tray_service_sender, tray_service_receiver) = async_channel::unbounded(); @@ -111,6 +114,51 @@ pub fn build_ui(app: &Application) { tray_service.spawn(); + update_sys_tray.connect_activate(clone!( + #[strong] + tray_handle, + move |_,param| { + let array: &[i32] = param.unwrap().fixed_array().unwrap(); + let vec = array.to_vec(); + let apt_update_count = vec[0]; + let flatpak_update_count = vec[1]; + let tray_icon = if apt_update_count + flatpak_update_count > 1 { + Some("update-high".into()) + } else { + Some("update-none".into()) + }; + tray_handle.update(|tray: &mut PikmanTray| { + tray.icon_name = tray_icon; + tray.apt_item_label = Some(strfmt::strfmt( + &t!("pikman_indicator_apt_count_item_label").to_string(), + &std::collections::HashMap::from([ + ( + "NUM".to_string(), + match apt_update_count { + -1 => t!("pikman_indicator_flatpak_item_label_calculating").into(), + _ => apt_update_count.to_string(), + }, + ), + ]), + ) + .unwrap()); + tray.flatpak_item_label = Some(strfmt::strfmt( + &t!("pikman_indicator_flatpak_count_item_label").to_string(), + &std::collections::HashMap::from([ + ( + "NUM".to_string(), + match flatpak_update_count { + -1 => t!("pikman_indicator_flatpak_item_label_calculating").into(), + _ => flatpak_update_count.to_string(), + }, + ), + ]), + ) + .unwrap()); + }); + })); + update_sys_tray.activate(Some(&glib::Variant::array_from_fixed_array(&[-1,-1]))); + thread::spawn(move || loop { match Command::new("ping").arg("google.com").arg("-c 1").output() { Ok(t) if t.status.success() => internet_loop_sender @@ -315,10 +363,19 @@ pub fn build_ui(app: &Application) { flatpak_retry_signal_action, #[strong] flatpak_update_view_stack_bin, + #[strong] + update_sys_tray, + #[strong] + apt_update_count, + #[strong] + flatpak_update_count, move |_, _| { flatpak_update_view_stack_bin.set_child(Some(&flatpak_update_page::flatpak_update_page( window, &flatpak_retry_signal_action, + &update_sys_tray, + &apt_update_count, + &flatpak_update_count, ))); } )); @@ -341,12 +398,21 @@ pub fn build_ui(app: &Application) { apt_update_view_stack_bin, #[weak] flatpak_ran_once, + #[strong] + update_sys_tray, + #[strong] + apt_update_count, + #[strong] + flatpak_update_count, move |_, _| { apt_update_view_stack_bin.set_child(Some(&apt_update_page::apt_update_page( window, &apt_retry_signal_action, &flatpak_retry_signal_action, flatpak_ran_once, + &update_sys_tray, + &apt_update_count, + &flatpak_update_count, ))); } )); @@ -356,6 +422,9 @@ pub fn build_ui(app: &Application) { &apt_retry_signal_action, &flatpak_retry_signal_action, flatpak_ran_once, + &update_sys_tray, + &apt_update_count, + &flatpak_update_count, ))); // Add to stack switcher @@ -405,44 +474,6 @@ pub fn build_ui(app: &Application) { let flatpak_manage_page_toggle_button = add_content_button(&window_adw_stack, false, "flatpak_manage_page".to_string(), t!("flatpak_manage_page_title").to_string(), &null_toggle_button); window_adw_view_switcher_sidebar_box.append(&flatpak_manage_page_toggle_button); - update_sys_tray.connect_activate(clone!( - #[strong] - tray_handle, - move |_,param| { - let array: &[i32] = param.unwrap().fixed_array().unwrap(); - let vec = array.to_vec(); - let apt_update_count = vec[0]; - let flatpak_update_count = vec[1]; - let tray_icon = if apt_update_count + flatpak_update_count > 1 { - Some("update-high".into()) - } else { - Some("update-none".into()) - }; - tray_handle.update(|tray: &mut PikmanTray| { - tray.icon_name = tray_icon; - tray.apt_item_label = Some(strfmt::strfmt( - &t!("pikman_indicator_apt_count_item_label").to_string(), - &std::collections::HashMap::from([ - ( - "NUM".to_string(), - apt_update_count.to_string(), - ), - ]), - ) - .unwrap()); - tray.flatpak_item_label = Some(strfmt::strfmt( - &t!("pikman_indicator_flatpak_count_item_label").to_string(), - &std::collections::HashMap::from([ - ( - "NUM".to_string(), - flatpak_update_count.to_string(), - ), - ]), - ) - .unwrap()); - }); - })); - app.connect_command_line(clone!( #[strong] apt_manage_page_toggle_button, @@ -506,6 +537,26 @@ pub fn build_ui(app: &Application) { } } )); + + let tray_service_context = MainContext::default(); + // The main loop executes the asynchronous block + tray_service_context.spawn_local(clone!( + #[strong] + window, + async move { + while let Ok(state) = tray_service_receiver.recv().await { + match state.as_str() { + "open" => { + if !window.is_visible() { + window.present(); + } + } + _ => todo!() + } + } + } + )); + } fn add_content_button(window_adw_stack: >k::Stack, active: bool, name: String, title: String, null_toggle_button: >k::ToggleButton) -> gtk::ToggleButton { diff --git a/src/bin/gui/flatpak_update_page/mod.rs b/src/bin/gui/flatpak_update_page/mod.rs index f014204..de4abbd 100644 --- a/src/bin/gui/flatpak_update_page/mod.rs +++ b/src/bin/gui/flatpak_update_page/mod.rs @@ -30,7 +30,12 @@ pub struct FlatpakRefStruct { pub fn flatpak_update_page( window: adw::ApplicationWindow, retry_signal_action: &SimpleAction, + update_sys_tray: &SimpleAction, + apt_update_count: &Rc>, + flatpak_update_count: &Rc>, ) -> gtk::Box { + (*flatpak_update_count.borrow_mut() = 0); + let (appstream_sync_percent_sender, appstream_sync_percent_receiver) = async_channel::unbounded::(); let appstream_sync_percent_sender = appstream_sync_percent_sender.clone(); @@ -369,6 +374,12 @@ pub fn flatpak_update_page( viewport_bin, #[strong] packages_viewport, + #[strong] + update_sys_tray, + #[strong] + apt_update_count, + #[strong] + flatpak_update_count, async move { while let Ok(state) = appstream_sync_status_receiver.recv().await { match state.as_ref() { @@ -539,6 +550,7 @@ pub fn flatpak_update_page( ); packages_boxedlist.append(&flatpak_row); + (*flatpak_update_count.borrow_mut() += 1); if flatref_struct.is_system && flatref_struct.is_last { system_last_triggered = true @@ -692,6 +704,7 @@ pub fn flatpak_update_page( ), ); packages_boxedlist.append(&flatpak_row); + (*flatpak_update_count.borrow_mut() += 1); if !flatref_struct.is_system && flatref_struct.is_last { user_last_triggered = true @@ -702,6 +715,7 @@ pub fn flatpak_update_page( } if user_last_triggered && system_last_triggered { packages_boxedlist.set_sensitive(true); + update_sys_tray.activate(Some(&glib::Variant::array_from_fixed_array(&[*apt_update_count.borrow(),*flatpak_update_count.borrow()]))); } flatpak_update_dialog.close(); }