Skip to content

Commit

Permalink
implement get_window_state()
Browse files Browse the repository at this point in the history
  • Loading branch information
jesicasusanto committed Jun 1, 2023
1 parent 3ec8b1f commit 97a9eba
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
1 change: 1 addition & 0 deletions openadapt/window/_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def get_active_window_meta():
win for win in windows if win['kCGWindowLayer'] == 0
]
active_window_info = active_windows_info[0]
print(active_window_info)
return active_window_info


Expand Down
80 changes: 61 additions & 19 deletions openadapt/window/_windows.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,75 @@
from loguru import logger
import pygetwindow as pgw


import pywinauto
from pywinauto import Desktop
import time
from pprint import pprint
def get_active_window_state():
meta = get_active_window_meta()
title = meta.title
geometry = meta.box
left, top, width, height = geometry
active_window = get_active_window()
meta = get_active_window_meta(active_window)
data = get_window_data(active_window)
state = {
"title": title,
"left": left,
"top": top,
"width": width,
"height": height,
"title": meta['texts'][0],
"left": meta['rectangle'].left,
"top": meta['rectangle'].top,
"width": meta['rectangle'].width(),
"height": meta['rectangle'].height(),
# TODO: get window state; see:
# https://github.com/MLDSAI/OpenAdapt/issues/75#issuecomment-1536762953
"meta": None,
"meta": meta,
"data": None,
"window_id": None,
"window_id": meta['control_id'],
}
return state

def get_active_window_meta() :
window = pgw.getActiveWindow()
if not window:
logger.warning(f"{window=}")
def get_active_window_meta(active_window) :
if not active_window:
logger.warning(f"{active_window=}")
return None
return window
logger.info(f"{active_window.get_properties()}=")
return active_window.get_properties()

def get_element_at_position(x, y):
# TODO
return None

def get_active_window():
app = pywinauto.application.Application(backend="uia").connect(active_only=True)
window = app.active()
logger.info(f"{window=}")
return window


def get_window_data(active_window):
state = get_descendants_info(active_window)
logger.info(f"{state=}")
#state = window.dump_window()
return state

def get_descendants_info(window):
result = []
for child in window.descendants():
properties = child.get_properties()
info = {
'friendly classname': properties.get('friendly classname'),
'texts': properties.get('texts'),
'rectangle': properties.get('rectangle')
}
descendants = get_descendants_info(child) # Recursively get descendants of the current child
if descendants:
info['descendants'] = descendants
result.append(info)
return result

def main():
import pickle
import time
time.sleep(1)

state = get_active_window_state()
pprint(state)
pickle.dumps(state)
import ipdb; ipdb.set_trace()


if __name__ == "__main__":
main()

0 comments on commit 97a9eba

Please sign in to comment.