Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add position lock feature - center of view (mouse not tracked) #30

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 71 additions & 8 deletions magnus
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class Main(object):
self.refresh_interval = 250
self.started_by_keypress = False
self.force_refresh = False
self.position_lock = False
self.position_x = 0
self.position_y = 0
self.drag_x = 0
self.drag_y = 0
self.drag_lock = False
self.drag_enable = False

def handle_shutdown(self, app):
if self.started_by_keypress:
Expand All @@ -60,13 +67,27 @@ class Main(object):
print(" --force-refresh")
print(" Refresh continually (according to refresh interval)")
print(" even if the mouse has not moved")
print(" --position x y")
print(" Set center of view, disable mouse tracking")
print(" --drag")
print(" Enables drag ability of main view, when --position is used")
return 0

if "--force-refresh" in args:
# If this argument is supplied, refresh the view even if the mouse
# has not moved. Useful if the screen content is video.
self.force_refresh = True


if '--position' in args:
i = args.index('--position')
self.position_lock = True
self.position_x = int(args[i+1])
self.position_y = int(args[i+2])

if '--drag' in args:
self.drag_enable = True

# Override refresh rate on command line
for arg in args:
if arg.startswith("--refresh-interval="):
Expand Down Expand Up @@ -162,6 +183,10 @@ class Main(object):
Keybinder.bind("<Alt><Super>equal", self.zoom_in, zoom)
Keybinder.bind("<Alt><Super>minus", self.zoom_out, zoom)

# bind mouse drag
self.w.connect("button-press-event", self.drag_on)
self.w.connect("button-release-event", self.drag_off)

# and, go
self.w.show_all()

Expand Down Expand Up @@ -191,6 +216,18 @@ class Main(object):
zoom.set_active(current_index + 1)
self.set_zoom(zoom)

def drag_on(self, a,b):
if not self.drag_enable:
return
display = Gdk.Display.get_default()
(screen, x, y, modifier) = display.get_pointer()
self.drag_lock = True
self.drag_x = x
self.drag_y = y

def drag_off(self, a,b):
self.drag_lock = False

def read_window_decorations_size(self, win, alloc):
sz = self.w.get_size()
self.decorations_width = alloc.width - sz.width
Expand Down Expand Up @@ -256,14 +293,40 @@ class Main(object):
width, height, width * len(light))

def poll(self, force_refresh=False):
display = Gdk.Display.get_default()
(screen, x, y, modifier) = display.get_pointer()
if x == self.last_x and y == self.last_y:
# bail if nothing would be different
if not force_refresh and not self.force_refresh:
return True
self.last_x = x
self.last_y = y
if self.position_lock:

if self.drag_lock:
# if dragged, move position lock

display = Gdk.Display.get_default()
(screen, x, y, modifier) = display.get_pointer()

# get delta
dx = (self.drag_x - x) // self.zoomlevel
dy = (self.drag_y - y) // self.zoomlevel

# apply delta
self.position_x += dx
self.position_y += dy

# reset drag start to current position
self.drag_x = x
self.drag_y = y

# use coordinates from position lock
x,y = self.position_x,self.position_y

else:
# get new coordinates
display = Gdk.Display.get_default()
(screen, x, y, modifier) = display.get_pointer()
if x == self.last_x and y == self.last_y:
# bail if nothing would be different
if not force_refresh and not self.force_refresh:
return True
self.last_x = x
self.last_y = y

if (x > self.window_x and
x <= (self.window_x + self.width + self.decorations_width) and
y > self.window_y and
Expand Down