Skip to content

Commit 65fd189

Browse files
committed
fix: no background window on Linux
1 parent 0aa7a17 commit 65fd189

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

RustApp/src/audio/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,8 @@ impl AppState {
8686
if let Err(e) = stream.play() {
8787
error!("{e}");
8888
}
89-
} else {
90-
if let Err(e) = stream.pause() {
91-
error!("{e}");
92-
}
89+
} else if let Err(e) = stream.pause() {
90+
error!("{e}");
9391
}
9492

9593
self.audio_stream = Some(Stream {

RustApp/src/ui/app.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ use cosmic::{
2424

2525
use super::{
2626
message::{AppMsg, ConfigMsg},
27-
tray::{SystemTray, SystemTrayMsg, SystemTrayStream},
2827
view::{main_window, settings_window},
2928
wave::AudioWave,
3029
};
30+
31+
#[cfg(not(target_os = "linux"))]
32+
use super::tray::{SystemTray, SystemTrayMsg, SystemTrayStream};
33+
3134
use crate::{
3235
audio::{AudioPacketFormat, AudioProcessParams},
3336
config::{
@@ -128,7 +131,9 @@ pub struct AppState {
128131
pub about_window: Option<CustomWindow>,
129132
pub logs: Vec<markdown::Item>,
130133
log_path: String,
134+
#[cfg(not(target_os = "linux"))]
131135
pub system_tray: Option<SystemTray>,
136+
#[cfg(not(target_os = "linux"))]
132137
pub system_tray_stream: Option<SystemTrayStream>,
133138
has_shown_minimize_notification: bool,
134139
launched_automatically: bool,
@@ -240,6 +245,7 @@ impl AppState {
240245
self.audio_stream = None;
241246
self.audio_wave.clear();
242247

248+
#[cfg(not(target_os = "linux"))]
243249
if let Some(system_tray) = self.system_tray.as_mut() {
244250
system_tray.update_menu_state(true, &fl!("state_disconnected"));
245251
}
@@ -349,6 +355,7 @@ impl Application for AppState {
349355
};
350356

351357
// initialize system tray
358+
#[cfg(not(target_os = "linux"))]
352359
let (system_tray, system_tray_stream) = match SystemTray::new() {
353360
Ok((mut tray, stream)) => {
354361
tray.update_menu_state(true, &fl!("state_disconnected"));
@@ -379,7 +386,9 @@ impl Application for AppState {
379386
about_window: None,
380387
logs: Vec::new(),
381388
log_path: flags.log_path.clone(),
389+
#[cfg(not(target_os = "linux"))]
382390
system_tray,
391+
#[cfg(not(target_os = "linux"))]
383392
system_tray_stream,
384393
has_shown_minimize_notification: false,
385394
launched_automatically: flags.launched_automatically,
@@ -400,10 +409,14 @@ impl Application for AppState {
400409
info!("config path: {}", flags.config_path);
401410
info!("log path: {}", flags.log_path);
402411

412+
#[cfg(not(target_os = "linux"))]
403413
if !flags.launched_automatically || !app.config.data().start_minimized {
404414
commands.push(app.open_main_window());
405415
}
406416

417+
#[cfg(target_os = "linux")]
418+
commands.push(app.open_main_window());
419+
407420
match single_instance::stream() {
408421
Ok(stream) => {
409422
commands.push(cosmic::iced::task::Task::run(stream, |event| match event {
@@ -455,6 +468,7 @@ impl Application for AppState {
455468
error!("{e}");
456469
}
457470

471+
#[cfg(not(target_os = "linux"))]
458472
if let Some(system_tray) = self.system_tray.as_mut() {
459473
system_tray.update_menu_state(false, &fl!("state_listening"));
460474
}
@@ -487,6 +501,7 @@ impl Application for AppState {
487501
error!("{e}");
488502
}
489503

504+
#[cfg(not(target_os = "linux"))]
490505
if let Some(system_tray) = self.system_tray.as_mut() {
491506
system_tray.update_menu_state(false, &fl!("state_connected"));
492507
}
@@ -756,6 +771,7 @@ impl Application for AppState {
756771
error!("{e}");
757772
}
758773
}
774+
#[cfg(not(target_os = "linux"))]
759775
AppMsg::SystemTray(tray_msg) => match tray_msg {
760776
SystemTrayMsg::Show => {
761777
if let Some(main_window) = &self.main_window {
@@ -791,6 +807,9 @@ impl Application for AppState {
791807
return Task::batch(vec![command, self.update_audio_stream()]);
792808
}
793809
}
810+
AppMsg::Exit => {
811+
return cosmic::iced_runtime::task::effect(cosmic::iced::runtime::Action::Exit);
812+
}
794813
}
795814

796815
Task::none()
@@ -822,8 +841,9 @@ impl Application for AppState {
822841
}
823842

824843
fn subscription(&self) -> cosmic::iced::Subscription<Self::Message> {
825-
let mut subscriptions = vec![Subscription::run(|| streamer::sub().map(AppMsg::Streamer))];
844+
let subscriptions = vec![Subscription::run(|| streamer::sub().map(AppMsg::Streamer))];
826845

846+
#[cfg(not(target_os = "linux"))]
827847
if let Some(system_tray_stream) = &self.system_tray_stream {
828848
subscriptions.push(Subscription::run_with_id(
829849
"system-tray",
@@ -848,8 +868,11 @@ impl Application for AppState {
848868
if let Some(window) = &self.main_window
849869
&& window.window_id == id
850870
{
851-
// close the app
871+
#[cfg(not(target_os = "linux"))]
852872
return Some(AppMsg::HideWindow);
873+
874+
#[cfg(target_os = "linux")]
875+
return Some(AppMsg::Exit);
853876
}
854877

855878
None

RustApp/src/ui/message.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::app::AudioDevice;
2+
#[cfg(not(target_os = "linux"))]
23
use super::tray::SystemTrayMsg;
34
use crate::{
45
config::{
@@ -24,7 +25,9 @@ pub enum AppMsg {
2425
ShowWindow,
2526
Menu(MenuMsg),
2627
LinkClicked(String),
28+
#[cfg(not(target_os = "linux"))]
2729
SystemTray(SystemTrayMsg),
30+
Exit,
2831
}
2932

3033
#[derive(Debug, Clone)]

RustApp/src/ui/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub mod app;
22
mod icon;
33
mod message;
4+
5+
#[cfg(not(target_os = "linux"))]
46
mod tray;
57
mod view;
68
mod wave;

RustApp/src/ui/tray.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ use futures::SinkExt;
33
use std::sync::Arc;
44
use tokio::sync::{Mutex, mpsc};
55

6-
#[cfg(not(target_os = "linux"))]
76
use tray_icon::{
87
TrayIcon, TrayIconBuilder, TrayIconEvent,
98
menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem},
109
};
1110

12-
#[cfg(not(target_os = "linux"))]
1311
use crate::{fl, tray_icon};
1412

1513
#[derive(Debug, Clone)]
@@ -25,18 +23,13 @@ pub struct SystemTrayStream {
2523
receiver: Arc<Mutex<mpsc::UnboundedReceiver<SystemTrayMsg>>>,
2624
}
2725

28-
#[cfg(not(target_os = "linux"))]
2926
pub struct SystemTray {
3027
tray_icon: TrayIcon,
3128
item_connect: MenuItem,
3229
item_disconnect: MenuItem,
3330
}
3431

35-
#[cfg(target_os = "linux")]
36-
pub struct SystemTray;
37-
3832
impl SystemTray {
39-
#[cfg(not(target_os = "linux"))]
4033
pub fn new() -> anyhow::Result<(Self, SystemTrayStream)> {
4134
let item_show = MenuItem::new(fl!("tray_show_window"), true, None);
4235
let item_connect = MenuItem::new(fl!("tray_connect"), true, None);
@@ -95,19 +88,6 @@ impl SystemTray {
9588
))
9689
}
9790

98-
#[cfg(target_os = "linux")]
99-
pub fn new() -> anyhow::Result<(Self, SystemTrayStream)> {
100-
// placeholder here
101-
let (_sender, receiver) = mpsc::unbounded_channel();
102-
Ok((
103-
Self {},
104-
SystemTrayStream {
105-
receiver: Arc::new(Mutex::new(receiver)),
106-
},
107-
))
108-
}
109-
110-
#[cfg(not(target_os = "linux"))]
11191
pub fn update_menu_state(&mut self, disconnected: bool, status: &str) {
11292
// update menu item states
11393
self.item_connect.set_enabled(disconnected);
@@ -119,11 +99,6 @@ impl SystemTray {
11999
error!("failed to set tray icon tooltip: {e}");
120100
});
121101
}
122-
123-
#[cfg(target_os = "linux")]
124-
pub fn update_menu_state(&mut self, _disconnected: bool, _status: &str) {
125-
// placeholder here
126-
}
127102
}
128103

129104
impl SystemTrayStream {

0 commit comments

Comments
 (0)