From ec26e50707c488d5a0e039bfa90f9e42d4a7135b Mon Sep 17 00:00:00 2001 From: deathaxe Date: Sat, 20 May 2023 11:39:14 +0200 Subject: [PATCH] Sort windows by activation order Fixes #4 This commit implements a window activation history and sorts quick panel items by that stack. As a result: 1. the most recently activated window is the first (and selected) one, so it is easy to switch between two windows. 2. the active window is added as last item in the list. --- plugin.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/plugin.py b/plugin.py index 21ced25..951929d 100644 --- a/plugin.py +++ b/plugin.py @@ -4,6 +4,8 @@ import sublime import sublime_plugin +_history = [] + class WindowInputHandler(sublime_plugin.ListInputHandler): def name(self): @@ -80,7 +82,8 @@ def create_item(window): details=[f"{second_line}"], ) - return [create_item(window) for window in sublime.windows()] + # add current window to the end of the selection list + return [create_item(window) for window in _history[1:]] + [create_item(_history[0])] class SwitchWindowCommand(sublime_plugin.ApplicationCommand): @@ -97,3 +100,39 @@ def run(self, window_id=None): if window.id() == window_id: window.bring_to_front() break + + +class SwitchWindowListener(sublime_plugin.EventListener): + def on_activated(self, view): + global _history + + window = view.window() + if not window: + return + + windows = sublime.windows() + + # add missing windows to stack + for w in windows: + if w not in _history: + _history.append(w) + + # remove closed windows from stack + for w in _history: + if w not in windows: + _history.remove(w) + + # abort on empty stack + if not _history: + return + + # abort if active window is already on top + if window == _history[0]: + return + + # move active window to top + try: + _history.remove(window) + except: + pass + _history.insert(0, window)