Added global hotkeys for windows, gnome and windows 10/11
This commit is contained in:
1 parent
c05dc3f9f5
commit
0fe753f16e
5 files changed
+1274
-11
No files matched your search
+252
-9
@@ -17,6 +17,8 @@ use iced::{
|
||||
Size, Subscription, Task, Vector, font, keyboard, mouse, window,
|
||||
};
|
||||
|
||||
#[cfg_attr(test, allow(dead_code))]
|
||||
mod hotkeys;
|
||||
mod notifications;
|
||||
#[allow(dead_code)]
|
||||
mod terminal;
|
||||
@@ -118,6 +120,9 @@ fn boot() -> (Moorly, Task<Message>) {
|
||||
.map(|_| Message::Ignore),
|
||||
]);
|
||||
|
||||
#[cfg(not(test))]
|
||||
hotkeys::start(global_hotkey_bindings(&state));
|
||||
|
||||
(state, Task::batch([task.map(|_| Message::Ignore), fonts]))
|
||||
}
|
||||
|
||||
@@ -317,11 +322,13 @@ fn subscription(state: &Moorly) -> Subscription<Message> {
|
||||
state.saved_shortcuts.command_palette.clone(),
|
||||
state.shortcut_capture.is_some(),
|
||||
state.command_palette_enabled,
|
||||
hotkeys::revision(),
|
||||
))
|
||||
.filter_map(
|
||||
|((shortcut, capturing, enabled), (key, modifiers, window))| {
|
||||
|((shortcut, capturing, enabled, _), (key, modifiers, window))| {
|
||||
(enabled
|
||||
&& !capturing
|
||||
&& !hotkeys::is_registered(hotkeys::Action::OpenCommandPalette)
|
||||
&& shortcut
|
||||
.as_ref()
|
||||
.is_some_and(|shortcut| shortcut.matches(&key, modifiers)))
|
||||
@@ -360,6 +367,7 @@ fn subscription(state: &Moorly) -> Subscription<Message> {
|
||||
palette_shortcut,
|
||||
terminal_keys,
|
||||
terminal_text,
|
||||
hotkeys::subscription().map(Message::Hotkey),
|
||||
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),
|
||||
@@ -515,6 +523,7 @@ impl std::fmt::Display for Shortcut {
|
||||
enum ShortcutAction {
|
||||
Settings,
|
||||
Quit,
|
||||
ToggleWindow,
|
||||
CommandPalette,
|
||||
QuickTerminal,
|
||||
QuickSsh,
|
||||
@@ -525,6 +534,7 @@ impl ShortcutAction {
|
||||
match self {
|
||||
Self::Settings => "Settings",
|
||||
Self::Quit => "Quit",
|
||||
Self::ToggleWindow => "Show/hide window",
|
||||
Self::CommandPalette => "Command palette",
|
||||
Self::QuickTerminal => "Quick terminal",
|
||||
Self::QuickSsh => "Quick SSH",
|
||||
@@ -536,6 +546,7 @@ impl ShortcutAction {
|
||||
struct Shortcuts {
|
||||
settings: Option<Shortcut>,
|
||||
quit: Option<Shortcut>,
|
||||
toggle_window: Option<Shortcut>,
|
||||
command_palette: Option<Shortcut>,
|
||||
quick_terminal: Option<Shortcut>,
|
||||
quick_ssh: Option<Shortcut>,
|
||||
@@ -546,6 +557,7 @@ impl Default for Shortcuts {
|
||||
Self {
|
||||
settings: Shortcut::parse("Ctrl+,"),
|
||||
quit: Shortcut::parse("Ctrl+Shift+Q"),
|
||||
toggle_window: Shortcut::parse("Ctrl+Space"),
|
||||
command_palette: Shortcut::parse("Ctrl+Shift+Space"),
|
||||
quick_terminal: Shortcut::parse("Ctrl+Shift+T"),
|
||||
quick_ssh: Shortcut::parse("Ctrl+Shift+S"),
|
||||
@@ -558,6 +570,7 @@ impl Shortcuts {
|
||||
match action {
|
||||
ShortcutAction::Settings => self.settings.as_ref(),
|
||||
ShortcutAction::Quit => self.quit.as_ref(),
|
||||
ShortcutAction::ToggleWindow => self.toggle_window.as_ref(),
|
||||
ShortcutAction::CommandPalette => self.command_palette.as_ref(),
|
||||
ShortcutAction::QuickTerminal => self.quick_terminal.as_ref(),
|
||||
ShortcutAction::QuickSsh => self.quick_ssh.as_ref(),
|
||||
@@ -568,6 +581,7 @@ impl Shortcuts {
|
||||
match action {
|
||||
ShortcutAction::Settings => self.settings = shortcut,
|
||||
ShortcutAction::Quit => self.quit = shortcut,
|
||||
ShortcutAction::ToggleWindow => self.toggle_window = shortcut,
|
||||
ShortcutAction::CommandPalette => self.command_palette = shortcut,
|
||||
ShortcutAction::QuickTerminal => self.quick_terminal = shortcut,
|
||||
ShortcutAction::QuickSsh => self.quick_ssh = shortcut,
|
||||
@@ -580,6 +594,7 @@ impl Shortcuts {
|
||||
let configurable = [
|
||||
(ShortcutAction::Settings, &self.settings),
|
||||
(ShortcutAction::Quit, &self.quit),
|
||||
(ShortcutAction::ToggleWindow, &self.toggle_window),
|
||||
(ShortcutAction::CommandPalette, &self.command_palette),
|
||||
(ShortcutAction::QuickTerminal, &self.quick_terminal),
|
||||
(ShortcutAction::QuickSsh, &self.quick_ssh),
|
||||
@@ -608,6 +623,36 @@ struct ShortcutCapture {
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
/// Global actions and their configured triggers. Disabled actions are still
|
||||
/// passed so an already registered shortcut is removed.
|
||||
fn global_hotkey_bindings(state: &Moorly) -> Vec<hotkeys::Binding> {
|
||||
let trigger =
|
||||
|action: ShortcutAction| state.saved_shortcuts.get(action).map(ToString::to_string);
|
||||
|
||||
vec![
|
||||
hotkeys::Binding {
|
||||
action: hotkeys::Action::ToggleWindowVisibility,
|
||||
trigger: trigger(ShortcutAction::ToggleWindow),
|
||||
},
|
||||
hotkeys::Binding {
|
||||
action: hotkeys::Action::OpenCommandPalette,
|
||||
trigger: state
|
||||
.command_palette_enabled
|
||||
.then(|| trigger(ShortcutAction::CommandPalette))
|
||||
.flatten(),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
/// Re-applies the saved bindings after settings are committed. The portal
|
||||
/// owns its shortcuts, so the service ignores updates on Wayland.
|
||||
fn sync_global_hotkeys(state: &Moorly) {
|
||||
#[cfg(not(test))]
|
||||
hotkeys::set_bindings(global_hotkey_bindings(state));
|
||||
#[cfg(test)]
|
||||
let _ = state;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
enum ProfileKind {
|
||||
@@ -1630,6 +1675,7 @@ struct Moorly {
|
||||
window_order: Vec<window::Id>,
|
||||
main_window: Option<window::Id>,
|
||||
focused_window: Option<window::Id>,
|
||||
main_window_hidden: bool,
|
||||
drag: Option<TabDrag>,
|
||||
theme: theme::Preference,
|
||||
saved_theme: theme::Preference,
|
||||
@@ -1696,6 +1742,8 @@ struct Moorly {
|
||||
terminal_sessions: HashMap<u32, TerminalSession>,
|
||||
next_terminal_tab: u32,
|
||||
notifications: notifications::NotificationManager,
|
||||
hotkey_notified: HashSet<String>,
|
||||
hotkey_triggers: HashMap<hotkeys::Action, String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
@@ -2630,6 +2678,8 @@ struct ShortcutConfig {
|
||||
settings: String,
|
||||
#[serde(default = "default_quit_shortcut")]
|
||||
quit: String,
|
||||
#[serde(default = "default_toggle_window_shortcut")]
|
||||
toggle_window: String,
|
||||
#[serde(default = "default_command_palette_shortcut")]
|
||||
command_palette: String,
|
||||
#[serde(default = "default_quick_terminal_shortcut")]
|
||||
@@ -2643,6 +2693,7 @@ impl Default for ShortcutConfig {
|
||||
Self {
|
||||
settings: default_settings_shortcut(),
|
||||
quit: default_quit_shortcut(),
|
||||
toggle_window: default_toggle_window_shortcut(),
|
||||
command_palette: default_command_palette_shortcut(),
|
||||
quick_terminal: default_quick_terminal_shortcut(),
|
||||
quick_ssh: default_quick_ssh_shortcut(),
|
||||
@@ -2691,6 +2742,13 @@ fn default_command_palette_shortcut() -> String {
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn default_toggle_window_shortcut() -> String {
|
||||
Shortcuts::default()
|
||||
.toggle_window
|
||||
.map(|shortcut| shortcut.to_string())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn default_quick_terminal_shortcut() -> String {
|
||||
Shortcuts::default()
|
||||
.quick_terminal
|
||||
@@ -4508,6 +4566,12 @@ fn config_file(state: &Moorly) -> ConfigFile {
|
||||
.as_ref()
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_default(),
|
||||
toggle_window: state
|
||||
.saved_shortcuts
|
||||
.toggle_window
|
||||
.as_ref()
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_default(),
|
||||
command_palette: state
|
||||
.saved_shortcuts
|
||||
.command_palette
|
||||
@@ -4551,6 +4615,7 @@ impl Default for Moorly {
|
||||
let shortcuts = Shortcuts {
|
||||
settings: Shortcut::parse(&config.shortcuts.settings),
|
||||
quit: Shortcut::parse(&config.shortcuts.quit),
|
||||
toggle_window: Shortcut::parse(&config.shortcuts.toggle_window),
|
||||
command_palette: Shortcut::parse(&config.shortcuts.command_palette),
|
||||
quick_terminal: Shortcut::parse(&config.shortcuts.quick_terminal),
|
||||
quick_ssh: Shortcut::parse(&config.shortcuts.quick_ssh),
|
||||
@@ -4568,6 +4633,7 @@ impl Default for Moorly {
|
||||
window_order: Vec::new(),
|
||||
main_window: None,
|
||||
focused_window: None,
|
||||
main_window_hidden: false,
|
||||
drag: None,
|
||||
theme,
|
||||
saved_theme: theme,
|
||||
@@ -4634,6 +4700,8 @@ impl Default for Moorly {
|
||||
terminal_sessions: HashMap::new(),
|
||||
next_terminal_tab: 1,
|
||||
notifications,
|
||||
hotkey_notified: HashSet::new(),
|
||||
hotkey_triggers: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4831,6 +4899,8 @@ enum Message {
|
||||
TabsScrolled(window::Id, scrollable::Viewport),
|
||||
ScrollTabs(window::Id, mouse::ScrollDelta),
|
||||
ScrollTabsBy(window::Id, f32),
|
||||
Hotkey(hotkeys::Event),
|
||||
FinishToggleWindowVisibility(Option<bool>),
|
||||
Quit,
|
||||
}
|
||||
|
||||
@@ -5520,6 +5590,7 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||
Message::SaveSettings => {
|
||||
apply_settings(state);
|
||||
save_config(state);
|
||||
sync_global_hotkeys(state);
|
||||
}
|
||||
Message::UndoChanges => undo_settings(state),
|
||||
Message::Notification(action) => match action {
|
||||
@@ -6141,7 +6212,12 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||
Message::ScrollTabsBy(window, amount) => {
|
||||
return scroll_tabs_by(state, window, amount);
|
||||
}
|
||||
Message::Hotkey(event) => return handle_hotkey_event(state, event),
|
||||
Message::FinishToggleWindowVisibility(minimized) => {
|
||||
return finish_toggle_window_visibility(state, minimized);
|
||||
}
|
||||
Message::Quit => {
|
||||
hotkeys::shutdown();
|
||||
// Drop PTY sessions explicitly so their child processes are
|
||||
// terminated before process::exit bypasses the state destructor.
|
||||
state.terminal_sessions.clear();
|
||||
@@ -6160,6 +6236,125 @@ fn close_command_palette(state: &mut Moorly, window: window::Id) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Main window to restore for a global action.
|
||||
fn main_window_id(state: &Moorly) -> Option<window::Id> {
|
||||
state
|
||||
.main_window
|
||||
.or(state.focused_window)
|
||||
.or_else(|| state.window_order.first().copied())
|
||||
}
|
||||
|
||||
fn show_main_window(state: &mut Moorly) -> Task<Message> {
|
||||
let Some(window) = main_window_id(state) else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
state.main_window_hidden = false;
|
||||
state.focused_window = Some(window);
|
||||
|
||||
Task::batch([
|
||||
window::set_mode(window, window::Mode::Windowed),
|
||||
window::minimize(window, false),
|
||||
window::gain_focus(window),
|
||||
])
|
||||
}
|
||||
|
||||
fn toggle_main_window(state: &mut Moorly) -> Task<Message> {
|
||||
let Some(window) = main_window_id(state) else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
if state.main_window_hidden {
|
||||
return show_main_window(state);
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return show_main_window(state);
|
||||
}
|
||||
|
||||
state.main_window_hidden = true;
|
||||
window::set_mode(window, window::Mode::Hidden)
|
||||
}
|
||||
|
||||
fn open_palette_from_hotkey(state: &mut Moorly) -> Task<Message> {
|
||||
let Some(window) = main_window_id(state) else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
let show = show_main_window(state);
|
||||
let palette = update(state, Message::ToggleCommandPalette(window));
|
||||
|
||||
Task::batch([show, palette])
|
||||
}
|
||||
|
||||
fn handle_hotkey_event(state: &mut Moorly, event: hotkeys::Event) -> Task<Message> {
|
||||
use notifications::{Notification, NotificationKind};
|
||||
|
||||
match event {
|
||||
hotkeys::Event::Activated(action) => match action {
|
||||
hotkeys::Action::ToggleWindowVisibility => return toggle_main_window(state),
|
||||
hotkeys::Action::OpenCommandPalette => return open_palette_from_hotkey(state),
|
||||
},
|
||||
hotkeys::Event::Status { action, status } => {
|
||||
let hotkeys::Status::Unavailable(reason) = status else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
let key = format!("status:{}:{reason}", action.id());
|
||||
|
||||
if state.hotkey_notified.insert(key) {
|
||||
state.notifications.add(
|
||||
Notification::new(
|
||||
NotificationKind::Warning,
|
||||
format!("{}: {reason}", action.description()),
|
||||
)
|
||||
.title("Global shortcut unavailable")
|
||||
.persistent(),
|
||||
);
|
||||
}
|
||||
}
|
||||
hotkeys::Event::ShortcutChanged { action, trigger } => {
|
||||
let Some(trigger) = trigger else {
|
||||
return Task::none();
|
||||
};
|
||||
let previous = state.hotkey_triggers.insert(action, trigger.clone());
|
||||
|
||||
if previous.is_some_and(|previous| previous != trigger) {
|
||||
state.notifications.add(
|
||||
Notification::new(
|
||||
NotificationKind::Info,
|
||||
format!("{} is now {trigger}.", action.description()),
|
||||
)
|
||||
.title("Shortcut updated"),
|
||||
);
|
||||
}
|
||||
}
|
||||
hotkeys::Event::BackendUnavailable(reason) => {
|
||||
let key = format!("backend:{reason}");
|
||||
|
||||
if state.hotkey_notified.insert(key) {
|
||||
state.notifications.add(
|
||||
Notification::new(NotificationKind::Warning, reason)
|
||||
.title("Global shortcuts unavailable")
|
||||
.persistent(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Task::none()
|
||||
}
|
||||
|
||||
/// Commits the captured shortcut if it is valid and unused.
|
||||
fn apply_shortcut(state: &mut Moorly) {
|
||||
let Some(capture) = state.shortcut_capture.as_ref() else {
|
||||
@@ -6210,6 +6405,7 @@ fn window_event(state: &mut Moorly, window: window::Id, event: window::Event) ->
|
||||
remove_window(state, window);
|
||||
|
||||
if state.windows.is_empty() {
|
||||
hotkeys::shutdown();
|
||||
return iced::exit();
|
||||
}
|
||||
|
||||
@@ -6219,6 +6415,7 @@ fn window_event(state: &mut Moorly, window: window::Id, event: window::Event) ->
|
||||
remove_window(state, window);
|
||||
|
||||
if state.windows.is_empty() {
|
||||
hotkeys::shutdown();
|
||||
return iced::exit();
|
||||
}
|
||||
}
|
||||
@@ -7725,23 +7922,39 @@ fn shortcuts_panel(state: &Moorly) -> Element<'_, Message> {
|
||||
ShortcutAction::Quit,
|
||||
state.shortcuts.get(ShortcutAction::Quit)
|
||||
),
|
||||
shortcut_row(
|
||||
]
|
||||
.spacing(8);
|
||||
|
||||
if hotkeys::backend() == hotkeys::Backend::Native {
|
||||
panel = panel.push(shortcut_row(
|
||||
&state.typography.h3,
|
||||
ShortcutAction::ToggleWindow,
|
||||
state.shortcuts.get(ShortcutAction::ToggleWindow),
|
||||
));
|
||||
}
|
||||
|
||||
panel = panel
|
||||
.push(shortcut_row(
|
||||
&state.typography.h3,
|
||||
ShortcutAction::CommandPalette,
|
||||
state.shortcuts.get(ShortcutAction::CommandPalette),
|
||||
),
|
||||
shortcut_row(
|
||||
))
|
||||
.push(shortcut_row(
|
||||
&state.typography.h3,
|
||||
ShortcutAction::QuickTerminal,
|
||||
state.shortcuts.get(ShortcutAction::QuickTerminal),
|
||||
),
|
||||
shortcut_row(
|
||||
))
|
||||
.push(shortcut_row(
|
||||
&state.typography.h3,
|
||||
ShortcutAction::QuickSsh,
|
||||
state.shortcuts.get(ShortcutAction::QuickSsh),
|
||||
),
|
||||
]
|
||||
.spacing(8);
|
||||
));
|
||||
|
||||
if hotkeys::backend() == hotkeys::Backend::Portal {
|
||||
panel = panel.push(
|
||||
text("Global shortcuts are managed by your desktop environment on Wayland.").size(12),
|
||||
);
|
||||
}
|
||||
|
||||
match &state.shortcut_capture {
|
||||
Some(capture) => panel = panel.push(shortcut_capture_panel(capture)),
|
||||
@@ -10884,6 +11097,7 @@ mod tests {
|
||||
window_order: vec![window],
|
||||
main_window: Some(window),
|
||||
focused_window: Some(window),
|
||||
main_window_hidden: false,
|
||||
drag: None,
|
||||
theme: theme::Preference::default(),
|
||||
saved_theme: theme::Preference::default(),
|
||||
@@ -10950,6 +11164,8 @@ mod tests {
|
||||
terminal_sessions: Default::default(),
|
||||
next_terminal_tab: 1,
|
||||
notifications: super::notifications::NotificationManager::default(),
|
||||
hotkey_notified: Default::default(),
|
||||
hotkey_triggers: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12252,6 +12468,33 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn toggle_window_has_a_default_global_shortcut() {
|
||||
let state = state();
|
||||
|
||||
assert_eq!(
|
||||
state
|
||||
.shortcuts
|
||||
.toggle_window
|
||||
.as_ref()
|
||||
.map(ToString::to_string),
|
||||
Some("Ctrl+Space".to_owned())
|
||||
);
|
||||
|
||||
let bindings = super::global_hotkey_bindings(&state);
|
||||
assert_eq!(bindings.len(), 2);
|
||||
assert_eq!(
|
||||
bindings[0].action,
|
||||
super::hotkeys::Action::ToggleWindowVisibility
|
||||
);
|
||||
assert_eq!(bindings[0].trigger.as_deref(), Some("Ctrl+Space"));
|
||||
assert_eq!(
|
||||
bindings[1].action,
|
||||
super::hotkeys::Action::OpenCommandPalette
|
||||
);
|
||||
assert_eq!(bindings[1].trigger.as_deref(), Some("Ctrl+Shift+Space"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quick_actions_have_default_shortcuts() {
|
||||
let state = state();
|
||||
|
||||
Reference in new issue
Block a user