From 485cbf4c18b65d8241e067fae5883213ce6f48c8 Mon Sep 17 00:00:00 2001 From: squid Date: Sun, 8 Nov 2026 21:04:33 +0100 Subject: [PATCH] updated shortcut functions on wayland/kde to stop showing shortcuts setting window on every launch --- src/hotkeys.rs | 92 +++++++++++++++++++++++++++++--------------------- src/main.rs | 42 +++++++++++++++++++++++ 2 files changed, 95 insertions(+), 39 deletions(-) diff --git a/src/hotkeys.rs b/src/hotkeys.rs index 180e030..a41502a 100644 --- a/src/hotkeys.rs +++ b/src/hotkeys.rs @@ -699,7 +699,7 @@ async fn run_portal( commands: &mut tokio::sync::mpsc::UnboundedReceiver, ) -> Result<(), String> { use ashpd::desktop::Session; - use ashpd::desktop::global_shortcuts::{GlobalShortcuts, NewShortcut}; + use ashpd::desktop::global_shortcuts::{GlobalShortcuts, ListShortcutsOptions, NewShortcut}; let connection = ashpd::zbus::Connection::session() .await @@ -752,11 +752,29 @@ async fn run_portal( }) .collect::>(); - if requested.is_empty() { - for binding in bindings { - shared.set_status(binding.action, Status::Disabled); - } + // KDE 6.3+ restores persisted shortcuts when the session is created. + // Ask for them first so a normal restart does not call BindShortcuts, + // which unconditionally opens the KDE shortcut configuration window. + let listed = proxy + .list_shortcuts(&session, ListShortcutsOptions::default()) + .await + .ok() + .and_then(|request| request.response().ok()) + .map(|response| response.shortcuts().to_vec()) + .unwrap_or_default(); + + let known = bindings.iter().any(|binding| { + binding.trigger.is_some() + && listed + .iter() + .any(|shortcut| shortcut.id() == binding.action.id()) + }); + + let shortcuts = if requested.is_empty() || known { + listed } else { + // First launch (or a portal that does not restore actions on session + // creation). This is the only call that may open a configuration UI. let request = proxy .bind_shortcuts(&session, &requested, None, Default::default()) .await @@ -766,35 +784,15 @@ async fn run_portal( let response = request.response().map_err(|error| { format!("The desktop environment rejected global shortcuts: {error}") })?; - let granted = response - .shortcuts() - .iter() - .map(|shortcut| (shortcut.id(), shortcut)) - .collect::>(); + response.shortcuts().to_vec() + }; - for binding in bindings { - if binding.trigger.is_none() { - shared.set_status(binding.action, Status::Disabled); - continue; - } - - match granted.get(binding.action.id()) { - Some(shortcut) => { - shared.set_status(binding.action, Status::Registered); - shared.send(Event::ShortcutChanged { - action: binding.action, - trigger: Some(shortcut.trigger_description().to_owned()), - }); - } - None => shared.set_status( - binding.action, - Status::Unavailable( - "The desktop environment did not grant this shortcut.".to_owned(), - ), - ), - } - } - } + apply_portal_shortcuts( + shared, + bindings, + &shortcuts, + "The shortcut is not configured in the desktop environment.", + ); loop { tokio::select! { @@ -823,7 +821,22 @@ fn apply_portal_changes( bindings: &[Binding], shortcuts: &[ashpd::desktop::global_shortcuts::Shortcut], ) { - let changed = shortcuts + apply_portal_shortcuts( + shared, + bindings, + shortcuts, + "The shortcut was removed in the desktop settings.", + ); +} + +#[cfg(target_os = "linux")] +fn apply_portal_shortcuts( + shared: &Shared, + bindings: &[Binding], + shortcuts: &[ashpd::desktop::global_shortcuts::Shortcut], + missing: &str, +) { + let known = shortcuts .iter() .map(|shortcut| (shortcut.id(), shortcut)) .collect::>(); @@ -834,7 +847,11 @@ fn apply_portal_changes( continue; } - match changed.get(binding.action.id()) { + match known.get(binding.action.id()) { + Some(shortcut) if shortcut.trigger_description().is_empty() => { + // The action exists but the user removed its key binding. + shared.set_status(binding.action, Status::Disabled); + } Some(shortcut) => { shared.set_status(binding.action, Status::Registered); shared.send(Event::ShortcutChanged { @@ -842,10 +859,7 @@ fn apply_portal_changes( trigger: Some(shortcut.trigger_description().to_owned()), }); } - None => shared.set_status( - binding.action, - Status::Unavailable("The shortcut was removed in the desktop settings.".to_owned()), - ), + None => shared.set_status(binding.action, Status::Unavailable(missing.to_owned())), } } } diff --git a/src/main.rs b/src/main.rs index fb2af4a..fd9e14d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1697,6 +1697,9 @@ struct Moorly { /// 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, + /// Set when the palette shortcut is used while the window is being + /// recreated, so focus can be requested after the new window exists. + pending_command_palette: bool, tray_available: bool, close_to_tray: bool, close_to_tray_draft: bool, @@ -4668,6 +4671,7 @@ impl Default for Moorly { focused_window: None, main_window_hidden: false, hidden_main_window: None, + pending_command_palette: false, tray_available: false, close_to_tray, close_to_tray_draft: close_to_tray, @@ -6421,7 +6425,16 @@ fn handle_tray_event(state: &mut Moorly, event: tray::Event) -> Task { } fn open_palette_from_hotkey(state: &mut Moorly) -> Task { + let reopening = state.hidden_main_window.is_some(); let show = show_main_window(state); + + if reopening { + // The new window does not exist yet, so focusing the palette now + // would act on nothing. Open it once the window reports `Opened`. + state.pending_command_palette = true; + return show; + } + let Some(window) = main_window_id(state) else { return show; }; @@ -6523,6 +6536,11 @@ fn window_event(state: &mut Moorly, window: window::Id, event: window::Event) -> window_state.position = position; } } + + if state.pending_command_palette && state.main_window == Some(window) { + state.pending_command_palette = false; + return update(state, Message::ToggleCommandPalette(window)); + } } window::Event::Moved(position) => { if let Some(window_state) = state.windows.get_mut(&window) { @@ -8143,8 +8161,12 @@ fn shortcut_row( .on_press(Message::OpenShortcutCapture(action)) .style(theme::outlined_option), ] + // Let the buttons drop to the next line in a narrow window instead of + // pushing the "..." button outside the pane. .spacing(8) .align_y(Alignment::Center) + .wrap() + .vertical_spacing(8) .into() } @@ -11261,6 +11283,7 @@ mod tests { focused_window: Some(window), main_window_hidden: false, hidden_main_window: None, + pending_command_palette: false, tray_available: false, close_to_tray: false, close_to_tray_draft: false, @@ -14085,6 +14108,25 @@ start_path = "/tmp" std::fs::remove_dir_all(&root).unwrap(); } + #[test] + fn palette_waits_for_the_reopened_window() { + let window = test_window(); + let mut state = self::state(); + state.pending_command_palette = true; + + let _ = super::window_event( + &mut state, + window, + super::window::Event::Opened { + position: None, + size: iced::Size::new(800.0, 600.0), + }, + ); + + assert!(!state.pending_command_palette); + assert!(window_state(&state).command_palette_open); + } + #[test] fn closing_the_window_only_hides_it_when_a_tray_is_available() { let window = test_window();