ensure window present when clicking on notification

This commit is contained in:
ranfdev
2023-10-26 10:06:11 +02:00
parent 2cc469234f
commit 9599d2c472

View File

@ -1,4 +1,5 @@
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
use adw::subclass::prelude::*; use adw::subclass::prelude::*;
use capnp_rpc::{rpc_twoparty_capnp, twoparty, RpcSystem}; use capnp_rpc::{rpc_twoparty_capnp, twoparty, RpcSystem};
@ -26,6 +27,7 @@ mod imp {
#[derive(Default)] #[derive(Default)]
pub struct NotifyApplication { pub struct NotifyApplication {
pub window: RefCell<WeakRef<NotifyWindow>>, pub window: RefCell<WeakRef<NotifyWindow>>,
pub socket_path: RefCell<PathBuf>,
pub hold_guard: OnceCell<gio::ApplicationHoldGuard>, pub hold_guard: OnceCell<gio::ApplicationHoldGuard>,
} }
@ -42,6 +44,7 @@ mod imp {
fn activate(&self) { fn activate(&self) {
debug!("AdwApplication<NotifyApplication>::activate"); debug!("AdwApplication<NotifyApplication>::activate");
self.parent_activate(); self.parent_activate();
self.obj().ensure_window_present();
} }
fn startup(&self) { fn startup(&self) {
@ -52,20 +55,20 @@ mod imp {
// Set icons for shell // Set icons for shell
gtk::Window::set_default_icon_name(APP_ID); gtk::Window::set_default_icon_name(APP_ID);
let socket_path = glib::user_data_dir().join("com.ranfdev.Notify.socket");
self.socket_path.replace(socket_path);
app.setup_css(); app.setup_css();
app.setup_gactions(); app.setup_gactions();
app.setup_accels(); app.setup_accels();
} }
fn command_line(&self, command_line: &gio::ApplicationCommandLine) -> glib::ExitCode { fn command_line(&self, command_line: &gio::ApplicationCommandLine) -> glib::ExitCode {
let socket_path = glib::user_data_dir().join("com.ranfdev.Notify.socket");
debug!("AdwApplication<NotifyApplication>::command_line"); debug!("AdwApplication<NotifyApplication>::command_line");
let arguments = command_line.arguments(); let arguments = command_line.arguments();
let is_daemon = arguments.get(1).map(|x| x.to_str()) == Some(Some("--daemon")); let is_daemon = arguments.get(1).map(|x| x.to_str()) == Some(Some("--daemon"));
let app = self.obj(); let app = self.obj();
if self.hold_guard.get().is_none() { if self.hold_guard.get().is_none() {
self.obj().ensure_rpc_running(&socket_path); app.ensure_rpc_running(&self.socket_path.borrow());
} }
glib::MainContext::default().spawn_local(async move { glib::MainContext::default().spawn_local(async move {
@ -78,18 +81,7 @@ mod imp {
return glib::ExitCode::SUCCESS; return glib::ExitCode::SUCCESS;
} }
{ app.ensure_window_present();
let w = self.window.borrow();
if let Some(window) = w.upgrade() {
if window.is_visible() {
window.present();
return glib::ExitCode::SUCCESS;
}
}
}
app.build_window(&socket_path);
app.main_window().present();
glib::ExitCode::SUCCESS glib::ExitCode::SUCCESS
} }
@ -106,6 +98,17 @@ glib::wrapper! {
} }
impl NotifyApplication { impl NotifyApplication {
fn ensure_window_present(&self) {
if let Some(window) = { self.imp().window.borrow().upgrade() } {
if window.is_visible() {
window.present();
return;
}
}
self.build_window(&self.imp().socket_path.borrow());
self.main_window().present();
}
fn main_window(&self) -> NotifyWindow { fn main_window(&self) -> NotifyWindow {
self.imp().window.borrow().upgrade().unwrap() self.imp().window.borrow().upgrade().unwrap()
} }