From 4e5479cdd89a0f62e2d346a628525dc36428bb95 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Tue, 7 Sep 2021 12:46:18 -0700 Subject: [PATCH] Use `GtkApplication` --- src/application.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 37 +++---------------- src/window.rs | 5 +-- 3 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 src/application.rs diff --git a/src/application.rs b/src/application.rs new file mode 100644 index 00000000..6b81d41d --- /dev/null +++ b/src/application.rs @@ -0,0 +1,89 @@ +use cascade::cascade; +use gtk4::{ + gdk, gio, + glib::{self, clone}, + prelude::*, + subclass::prelude::*, +}; +use once_cell::unsync::OnceCell; +use std::{cell::Cell, sync::Arc}; + +use crate::notifications::Notifications; +use crate::status_notifier_watcher; +use crate::window::PanelWindow; + +#[derive(Default)] +pub struct PanelAppInner { + notifications: OnceCell>, + activated: Cell, +} + +#[glib::object_subclass] +impl ObjectSubclass for PanelAppInner { + const NAME: &'static str = "S76CosmicPanelApp"; + type ParentType = gtk4::Application; + type Type = PanelApp; +} + +impl ObjectImpl for PanelAppInner { + fn constructed(&self, obj: &PanelApp) { + obj.set_application_id(Some("com.system76.cosmicpanel")); + + self.parent_constructed(obj); + } +} + +impl ApplicationImpl for PanelAppInner { + fn activate(&self, obj: &PanelApp) { + self.parent_activate(obj); + + if self.activated.get() { + return; + } + self.activated.set(true); + + let display = gdk::Display::default().unwrap(); + let monitors = display.monitors().unwrap(); + + for i in 0..monitors.n_items() { + obj.add_window_for_monitor(monitors.item(i).unwrap().downcast().unwrap()); + } + + monitors.connect_items_changed( + clone!(@weak obj => move |monitors, position, _removed, added| { + for i in position..position + added { + obj.add_window_for_monitor(monitors + .item(i) + .unwrap() + .downcast::() + .unwrap()); + } + }), + ); + + status_notifier_watcher::start(); + + let _ = self.notifications.set(Notifications::new()); + } +} + +impl GtkApplicationImpl for PanelAppInner {} + +glib::wrapper! { + pub struct PanelApp(ObjectSubclass) + @extends gtk4::Application, gio::Application, + @implements gio::ActionGroup, gio::ActionMap; +} + +impl PanelApp { + pub fn new() -> Self { + glib::Object::new::(&[]).unwrap() + } + + fn add_window_for_monitor(&self, monitor: gdk::Monitor) { + self.add_window(&cascade! { + PanelWindow::new(monitor); + ..show(); + }); + } +} diff --git a/src/main.rs b/src/main.rs index 994069e1..c6be6dec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ -use gtk4::{gdk, glib, prelude::*}; +use gtk4::prelude::*; +mod application; mod deref_cell; mod mpris; mod mpris_player; @@ -13,36 +14,8 @@ mod time_button; mod window; mod x; +use application::PanelApp; + fn main() { - gtk4::init().unwrap(); - let main_context = glib::MainContext::default(); - let _acquire_guard = main_context.acquire().unwrap(); - - let display = gdk::Display::default().unwrap(); - let monitors = display.monitors().unwrap(); - - for i in 0..monitors.n_items() { - let monitor = monitors - .item(i) - .unwrap() - .downcast::() - .unwrap(); - window::PanelWindow::new(monitor).show(); - } - - monitors.connect_items_changed(|monitors, position, _removed, added| { - for i in position..position + added { - let monitor = monitors - .item(i) - .unwrap() - .downcast::() - .unwrap(); - window::PanelWindow::new(monitor).show(); - } - }); - - status_notifier_watcher::start(); - let _notificiations = notifications::Notifications::new(); - - glib::MainLoop::new(None, false).run(); + PanelApp::new().run(); } diff --git a/src/window.rs b/src/window.rs index da39106b..1fe429d0 100644 --- a/src/window.rs +++ b/src/window.rs @@ -35,7 +35,7 @@ pub struct PanelWindowInner { #[glib::object_subclass] impl ObjectSubclass for PanelWindowInner { const NAME: &'static str = "S76PanelWindow"; - type ParentType = gtk4::Window; + type ParentType = gtk4::ApplicationWindow; type Type = PanelWindow; } @@ -112,10 +112,11 @@ impl WidgetImpl for PanelWindowInner { } impl WindowImpl for PanelWindowInner {} +impl ApplicationWindowImpl for PanelWindowInner {} glib::wrapper! { pub struct PanelWindow(ObjectSubclass) - @extends gtk4::Window, gtk4::Widget, + @extends gtk4::ApplicationWindow, gtk4::Window, gtk4::Widget, @implements gtk4::Accessible, gtk4::Buildable, gtk4::ConstraintTarget, gtk4::Native, gtk4::Root, gtk4::ShortcutManager; }