def undo_window_renaming(ipc: i3ipc.Connection) -> None: """reset workspace names to original name""" for workspace in ipc.get_tree().workspaces(): name_parts = parse_workspace_name(workspace.name) name_parts["icons"] = None new_name = construct_workspace_name(name_parts) ipc.command('rename workspace "%s" to "%s"' % (workspace.name, new_name)) ipc.main_quit() sys.exit(0)
class Fader: ipc = None def __init__(self, active_opacity, inactive_opacity, floating_opacity, fade_time, frame_time): self.active_opacity = active_opacity self.inactive_opacity = inactive_opacity self.floating_opacity = floating_opacity self.fade_time = fade_time self.frame_time = frame_time self.fader_running = False self.fade_queue = [] self.fade_data = {} self.current_win = None self.new_win = None def start(self): if self.ipc is not None: raise Exception('Already started') self.ipc = Connection() self.ipc.on(Event.WINDOW_FOCUS, self.on_window_focus) self.ipc.on(Event.WINDOW_NEW, self.on_window_new) self.ipc.on(Event.WINDOW_FLOATING, self.on_window_floating) for win in self.ipc.get_tree(): if win.focused: change_opacity(win, self.active_opacity) self.current_win = win else: change_opacity(win, self.inactive_opacity) self.ipc.main() def stop(self): if self.ipc is None: raise Exception('Not started') self.ipc.off(self.on_window_focus) self.ipc.off(self.on_window_new) self.ipc.off(self.on_window_floating) for win in self.ipc.get_tree(): change_opacity(win, 1) self.ipc.main_quit() def enqueue_fade(self, win, start, target, duration): if win.id in self.fade_queue: f = self.fade_data[win.id] change = (self.frame_time / duration) * (target - f['opacity']) f['change'] = change f['target'] = target else: change_opacity(win, start) change = (self.frame_time / duration) * (target - start) fade_data = { 'opacity': start, 'change': change, 'target': target, 'win': win } self.fade_queue.append(win.id) self.fade_data[win.id] = fade_data def start_fader(self): if not self.fader_running: self.fader_running = True Thread(target=self.fader).start() def fader(self): while self.fade_queue: for win_id in self.fade_queue.copy(): f = self.fade_data[win_id] f['opacity'] += f['change'] finished = False if f['change'] > 0: if f['opacity'] >= f['target']: finished = True elif f['opacity'] <= f['target']: finished = True if finished: change_opacity(f['win'], f['target']) self.fade_queue.remove(win_id) del self.fade_data[win_id] else: change_opacity(f['win'], f['opacity']) sleep(self.frame_time) self.fader_running = False def on_window_new(self, ipc, event): if event.container.type == 'floating_con': change_opacity(event.container, self.floating_opacity) else: change_opacity(event.container, self.inactive_opacity) self.new_win = event.container.id def on_window_floating(self, ipc, event): if event.container.id == self.current_win.id: self.current_win = event.container def on_window_focus(self, ipc, event): if self.current_win.id == event.container.id: return if self.current_win.type == 'floating_con': trans = self.floating_opacity else: trans = self.inactive_opacity if event.container.id == self.new_win: change_opacity(self.current_win, trans) change_opacity(event.container, self.active_opacity) else: self.enqueue_fade(self.current_win, self.active_opacity, trans, self.fade_time) if event.container.type == 'floating_con': self.enqueue_fade(event.container, self.floating_opacity, self.active_opacity, self.fade_time) else: self.enqueue_fade(event.container, self.inactive_opacity, self.active_opacity, self.fade_time) self.start_fader() self.current_win = event.container self.new_win = None