Improve logic
This commit is contained in:
33
owocr/run.py
33
owocr/run.py
@@ -42,7 +42,7 @@ except ImportError:
|
|||||||
try:
|
try:
|
||||||
import objc
|
import objc
|
||||||
from AppKit import NSData, NSImage, NSBitmapImageRep, NSDeviceRGBColorSpace, NSGraphicsContext, NSZeroPoint, NSZeroRect, NSCompositingOperationCopy
|
from AppKit import NSData, NSImage, NSBitmapImageRep, NSDeviceRGBColorSpace, NSGraphicsContext, NSZeroPoint, NSZeroRect, NSCompositingOperationCopy
|
||||||
from Quartz import CGWindowListCopyWindowInfo, kCGWindowListOptionAll, kCGWindowListOptionOnScreenOnly, kCGWindowListExcludeDesktopElements, kCGWindowName, kCGNullWindowID
|
from Quartz import CGWindowListCopyWindowInfo, CGWindowListCreateDescriptionFromArray, kCGWindowListOptionOnScreenAboveWindow, kCGWindowListOptionIncludingWindow, kCGWindowListOptionOnScreenOnly, kCGWindowListExcludeDesktopElements, kCGWindowName, kCGNullWindowID
|
||||||
import psutil
|
import psutil
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
@@ -175,7 +175,7 @@ class MacOSWindowTracker(threading.Thread):
|
|||||||
while found and not self.stop:
|
while found and not self.stop:
|
||||||
found = False
|
found = False
|
||||||
with objc.autorelease_pool():
|
with objc.autorelease_pool():
|
||||||
window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID)
|
window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenAboveWindow | kCGWindowListOptionIncludingWindow, self.window_id)
|
||||||
for i, window in enumerate(window_list):
|
for i, window in enumerate(window_list):
|
||||||
if self.window_id == window['kCGWindowNumber']:
|
if self.window_id == window['kCGWindowNumber']:
|
||||||
found = True
|
found = True
|
||||||
@@ -184,14 +184,12 @@ class MacOSWindowTracker(threading.Thread):
|
|||||||
is_active = window_list[i-1].get(kCGWindowName, '') == 'Dock'
|
is_active = window_list[i-1].get(kCGWindowName, '') == 'Dock'
|
||||||
break
|
break
|
||||||
if not found:
|
if not found:
|
||||||
window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID)
|
window_list = CGWindowListCreateDescriptionFromArray([self.window_id])
|
||||||
for window in window_list:
|
if len(window_list) > 0:
|
||||||
if self.window_id == window['kCGWindowNumber']:
|
|
||||||
found = True
|
found = True
|
||||||
bounds = window['kCGWindowBounds']
|
bounds = window_list[0]['kCGWindowBounds']
|
||||||
is_minimized = True
|
is_minimized = True
|
||||||
is_active = False
|
is_active = False
|
||||||
break
|
|
||||||
if bounds['X'] != self.window_x or bounds['Y'] != self.window_y:
|
if bounds['X'] != self.window_x or bounds['Y'] != self.window_y:
|
||||||
on_window_moved((bounds['X'], bounds['Y']))
|
on_window_moved((bounds['X'], bounds['Y']))
|
||||||
self.window_x = bounds['X']
|
self.window_x = bounds['X']
|
||||||
@@ -652,39 +650,34 @@ def run(read_from=None,
|
|||||||
if sys.platform == 'darwin':
|
if sys.platform == 'darwin':
|
||||||
window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID)
|
window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID)
|
||||||
window_titles = []
|
window_titles = []
|
||||||
|
window_indexes = []
|
||||||
window_id = 0
|
window_id = 0
|
||||||
after_dock = False
|
after_dock = False
|
||||||
target_title = None
|
target_index = None
|
||||||
for i, window in enumerate(window_list):
|
for i, window in enumerate(window_list):
|
||||||
window_title = window.get(kCGWindowName, '')
|
window_title = window.get(kCGWindowName, '')
|
||||||
if after_dock and psutil.Process(window['kCGWindowOwnerPID']).name() not in ('Terminal', 'iTerm2'):
|
if after_dock and psutil.Process(window['kCGWindowOwnerPID']).name() not in ('Terminal', 'iTerm2'):
|
||||||
window_titles.append(window_title)
|
window_titles.append(window_title)
|
||||||
|
window_indexes.append(i)
|
||||||
if window_title == 'Dock':
|
if window_title == 'Dock':
|
||||||
after_dock = True
|
after_dock = True
|
||||||
|
|
||||||
if screen_capture_coords in window_titles:
|
if screen_capture_coords in window_titles:
|
||||||
target_title = screen_capture_coords
|
target_index = window_indexes[window_titles.index(screen_capture_coords)]
|
||||||
else:
|
else:
|
||||||
for t in window_titles:
|
for t in window_titles:
|
||||||
if screen_capture_coords in t:
|
if screen_capture_coords in t:
|
||||||
target_title = t
|
target_index = window_indexes[window_titles.index(t)]
|
||||||
break
|
break
|
||||||
|
|
||||||
if not target_title:
|
if not target_index:
|
||||||
msg = '"screen_capture_coords" must be empty (for the whole screen), a valid set of coordinates, or a valid window name'
|
msg = '"screen_capture_coords" must be empty (for the whole screen), a valid set of coordinates, or a valid window name'
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
|
|
||||||
for i, window in enumerate(window_list):
|
window_id = window_list[target_index]['kCGWindowNumber']
|
||||||
window_title = window.get(kCGWindowName, '')
|
bounds = window_list[target_index]['kCGWindowBounds']
|
||||||
if target_title == window_title:
|
|
||||||
window_id = window['kCGWindowNumber']
|
|
||||||
bounds = window['kCGWindowBounds']
|
|
||||||
break
|
|
||||||
|
|
||||||
if screen_capture_only_active_windows:
|
if screen_capture_only_active_windows:
|
||||||
screencapture_window_active = False
|
screencapture_window_active = False
|
||||||
else:
|
|
||||||
screencapture_window_visible = False
|
|
||||||
sct_params = {'top': bounds['Y'], 'left': bounds['X'], 'width': bounds['Width'], 'height': bounds['Height']}
|
sct_params = {'top': bounds['Y'], 'left': bounds['X'], 'width': bounds['Width'], 'height': bounds['Height']}
|
||||||
macos_window_tracker = MacOSWindowTracker(screen_capture_only_active_windows, window_id)
|
macos_window_tracker = MacOSWindowTracker(screen_capture_only_active_windows, window_id)
|
||||||
macos_window_tracker.start()
|
macos_window_tracker.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user