Skip to content

Commit ef61d15

Browse files
committed
Fix panic on startup on Mac when creating the tray icon.
1 parent b1ad794 commit ef61d15

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ mod organize_type;
2020

2121
use std::collections::BTreeMap;
2222

23+
use ::tray_icon::TrayIcon;
2324
use config::Config;
2425
use consts::{APPNAME, BOLD_FONT_TTF, ICON_FONT_TTF, MEDIUM_FONT, MEDIUM_FONT_TTF};
2526
use iced::{Task, daemon, window::Id};
2627
use interprocess::local_socket::{self, GenericNamespaced, ToNsName, traits::Stream};
27-
use tray_icon::create_tray_icon;
2828
use window::AppWindow;
2929

3030
fn main() -> Result<(), iced::Error> {
@@ -36,13 +36,10 @@ fn main() -> Result<(), iced::Error> {
3636
return Ok(());
3737
};
3838

39-
#[cfg(not(target_os = "linux"))]
40-
let _tray_icon = create_tray_icon();
41-
4239
#[cfg(target_os = "linux")]
4340
std::thread::spawn(|| {
4441
gtk::init().expect("GTK must be initialized");
45-
let _tray_icon = create_tray_icon();
42+
let _tray_icon = tray_icon::create_tray_icon();
4643
gtk::main();
4744
});
4845

@@ -65,6 +62,7 @@ pub struct App {
6562

6663
config: Config,
6764
windows: BTreeMap<Id, AppWindow>,
65+
tray_icon: Option<TrayIcon>,
6866
}
6967

7068
#[derive(Debug, Clone)]
@@ -80,6 +78,7 @@ pub enum Message {
8078
ExitApp,
8179
Settings(Id, settings::Message),
8280
Capture(Id, capture::Message),
81+
CreateTrayIcon,
8382
}
8483

8584
impl App {
@@ -95,6 +94,11 @@ impl App {
9594
),
9695
Err(_) => (Config::default(), Task::done(Message::OpenSettingsWindow)),
9796
};
97+
let task = Task::batch([
98+
task,
99+
#[cfg(not(target_os = "linux"))]
100+
Task::done(Message::CreateTrayIcon),
101+
]);
98102

99103
(
100104
App {
@@ -104,6 +108,7 @@ impl App {
104108

105109
config,
106110
windows: BTreeMap::new(),
111+
tray_icon: None,
107112
},
108113
task,
109114
)

src/update.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
capture::{self, Capture},
1010
consts::APPICON,
1111
settings::{self, Settings},
12+
tray_icon::create_tray_icon,
1213
window::AppWindow,
1314
};
1415

@@ -199,6 +200,9 @@ impl App {
199200
return Task::batch(tasks);
200201
}
201202
}
203+
Message::CreateTrayIcon => {
204+
self.tray_icon = Some(create_tray_icon());
205+
}
202206
}
203207
Task::none()
204208
}

0 commit comments

Comments
 (0)