|
11 | 11 | from time import sleep |
12 | 12 | from io import BytesIO |
13 | 13 | from PIL import Image |
| 14 | +import win32process |
14 | 15 | import subprocess |
| 16 | +import win32con |
15 | 17 | import requests |
16 | 18 | import ctypes |
17 | 19 | import base64 |
@@ -185,19 +187,41 @@ def launch_app(self,name:str)->tuple[str,int]: |
185 | 187 | response,status=self.execute_command(f'Start-Process shell:AppsFolder\\{appid}') |
186 | 188 | return response,status |
187 | 189 |
|
188 | | - def switch_app(self,name:str): |
| 190 | + def switch_app(self,name:str='',handle:int=None): |
189 | 191 | 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.' |
198 | 218 | 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 | + |
201 | 225 |
|
202 | 226 | def get_element_handle_from_label(self,label:int)->uia.Control: |
203 | 227 | tree_state=self.desktop_state.tree_state |
|
0 commit comments