Skip to content

Commit d3f07c9

Browse files
fix: windows.get_active_window() (#333)
* return top_window of the active app * remove runtime exception * handle runtime error * handle COMerror * add logger.warning * add logger.warning * add get_properties * remove unnecessary imports * remove -> Desktop * monkey patching * fix get_properties * monkey patch __class__ * fix monkey patching * format with black
1 parent 3f32883 commit d3f07c9

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

openadapt/window/_windows.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import collections
2-
import sys
31
from loguru import logger
42
import pywinauto
5-
from pywinauto import Desktop
6-
import time
73
from pprint import pprint
84
import pickle
95

@@ -82,21 +78,21 @@ def get_active_element_state(x: int, y: int):
8278
"""
8379
active_window = get_active_window()
8480
active_element = active_window.from_point(x, y)
85-
properties = active_element.get_properties()
81+
properties = get_properties(active_element)
8682
properties["rectangle"] = dictify_rect(properties["rectangle"])
8783
return properties
8884

8985

90-
def get_active_window(depth=10, max_width=10, filename=None) -> Desktop:
86+
def get_active_window():
9187
"""
9288
Get the active window object.
9389
9490
Returns:
9591
Desktop: The active window object.
9692
"""
9793
app = pywinauto.application.Application(backend="uia").connect(active_only=True)
98-
window = app.active()
99-
return window
94+
window = app.top_window()
95+
return window.wrapper_object()
10096

10197

10298
def get_element_properties(element):
@@ -120,8 +116,7 @@ def get_element_properties(element):
120116
'children': [{'prop1': 'child_value1', 'prop2': 'child_value2',
121117
'children': []}]}
122118
"""
123-
124-
properties = element.get_properties()
119+
properties = get_properties(element)
125120
children = element.children()
126121

127122
if children:
@@ -143,6 +138,37 @@ def dictify_rect(rect):
143138
return rect_dict
144139

145140

141+
def get_properties(element):
142+
"""
143+
Retrieves specific writable properties of an element.
144+
145+
This function retrieves a dictionary of writable properties for a given element.
146+
It achieves this by temporarily modifying the class of the element object using
147+
monkey patching.This approach is necessary because in some cases, the original
148+
class of the element may have a `get_properties()` function that raises errors.
149+
150+
Args:
151+
element: The element for which to retrieve writable properties.
152+
153+
Returns:
154+
A dictionary containing the writable properties of the element,
155+
with property names as keys and their corres
156+
ponding values.
157+
158+
"""
159+
_element_class = element.__class__
160+
161+
class TempElement(element.__class__):
162+
writable_props = pywinauto.base_wrapper.BaseWrapper.writable_props
163+
164+
# Instantiate the subclass
165+
element.__class__ = TempElement
166+
# Retrieve properties using get_properties()
167+
properties = element.get_properties()
168+
element.__class__ = _element_class
169+
return properties
170+
171+
146172
def main():
147173
"""
148174
Test function for retrieving and inspecting the state of the active window.

0 commit comments

Comments
 (0)