diff --git a/src/main.rs b/src/main.rs index 5730afd..de1c844 100644 --- a/src/main.rs +++ b/src/main.rs @@ -127,19 +127,49 @@ fn pointer_event( } } -/// Escape is captured by focused text inputs, so it is listened for globally -/// instead of through `keyboard::listen`. -fn escape_event( +/// Key presses are captured by focused text inputs, so palette keys are +/// listened for globally instead of through `keyboard::listen`. +fn key_press_event( + event: iced::Event, + _status: iced::event::Status, + window: window::Id, +) -> Option<(keyboard::Key, keyboard::Modifiers, window::Id)> { + match event { + iced::Event::Keyboard(keyboard::Event::KeyPressed { + key, + modifiers, + repeat: false, + .. + }) => Some((key, modifiers, window)), + _ => None, + } +} + +fn palette_event( event: iced::Event, _status: iced::event::Status, window: window::Id, ) -> Option { - match event { - iced::Event::Keyboard(keyboard::Event::KeyPressed { - key: keyboard::Key::Named(keyboard::key::Named::Escape), - repeat: false, - .. - }) => Some(Message::CloseCommandPalette(window)), + let iced::Event::Keyboard(keyboard::Event::KeyPressed { + key, repeat: false, .. + }) = event + else { + return None; + }; + + match key { + keyboard::Key::Named(keyboard::key::Named::Escape) => { + Some(Message::CloseCommandPalette(window)) + } + keyboard::Key::Named(keyboard::key::Named::ArrowUp) => { + Some(Message::CommandPaletteUp(window)) + } + keyboard::Key::Named(keyboard::key::Named::ArrowDown) => { + Some(Message::CommandPaletteDown(window)) + } + keyboard::Key::Named(keyboard::key::Named::Enter) => { + Some(Message::RunSelectedCommand(window)) + } _ => None, } } @@ -150,12 +180,11 @@ fn subscription(state: &Moorly) -> Subscription { .with(( state.saved_shortcuts.settings.clone(), state.saved_shortcuts.quit.clone(), - state.saved_shortcuts.command_palette.clone(), state.shortcut_capture.is_some(), active_window, )) .filter_map( - |((settings_shortcut, quit_shortcut, palette_shortcut, capturing, window), event)| { + |((settings_shortcut, quit_shortcut, capturing, window), event)| { let keyboard::Event::KeyPressed { key, modifiers, @@ -177,13 +206,6 @@ fn subscription(state: &Moorly) -> Subscription { return Some(Message::Quit); } - if palette_shortcut - .as_ref() - .is_some_and(|shortcut| shortcut.matches(&key, modifiers)) - { - return window.map(Message::ToggleCommandPalette); - } - if let Some(shortcut) = &settings_shortcut { if shortcut.matches(&key, modifiers) { return window.map(|window| Message::SelectTab(window, Tab::Settings)); @@ -227,12 +249,26 @@ fn subscription(state: &Moorly) -> Subscription { .values() .any(|animation| animation.is_animating(now)); + let palette_shortcut = iced::event::listen_with(key_press_event) + .with(( + state.saved_shortcuts.command_palette.clone(), + state.shortcut_capture.is_some(), + )) + .filter_map(|((shortcut, capturing), (key, modifiers, window))| { + (!capturing + && shortcut + .as_ref() + .is_some_and(|shortcut| shortcut.matches(&key, modifiers))) + .then_some(Message::ToggleCommandPalette(window)) + }); + let mut subscriptions = vec![ keyboard, + palette_shortcut, 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), - iced::event::listen_with(escape_event), + iced::event::listen_with(palette_event), ]; if state.drag.is_some() { @@ -958,6 +994,99 @@ struct TransferDialog { destination: usize, } +/// Actions offered by the command palette. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Command { + Settings, + Close, + Move, + MoveToWindow(window::Id), + FocusWindow(window::Id), + FocusTab(window::Id, Tab), + Exit, +} + +impl Command { + const ALL: [Self; 4] = [Self::Settings, Self::Close, Self::Move, Self::Exit]; + + fn label(&self, state: &Moorly) -> String { + match self { + Self::Settings => "Settings".to_owned(), + Self::Close => "Close".to_owned(), + Self::Move => "Move".to_owned(), + Self::MoveToWindow(id) | Self::FocusWindow(id) => window_title(state, *id), + Self::FocusTab(_, tab) => tab.title(), + Self::Exit => "Exit".to_owned(), + } + } + + fn description(&self, state: &Moorly) -> String { + match self { + Self::Settings => "Opens the settings tab".to_owned(), + Self::Close => "Closes the active tab".to_owned(), + Self::Move => "Moves the active tab to a new window".to_owned(), + Self::MoveToWindow(id) => { + format!("Moves the active tab to {}", window_title(state, *id)) + } + Self::FocusWindow(_) => "Focuses this window".to_owned(), + Self::FocusTab(id, _) => { + format!("Focuses this tab in {}", window_title(state, *id)) + } + Self::Exit => "Quits the program".to_owned(), + } + } + + fn matching(state: &Moorly, source: window::Id, query: &str) -> Vec { + let mut commands: Vec = Self::ALL + .into_iter() + .filter(|command| fuzzy_match(&command.label(state), query)) + .collect(); + + for id in &state.window_order { + let Some(window_state) = state.windows.get(id) else { + continue; + }; + + if *id != source && fuzzy_match(&window_title(state, *id), query) { + commands.push(Self::FocusWindow(*id)); + } + + commands.extend( + window_state + .tabs + .iter() + .filter(|tab| fuzzy_match(&tab.title(), query)) + .map(|tab| Self::FocusTab(*id, *tab)), + ); + } + + // While Move matches, offer every other window as a destination. + if commands.contains(&Self::Move) { + commands.extend( + drop_targets(state, source) + .into_iter() + .map(|(id, _)| Self::MoveToWindow(id)), + ); + } + + commands + } +} + +/// Case-insensitive subsequence match, so `stg` finds `Settings`. +fn fuzzy_match(haystack: &str, needle: &str) -> bool { + let needle = needle.trim(); + if needle.is_empty() { + return true; + } + + let mut haystack = haystack.chars().flat_map(char::to_lowercase); + needle + .chars() + .flat_map(char::to_lowercase) + .all(|needle| haystack.any(|haystack| haystack == needle)) +} + /// Per-window UI state: tabs, menus and tab-strip scrolling. struct WindowState { tabs: Vec, @@ -966,6 +1095,7 @@ struct WindowState { tabs_submenu_open: bool, command_palette_open: bool, command_palette_query: String, + command_palette_selected: usize, command_palette_input: Id, tab_offset: f32, scroll_id: Id, @@ -987,6 +1117,7 @@ impl WindowState { tabs_submenu_open: false, command_palette_open: false, command_palette_query: String::new(), + command_palette_selected: 0, command_palette_input: Id::unique(), tab_offset: 0.0, scroll_id: Id::unique(), @@ -1004,6 +1135,7 @@ impl WindowState { tabs_submenu_open: false, command_palette_open: false, command_palette_query: String::new(), + command_palette_selected: 0, command_palette_input: Id::unique(), tab_offset: 0.0, scroll_id: Id::unique(), @@ -2899,6 +3031,10 @@ enum Message { ToggleCommandPalette(window::Id), CloseCommandPalette(window::Id), CommandPaletteChanged(window::Id, String), + CommandPaletteUp(window::Id), + CommandPaletteDown(window::Id), + RunSelectedCommand(window::Id), + RunCommand(window::Id, Command), SelectTheme(ThemeOption), OpenThemeNameDialog, ThemeNameChanged(String), @@ -3124,24 +3260,104 @@ fn update(state: &mut Moorly, message: Message) -> Task { return Task::none(); }; - window_state.command_palette_open = !window_state.command_palette_open; - window_state.command_palette_query.clear(); - if window_state.command_palette_open { + close_command_palette(state, window); + } else { + window_state.command_palette_open = true; + window_state.command_palette_query.clear(); + window_state.command_palette_selected = 0; return operation::focus(window_state.command_palette_input.clone()); } } - Message::CloseCommandPalette(window) => { - if let Some(window_state) = state.windows.get_mut(&window) { - window_state.command_palette_open = false; - window_state.command_palette_query.clear(); - } - } + Message::CloseCommandPalette(window) => close_command_palette(state, window), Message::CommandPaletteChanged(window, query) => { if let Some(window_state) = state.windows.get_mut(&window) && window_state.command_palette_open { window_state.command_palette_query = query; + window_state.command_palette_selected = 0; + } + } + Message::CommandPaletteUp(window) => { + if let Some(window_state) = state.windows.get_mut(&window) + && window_state.command_palette_open + { + window_state.command_palette_selected = + window_state.command_palette_selected.saturating_sub(1); + } + } + Message::CommandPaletteDown(window) => { + let query = state + .windows + .get(&window) + .filter(|window_state| window_state.command_palette_open) + .map(|window_state| window_state.command_palette_query.clone()); + + if let Some(query) = query { + let last = Command::matching(state, window, &query) + .len() + .saturating_sub(1); + + if let Some(window_state) = state.windows.get_mut(&window) { + window_state.command_palette_selected = + (window_state.command_palette_selected + 1).min(last); + } + } + } + Message::RunSelectedCommand(window) => { + let command = state + .windows + .get(&window) + .filter(|window_state| window_state.command_palette_open) + .and_then(|window_state| { + Command::matching(state, window, &window_state.command_palette_query) + .get(window_state.command_palette_selected) + .copied() + }); + + if let Some(command) = command { + return update(state, Message::RunCommand(window, command)); + } + } + Message::RunCommand(window, command) => { + let tab = state + .windows + .get(&window) + .and_then(|window_state| window_state.active_tab); + close_command_palette(state, window); + + match command { + Command::Settings => { + return update(state, Message::SelectTab(window, Tab::Settings)); + } + Command::Close => { + return update(state, Message::CloseActiveTab(window)); + } + Command::Move => { + if let Some(tab) = tab { + return detach_tab(state, window, tab, None); + } + } + Command::MoveToWindow(destination) => { + if let Some(tab) = tab { + move_tab_to_window(state, window, destination, tab); + return close_window_if_empty(state, window); + } + } + Command::FocusWindow(id) => { + state.focused_window = Some(id); + return window::gain_focus(id); + } + Command::FocusTab(id, tab) => { + state.focused_window = Some(id); + return Task::batch([ + update(state, Message::SelectTab(id, tab)), + window::gain_focus(id), + ]); + } + Command::Exit => { + return update(state, Message::Quit); + } } } Message::SelectTheme(option) => { @@ -3867,6 +4083,14 @@ fn update(state: &mut Moorly, message: Message) -> Task { Task::none() } +fn close_command_palette(state: &mut Moorly, window: window::Id) { + if let Some(window_state) = state.windows.get_mut(&window) { + window_state.command_palette_open = false; + window_state.command_palette_query.clear(); + window_state.command_palette_selected = 0; + } +} + /// 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 { @@ -6820,7 +7044,55 @@ fn main_view(state: &Moorly, window: window::Id) -> Element<'_, Message> { root.into() } -/// Top-of-window command palette field. It is empty until commands exist. +/// One command row: name over description, with the Enter hint on the +/// selected row. +fn command_palette_item( + state: &Moorly, + window: window::Id, + command: Command, + selected: bool, +) -> Element<'static, Message> { + let details = column![ + text(command.label(state)).size(16), + text(command.description(state)).size(12), + ] + .spacing(2); + + let mut content = row![details.width(Length::Fill)] + .align_y(Alignment::Center) + .height(Length::Fill); + + if selected { + content = content.push( + container(text("Enter").size(11)) + .height(Length::Fill) + .align_y(Alignment::Center) + .padding([0, 8]) + .style(|theme: &iced::Theme| { + let palette = theme.extended_palette(); + + container::Style { + border: Border { + color: palette.background.strong.color, + width: 1.0, + radius: 4.0.into(), + }, + ..container::Style::default() + } + }), + ); + } + + button(content) + .on_press(Message::RunCommand(window, command)) + .width(Length::Fill) + .height(52) + .padding([4, 10]) + .style(move |theme, status| theme::selectable_row(theme, status, selected)) + .into() +} + +/// Top-of-window command palette field and matching command list. fn command_palette_layer(state: &Moorly, window: window::Id) -> Option> { let window_state = state.windows.get(&window)?; if !window_state.command_palette_open { @@ -6834,7 +7106,29 @@ fn command_palette_layer(state: &Moorly, window: window::Id) -> Option Message { Message::CaptureKey { key, modifiers } }