diff --git a/docs/changelog.rst b/docs/changelog.rst index 21693fa79..3e019a419 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -54,6 +54,8 @@ Detailed list of changes - Wayland: Fix high CPU usage when using some input methods (:pull:`5369`) +- Remote control: When matching window by `state:focused` match the window belonging to the OS window that was last focused (:iss:`5602`) + 0.26.4 [2022-10-17] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 8f2717a81..2b885333d 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -786,6 +786,10 @@ def current_os_window() -> Optional[int]: pass +def last_focused_os_window_id() -> int: + pass + + def cocoa_set_menubar_title(title: str) -> None: pass diff --git a/kitty/state.c b/kitty/state.c index 582880e85..b0e6f68d9 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -96,7 +96,7 @@ pt_to_px(double pt, id_type os_window_id) { OSWindow* -current_os_window() { +current_os_window(void) { if (global_state.callback_os_window) return global_state.callback_os_window; for (size_t i = 0; i < global_state.num_os_windows; i++) { if (global_state.os_windows[i].is_focused) return global_state.os_windows + i; @@ -104,6 +104,19 @@ current_os_window() { return global_state.os_windows; } +static id_type +last_focused_os_window_id(void) { + id_type ans = 0, max_fc_count = 0; + for (size_t i = 0; i < global_state.num_os_windows; i++) { + OSWindow *w = &global_state.os_windows[i]; + if (w->last_focused_counter > max_fc_count) { + ans = w->id; max_fc_count = w->last_focused_counter; + } + } + return ans; +} + + OSWindow* os_window_for_kitty_window(id_type kitty_window_id) { for (size_t i = 0; i < global_state.num_os_windows; i++) { @@ -665,6 +678,11 @@ PYWRAP0(next_window_id) { return PyLong_FromUnsignedLongLong(global_state.window_id_counter + 1); } +PYWRAP0(last_focused_os_window_id) { + return PyLong_FromUnsignedLongLong(last_focused_os_window_id()); +} + + PYWRAP1(handle_for_window_id) { id_type os_window_id; PA("K", &os_window_id); @@ -1266,6 +1284,7 @@ KK5I(add_borders_rect) static PyMethodDef module_methods[] = { MW(current_os_window, METH_NOARGS), MW(next_window_id, METH_NOARGS), + MW(last_focused_os_window_id, METH_NOARGS), MW(set_options, METH_VARARGS), MW(get_options, METH_NOARGS), MW(click_mouse_url, METH_VARARGS), diff --git a/kitty/window.py b/kitty/window.py index 5dcdd01d0..d96ed1802 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -37,7 +37,7 @@ from .fast_data_types import ( STRIKETHROUGH, TINT_PROGRAM, Color, KeyEvent, Screen, add_timer, add_window, cell_size_for_window, click_mouse_cmd_output, click_mouse_url, compile_program, current_os_window, encode_key_for_tty, get_boss, get_click_interval, get_options, - init_cell_program, mark_os_window_dirty, mouse_selection, + init_cell_program, mark_os_window_dirty, mouse_selection, last_focused_os_window_id, move_cursor_to_mouse_if_in_prompt, pt_to_px, set_titlebar_color, set_window_logo, set_window_padding, set_window_render_data, update_ime_position_for_window, update_window_title, update_window_visibility, wakeup_main_loop, @@ -707,7 +707,7 @@ class Window: if query == 'active': return active_tab is not None and self is active_tab.active_window if query == 'focused': - return active_tab is not None and self is active_tab.active_window and current_os_window() == self.os_window_id + return active_tab is not None and self is active_tab.active_window and last_focused_os_window_id() == self.os_window_id if query == 'needs_attention': return self.needs_attention if query == 'parent_active':