Skip to content

Commit 1226982

Browse files
committed
Fix panic on startup on Mac when creating the tray icon.
1 parent 9d78367 commit 1226982

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 {
@@ -97,6 +96,11 @@ impl App {
9796
}
9897
Err(_) => (Config::default(), Task::done(Message::OpenSettingsWindow)),
9998
};
99+
let task = Task::batch([
100+
task,
101+
#[cfg(not(target_os = "linux"))]
102+
Task::done(Message::CreateTrayIcon),
103+
]);
100104

101105
(
102106
App {
@@ -106,6 +110,7 @@ impl App {
106110

107111
config,
108112
windows: BTreeMap::new(),
113+
tray_icon: None,
109114
},
110115
task,
111116
)

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

@@ -196,6 +197,9 @@ impl App {
196197
return Task::batch(tasks);
197198
}
198199
}
200+
Message::CreateTrayIcon => {
201+
self.tray_icon = Some(create_tray_icon());
202+
}
199203
}
200204
Task::none()
201205
}

0 commit comments

Comments
 (0)