desktops.py (1650B)
1 import contextlib 2 import time 3 4 from talon import actions, ctrl, Module, ui, Context 5 6 7 mod = Module() 8 9 10 @mod.action_class 11 class ModuleActions: 12 def desktop(number: int): 13 "change the current desktop" 14 15 def window_move_desktop_left(): 16 """move the current window to the desktop to the left""" 17 18 def window_move_desktop_right(): 19 """move the current window to the desktop to the right""" 20 21 def window_move_desktop(desktop_number: int): 22 """move the current window to a different desktop""" 23 24 25 ctx = Context() 26 ctx.matches = r""" 27 os: mac 28 """ 29 30 31 @contextlib.contextmanager 32 def _drag_window_mac(win=None): 33 if win is None: 34 win = ui.active_window() 35 fs = win.children.find(AXSubrole="AXFullScreenButton")[0] 36 rect = fs.AXFrame["$rect2d"] 37 x = rect["x"] + rect["width"] + 5 38 y = rect["y"] + rect["height"] / 2 39 ctrl.mouse_move(x, y) 40 ctrl.mouse_click(button=0, down=True) 41 yield 42 time.sleep(0.1) 43 ctrl.mouse_click(button=0, up=True) 44 45 46 @ctx.action_class("self") 47 class MacActions: 48 def desktop(number: int): 49 if number < 10: 50 actions.key("ctrl-{}".format(number)) 51 52 def window_move_desktop_left(): 53 with _drag_window_mac(): 54 actions.key("ctrl-cmd-alt-left") 55 56 def window_move_desktop_right(): 57 with _drag_window_mac(): 58 actions.key("ctrl-cmd-alt-right") 59 60 def window_move_desktop(desktop_number: int): 61 if ui.apps(bundle="com.amethyst.Amethyst"): 62 actions.key(f"ctrl-alt-shift-{desktop_number}") 63 else: 64 with _drag_window_mac(): 65 actions.key(f"ctrl-{desktop_number}")