예제 #1
0
파일: bar.py 프로젝트: piotrszleg/i3_bar
class I3Thread(threading.Thread):
    def __init__(self, queue):
        self.queue = queue
        self.i3 = Connection()
        super().__init__()

    def i3_update(self):
        self.queue.put(self.i3.get_tree().descendants())

    def on_new_window(self, _, event):
        # when bar window becomes visible
        if event.container.name == TITLE:
            # make it floating and sticky
            self.i3.command(
                f"[title=\"{TITLE}\"] floating enable, sticky enable")
            # send the containers structure to it
            self.i3_update()
            # disable this event handler
            self.i3.off(self.on_new_window)

    def run(self):
        # this event is used to set up the bar window
        self.i3.on(Event.WINDOW_NEW, self.on_new_window)

        def update_on(event):
            self.i3.on(event, (lambda _, __: self.i3_update()))

        update_on(Event.WORKSPACE)
        update_on(Event.WINDOW_NEW)
        update_on(Event.WINDOW_CLOSE)
        self.i3.main()

    def switch_to_workspace(self, workspace_index):
        self.i3.command(f"workspace {workspace_index}")
예제 #2
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