Added tray function for windows and linux(kde/gnome). Global shortcut to show/hide program windows
This commit is contained in:
1 parent
0fe753f16e
commit
abbca0e411
5 files changed
+522
-18
No files matched your search
+206
-16
@@ -23,6 +23,8 @@ mod notifications;
|
||||
#[allow(dead_code)]
|
||||
mod terminal;
|
||||
mod theme;
|
||||
#[cfg_attr(test, allow(dead_code))]
|
||||
mod tray;
|
||||
|
||||
const MENUBAR_HEIGHT: f32 = 40.0;
|
||||
/// Settings heading sizes: tab title, sub-section header, normal text, caption.
|
||||
@@ -123,6 +125,21 @@ fn boot() -> (Moorly, Task<Message>) {
|
||||
#[cfg(not(test))]
|
||||
hotkeys::start(global_hotkey_bindings(&state));
|
||||
|
||||
#[cfg(not(test))]
|
||||
match tray::start() {
|
||||
Ok(()) => {
|
||||
state.tray_available = true;
|
||||
tray::set_window_visible(true);
|
||||
}
|
||||
Err(reason) => {
|
||||
state.notifications.add(
|
||||
notifications::Notification::new(notifications::NotificationKind::Warning, reason)
|
||||
.title("System tray unavailable")
|
||||
.persistent(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
(state, Task::batch([task.map(|_| Message::Ignore), fonts]))
|
||||
}
|
||||
|
||||
@@ -368,6 +385,7 @@ fn subscription(state: &Moorly) -> Subscription<Message> {
|
||||
terminal_keys,
|
||||
terminal_text,
|
||||
hotkeys::subscription().map(Message::Hotkey),
|
||||
tray::subscription().map(Message::Tray),
|
||||
iced::time::every(Duration::from_secs(1)).map(|_| Message::ReloadThemes),
|
||||
window::events().map(|(window, event)| Message::WindowEvent(window, event)),
|
||||
iced::event::listen_with(pointer_event),
|
||||
@@ -1676,6 +1694,12 @@ struct Moorly {
|
||||
main_window: Option<window::Id>,
|
||||
focused_window: Option<window::Id>,
|
||||
main_window_hidden: bool,
|
||||
/// Wayland cannot hide or restore a toplevel, so the window is destroyed
|
||||
/// while hidden and recreated from this state on show.
|
||||
hidden_main_window: Option<WindowState>,
|
||||
tray_available: bool,
|
||||
close_to_tray: bool,
|
||||
close_to_tray_draft: bool,
|
||||
drag: Option<TabDrag>,
|
||||
theme: theme::Preference,
|
||||
saved_theme: theme::Preference,
|
||||
@@ -2489,12 +2513,17 @@ impl Default for ConfigFile {
|
||||
struct GeneralConfig {
|
||||
#[serde(default = "default_true")]
|
||||
fade_settings: bool,
|
||||
/// Closing the main window hides it instead of exiting. Defaults to the
|
||||
/// old behaviour so upgrades do not strand anyone without a tray host.
|
||||
#[serde(default)]
|
||||
close_to_tray: bool,
|
||||
}
|
||||
|
||||
impl Default for GeneralConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
fade_settings: true,
|
||||
close_to_tray: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4445,6 +4474,7 @@ fn new_tab_close_animation() -> Animation<bool> {
|
||||
fn apply_settings(state: &mut Moorly) {
|
||||
state.show_start_tab = state.show_start_tab_draft;
|
||||
state.fade_settings = state.fade_settings_draft;
|
||||
state.close_to_tray = state.close_to_tray_draft;
|
||||
state.hide_tab_close = state.hide_tab_close_draft;
|
||||
state.show_new_session_button = state.show_new_session_button_draft;
|
||||
state.show_command_palette_description = state.show_command_palette_description_draft;
|
||||
@@ -4483,6 +4513,7 @@ fn apply_settings(state: &mut Moorly) {
|
||||
fn undo_settings(state: &mut Moorly) {
|
||||
state.show_start_tab_draft = state.show_start_tab;
|
||||
state.fade_settings_draft = state.fade_settings;
|
||||
state.close_to_tray_draft = state.close_to_tray;
|
||||
state.hide_tab_close_draft = state.hide_tab_close;
|
||||
state.show_new_session_button_draft = state.show_new_session_button;
|
||||
state.show_command_palette_description_draft = state.show_command_palette_description;
|
||||
@@ -4533,6 +4564,7 @@ fn config_file(state: &Moorly) -> ConfigFile {
|
||||
ConfigFile {
|
||||
general: GeneralConfig {
|
||||
fade_settings: state.fade_settings,
|
||||
close_to_tray: state.close_to_tray,
|
||||
},
|
||||
startup: StartupConfig {
|
||||
show_start_tab: state.show_start_tab,
|
||||
@@ -4607,6 +4639,7 @@ impl Default for Moorly {
|
||||
builtin_toast_config(&config.appearance);
|
||||
let show_start_tab = config.startup.show_start_tab;
|
||||
let fade_settings = config.general.fade_settings;
|
||||
let close_to_tray = config.general.close_to_tray;
|
||||
let hide_tab_close = config.appearance.hide_tab_close;
|
||||
let show_new_session_button = config.appearance.show_new_session_button;
|
||||
let show_command_palette_description = config.appearance.show_command_palette_description;
|
||||
@@ -4634,6 +4667,10 @@ impl Default for Moorly {
|
||||
main_window: None,
|
||||
focused_window: None,
|
||||
main_window_hidden: false,
|
||||
hidden_main_window: None,
|
||||
tray_available: false,
|
||||
close_to_tray,
|
||||
close_to_tray_draft: close_to_tray,
|
||||
drag: None,
|
||||
theme,
|
||||
saved_theme: theme,
|
||||
@@ -4762,6 +4799,7 @@ enum Message {
|
||||
SelectSettingsSection(SettingsSection),
|
||||
SetShowStartTab(bool),
|
||||
SetFadeSettings(bool),
|
||||
SetCloseToTray(bool),
|
||||
SetHideTabClose(bool),
|
||||
SetShowNewSessionButton(bool),
|
||||
SetShowCommandPaletteDescription(bool),
|
||||
@@ -4900,6 +4938,7 @@ enum Message {
|
||||
ScrollTabs(window::Id, mouse::ScrollDelta),
|
||||
ScrollTabsBy(window::Id, f32),
|
||||
Hotkey(hotkeys::Event),
|
||||
Tray(tray::Event),
|
||||
FinishToggleWindowVisibility(Option<bool>),
|
||||
Quit,
|
||||
}
|
||||
@@ -5509,6 +5548,7 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||
}
|
||||
Message::SetShowStartTab(value) => state.show_start_tab_draft = value,
|
||||
Message::SetFadeSettings(value) => state.fade_settings_draft = value,
|
||||
Message::SetCloseToTray(value) => state.close_to_tray_draft = value,
|
||||
Message::SetHideTabClose(value) => state.hide_tab_close_draft = value,
|
||||
Message::SetShowNewSessionButton(value) => state.show_new_session_button_draft = value,
|
||||
Message::SetShowCommandPaletteDescription(value) => {
|
||||
@@ -6213,10 +6253,12 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||
return scroll_tabs_by(state, window, amount);
|
||||
}
|
||||
Message::Hotkey(event) => return handle_hotkey_event(state, event),
|
||||
Message::Tray(event) => return handle_tray_event(state, event),
|
||||
Message::FinishToggleWindowVisibility(minimized) => {
|
||||
return finish_toggle_window_visibility(state, minimized);
|
||||
}
|
||||
Message::Quit => {
|
||||
tray::shutdown();
|
||||
hotkeys::shutdown();
|
||||
// Drop PTY sessions explicitly so their child processes are
|
||||
// terminated before process::exit bypasses the state destructor.
|
||||
@@ -6245,12 +6287,17 @@ fn main_window_id(state: &Moorly) -> Option<window::Id> {
|
||||
}
|
||||
|
||||
fn show_main_window(state: &mut Moorly) -> Task<Message> {
|
||||
if let Some(window_state) = state.hidden_main_window.take() {
|
||||
return reopen_main_window(state, window_state);
|
||||
}
|
||||
|
||||
let Some(window) = main_window_id(state) else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
state.main_window_hidden = false;
|
||||
state.focused_window = Some(window);
|
||||
tray::set_window_visible(true);
|
||||
|
||||
Task::batch([
|
||||
window::set_mode(window, window::Mode::Windowed),
|
||||
@@ -6259,39 +6306,126 @@ fn show_main_window(state: &mut Moorly) -> Task<Message> {
|
||||
])
|
||||
}
|
||||
|
||||
fn toggle_main_window(state: &mut Moorly) -> Task<Message> {
|
||||
let Some(window) = main_window_id(state) else {
|
||||
return Task::none();
|
||||
/// Wayland cannot unmap or restore an existing toplevel, so the hidden window
|
||||
/// is recreated from its saved UI state.
|
||||
fn reopen_main_window(state: &mut Moorly, window_state: WindowState) -> Task<Message> {
|
||||
let settings = window::Settings {
|
||||
size: window_state.size,
|
||||
position: window_state
|
||||
.position
|
||||
.map_or(window::Position::Default, window::Position::Specific),
|
||||
exit_on_close_request: false,
|
||||
..window::Settings::default()
|
||||
};
|
||||
let (id, open) = window::open(settings);
|
||||
|
||||
state.windows.insert(id, window_state);
|
||||
state.window_order.push(id);
|
||||
state.main_window = Some(id);
|
||||
state.focused_window = Some(id);
|
||||
state.main_window_hidden = false;
|
||||
tray::set_window_visible(true);
|
||||
|
||||
Task::batch([open.map(|_| Message::Ignore), window::gain_focus(id)])
|
||||
}
|
||||
|
||||
fn toggle_main_window(state: &mut Moorly) -> Task<Message> {
|
||||
if state.main_window_hidden {
|
||||
return show_main_window(state);
|
||||
}
|
||||
|
||||
let Some(window) = main_window_id(state) else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
// A minimized window is still visible as far as `Mode` is concerned, so
|
||||
// ask the platform before deciding to hide it.
|
||||
window::is_minimized(window).map(Message::FinishToggleWindowVisibility)
|
||||
}
|
||||
|
||||
fn finish_toggle_window_visibility(state: &mut Moorly, minimized: Option<bool>) -> Task<Message> {
|
||||
let Some(window) = main_window_id(state) else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
if minimized == Some(true) {
|
||||
// Wayland cannot unminimize either, so replace the window instead.
|
||||
if needs_window_recreation()
|
||||
&& let Some(window) = main_window_id(state)
|
||||
{
|
||||
state.main_window_hidden = true;
|
||||
tray::set_window_visible(false);
|
||||
let close = detach_main_window(state, window);
|
||||
let open = show_main_window(state);
|
||||
return Task::batch([close, open]);
|
||||
}
|
||||
|
||||
return show_main_window(state);
|
||||
}
|
||||
|
||||
state.main_window_hidden = true;
|
||||
window::set_mode(window, window::Mode::Hidden)
|
||||
hide_main_window(state)
|
||||
}
|
||||
|
||||
fn open_palette_from_hotkey(state: &mut Moorly) -> Task<Message> {
|
||||
fn needs_window_recreation() -> bool {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
hotkeys::is_wayland_session()
|
||||
}
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
{
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Hides the main window without closing it, keeping the application and its
|
||||
/// sessions alive. Shared by the global hotkey and the tray.
|
||||
fn hide_main_window(state: &mut Moorly) -> Task<Message> {
|
||||
if state.main_window_hidden || state.hidden_main_window.is_some() {
|
||||
return Task::none();
|
||||
}
|
||||
|
||||
let Some(window) = main_window_id(state) else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
state.main_window_hidden = true;
|
||||
tray::set_window_visible(false);
|
||||
|
||||
if needs_window_recreation() {
|
||||
detach_main_window(state, window)
|
||||
} else {
|
||||
window::set_mode(window, window::Mode::Hidden)
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes the main window from the UI state and destroys its native window,
|
||||
/// keeping all tabs and sessions for a later restore.
|
||||
fn detach_main_window(state: &mut Moorly, window: window::Id) -> Task<Message> {
|
||||
let Some(window_state) = state.windows.remove(&window) else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
state.window_order.retain(|id| *id != window);
|
||||
if state.focused_window == Some(window) {
|
||||
state.focused_window = state.window_order.first().copied();
|
||||
}
|
||||
if state.main_window == Some(window) {
|
||||
state.main_window = None;
|
||||
}
|
||||
state.hidden_main_window = Some(window_state);
|
||||
|
||||
window::close(window)
|
||||
}
|
||||
|
||||
fn handle_tray_event(state: &mut Moorly, event: tray::Event) -> Task<Message> {
|
||||
match event {
|
||||
tray::Event::Toggle => toggle_main_window(state),
|
||||
tray::Event::Quit => update(state, Message::Quit),
|
||||
}
|
||||
}
|
||||
|
||||
fn open_palette_from_hotkey(state: &mut Moorly) -> Task<Message> {
|
||||
let show = show_main_window(state);
|
||||
let Some(window) = main_window_id(state) else {
|
||||
return show;
|
||||
};
|
||||
|
||||
let palette = update(state, Message::ToggleCommandPalette(window));
|
||||
|
||||
Task::batch([show, palette])
|
||||
@@ -6400,11 +6534,23 @@ fn window_event(state: &mut Moorly, window: window::Id, event: window::Event) ->
|
||||
window_state.size = size;
|
||||
}
|
||||
}
|
||||
window::Event::Focused => state.focused_window = Some(window),
|
||||
window::Event::Focused => {
|
||||
state.focused_window = Some(window);
|
||||
if state.main_window == Some(window) {
|
||||
tray::set_window_visible(true);
|
||||
}
|
||||
}
|
||||
window::Event::CloseRequested => {
|
||||
// Keep sessions and the tray alive when the user opted in, but
|
||||
// only if there is actually a tray icon to restore the window.
|
||||
if state.main_window == Some(window) && state.close_to_tray && state.tray_available {
|
||||
return hide_main_window(state);
|
||||
}
|
||||
|
||||
remove_window(state, window);
|
||||
|
||||
if state.windows.is_empty() {
|
||||
tray::shutdown();
|
||||
hotkeys::shutdown();
|
||||
return iced::exit();
|
||||
}
|
||||
@@ -6415,6 +6561,11 @@ fn window_event(state: &mut Moorly, window: window::Id, event: window::Event) ->
|
||||
remove_window(state, window);
|
||||
|
||||
if state.windows.is_empty() {
|
||||
if state.hidden_main_window.is_some() {
|
||||
return Task::none();
|
||||
}
|
||||
|
||||
tray::shutdown();
|
||||
hotkeys::shutdown();
|
||||
return iced::exit();
|
||||
}
|
||||
@@ -6844,6 +6995,8 @@ fn close_window_if_empty(state: &mut Moorly, window: window::Id) -> Task<Message
|
||||
remove_window(state, window);
|
||||
|
||||
if state.windows.is_empty() {
|
||||
tray::shutdown();
|
||||
hotkeys::shutdown();
|
||||
return iced::exit();
|
||||
}
|
||||
|
||||
@@ -9471,12 +9624,21 @@ fn command_palette_row(
|
||||
}
|
||||
|
||||
fn general_panel(state: &Moorly) -> Element<'_, Message> {
|
||||
row![
|
||||
styled_text(&state.typography.h3, "Fade In/Fade out animation"),
|
||||
space::horizontal(),
|
||||
toggler(state.fade_settings_draft).on_toggle(Message::SetFadeSettings),
|
||||
column![
|
||||
row![
|
||||
styled_text(&state.typography.h3, "Fade In/Fade out animation"),
|
||||
space::horizontal(),
|
||||
toggler(state.fade_settings_draft).on_toggle(Message::SetFadeSettings),
|
||||
]
|
||||
.align_y(Alignment::Center),
|
||||
row![
|
||||
styled_text(&state.typography.h3, "Close window to tray"),
|
||||
space::horizontal(),
|
||||
toggler(state.close_to_tray_draft).on_toggle(Message::SetCloseToTray),
|
||||
]
|
||||
.align_y(Alignment::Center),
|
||||
]
|
||||
.align_y(Alignment::Center)
|
||||
.spacing(12)
|
||||
.into()
|
||||
}
|
||||
|
||||
@@ -11098,6 +11260,10 @@ mod tests {
|
||||
main_window: Some(window),
|
||||
focused_window: Some(window),
|
||||
main_window_hidden: false,
|
||||
hidden_main_window: None,
|
||||
tray_available: false,
|
||||
close_to_tray: false,
|
||||
close_to_tray_draft: false,
|
||||
drag: None,
|
||||
theme: theme::Preference::default(),
|
||||
saved_theme: theme::Preference::default(),
|
||||
@@ -13918,4 +14084,28 @@ start_path = "/tmp"
|
||||
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_the_window_only_hides_it_when_a_tray_is_available() {
|
||||
let window = test_window();
|
||||
|
||||
let mut state = self::state();
|
||||
state.close_to_tray = true;
|
||||
state.tray_available = true;
|
||||
let _ = update(
|
||||
&mut state,
|
||||
Message::WindowEvent(window, super::window::Event::CloseRequested),
|
||||
);
|
||||
assert!(state.windows.contains_key(&window) || state.hidden_main_window.is_some());
|
||||
assert!(state.main_window_hidden);
|
||||
|
||||
let mut state = self::state();
|
||||
state.close_to_tray = true;
|
||||
state.tray_available = false;
|
||||
let _ = update(
|
||||
&mut state,
|
||||
Message::WindowEvent(window, super::window::Event::CloseRequested),
|
||||
);
|
||||
assert!(!state.windows.contains_key(&window));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user