Added a setting for Minimize to tray
Moorly CI / windows-build (push) Successful in 3m36s
Moorly CI / linux-build (push) Successful in 4m11s

This commit is contained in:
Squid committed 2026-11-08 21:43:40 +01:00
1 parent e7afad602b
commit 4140d5a07c
1 file changed
+102 -3
+102 -3
View File
@@ -1722,6 +1722,8 @@ struct Moorly {
tray_available: bool, tray_available: bool,
close_to_tray: bool, close_to_tray: bool,
close_to_tray_draft: bool, close_to_tray_draft: bool,
minimize_to_tray: bool,
minimize_to_tray_draft: bool,
drag: Option<TabDrag>, drag: Option<TabDrag>,
theme: theme::Preference, theme: theme::Preference,
saved_theme: theme::Preference, saved_theme: theme::Preference,
@@ -2539,6 +2541,9 @@ struct GeneralConfig {
/// old behaviour so upgrades do not strand anyone without a tray host. /// old behaviour so upgrades do not strand anyone without a tray host.
#[serde(default)] #[serde(default)]
close_to_tray: bool, close_to_tray: bool,
/// Minimizing the main window hides it to the tray instead.
#[serde(default)]
minimize_to_tray: bool,
} }
impl Default for GeneralConfig { impl Default for GeneralConfig {
@@ -2546,6 +2551,7 @@ impl Default for GeneralConfig {
Self { Self {
fade_settings: true, fade_settings: true,
close_to_tray: false, close_to_tray: false,
minimize_to_tray: false,
} }
} }
} }
@@ -4497,6 +4503,7 @@ fn apply_settings(state: &mut Moorly) {
state.show_start_tab = state.show_start_tab_draft; state.show_start_tab = state.show_start_tab_draft;
state.fade_settings = state.fade_settings_draft; state.fade_settings = state.fade_settings_draft;
state.close_to_tray = state.close_to_tray_draft; state.close_to_tray = state.close_to_tray_draft;
state.minimize_to_tray = state.minimize_to_tray_draft;
state.hide_tab_close = state.hide_tab_close_draft; state.hide_tab_close = state.hide_tab_close_draft;
state.show_new_session_button = state.show_new_session_button_draft; state.show_new_session_button = state.show_new_session_button_draft;
state.show_command_palette_description = state.show_command_palette_description_draft; state.show_command_palette_description = state.show_command_palette_description_draft;
@@ -4536,6 +4543,7 @@ fn undo_settings(state: &mut Moorly) {
state.show_start_tab_draft = state.show_start_tab; state.show_start_tab_draft = state.show_start_tab;
state.fade_settings_draft = state.fade_settings; state.fade_settings_draft = state.fade_settings;
state.close_to_tray_draft = state.close_to_tray; state.close_to_tray_draft = state.close_to_tray;
state.minimize_to_tray_draft = state.minimize_to_tray;
state.hide_tab_close_draft = state.hide_tab_close; state.hide_tab_close_draft = state.hide_tab_close;
state.show_new_session_button_draft = state.show_new_session_button; state.show_new_session_button_draft = state.show_new_session_button;
state.show_command_palette_description_draft = state.show_command_palette_description; state.show_command_palette_description_draft = state.show_command_palette_description;
@@ -4587,6 +4595,7 @@ fn config_file(state: &Moorly) -> ConfigFile {
general: GeneralConfig { general: GeneralConfig {
fade_settings: state.fade_settings, fade_settings: state.fade_settings,
close_to_tray: state.close_to_tray, close_to_tray: state.close_to_tray,
minimize_to_tray: state.minimize_to_tray,
}, },
startup: StartupConfig { startup: StartupConfig {
show_start_tab: state.show_start_tab, show_start_tab: state.show_start_tab,
@@ -4662,6 +4671,7 @@ impl Default for Moorly {
let show_start_tab = config.startup.show_start_tab; let show_start_tab = config.startup.show_start_tab;
let fade_settings = config.general.fade_settings; let fade_settings = config.general.fade_settings;
let close_to_tray = config.general.close_to_tray; let close_to_tray = config.general.close_to_tray;
let minimize_to_tray = config.general.minimize_to_tray;
let hide_tab_close = config.appearance.hide_tab_close; let hide_tab_close = config.appearance.hide_tab_close;
let show_new_session_button = config.appearance.show_new_session_button; let show_new_session_button = config.appearance.show_new_session_button;
let show_command_palette_description = config.appearance.show_command_palette_description; let show_command_palette_description = config.appearance.show_command_palette_description;
@@ -4694,6 +4704,8 @@ impl Default for Moorly {
tray_available: false, tray_available: false,
close_to_tray, close_to_tray,
close_to_tray_draft: close_to_tray, close_to_tray_draft: close_to_tray,
minimize_to_tray,
minimize_to_tray_draft: minimize_to_tray,
drag: None, drag: None,
theme, theme,
saved_theme: theme, saved_theme: theme,
@@ -4823,6 +4835,7 @@ enum Message {
SetShowStartTab(bool), SetShowStartTab(bool),
SetFadeSettings(bool), SetFadeSettings(bool),
SetCloseToTray(bool), SetCloseToTray(bool),
SetMinimizeToTray(bool),
SetHideTabClose(bool), SetHideTabClose(bool),
SetShowNewSessionButton(bool), SetShowNewSessionButton(bool),
SetShowCommandPaletteDescription(bool), SetShowCommandPaletteDescription(bool),
@@ -4963,6 +4976,7 @@ enum Message {
Hotkey(hotkeys::Event), Hotkey(hotkeys::Event),
Tray(tray::Event), Tray(tray::Event),
FinishToggleWindowVisibility(Option<bool>), FinishToggleWindowVisibility(Option<bool>),
FinishMinimizeToTray(window::Id, Option<bool>),
Quit, Quit,
} }
@@ -5572,6 +5586,7 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
Message::SetShowStartTab(value) => state.show_start_tab_draft = value, Message::SetShowStartTab(value) => state.show_start_tab_draft = value,
Message::SetFadeSettings(value) => state.fade_settings_draft = value, Message::SetFadeSettings(value) => state.fade_settings_draft = value,
Message::SetCloseToTray(value) => state.close_to_tray_draft = value, Message::SetCloseToTray(value) => state.close_to_tray_draft = value,
Message::SetMinimizeToTray(value) => state.minimize_to_tray_draft = value,
Message::SetHideTabClose(value) => state.hide_tab_close_draft = value, Message::SetHideTabClose(value) => state.hide_tab_close_draft = value,
Message::SetShowNewSessionButton(value) => state.show_new_session_button_draft = value, Message::SetShowNewSessionButton(value) => state.show_new_session_button_draft = value,
Message::SetShowCommandPaletteDescription(value) => { Message::SetShowCommandPaletteDescription(value) => {
@@ -6280,6 +6295,16 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
Message::FinishToggleWindowVisibility(minimized) => { Message::FinishToggleWindowVisibility(minimized) => {
return finish_toggle_window_visibility(state, minimized); return finish_toggle_window_visibility(state, minimized);
} }
Message::FinishMinimizeToTray(window, minimized) => {
if minimized == Some(true)
&& state.minimize_to_tray
&& state.tray_available
&& state.main_window == Some(window)
&& !state.main_window_hidden
{
return hide_main_window(state);
}
}
Message::Quit => { Message::Quit => {
tray::shutdown(); tray::shutdown();
hotkeys::shutdown(); hotkeys::shutdown();
@@ -6570,6 +6595,24 @@ fn window_event(state: &mut Moorly, window: window::Id, event: window::Event) ->
tray::set_window_visible(true); tray::set_window_visible(true);
} }
} }
window::Event::Unfocused => {
if state.minimize_to_tray
&& state.tray_available
&& state.main_window == Some(window)
&& !state.main_window_hidden
{
// Unfocused also happens when switching apps, and the
// platform may not have applied the minimize state yet.
// Re-check once after the focus change settles.
return Task::future(async {
tokio::time::sleep(Duration::from_millis(200)).await;
})
.then(move |_| {
window::is_minimized(window)
.map(move |minimized| Message::FinishMinimizeToTray(window, minimized))
});
}
}
window::Event::CloseRequested => { window::Event::CloseRequested => {
// Keep sessions and the tray alive when the user opted in, but // Keep sessions and the tray alive when the user opted in, but
// only if there is actually a tray icon to restore the window. // only if there is actually a tray icon to restore the window.
@@ -9649,7 +9692,7 @@ fn command_palette_row(
} }
fn general_panel(state: &Moorly) -> Element<'_, Message> { fn general_panel(state: &Moorly) -> Element<'_, Message> {
column![ let mut panel = column![
row![ row![
styled_text(&state.typography.h3, "Fade In/Fade out animation"), styled_text(&state.typography.h3, "Fade In/Fade out animation"),
space::horizontal(), space::horizontal(),
@@ -9663,8 +9706,21 @@ fn general_panel(state: &Moorly) -> Element<'_, Message> {
] ]
.align_y(Alignment::Center), .align_y(Alignment::Center),
] ]
.spacing(12) .spacing(12);
.into()
// Wayland clients cannot query whether their window was minimized.
if !needs_window_recreation() {
panel = panel.push(
row![
styled_text(&state.typography.h3, "Minimize window to tray"),
space::horizontal(),
toggler(state.minimize_to_tray_draft).on_toggle(Message::SetMinimizeToTray),
]
.align_y(Alignment::Center),
);
}
panel.into()
} }
/// Startup settings that are edited as a draft until saved. /// Startup settings that are edited as a draft until saved.
@@ -9856,6 +9912,7 @@ fn settings_dirty(state: &Moorly) -> bool {
|| state.typography_draft != state.typography || state.typography_draft != state.typography
|| state.command_palette_enabled_draft != state.command_palette_enabled || state.command_palette_enabled_draft != state.command_palette_enabled
|| state.close_to_tray_draft != state.close_to_tray || state.close_to_tray_draft != state.close_to_tray
|| state.minimize_to_tray_draft != state.minimize_to_tray
|| state.theme_choice != state.saved_theme_choice || state.theme_choice != state.saved_theme_choice
|| state.shortcuts != state.saved_shortcuts || state.shortcuts != state.saved_shortcuts
} }
@@ -11291,6 +11348,8 @@ mod tests {
tray_available: false, tray_available: false,
close_to_tray: false, close_to_tray: false,
close_to_tray_draft: false, close_to_tray_draft: false,
minimize_to_tray: false,
minimize_to_tray_draft: false,
drag: None, drag: None,
theme: theme::Preference::default(), theme: theme::Preference::default(),
saved_theme: theme::Preference::default(), saved_theme: theme::Preference::default(),
@@ -11471,6 +11530,46 @@ mod tests {
assert!(!super::settings_dirty(&state)); assert!(!super::settings_dirty(&state));
} }
#[test]
fn minimize_to_tray_changes_wait_for_save_or_undo() {
let mut state = state();
let _ = update(&mut state, Message::SetMinimizeToTray(true));
assert!(!state.minimize_to_tray);
assert!(state.minimize_to_tray_draft);
assert!(super::settings_dirty(&state));
let _ = update(&mut state, Message::UndoChanges);
assert_eq!(state.minimize_to_tray_draft, state.minimize_to_tray);
assert!(!super::settings_dirty(&state));
let _ = update(&mut state, Message::SetMinimizeToTray(true));
super::apply_settings(&mut state);
assert!(state.minimize_to_tray);
assert_eq!(state.minimize_to_tray_draft, state.minimize_to_tray);
assert!(!super::settings_dirty(&state));
}
#[test]
fn minimizing_hides_to_tray_only_when_enabled() {
let window = test_window();
let mut state = state();
state.tray_available = true;
let _ = update(
&mut state,
Message::FinishMinimizeToTray(window, Some(true)),
);
assert!(!state.main_window_hidden);
state.minimize_to_tray = true;
let _ = update(
&mut state,
Message::FinishMinimizeToTray(window, Some(true)),
);
assert!(state.main_window_hidden);
}
#[test] #[test]
fn hiding_tab_close_waits_for_save_or_undo() { fn hiding_tab_close_waits_for_save_or_undo() {
let mut state = state(); let mut state = state();