Skip to content

Commit 9599d2c

Browse files
committed
ensure window present when clicking on notification
1 parent 2cc4692 commit 9599d2c

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/application.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::path::Path;
2+
use std::path::PathBuf;
23

34
use adw::subclass::prelude::*;
45
use capnp_rpc::{rpc_twoparty_capnp, twoparty, RpcSystem};
@@ -26,6 +27,7 @@ mod imp {
2627
#[derive(Default)]
2728
pub struct NotifyApplication {
2829
pub window: RefCell<WeakRef<NotifyWindow>>,
30+
pub socket_path: RefCell<PathBuf>,
2931
pub hold_guard: OnceCell<gio::ApplicationHoldGuard>,
3032
}
3133

@@ -42,6 +44,7 @@ mod imp {
4244
fn activate(&self) {
4345
debug!("AdwApplication<NotifyApplication>::activate");
4446
self.parent_activate();
47+
self.obj().ensure_window_present();
4548
}
4649

4750
fn startup(&self) {
@@ -52,20 +55,20 @@ mod imp {
5255
// Set icons for shell
5356
gtk::Window::set_default_icon_name(APP_ID);
5457

58+
let socket_path = glib::user_data_dir().join("com.ranfdev.Notify.socket");
59+
self.socket_path.replace(socket_path);
5560
app.setup_css();
5661
app.setup_gactions();
5762
app.setup_accels();
5863
}
5964
fn command_line(&self, command_line: &gio::ApplicationCommandLine) -> glib::ExitCode {
60-
let socket_path = glib::user_data_dir().join("com.ranfdev.Notify.socket");
61-
6265
debug!("AdwApplication<NotifyApplication>::command_line");
6366
let arguments = command_line.arguments();
6467
let is_daemon = arguments.get(1).map(|x| x.to_str()) == Some(Some("--daemon"));
6568
let app = self.obj();
6669

6770
if self.hold_guard.get().is_none() {
68-
self.obj().ensure_rpc_running(&socket_path);
71+
app.ensure_rpc_running(&self.socket_path.borrow());
6972
}
7073

7174
glib::MainContext::default().spawn_local(async move {
@@ -78,18 +81,7 @@ mod imp {
7881
return glib::ExitCode::SUCCESS;
7982
}
8083

81-
{
82-
let w = self.window.borrow();
83-
if let Some(window) = w.upgrade() {
84-
if window.is_visible() {
85-
window.present();
86-
return glib::ExitCode::SUCCESS;
87-
}
88-
}
89-
}
90-
91-
app.build_window(&socket_path);
92-
app.main_window().present();
84+
app.ensure_window_present();
9385

9486
glib::ExitCode::SUCCESS
9587
}
@@ -106,6 +98,17 @@ glib::wrapper! {
10698
}
10799

108100
impl NotifyApplication {
101+
fn ensure_window_present(&self) {
102+
if let Some(window) = { self.imp().window.borrow().upgrade() } {
103+
if window.is_visible() {
104+
window.present();
105+
return;
106+
}
107+
}
108+
self.build_window(&self.imp().socket_path.borrow());
109+
self.main_window().present();
110+
}
111+
109112
fn main_window(&self) -> NotifyWindow {
110113
self.imp().window.borrow().upgrade().unwrap()
111114
}

0 commit comments

Comments
 (0)