Skip to content

Commit d12d77c

Browse files
committed
corrected the locking of focus while switching between apps
1 parent eb898e5 commit d12d77c

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def main():
1111
llm = ChatGoogleGenerativeAI(model='gemini-flash-lite-latest', temperature=0.2)
1212
# llm=ChatOpenRouter(api_key=os.getenv("OPENROUTER_API_KEY"),model="meta-llama/llama-4-maverick:free",temperature=0.2)
1313
# llm=ChatCerebras(api_key=os.getenv("CEREBRAS_API_KEY"), model="gpt-oss-120b", temperature=0.2)
14-
agent = Agent(llm=llm, browser=Browser.EDGE, use_vision=False, auto_minimize=True)
14+
agent = Agent(llm=llm, browser=Browser.EDGE, use_vision=False, auto_minimize=False)
1515
agent.print_response(query=input("Enter a query: "))
1616

1717
if __name__ == "__main__":

windows_use/agent/desktop/service.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from time import sleep
1212
from io import BytesIO
1313
from PIL import Image
14+
import win32process
1415
import subprocess
16+
import win32con
1517
import requests
1618
import ctypes
1719
import base64
@@ -185,19 +187,41 @@ def launch_app(self,name:str)->tuple[str,int]:
185187
response,status=self.execute_command(f'Start-Process shell:AppsFolder\\{appid}')
186188
return response,status
187189

188-
def switch_app(self,name:str):
190+
def switch_app(self,name:str='',handle:int=None):
189191
apps={app.name:app for app in [self.desktop_state.active_app]+self.desktop_state.apps if app is not None}
190-
matched_app:Optional[tuple[str,float]]=process.extractOne(name,list(apps.keys()),score_cutoff=70)
191-
if matched_app is None:
192-
return (f'Application {name.title()} not found.',1)
193-
app_name,_=matched_app
194-
app=apps.get(app_name)
195-
if uia.IsIconic(app.handle):
196-
uia.ShowWindow(app.handle, cmdShow=9)
197-
return (f'{app_name.title()} restored from Minimized state.',0)
192+
if not handle:
193+
matched_app:Optional[tuple[str,float]]=process.extractOne(name,list(apps.keys()),score_cutoff=70)
194+
if matched_app is None:
195+
return (f'Application {name.title()} not found.',1)
196+
app_name,_=matched_app
197+
app=apps.get(app_name)
198+
target_handle=app.handle
199+
else:
200+
target=None
201+
for app in apps.values():
202+
if app.handle==handle:
203+
target=app
204+
break
205+
if target is None:
206+
return (f'Application with handle {handle} not found.',1)
207+
app_name=target.name
208+
target_handle=target.handle
209+
210+
foreground_handle=uia.GetForegroundWindow()
211+
foreground_thread,_=win32process.GetWindowThreadProcessId(foreground_handle)
212+
target_thread,_=win32process.GetWindowThreadProcessId(target_handle)
213+
win32process.AttachThreadInput(foreground_thread,target_thread,True)
214+
215+
if uia.IsIconic(target_handle):
216+
uia.ShowWindow(target_handle, win32con.SW_RESTORE)
217+
content=f'{app_name.title()} restored from Minimized state.'
198218
else:
199-
uia.SetForegroundWindow(app.handle)
200-
return (f'Switched to {app_name.title()} window.',0)
219+
uia.SetForegroundWindow(target_handle)
220+
content=f'Switched to {app_name.title()} window.'
221+
222+
win32process.AttachThreadInput(foreground_thread,target_thread,False)
223+
return content,0
224+
201225

202226
def get_element_handle_from_label(self,label:int)->uia.Control:
203227
tree_state=self.desktop_state.tree_state

0 commit comments

Comments
 (0)