Skip to content

Commit 0d969c8

Browse files
authored
Merge pull request #106 from teamclouday/system-tray-fix
Exclude system tray feature on linux
2 parents ee4d4e2 + 4cdc00e commit 0d969c8

File tree

6 files changed

+72
-28
lines changed

6 files changed

+72
-28
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
if: matrix.platform == 'linux'
4040
run: |
4141
sudo apt-get update
42-
sudo apt-get install -y libasound2-dev libjack-jackd2-dev libxkbcommon-dev protobuf-compiler libgtk-3-dev libxdo-dev libappindicator3-dev
42+
sudo apt-get install -y libasound2-dev libjack-jackd2-dev libxkbcommon-dev protobuf-compiler
4343
4444
- name: Install dependencies (Windows)
4545
if: matrix.platform == 'windows'

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
if: matrix.platform == 'linux'
3434
run: |
3535
sudo apt-get update
36-
sudo apt-get install -y libasound2-dev libjack-jackd2-dev libxkbcommon-dev protobuf-compiler libgtk-3-dev libxdo-dev libappindicator3-dev
36+
sudo apt-get install -y libasound2-dev libjack-jackd2-dev libxkbcommon-dev protobuf-compiler
3737
3838
- name: Install dependencies (Windows)
3939
if: matrix.platform == 'windows'

RustApp/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ libcosmic = { git = "https://github.com/pop-os/libcosmic.git", rev = "8412dd5939
8686
"highlighter",
8787
"about",
8888
] }
89-
tray-icon = "0.21.1"
9089
notify-rust = "4"
9190
byteorder = "1"
9291
nusb = { version = "0.1", optional = true }
@@ -113,6 +112,9 @@ mslnk = "0.1"
113112
[target.'cfg(target_os = "windows")'.build-dependencies]
114113
winres = "0.1"
115114

115+
[target.'cfg(not(target_os = "linux"))'.dependencies]
116+
tray-icon = "0.21.1"
117+
116118
# [patch."https://github.com/pop-os/libcosmic.git"]
117119
# libcosmic = { path = "../../libcosmic" }
118120

RustApp/src/ui/app.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub struct AppState {
136136
log_path: String,
137137
pub system_tray: Option<SystemTray>,
138138
pub system_tray_stream: Option<SystemTrayStream>,
139+
has_shown_minimize_notification: bool,
139140
}
140141

141142
pub struct CustomWindow {
@@ -363,6 +364,7 @@ impl Application for AppState {
363364
log_path: flags.log_path.clone(),
364365
system_tray,
365366
system_tray_stream,
367+
has_shown_minimize_notification: false,
366368
};
367369

368370
commands.push(
@@ -677,14 +679,17 @@ impl Application for AppState {
677679
self.about_window = None;
678680
}
679681

680-
let _ = Notification::new()
681-
.summary("AndroidMic")
682-
.body(&fl!("minimized_to_tray"))
683-
.auto_icon()
684-
.show()
685-
.map_err(|e| {
686-
error!("failed to show notification: {e}");
687-
});
682+
if !self.has_shown_minimize_notification {
683+
let _ = Notification::new()
684+
.summary("AndroidMic")
685+
.body(&fl!("minimized_to_tray"))
686+
.auto_icon()
687+
.show()
688+
.map_err(|e| {
689+
error!("failed to show notification: {e}");
690+
});
691+
self.has_shown_minimize_notification = true;
692+
}
688693

689694
return cosmic::iced_runtime::Task::batch(effects);
690695
}
@@ -708,25 +713,35 @@ impl Application for AppState {
708713
}
709714
AppMsg::SystemTray(tray_msg) => match tray_msg {
710715
SystemTrayMsg::Show => {
711-
let settings = window::Settings {
712-
size: Size::new(800.0, 600.0),
713-
position: window::Position::Centered,
714-
icon: window_icon!("icon"),
715-
..Default::default()
716-
};
717-
718-
let (new_id, command) = cosmic::iced::window::open(settings);
719-
self.main_window = Some(CustomWindow { window_id: new_id });
720-
let set_window_title = self.set_window_title(fl!("main_window_title"), new_id);
721-
722-
return command
723-
.map(|_| cosmic::action::Action::None)
724-
.chain(set_window_title)
725-
.chain(cosmic::iced_runtime::task::effect(
716+
if let Some(main_window) = &self.main_window {
717+
// avoid duplicate window
718+
return cosmic::iced_runtime::task::effect(
726719
cosmic::iced::runtime::Action::Window(window::Action::GainFocus(
727-
new_id,
720+
main_window.window_id,
728721
)),
729-
));
722+
);
723+
} else {
724+
let settings = window::Settings {
725+
size: Size::new(800.0, 600.0),
726+
position: window::Position::Centered,
727+
icon: window_icon!("icon"),
728+
..Default::default()
729+
};
730+
731+
let (new_id, command) = cosmic::iced::window::open(settings);
732+
self.main_window = Some(CustomWindow { window_id: new_id });
733+
let set_window_title =
734+
self.set_window_title(fl!("main_window_title"), new_id);
735+
736+
return command
737+
.map(|_| cosmic::action::Action::None)
738+
.chain(set_window_title)
739+
.chain(cosmic::iced_runtime::task::effect(
740+
cosmic::iced::runtime::Action::Window(window::Action::GainFocus(
741+
new_id,
742+
)),
743+
));
744+
}
730745
}
731746
SystemTrayMsg::Exit => {
732747
return cosmic::iced_runtime::task::effect(cosmic::iced::runtime::Action::Exit);

RustApp/src/ui/icon.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ macro_rules! window_icon {
4848
($name:literal) => {{ window_icon!($name, 32, 32) }};
4949
}
5050

51+
#[cfg(not(target_os = "linux"))]
5152
#[macro_export]
5253
macro_rules! tray_icon {
5354
($name:literal, $width:expr, $height:expr) => {{

RustApp/src/ui/tray.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ use cosmic::iced::{futures::Stream, stream};
22
use futures::SinkExt;
33
use std::sync::Arc;
44
use tokio::sync::{Mutex, mpsc};
5+
6+
#[cfg(not(target_os = "linux"))]
57
use tray_icon::{
68
TrayIcon, TrayIconBuilder, TrayIconEvent,
79
menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem},
810
};
911

12+
#[cfg(not(target_os = "linux"))]
1013
use crate::{fl, tray_icon};
1114

1215
#[derive(Debug, Clone)]
@@ -22,13 +25,18 @@ pub struct SystemTrayStream {
2225
receiver: Arc<Mutex<mpsc::UnboundedReceiver<SystemTrayMsg>>>,
2326
}
2427

28+
#[cfg(not(target_os = "linux"))]
2529
pub struct SystemTray {
2630
tray_icon: TrayIcon,
2731
item_connect: MenuItem,
2832
item_disconnect: MenuItem,
2933
}
3034

35+
#[cfg(target_os = "linux")]
36+
pub struct SystemTray;
37+
3138
impl SystemTray {
39+
#[cfg(not(target_os = "linux"))]
3240
pub fn new() -> anyhow::Result<(Self, SystemTrayStream)> {
3341
let item_show = MenuItem::new(fl!("tray_show_window"), true, None);
3442
let item_connect = MenuItem::new(fl!("tray_connect"), true, None);
@@ -88,6 +96,19 @@ impl SystemTray {
8896
))
8997
}
9098

99+
#[cfg(target_os = "linux")]
100+
pub fn new() -> anyhow::Result<(Self, SystemTrayStream)> {
101+
// placeholder here
102+
let (_sender, receiver) = mpsc::unbounded_channel();
103+
Ok((
104+
Self {},
105+
SystemTrayStream {
106+
receiver: Arc::new(Mutex::new(receiver)),
107+
},
108+
))
109+
}
110+
111+
#[cfg(not(target_os = "linux"))]
91112
pub fn update_menu_state(&mut self, disconnected: bool, status: &str) {
92113
// update menu item states
93114
self.item_connect.set_enabled(disconnected);
@@ -99,6 +120,11 @@ impl SystemTray {
99120
error!("failed to set tray icon tooltip: {e}");
100121
});
101122
}
123+
124+
#[cfg(target_os = "linux")]
125+
pub fn update_menu_state(&mut self, _disconnected: bool, _status: &str) {
126+
// placeholder here
127+
}
102128
}
103129

104130
impl SystemTrayStream {

0 commit comments

Comments
 (0)