added windows interaction test
adding more variant stuff
This commit is contained in:
139
app.py
139
app.py
@@ -721,6 +721,145 @@ def handle_sync_from_schematic():
|
||||
except Exception as e:
|
||||
emit('variant_error', {'error': str(e)})
|
||||
|
||||
@socketio.on('test_window_interaction')
|
||||
def handle_test_window_interaction():
|
||||
try:
|
||||
import pygetwindow as gw
|
||||
import pyautogui
|
||||
import time
|
||||
import win32gui
|
||||
import win32con
|
||||
|
||||
emit('window_test_status', {'status': 'Looking for KiCad schematic window...'})
|
||||
|
||||
# List all windows for debugging
|
||||
all_windows = gw.getAllTitles()
|
||||
emit('window_test_status', {'status': f'DEBUG: Found {len(all_windows)} windows total'})
|
||||
|
||||
# Find KiCad schematic editor window
|
||||
windows = gw.getWindowsWithTitle('Schematic Editor')
|
||||
emit('window_test_status', {'status': f'DEBUG: Found {len(windows)} windows with "Schematic Editor"'})
|
||||
|
||||
window_found = False
|
||||
if not windows:
|
||||
# Try alternative window title
|
||||
schematic_windows = [w for w in all_windows if 'kicad' in w.lower() and 'schematic' in w.lower()]
|
||||
emit('window_test_status', {'status': f'DEBUG: Found {len(schematic_windows)} windows with "kicad" and "schematic"'})
|
||||
|
||||
if schematic_windows:
|
||||
emit('window_test_status', {'status': f'DEBUG: Using window: {schematic_windows[0]}'})
|
||||
windows = gw.getWindowsWithTitle(schematic_windows[0])
|
||||
window_found = len(windows) > 0
|
||||
else:
|
||||
window_found = True
|
||||
|
||||
# If window is found, close it
|
||||
if window_found:
|
||||
window = windows[0]
|
||||
emit('window_test_status', {'status': f'Found window: "{window.title}"'})
|
||||
|
||||
# Get window position and size
|
||||
hwnd = window._hWnd
|
||||
rect = win32gui.GetWindowRect(hwnd)
|
||||
x, y, x2, y2 = rect
|
||||
width = x2 - x
|
||||
height = y2 - y
|
||||
emit('window_test_status', {'status': f'DEBUG: Window position=({x},{y}), size=({width}x{height})'})
|
||||
|
||||
# Click on the window's title bar to activate it (more reliable than SetForegroundWindow)
|
||||
click_x = x + width // 2
|
||||
click_y = y + 10 # Title bar is usually at the top
|
||||
emit('window_test_status', {'status': f'Clicking window at ({click_x}, {click_y}) to activate...'})
|
||||
pyautogui.click(click_x, click_y)
|
||||
time.sleep(0.5)
|
||||
|
||||
emit('window_test_status', {'status': 'Sending Ctrl+S (save)...'})
|
||||
pyautogui.hotkey('ctrl', 's')
|
||||
time.sleep(1.0)
|
||||
|
||||
emit('window_test_status', {'status': 'Attempting to close window...'})
|
||||
|
||||
# Method 1: Try WM_CLOSE message
|
||||
emit('window_test_status', {'status': 'DEBUG: Sending WM_CLOSE message...'})
|
||||
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
|
||||
time.sleep(1.0)
|
||||
|
||||
# Check if window still exists
|
||||
if win32gui.IsWindow(hwnd):
|
||||
emit('window_test_status', {'status': 'DEBUG: Window still exists after WM_CLOSE, trying to click and send Alt+F4...'})
|
||||
|
||||
# Click on window again to make sure it has focus
|
||||
try:
|
||||
rect = win32gui.GetWindowRect(hwnd)
|
||||
x, y, x2, y2 = rect
|
||||
click_x = x + (x2 - x) // 2
|
||||
click_y = y + 10
|
||||
pyautogui.click(click_x, click_y)
|
||||
time.sleep(0.3)
|
||||
except:
|
||||
emit('window_test_status', {'status': 'DEBUG: Could not click window (may already be closed)'})
|
||||
|
||||
pyautogui.hotkey('alt', 'F4')
|
||||
time.sleep(1.0)
|
||||
|
||||
# Final check
|
||||
if win32gui.IsWindow(hwnd):
|
||||
emit('window_test_status', {'status': 'DEBUG: Window still exists after Alt+F4 - may need manual intervention'})
|
||||
else:
|
||||
emit('window_test_status', {'status': 'DEBUG: Window closed successfully via Alt+F4'})
|
||||
else:
|
||||
emit('window_test_status', {'status': 'DEBUG: Window closed successfully via WM_CLOSE'})
|
||||
else:
|
||||
emit('window_test_status', {'status': 'No KiCad schematic window found - will open it'})
|
||||
|
||||
# Wait a couple seconds before reopening
|
||||
emit('window_test_status', {'status': 'Waiting 2 seconds before reopening...'})
|
||||
time.sleep(2.0)
|
||||
|
||||
# Reopen the schematic editor
|
||||
schematic_file = app_args.get('Schematic File', '')
|
||||
if not schematic_file:
|
||||
emit('window_test_error', {'error': 'No schematic file specified in app arguments'})
|
||||
return
|
||||
|
||||
emit('window_test_status', {'status': f'Relaunching schematic editor with: {schematic_file}'})
|
||||
|
||||
# Launch KiCad schematic editor
|
||||
# The schematic editor executable is typically in the same directory as kicad.exe
|
||||
import os
|
||||
kicad_bin_dir = r"C:\Program Files\KiCad\9.0\bin" # Default KiCad 9 installation path
|
||||
if not os.path.exists(kicad_bin_dir):
|
||||
kicad_bin_dir = r"C:\Program Files\KiCad\8.0\bin" # Try KiCad 8
|
||||
|
||||
eeschema_exe = os.path.join(kicad_bin_dir, "eeschema.exe")
|
||||
|
||||
if not os.path.exists(eeschema_exe):
|
||||
emit('window_test_error', {'error': f'KiCad executable not found at: {eeschema_exe}'})
|
||||
return
|
||||
|
||||
emit('window_test_status', {'status': f'DEBUG: Launching {eeschema_exe} {schematic_file}'})
|
||||
|
||||
# Launch KiCad with the schematic file
|
||||
result = subprocess.Popen([eeschema_exe, schematic_file], shell=False)
|
||||
emit('window_test_status', {'status': f'DEBUG: Process started with PID {result.pid}'})
|
||||
|
||||
time.sleep(2.0)
|
||||
|
||||
# Verify the window opened
|
||||
all_windows = gw.getAllTitles()
|
||||
schematic_windows = [w for w in all_windows if 'kicad' in w.lower() and 'schematic' in w.lower()]
|
||||
if schematic_windows:
|
||||
emit('window_test_status', {'status': f'Successfully reopened schematic: {schematic_windows[0]}'})
|
||||
else:
|
||||
emit('window_test_status', {'status': 'Schematic editor launched but window not detected yet'})
|
||||
|
||||
emit('window_test_complete', {'message': 'Window interaction test completed!'})
|
||||
except ImportError as e:
|
||||
emit('window_test_error', {'error': f'Missing required library: {str(e)}. Please install: pip install pygetwindow pyautogui pywin32'})
|
||||
except Exception as e:
|
||||
import traceback
|
||||
emit('window_test_error', {'error': f'{str(e)}\n\nTraceback:\n{traceback.format_exc()}'})
|
||||
|
||||
def shutdown_server():
|
||||
print("Server stopped")
|
||||
os._exit(0)
|
||||
|
||||
Reference in New Issue
Block a user