text
stringlengths
1
93.6k
def remove_workspace(self, workspace, by_name=False):
if by_name:
for index, history_workspace in enumerate(self.history):
if history_workspace.name == workspace.name:
del self.history[index]
return True
return False
# by id
try:
self.history.remove(workspace)
except ValueError:
return False
return True
def dispatch_event(self, connection, event):
event_handler = getattr(self, 'on_' + event.change, None)
if not event_handler:
return
current = Workspace.from_container(event.current)
old = Workspace.from_container(event.old) if event.old else None
is_changed = event_handler(current, old)
if is_changed:
self.write_history()
def on_focus(self, current, old):
if not self.history and old:
self.history.append(old)
self.remove_workspace(current)
self.history.insert(0, current)
if self.size and len(self.history) > self.size:
self.history = self.history[:self.size]
return True
def on_rename(self, current, old):
try:
index = self.history.index(current)
except ValueError:
return False
self.history[index] = current
return True
def on_init(self, current, old):
if not self.keep_empty:
return False
return self.remove_workspace(current, by_name=True)
def on_empty(self, current, old):
if self.keep_empty:
return False
return self.remove_workspace(current)
class GUI(object):
def __init__(self, i3, history, mod='Super_L', reverse=False,
gui_options=None):
self.i3 = i3
self.history = history
self.position = (len(history) - 1) if reverse else 1
signal.signal(signal.SIGUSR1, self.sigusr1_handler)
signal.signal(signal.SIGUSR2, self.sigusr2_handler)
root = tkinter.Tk(className='i3-workspace-switcher')
root.bind_all('<KeyRelease-{}>'.format(mod), self.mod_released)
width = max(map(len, history))
height = len(history)
listbox = tkinter.Listbox(root, width=width, height=height)
if gui_options:
listbox.config(**gui_options)
listbox.pack()
listbox.focus()
for workspace_name in history:
listbox.insert('end', workspace_name)
self.root = root
self.listbox = listbox
self.draw()
def exit(self):
self.root.destroy()
self.i3.command('workspace ' + self.history[self.position])
def run(self):
self.root.mainloop()
def draw(self):
self.listbox.activate(self.position)
def mod_released(self, event):
self.exit()
def sigusr1_handler(self, signal, frame):
position = self.position + 1
if position >= len(self.history):
position = 0
self.position = position
self.draw()
def sigusr2_handler(self, signal, frame):
position = self.position - 1
if position < 0:
position = len(self.history) - 1