Skip to content

Commit

Permalink
Try a bad thread impl
Browse files Browse the repository at this point in the history
  • Loading branch information
xylix committed Mar 25, 2020
1 parent 01a5f18 commit 471e444
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion aw_watcher_window/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_current_window_linux() -> Optional[dict]:

def get_current_window_macos() -> Optional[dict]:
from . import macos

# The side effectful import breaks thigns here
app = macos.get_current_app()
print ("appname" + macos.get_app_name(app) + ", title" + macos.get_app_title(app))
return {"appname": macos.get_app_name(app), "title": macos.get_app_title(app)}
Expand Down
20 changes: 11 additions & 9 deletions aw_watcher_window/macos.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Dict, Optional
from AppKit import NSObject, NSWorkspace, NSRunningApplication, NSWorkspaceDidActivateApplicationNotification
from threading import Thread
from typing import Dict, Optional, NoReturn
from AppKit import NSObject, NSNotification, NSWorkspace, NSRunningApplication, NSWorkspaceDidActivateApplicationNotification
from Quartz import (
CGWindowListCopyWindowInfo,
kCGWindowListOptionOnScreenOnly,
Expand All @@ -13,16 +14,18 @@ class Observer(NSObject):

def get_front_app(self) -> NSRunningApplication:
return self.app
def set_front_app_(self) -> NSRunningApplication:

def handle_(self, noti: NSNotification) -> None:
self._set_front_app()
def _set_front_app(self) -> None:
self.app = NSWorkspace.sharedWorkspace().frontmostApplication()

observer = Observer.new()
NSWorkspace.sharedWorkspace().notificationCenter().addObserver_selector_name_object_(
observer,
"set_front_app:",
NSWorkspaceDidActivateApplicationNotification,
None)

observer,
"handle:",
NSWorkspaceDidActivateApplicationNotification,
None)
AppHelper.runConsoleEventLoop()

def get_current_app() -> NSRunningApplication:
Expand All @@ -41,7 +44,6 @@ def get_app_title(app: NSRunningApplication) -> str:
for window in windowList:
lookupPid = window['kCGWindowOwnerPID']
if (lookupPid == pid):
print(window)
return window.get('kCGWindowName', 'Non-detected window title')

return "Couldn't find title by pid"
Expand Down

0 comments on commit 471e444

Please sign in to comment.