Skip to content

Commit

Permalink
App now refreshes clips before refreshing the window list
Browse files Browse the repository at this point in the history
This avoids the app sometimes thinking it's unclipped for a second if another app changes the clipping bounds.
Also changed looping functions to have an explicit loop function that calls the inner function, for clarity and so the inner function can be called separately without spawning another loop.
  • Loading branch information
Levtastic committed Dec 14, 2016
1 parent 5ed8d9a commit a1498ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
28 changes: 19 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@

import sys, os

from datetime import datetime

from window import Window
from cursor import Clip
from ui import UI


class App:
list_refresh_delay = 1 * 1000
clip_refresh_delay = 1 * 1000
refresh_delay = 500 # half a second

def __init__(self):
self.windows = {}
self.selected_window = None
self.current_clip = None

self.ui = ui = UI('Trapper', self.resourcePath, self.list_refresh_delay)
self.ui = ui = UI('Trapper', self.resourcePath, self.refresh_delay)
ui.add_callback('getListOfWindows', self.getListOfWindows)
ui.add_callback('changeSelectedWindow', self.changeSelectedWindow)
ui.add_callback('clipCursor', self.clipCursor)
ui.add_callback('refreshClip', self.refreshClip)
ui.add_callback('isClipped', self.isClipped)

def __del__(self):
Expand Down Expand Up @@ -55,28 +57,36 @@ def clipCursor(self, selection):
elif self.selected_window:
self.current_clip = Clip(self.selected_window.bounds)
self.current_clip.clip()
self.ui.root.after(self.clip_refresh_delay, lambda: self.refreshClip(self.current_clip))
self.ui.root.after(self.refresh_delay, lambda: self.refreshClipLoop(self.current_clip))

return True

return False

def refreshClip(self, previous_clip):
def refreshClipLoop(self, previous_clip):
if (not self.current_clip) or self.current_clip != previous_clip:
return

if self.refreshClip():
self.ui.root.after(self.refresh_delay, lambda: self.refreshClipLoop(self.current_clip))

def refreshClip(self):
if (not self.current_clip):
return

if (not self.selected_window) or (not self.selected_window.exists):
self.current_clip.release()
self.current_clip = None
return

window_bounds = self.selected_window.bounds
if previous_clip.bounds != window_bounds:
if self.current_clip.bounds != window_bounds:
# window has moved, so get a new clip for the new coords
previous_clip.release()
self.current_clip.release()
self.current_clip = Clip(window_bounds)

self.current_clip.clip()
self.ui.root.after(self.clip_refresh_delay, lambda: self.refreshClip(self.current_clip))


def isClipped(self):
return (self.selected_window and self.selected_window.exists
and self.current_clip and self.current_clip.clipped)
Expand Down
9 changes: 6 additions & 3 deletions ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, app_name, resource_path_func, list_refresh_delay):

self.buttons.clip_button.configure(command=self.clip_button_clicked)
self.list.results_listbox.bind('<<ListboxSelect>>', self.selection_changed)
self.root.after(1, self.refreshWindowList)
self.root.after(1, self.refreshWindowListLoop)

@property
def mainloop(self):
Expand Down Expand Up @@ -61,6 +61,7 @@ def clip_button_clicked(self):

def refreshIfClipped(self, is_clipped = None):
if is_clipped is None:
self.run_callback('refreshClip')
is_clipped = self.run_callback('isClipped')

button = self.buttons.clip_button
Expand Down Expand Up @@ -93,6 +94,10 @@ def selection_changed(self, event):

clipping = self.run_callback('changeSelectedWindow', value)

def refreshWindowListLoop(self):
self.refreshWindowList()
self.root.after(self.list_refresh_delay, self.refreshWindowListLoop)

def refreshWindowList(self):
listbox = self.list.results_listbox
label = self.list.results_label
Expand All @@ -116,8 +121,6 @@ def refreshWindowList(self):

self.refreshIfClipped()

self.root.after(self.list_refresh_delay, self.refreshWindowList)


class Section(Frame):
def __init__(self, master=None):
Expand Down

0 comments on commit a1498ed

Please sign in to comment.