Skip to content

Commit

Permalink
fix: windows.get_active_window() (#333)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jesicasusanto authored Jul 17, 2023
1 parent 3f32883 commit d3f07c9
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions openadapt/window/_windows.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import collections
import sys
from loguru import logger
import pywinauto
from pywinauto import Desktop
import time
from pprint import pprint
import pickle

Expand Down Expand Up @@ -82,21 +78,21 @@ def get_active_element_state(x: int, y: int):
"""
active_window = get_active_window()
active_element = active_window.from_point(x, y)
properties = active_element.get_properties()
properties = get_properties(active_element)
properties["rectangle"] = dictify_rect(properties["rectangle"])
return properties


def get_active_window(depth=10, max_width=10, filename=None) -> Desktop:
def get_active_window():
"""
Get the active window object.
Returns:
Desktop: The active window object.
"""
app = pywinauto.application.Application(backend="uia").connect(active_only=True)
window = app.active()
return window
window = app.top_window()
return window.wrapper_object()


def get_element_properties(element):
Expand All @@ -120,8 +116,7 @@ def get_element_properties(element):
'children': [{'prop1': 'child_value1', 'prop2': 'child_value2',
'children': []}]}
"""

properties = element.get_properties()
properties = get_properties(element)
children = element.children()

if children:
Expand All @@ -143,6 +138,37 @@ def dictify_rect(rect):
return rect_dict


def get_properties(element):
"""
Retrieves specific writable properties of an element.
This function retrieves a dictionary of writable properties for a given element.
It achieves this by temporarily modifying the class of the element object using
monkey patching.This approach is necessary because in some cases, the original
class of the element may have a `get_properties()` function that raises errors.
Args:
element: The element for which to retrieve writable properties.
Returns:
A dictionary containing the writable properties of the element,
with property names as keys and their corres
ponding values.
"""
_element_class = element.__class__

class TempElement(element.__class__):
writable_props = pywinauto.base_wrapper.BaseWrapper.writable_props

# Instantiate the subclass
element.__class__ = TempElement
# Retrieve properties using get_properties()
properties = element.get_properties()
element.__class__ = _element_class
return properties


def main():
"""
Test function for retrieving and inspecting the state of the active window.
Expand Down

0 comments on commit d3f07c9

Please sign in to comment.