-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow-detection.py
More file actions
291 lines (241 loc) · 9.05 KB
/
window-detection.py
File metadata and controls
291 lines (241 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python3
"""
Window Detection Example - Simplified Implementation
Demonstrates core concepts for AI chat window detection
Note: This is a simplified educational example created for demo purposes.
It demonstrates the concept but is not a complete implementation.
"""
import logging
from typing import Dict, List, Optional
from dataclasses import dataclass
@dataclass
class WindowInfo:
"""Represents information about a detected window"""
hwnd: int
title: str
process_name: str
class_name: str
is_ai_service: bool
service_type: Optional[str] = None
priority: int = 999
class WindowDetectionEngine:
"""
Simplified window detection engine for educational purposes.
This example demonstrates the core concepts without using actual
Windows API calls or real AI service detection.
"""
def __init__(self, config: Dict):
self.config = config
self.logger = logging.getLogger(__name__)
self.ai_keywords = self._load_detection_patterns()
def _load_detection_patterns(self) -> Dict[str, List[str]]:
"""Load AI service detection patterns from configuration"""
patterns = {
'ai_service_a': [
'ai-chat-service-a.com',
'chat.ai-service-a.com',
'conversation assistant'
],
'ai_service_b': [
'ai-service-b.ai',
'chat assistant',
'ai conversation'
],
'ai_service_c': [
'ai-service-c.com',
'assistant chat',
'ai helper'
]
}
return patterns
def enumerate_windows(self) -> List[WindowInfo]:
"""
Simulate window enumeration.
In a real implementation, this would use Windows API.
"""
# Simulated window data for demonstration
mock_windows = [
WindowInfo(
hwnd=12345,
title="AI Service A - Chat Assistant",
process_name="chrome.exe",
class_name="Chrome_WidgetWin_1",
is_ai_service=False
),
WindowInfo(
hwnd=12346,
title="AI Service B - Conversation",
process_name="msedge.exe",
class_name="Chrome_WidgetWin_1",
is_ai_service=False
),
WindowInfo(
hwnd=12347,
title="Code Editor - main.py",
process_name="code.exe",
class_name="Chrome_WidgetWin_1",
is_ai_service=False
),
WindowInfo(
hwnd=12348,
title="AI Service C - Assistant Chat",
process_name="msedge.exe",
class_name="Chrome_WidgetWin_1",
is_ai_service=False
)
]
return mock_windows
def detect_ai_windows(self) -> List[WindowInfo]:
"""
Detect AI chat service windows from all available windows.
"""
all_windows = self.enumerate_windows()
ai_windows = []
for window in all_windows:
service_type = self._identify_ai_service(window.title)
if service_type:
window.is_ai_service = True
window.service_type = service_type
window.priority = self._get_service_priority(service_type)
ai_windows.append(window)
self.logger.info(
f"Detected AI service: {service_type} "
f"(Window: {window.title})"
)
# Sort by priority
ai_windows.sort(key=lambda w: w.priority)
return ai_windows
def _identify_ai_service(self, window_title: str) -> Optional[str]:
"""
Identify AI service type based on window title.
"""
title_lower = window_title.lower()
for service_type, keywords in self.ai_keywords.items():
for keyword in keywords:
if keyword.lower() in title_lower:
return service_type
return None
def _get_service_priority(self, service_type: str) -> int:
"""Get priority for service type from configuration"""
priority_map = {
'ai_service_a': 1,
'ai_service_b': 2,
'ai_service_c': 3
}
return priority_map.get(service_type, 999)
class WindowManager:
"""
Simplified window management for educational purposes.
Demonstrates arrangement and control concepts.
"""
def __init__(self, config: Dict):
self.config = config
self.detection_engine = WindowDetectionEngine(config)
self.logger = logging.getLogger(__name__)
def arrange_windows_grid(self, windows: List[WindowInfo],
cols: int = 4, rows: int = 2) -> Dict:
"""
Simulate grid arrangement of windows.
In a real implementation, this would use Windows API.
"""
if not windows:
return {"arranged": 0, "failed": 0}
# Simulate display dimensions
display_width = 1920
display_height = 1080
# Calculate window dimensions
window_width = display_width // cols
window_height = display_height // rows
arranged_count = 0
failed_count = 0
for i, window in enumerate(windows[:cols * rows]):
try:
# Calculate position
col = i % cols
row = i // cols
x = col * window_width
y = row * window_height
# Simulate window positioning
success = self._position_window(
window.hwnd, x, y, window_width, window_height
)
if success:
arranged_count += 1
self.logger.info(
f"Positioned {window.service_type} at "
f"({x}, {y}) {window_width}x{window_height}"
)
else:
failed_count += 1
except Exception as e:
self.logger.error(f"Failed to arrange window {window.hwnd}: {e}")
failed_count += 1
return {
"arranged": arranged_count,
"failed": failed_count,
"total": len(windows)
}
def _position_window(self, hwnd: int, x: int, y: int,
width: int, height: int) -> bool:
"""
Simulate window positioning.
In a real implementation, this would use SetWindowPos.
"""
# Simulate positioning operation
self.logger.debug(f"Moving window {hwnd} to ({x}, {y}) {width}x{height}")
return True # Simulate success
def minimize_all_ai_windows(self) -> int:
"""Simulate minimizing all AI windows"""
ai_windows = self.detection_engine.detect_ai_windows()
minimized_count = 0
for window in ai_windows:
# Simulate minimize operation
self.logger.info(f"Minimizing {window.service_type}")
minimized_count += 1
return minimized_count
def restore_all_ai_windows(self) -> int:
"""Simulate restoring all AI windows"""
ai_windows = self.detection_engine.detect_ai_windows()
restored_count = 0
for window in ai_windows:
# Simulate restore operation
self.logger.info(f"Restoring {window.service_type}")
restored_count += 1
return restored_count
def demo_usage():
"""Demonstrate usage of the window management system"""
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Mock configuration
config = {
"window": {
"layout": {
"cols": 4,
"rows": 2
}
}
}
# Create window manager
window_manager = WindowManager(config)
# Detect AI windows
print("Detecting AI chat windows...")
ai_windows = window_manager.detection_engine.detect_ai_windows()
print(f"Found {len(ai_windows)} AI service windows:")
for window in ai_windows:
print(f" - {window.service_type}: {window.title}")
# Arrange windows in grid
print("\nArranging windows in 4x2 grid...")
result = window_manager.arrange_windows_grid(ai_windows, cols=4, rows=2)
print(f"Arrangement result: {result}")
# Demonstrate other operations
print("\nMinimizing all AI windows...")
minimized = window_manager.minimize_all_ai_windows()
print(f"Minimized {minimized} windows")
print("\nRestoring all AI windows...")
restored = window_manager.restore_all_ai_windows()
print(f"Restored {restored} windows")
if __name__ == "__main__":
demo_usage()