Fixed so command palette regains focus when opening from hidden window state
This commit is contained in:
1 parent
4140d5a07c
commit
c998122587
1 file changed
+46
-7
+46
-7
@@ -4815,6 +4815,7 @@ enum Message {
|
|||||||
CloseActiveTab(window::Id),
|
CloseActiveTab(window::Id),
|
||||||
CloseOtherTabs(window::Id),
|
CloseOtherTabs(window::Id),
|
||||||
ToggleCommandPalette(window::Id),
|
ToggleCommandPalette(window::Id),
|
||||||
|
OpenCommandPalette(window::Id),
|
||||||
CloseCommandPalette(window::Id),
|
CloseCommandPalette(window::Id),
|
||||||
CommandPaletteChanged(window::Id, String),
|
CommandPaletteChanged(window::Id, String),
|
||||||
CommandPaletteUp(window::Id),
|
CommandPaletteUp(window::Id),
|
||||||
@@ -5207,13 +5208,10 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
|||||||
if window_state.command_palette_open {
|
if window_state.command_palette_open {
|
||||||
close_command_palette(state, window);
|
close_command_palette(state, window);
|
||||||
} else {
|
} else {
|
||||||
window_state.command_palette_open = true;
|
return open_command_palette(state, window);
|
||||||
window_state.quick_menu_open = false;
|
|
||||||
window_state.command_palette_query.clear();
|
|
||||||
window_state.command_palette_selected = 0;
|
|
||||||
return operation::focus(window_state.command_palette_input.clone());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Message::OpenCommandPalette(window) => return open_command_palette(state, window),
|
||||||
Message::CloseCommandPalette(window) => close_command_palette(state, window),
|
Message::CloseCommandPalette(window) => close_command_palette(state, window),
|
||||||
Message::CommandPaletteChanged(window, query) => {
|
Message::CommandPaletteChanged(window, query) => {
|
||||||
if let Some(window_state) = state.windows.get_mut(&window)
|
if let Some(window_state) = state.windows.get_mut(&window)
|
||||||
@@ -6326,6 +6324,24 @@ fn close_command_palette(state: &mut Moorly, window: window::Id) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Opens the palette and focuses its field, leaving an already open palette
|
||||||
|
/// untouched. The global shortcut uses this so restoring a hidden window
|
||||||
|
/// never closes the palette it had open.
|
||||||
|
fn open_command_palette(state: &mut Moorly, window: window::Id) -> Task<Message> {
|
||||||
|
let Some(window_state) = state.windows.get_mut(&window) else {
|
||||||
|
return Task::none();
|
||||||
|
};
|
||||||
|
|
||||||
|
if !window_state.command_palette_open {
|
||||||
|
window_state.command_palette_open = true;
|
||||||
|
window_state.quick_menu_open = false;
|
||||||
|
window_state.command_palette_query.clear();
|
||||||
|
window_state.command_palette_selected = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
operation::focus(window_state.command_palette_input.clone())
|
||||||
|
}
|
||||||
|
|
||||||
/// Main window to restore for a global action.
|
/// Main window to restore for a global action.
|
||||||
fn main_window_id(state: &Moorly) -> Option<window::Id> {
|
fn main_window_id(state: &Moorly) -> Option<window::Id> {
|
||||||
state
|
state
|
||||||
@@ -6476,7 +6492,7 @@ fn open_palette_from_hotkey(state: &mut Moorly) -> Task<Message> {
|
|||||||
return show;
|
return show;
|
||||||
};
|
};
|
||||||
|
|
||||||
let palette = update(state, Message::ToggleCommandPalette(window));
|
let palette = update(state, Message::OpenCommandPalette(window));
|
||||||
|
|
||||||
Task::batch([show, palette])
|
Task::batch([show, palette])
|
||||||
}
|
}
|
||||||
@@ -6576,7 +6592,7 @@ fn window_event(state: &mut Moorly, window: window::Id, event: window::Event) ->
|
|||||||
|
|
||||||
if state.pending_command_palette && state.main_window == Some(window) {
|
if state.pending_command_palette && state.main_window == Some(window) {
|
||||||
state.pending_command_palette = false;
|
state.pending_command_palette = false;
|
||||||
return update(state, Message::ToggleCommandPalette(window));
|
return update(state, Message::OpenCommandPalette(window));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Event::Moved(position) => {
|
window::Event::Moved(position) => {
|
||||||
@@ -14290,4 +14306,27 @@ start_path = "/tmp"
|
|||||||
);
|
);
|
||||||
assert!(!state.windows.contains_key(&window));
|
assert!(!state.windows.contains_key(&window));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn palette_stays_open_when_the_window_it_was_open_in_is_restored() {
|
||||||
|
let mut state = self::state();
|
||||||
|
let window = self::test_window();
|
||||||
|
let _ = super::update(&mut state, super::Message::ToggleCommandPalette(window));
|
||||||
|
assert!(self::window_state(&state).command_palette_open);
|
||||||
|
|
||||||
|
let _ = super::hide_main_window(&mut state);
|
||||||
|
let _ = super::open_palette_from_hotkey(&mut state);
|
||||||
|
|
||||||
|
let reopened = state.main_window.expect("main window was restored");
|
||||||
|
let _ = super::window_event(
|
||||||
|
&mut state,
|
||||||
|
reopened,
|
||||||
|
super::window::Event::Opened {
|
||||||
|
position: None,
|
||||||
|
size: iced::Size::new(800.0, 600.0),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(state.windows[&reopened].command_palette_open);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user