Skip to content

Commit

Permalink
Sort windows by activation order
Browse files Browse the repository at this point in the history
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.
deathaxe committed May 20, 2023
1 parent 488b660 commit ec26e50
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion plugin.py
Original file line number Diff line number Diff line change
@@ -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"<i>{second_line}</i>"],
)

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)

0 comments on commit ec26e50

Please sign in to comment.