Mirrowel commited on
Commit
e9dcbc0
·
1 Parent(s): 846ba25

perf(ui): ⚡️ implement virtual scrolling for model filter lists

Browse files

Replace widget-based model lists with canvas-based virtual rendering to drastically improve performance with large model lists (1000+ models).

- Add caching layer in FilterEngine with automatic invalidation on rule changes
- Implement VirtualModelList component that only renders visible rows
- Replace SyncModelListPanel with VirtualSyncModelLists using canvas drawing
- Batch status computation in _rebuild_cache() instead of per-model calls
- Reduce widget count from O(n) to O(visible_rows) for better memory usage
- Maintain synchronized scrolling between left and right model lists
- Preserve all existing functionality (search, highlighting, tooltips)

The previous implementation created individual CTkFrame widgets for each model, causing severe lag with large provider model lists. The new virtual scrolling approach draws only visible items directly on a canvas, providing smooth scrolling even with thousands of models.

Files changed (1) hide show
  1. src/proxy_app/model_filter_gui.py +785 -664
src/proxy_app/model_filter_gui.py CHANGED
@@ -149,6 +149,7 @@ class FilterEngine:
149
 
150
  Handles pattern matching, rule storage, and status calculation.
151
  Tracks changes for save/discard functionality.
 
152
  """
153
 
154
  def __init__(self):
@@ -160,6 +161,17 @@ class FilterEngine:
160
  self._original_whitelist_patterns: Set[str] = set()
161
  self._current_provider: Optional[str] = None
162
 
 
 
 
 
 
 
 
 
 
 
 
163
  def reset(self):
164
  """Clear all rules and reset state."""
165
  self.ignore_rules.clear()
@@ -168,6 +180,7 @@ class FilterEngine:
168
  self._whitelist_color_index = 0
169
  self._original_ignore_patterns.clear()
170
  self._original_whitelist_patterns.clear()
 
171
 
172
  def _get_next_ignore_color(self) -> str:
173
  """Get next color for ignore rules (cycles through palette)."""
@@ -196,6 +209,7 @@ class FilterEngine:
196
  pattern=pattern, color=self._get_next_ignore_color(), rule_type="ignore"
197
  )
198
  self.ignore_rules.append(rule)
 
199
  return rule
200
 
201
  def add_whitelist_rule(self, pattern: str) -> Optional[FilterRule]:
@@ -215,6 +229,7 @@ class FilterEngine:
215
  rule_type="whitelist",
216
  )
217
  self.whitelist_rules.append(rule)
 
218
  return rule
219
 
220
  def remove_ignore_rule(self, pattern: str) -> bool:
@@ -222,6 +237,7 @@ class FilterEngine:
222
  for i, rule in enumerate(self.ignore_rules):
223
  if rule.pattern == pattern:
224
  self.ignore_rules.pop(i)
 
225
  return True
226
  return False
227
 
@@ -230,6 +246,7 @@ class FilterEngine:
230
  for i, rule in enumerate(self.whitelist_rules):
231
  if rule.pattern == pattern:
232
  self.whitelist_rules.pop(i)
 
233
  return True
234
  return False
235
 
@@ -257,9 +274,9 @@ class FilterEngine:
257
  # Exact match against full ID or provider model name
258
  return model_id == pattern or provider_model_name == pattern
259
 
260
- def get_model_status(self, model_id: str) -> ModelStatus:
261
  """
262
- Determine the status of a model based on current rules.
263
 
264
  Priority: Whitelist > Ignore > Normal
265
  """
@@ -288,32 +305,53 @@ class FilterEngine:
288
  model_id=model_id, status="normal", color=NORMAL_COLOR, affecting_rule=None
289
  )
290
 
291
- def get_all_statuses(self, models: List[str]) -> List[ModelStatus]:
292
- """Get status for all models."""
293
- return [self.get_model_status(m) for m in models]
 
 
294
 
295
- def update_affected_counts(self, models: List[str]):
296
- """Update the affected_count and affected_models for all rules."""
297
- # Reset counts
 
 
298
  for rule in self.ignore_rules + self.whitelist_rules:
299
  rule.affected_count = 0
300
  rule.affected_models = []
301
 
302
- # Count affected models
303
  for model_id in models:
304
- status = self.get_model_status(model_id)
 
 
305
  if status.affecting_rule:
306
  status.affecting_rule.affected_count += 1
307
  status.affecting_rule.affected_models.append(model_id)
308
 
309
- def get_available_count(self, models: List[str]) -> Tuple[int, int]:
310
- """Returns (available_count, total_count)."""
311
- available = 0
312
- for model_id in models:
313
- status = self.get_model_status(model_id)
314
  if status.status != "ignored":
315
  available += 1
316
- return available, len(models)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
 
318
  def preview_pattern(
319
  self, pattern: str, rule_type: str, models: List[str]
@@ -1002,800 +1040,883 @@ class ToolTip:
1002
 
1003
 
1004
  # ════════════════════════════════════════════════════════════════════════════════
1005
- # RULE CHIP COMPONENT
1006
  # ════════════════════════════════════════════════════════════════════════════════
1007
 
 
 
 
1008
 
1009
- class RuleChip(ctk.CTkFrame):
 
1010
  """
1011
- Individual rule display showing pattern, affected count, and delete button.
1012
 
1013
- The pattern text is colored with the rule's assigned color.
 
 
1014
  """
1015
 
1016
  def __init__(
1017
  self,
1018
- master,
1019
- rule: FilterRule,
1020
- on_delete: Callable[[str], None],
1021
- on_click: Callable[[FilterRule], None],
1022
  ):
1023
- super().__init__(
1024
- master,
1025
- fg_color=BG_TERTIARY,
1026
- corner_radius=6,
1027
- border_width=1,
1028
- border_color=BORDER_COLOR,
1029
- )
1030
-
1031
- self.rule = rule
1032
- self.on_delete = on_delete
1033
  self.on_click = on_click
1034
- self._is_highlighted = False
1035
 
1036
- self._create_content()
 
 
 
 
 
1037
 
1038
- # Click binding
1039
- self.bind("<Button-1>", self._handle_click)
 
1040
 
1041
- def _create_content(self):
1042
- """Build chip content."""
1043
- # Container for horizontal layout
1044
- content = ctk.CTkFrame(self, fg_color="transparent")
1045
- content.pack(fill="x", padx=8, pady=6)
1046
 
1047
- # Pattern text (colored)
1048
- self.pattern_label = ctk.CTkLabel(
1049
- content,
1050
- text=self.rule.pattern,
1051
- font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1052
- text_color=self.rule.color,
1053
- anchor="w",
1054
- )
1055
- self.pattern_label.pack(side="left", fill="x", expand=True)
1056
- self.pattern_label.bind("<Button-1>", self._handle_click)
1057
 
1058
- # Affected count
1059
- self.count_label = ctk.CTkLabel(
1060
- content,
1061
- text=f"({self.rule.affected_count})",
1062
- font=(FONT_FAMILY, FONT_SIZE_SMALL),
1063
- text_color=TEXT_MUTED,
1064
- width=35,
1065
  )
1066
- self.count_label.pack(side="left", padx=(5, 5))
1067
- self.count_label.bind("<Button-1>", self._handle_click)
1068
 
1069
- # Delete button
1070
- delete_btn = ctk.CTkButton(
1071
- content,
1072
- text="×",
1073
- font=(FONT_FAMILY, FONT_SIZE_LARGE, "bold"),
1074
- fg_color="transparent",
1075
- hover_color=ACCENT_RED,
1076
- text_color=TEXT_MUTED,
1077
- width=24,
1078
- height=24,
1079
- corner_radius=4,
1080
- command=self._handle_delete,
1081
- )
1082
- delete_btn.pack(side="right")
1083
 
1084
- # Tooltip showing affected models
1085
- self._update_tooltip()
1086
 
1087
- def _handle_click(self, event=None):
1088
- """Handle click on rule chip."""
1089
- self.on_click(self.rule)
 
 
 
 
1090
 
1091
- def _handle_delete(self):
1092
- """Handle delete button click."""
1093
- self.on_delete(self.rule.pattern)
1094
 
1095
- def update_count(self, count: int, affected_models: List[str]):
1096
- """Update the affected count and tooltip."""
1097
- self.rule.affected_count = count
1098
- self.rule.affected_models = affected_models
1099
- self.count_label.configure(text=f"({count})")
1100
- self._update_tooltip()
1101
 
1102
- def _update_tooltip(self):
1103
- """Update tooltip with affected models."""
1104
- if self.rule.affected_models:
1105
- if len(self.rule.affected_models) <= 5:
1106
- models_text = "\n".join(self.rule.affected_models)
1107
- else:
1108
- models_text = "\n".join(self.rule.affected_models[:5])
1109
- models_text += f"\n... and {len(self.rule.affected_models) - 5} more"
1110
- ToolTip(self, f"Matches:\n{models_text}")
1111
- else:
1112
- ToolTip(self, "No models match this pattern")
1113
 
1114
- def set_highlighted(self, highlighted: bool):
1115
- """Set highlighted state."""
1116
- self._is_highlighted = highlighted
1117
- if highlighted:
1118
- self.configure(border_color=self.rule.color, border_width=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1119
  else:
1120
- self.configure(border_color=BORDER_COLOR, border_width=1)
 
 
1121
 
 
 
 
 
1122
 
1123
- # ════════════════════════════════════════════════════════════════════════════════
1124
- # RULE PANEL COMPONENT
1125
- # ════════════════════════════════════════════════════════════════════════════════
 
 
 
 
 
 
1126
 
 
 
 
1127
 
1128
- class RulePanel(ctk.CTkFrame):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1129
  """
1130
- Panel containing rule chips, input field, and add button.
1131
 
1132
- Handles adding and removing rules, with callbacks for changes.
 
 
 
1133
  """
1134
 
1135
  def __init__(
1136
  self,
1137
  master,
1138
- title: str,
1139
- rule_type: str, # 'ignore' or 'whitelist'
1140
- on_rules_changed: Callable[[], None],
1141
- on_rule_clicked: Callable[[FilterRule], None],
1142
- on_input_changed: Callable[[str, str], None], # (text, rule_type)
1143
  ):
1144
- super().__init__(master, fg_color=BG_SECONDARY, corner_radius=8)
1145
 
1146
- self.title = title
1147
- self.rule_type = rule_type
1148
- self.on_rules_changed = on_rules_changed
1149
- self.on_rule_clicked = on_rule_clicked
1150
- self.on_input_changed = on_input_changed
1151
- self.rule_chips: Dict[str, RuleChip] = {}
1152
 
1153
  self._create_content()
1154
 
1155
  def _create_content(self):
1156
- """Build panel content."""
1157
- # Title
1158
- title_label = ctk.CTkLabel(
 
 
 
 
 
1159
  self,
1160
- text=self.title,
1161
  font=(FONT_FAMILY, FONT_SIZE_NORMAL, "bold"),
1162
  text_color=TEXT_PRIMARY,
1163
  )
1164
- title_label.pack(anchor="w", padx=12, pady=(12, 8))
1165
 
1166
- # Rules container (scrollable)
1167
- self.rules_frame = ctk.CTkScrollableFrame(
 
 
 
 
 
1168
  self,
1169
- fg_color="transparent",
1170
- height=120,
1171
- scrollbar_fg_color=BG_TERTIARY,
1172
- scrollbar_button_color=BORDER_COLOR,
1173
  )
1174
- self.rules_frame.pack(fill="both", expand=True, padx=8, pady=(0, 8))
1175
 
1176
- # Empty state label
1177
- self.empty_label = ctk.CTkLabel(
1178
- self.rules_frame,
1179
- text="No rules configured\nAdd patterns below",
1180
- font=(FONT_FAMILY, FONT_SIZE_SMALL),
1181
- text_color=TEXT_MUTED,
1182
- justify="center",
1183
  )
1184
- self.empty_label.pack(expand=True, pady=20)
1185
 
1186
- # Input frame
1187
- input_frame = ctk.CTkFrame(self, fg_color="transparent")
1188
- input_frame.pack(fill="x", padx=8, pady=(0, 8))
 
 
 
 
 
1189
 
1190
- # Pattern input
1191
- self.input_entry = ctk.CTkEntry(
1192
- input_frame,
1193
- placeholder_text="pattern1, pattern2*, ...",
 
 
 
 
 
 
 
 
 
 
 
 
1194
  font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1195
- fg_color=BG_TERTIARY,
1196
- border_color=BORDER_COLOR,
1197
- text_color=TEXT_PRIMARY,
1198
- placeholder_text_color=TEXT_MUTED,
1199
- height=36,
1200
  )
1201
- self.input_entry.pack(side="left", fill="x", expand=True, padx=(0, 8))
1202
- self.input_entry.bind("<Return>", self._on_add_clicked)
1203
- self.input_entry.bind("<KeyRelease>", self._on_input_key)
1204
 
1205
- # Add button
1206
- add_btn = ctk.CTkButton(
1207
- input_frame,
1208
- text="+ Add",
 
 
 
 
 
 
 
 
 
1209
  font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1210
  fg_color=ACCENT_BLUE,
1211
  hover_color="#3a8aee",
1212
- width=70,
1213
- height=36,
1214
- command=self._on_add_clicked,
1215
  )
1216
- add_btn.pack(side="right")
1217
 
1218
- def _on_input_key(self, event=None):
1219
- """Handle key release in input field - for real-time preview."""
1220
- text = self.input_entry.get().strip()
1221
- self.on_input_changed(text, self.rule_type)
 
 
 
1222
 
1223
- def _on_add_clicked(self, event=None):
1224
- """Handle add button click."""
1225
- text = self.input_entry.get().strip()
1226
- if text:
1227
- # Parse comma-separated patterns
1228
- patterns = [p.strip() for p in text.split(",") if p.strip()]
1229
- if patterns:
1230
- self.input_entry.delete(0, "end")
1231
- for pattern in patterns:
1232
- self._emit_add_pattern(pattern)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1233
 
1234
- def _emit_add_pattern(self, pattern: str):
1235
- """Emit request to add a pattern (handled by parent)."""
1236
- # This will be connected to the main window's add method
1237
- if hasattr(self, "_add_pattern_callback"):
1238
- self._add_pattern_callback(pattern)
1239
 
1240
- def set_add_callback(self, callback: Callable[[str], None]):
1241
- """Set the callback for adding patterns."""
1242
- self._add_pattern_callback = callback
 
 
 
1243
 
1244
- def add_rule_chip(self, rule: FilterRule):
1245
- """Add a rule chip to the panel."""
1246
- if rule.pattern in self.rule_chips:
1247
- return
1248
 
1249
- # Hide empty label
1250
- self.empty_label.pack_forget()
 
 
1251
 
1252
- chip = RuleChip(
1253
- self.rules_frame,
1254
- rule,
1255
- on_delete=self._on_rule_delete,
1256
- on_click=self.on_rule_clicked,
1257
- )
1258
- chip.pack(fill="x", pady=2)
1259
- self.rule_chips[rule.pattern] = chip
1260
 
1261
- def remove_rule_chip(self, pattern: str):
1262
- """Remove a rule chip from the panel."""
1263
- if pattern in self.rule_chips:
1264
- self.rule_chips[pattern].destroy()
1265
- del self.rule_chips[pattern]
1266
 
1267
- # Show empty label if no rules
1268
- if not self.rule_chips:
1269
- self.empty_label.pack(expand=True, pady=20)
 
 
 
1270
 
1271
- def _on_rule_delete(self, pattern: str):
1272
- """Handle rule deletion."""
1273
- if hasattr(self, "_delete_pattern_callback"):
1274
- self._delete_pattern_callback(pattern)
1275
 
1276
- def set_delete_callback(self, callback: Callable[[str], None]):
1277
- """Set the callback for deleting patterns."""
1278
- self._delete_pattern_callback = callback
1279
 
1280
- def update_rule_counts(self, rules: List[FilterRule], models: List[str]):
1281
- """Update affected counts for all rule chips."""
1282
- for rule in rules:
1283
- if rule.pattern in self.rule_chips:
1284
- self.rule_chips[rule.pattern].update_count(
1285
- rule.affected_count, rule.affected_models
1286
- )
1287
 
1288
- def highlight_rule(self, pattern: str):
1289
- """Highlight a specific rule chip."""
1290
- for p, chip in self.rule_chips.items():
1291
- chip.set_highlighted(p == pattern)
 
1292
 
1293
- def clear_highlights(self):
1294
- """Clear all rule highlights."""
1295
- for chip in self.rule_chips.values():
1296
- chip.set_highlighted(False)
 
 
 
1297
 
1298
- def clear_all(self):
1299
- """Remove all rule chips."""
1300
- for chip in list(self.rule_chips.values()):
1301
- chip.destroy()
1302
- self.rule_chips.clear()
1303
- self.empty_label.pack(expand=True, pady=20)
1304
 
1305
- def get_input_text(self) -> str:
1306
- """Get current input text."""
1307
- return self.input_entry.get().strip()
 
1308
 
1309
- def clear_input(self):
1310
- """Clear the input field."""
1311
- self.input_entry.delete(0, "end")
 
 
 
 
 
 
 
 
1312
 
1313
 
1314
  # ════════════════════════════════════════════════════════════════════════════════
1315
- # MODEL LIST ITEM
1316
  # ════════════════════════════════════════════════════════════════════════════════
1317
 
1318
 
1319
- class ModelListItem(ctk.CTkFrame):
1320
  """
1321
- Single model row in the list.
1322
 
1323
- Shows model name with appropriate coloring based on status.
1324
  """
1325
 
1326
  def __init__(
1327
  self,
1328
  master,
1329
- status: ModelStatus,
1330
- show_status_indicator: bool = False,
1331
- on_click: Optional[Callable[[str], None]] = None,
1332
- on_right_click: Optional[Callable[[str, any], None]] = None,
1333
  ):
1334
- super().__init__(master, fg_color="transparent", height=28)
 
 
 
 
 
 
1335
 
1336
- self.status = status
 
1337
  self.on_click = on_click
1338
- self.on_right_click = on_right_click
1339
  self._is_highlighted = False
1340
- self._show_status_indicator = show_status_indicator
1341
 
1342
  self._create_content()
1343
 
1344
- def _create_content(self):
1345
- """Build item content."""
1346
- self.pack_propagate(False)
1347
-
1348
- # Container
1349
- self.container = ctk.CTkFrame(self, fg_color="transparent")
1350
- self.container.pack(fill="both", expand=True, padx=4, pady=1)
1351
 
1352
- # Status indicator (for filtered list)
1353
- if self._show_status_indicator:
1354
- indicator_text = {"normal": "●", "ignored": "✗", "whitelisted": "★"}.get(
1355
- self.status.status, "●"
1356
- )
1357
 
1358
- self.indicator = ctk.CTkLabel(
1359
- self.container,
1360
- text=indicator_text,
1361
- font=(FONT_FAMILY, FONT_SIZE_SMALL),
1362
- text_color=self.status.color,
1363
- width=18,
1364
- )
1365
- self.indicator.pack(side="left")
1366
- self.indicator.bind("<Button-1>", self._handle_click)
1367
- self.indicator.bind("<Button-3>", self._handle_right_click)
1368
-
1369
- # Model name
1370
- self.name_label = ctk.CTkLabel(
1371
- self.container,
1372
- text=self.status.display_name,
1373
  font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1374
- text_color=self.status.color
1375
- if self._show_status_indicator
1376
- else TEXT_PRIMARY,
1377
  anchor="w",
1378
  )
1379
- self.name_label.pack(side="left", fill="x", expand=True)
1380
- self.name_label.bind("<Button-1>", self._handle_click)
1381
- self.name_label.bind("<Button-3>", self._handle_right_click)
1382
 
1383
- # Bindings for the frame itself
1384
- self.bind("<Button-1>", self._handle_click)
1385
- self.bind("<Button-3>", self._handle_right_click)
1386
- self.container.bind("<Button-1>", self._handle_click)
1387
- self.container.bind("<Button-3>", self._handle_right_click)
 
 
 
 
 
1388
 
1389
- # Hover effect
1390
- self.bind("<Enter>", self._on_enter)
1391
- self.bind("<Leave>", self._on_leave)
1392
- self.container.bind("<Enter>", self._on_enter)
1393
- self.container.bind("<Leave>", self._on_leave)
 
 
 
 
 
 
 
 
 
 
 
 
1394
 
1395
  def _handle_click(self, event=None):
1396
- """Handle left click."""
1397
- if self.on_click:
1398
- self.on_click(self.status.model_id)
1399
 
1400
- def _handle_right_click(self, event):
1401
- """Handle right click."""
1402
- if self.on_right_click:
1403
- self.on_right_click(self.status.model_id, event)
1404
-
1405
- def _on_enter(self, event=None):
1406
- """Mouse enter - show hover state."""
1407
- if not self._is_highlighted:
1408
- self.container.configure(fg_color=BG_HOVER)
1409
-
1410
- def _on_leave(self, event=None):
1411
- """Mouse leave - hide hover state."""
1412
- if not self._is_highlighted:
1413
- self.container.configure(fg_color="transparent")
1414
-
1415
- def update_status(self, status: ModelStatus):
1416
- """Update the model's status and appearance."""
1417
- self.status = status
1418
-
1419
- if self._show_status_indicator:
1420
- indicator_text = {"normal": "●", "ignored": "✗", "whitelisted": "★"}.get(
1421
- status.status, "●"
1422
- )
1423
- self.indicator.configure(text=indicator_text, text_color=status.color)
1424
- self.name_label.configure(text_color=status.color)
1425
  else:
1426
- self.name_label.configure(text_color=TEXT_PRIMARY)
1427
 
1428
  def set_highlighted(self, highlighted: bool):
1429
  """Set highlighted state."""
1430
- self._is_highlighted = highlighted
1431
- if highlighted:
1432
- self.container.configure(fg_color=HIGHLIGHT_BG)
1433
- else:
1434
- self.container.configure(fg_color="transparent")
1435
-
1436
- def matches_search(self, query: str) -> bool:
1437
- """Check if this item matches a search query."""
1438
- if not query:
1439
- return True
1440
- return query.lower() in self.status.model_id.lower()
1441
-
1442
-
1443
- # ════════════════════════════════════════════════════════════════════════════════
1444
- # SYNCHRONIZED MODEL LIST PANEL
1445
- # ════════════════════════════════════════════════════════════════════════════════
1446
-
1447
-
1448
- class SyncModelListPanel(ctk.CTkFrame):
1449
- """
1450
- Two synchronized scrollable model lists side by side.
1451
-
1452
- Left list: All fetched models (plain display)
1453
- Right list: Same models with colored status indicators
1454
-
1455
- Both lists scroll together and filter together.
1456
- """
1457
-
1458
- def __init__(
1459
- self,
1460
- master,
1461
- on_model_click: Callable[[str], None],
1462
- on_model_right_click: Callable[[str, any], None],
1463
- ):
1464
- super().__init__(master, fg_color="transparent")
1465
-
1466
- self.on_model_click = on_model_click
1467
- self.on_model_right_click = on_model_right_click
1468
-
1469
- self.models: List[str] = []
1470
- self.statuses: Dict[str, ModelStatus] = {}
1471
- self.left_items: Dict[str, ModelListItem] = {}
1472
- self.right_items: Dict[str, ModelListItem] = {}
1473
- self.search_query: str = ""
1474
-
1475
- self._create_content()
1476
-
1477
- def _create_content(self):
1478
- """Build the dual list layout."""
1479
- # Configure grid
1480
- self.grid_columnconfigure(0, weight=1)
1481
- self.grid_columnconfigure(1, weight=1)
1482
- self.grid_rowconfigure(1, weight=1)
1483
-
1484
- # Left header
1485
- left_header = ctk.CTkLabel(
1486
- self,
1487
- text="All Fetched Models",
1488
- font=(FONT_FAMILY, FONT_SIZE_NORMAL, "bold"),
1489
- text_color=TEXT_PRIMARY,
1490
- )
1491
- left_header.grid(row=0, column=0, sticky="w", padx=8, pady=(0, 5))
1492
-
1493
- self.left_count_label = ctk.CTkLabel(
1494
- self, text="(0)", font=(FONT_FAMILY, FONT_SIZE_SMALL), text_color=TEXT_MUTED
1495
- )
1496
- self.left_count_label.grid(row=0, column=0, sticky="e", padx=8, pady=(0, 5))
1497
-
1498
- # Right header
1499
- right_header = ctk.CTkLabel(
1500
- self,
1501
- text="Filtered Status",
1502
- font=(FONT_FAMILY, FONT_SIZE_NORMAL, "bold"),
1503
- text_color=TEXT_PRIMARY,
1504
- )
1505
- right_header.grid(row=0, column=1, sticky="w", padx=8, pady=(0, 5))
1506
-
1507
- self.right_count_label = ctk.CTkLabel(
1508
- self, text="", font=(FONT_FAMILY, FONT_SIZE_SMALL), text_color=TEXT_MUTED
1509
- )
1510
- self.right_count_label.grid(row=0, column=1, sticky="e", padx=8, pady=(0, 5))
1511
 
1512
- # Left list container
1513
- left_frame = ctk.CTkFrame(self, fg_color=BG_TERTIARY, corner_radius=6)
1514
- left_frame.grid(row=1, column=0, sticky="nsew", padx=(0, 5))
1515
 
1516
- self.left_canvas = ctk.CTkCanvas(
1517
- left_frame,
1518
- bg=self._apply_appearance_mode(BG_TERTIARY),
1519
- highlightthickness=0,
1520
- )
1521
- self.left_scrollbar = ctk.CTkScrollbar(left_frame, command=self._sync_scroll)
1522
- self.left_inner = ctk.CTkFrame(self.left_canvas, fg_color="transparent")
1523
 
1524
- self.left_canvas.pack(side="left", fill="both", expand=True)
1525
- self.left_scrollbar.pack(side="right", fill="y")
1526
 
1527
- self.left_canvas_window = self.left_canvas.create_window(
1528
- (0, 0), window=self.left_inner, anchor="nw"
1529
- )
1530
 
1531
- self.left_canvas.configure(yscrollcommand=self.left_scrollbar.set)
 
1532
 
1533
- # Right list container
1534
- right_frame = ctk.CTkFrame(self, fg_color=BG_TERTIARY, corner_radius=6)
1535
- right_frame.grid(row=1, column=1, sticky="nsew", padx=(5, 0))
 
 
 
 
 
 
 
1536
 
1537
- self.right_canvas = ctk.CTkCanvas(
1538
- right_frame,
1539
- bg=self._apply_appearance_mode(BG_TERTIARY),
1540
- highlightthickness=0,
1541
- )
1542
- self.right_scrollbar = ctk.CTkScrollbar(right_frame, command=self._sync_scroll)
1543
- self.right_inner = ctk.CTkFrame(self.right_canvas, fg_color="transparent")
1544
 
1545
- self.right_canvas.pack(side="left", fill="both", expand=True)
1546
- self.right_scrollbar.pack(side="right", fill="y")
1547
 
1548
- self.right_canvas_window = self.right_canvas.create_window(
1549
- (0, 0), window=self.right_inner, anchor="nw"
 
 
 
 
 
 
1550
  )
 
1551
 
1552
- self.right_canvas.configure(yscrollcommand=self.right_scrollbar.set)
1553
-
1554
- # Bind scroll events
1555
- self.left_canvas.bind("<MouseWheel>", self._on_mousewheel)
1556
- self.right_canvas.bind("<MouseWheel>", self._on_mousewheel)
1557
- self.left_inner.bind("<MouseWheel>", self._on_mousewheel)
1558
- self.right_inner.bind("<MouseWheel>", self._on_mousewheel)
1559
-
1560
- # Bind resize
1561
- self.left_inner.bind("<Configure>", self._on_inner_configure)
1562
- self.left_canvas.bind("<Configure>", self._on_canvas_configure)
1563
 
1564
- # Loading state
1565
- self.loading_frame = ctk.CTkFrame(self, fg_color=BG_TERTIARY, corner_radius=6)
1566
- self.loading_label = ctk.CTkLabel(
1567
- self.loading_frame,
1568
- text="Loading...",
1569
- font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1570
  text_color=TEXT_MUTED,
 
1571
  )
1572
- self.loading_label.pack(expand=True)
1573
 
1574
- # Error state
1575
- self.error_frame = ctk.CTkFrame(self, fg_color=BG_TERTIARY, corner_radius=6)
1576
- self.error_label = ctk.CTkLabel(
1577
- self.error_frame,
1578
- text="",
 
 
 
1579
  font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1580
- text_color=ACCENT_RED,
 
 
 
 
1581
  )
1582
- self.error_label.pack(expand=True, pady=20)
 
 
1583
 
1584
- self.retry_btn = ctk.CTkButton(
1585
- self.error_frame,
1586
- text="Retry",
 
1587
  font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1588
  fg_color=ACCENT_BLUE,
1589
  hover_color="#3a8aee",
1590
- width=100,
 
 
1591
  )
1592
- self.retry_btn.pack()
1593
-
1594
- def _apply_appearance_mode(self, color):
1595
- """Apply appearance mode to color."""
1596
- return color
1597
-
1598
- def _sync_scroll(self, *args):
1599
- """Synchronized scroll handler."""
1600
- self.left_canvas.yview(*args)
1601
- self.right_canvas.yview(*args)
1602
-
1603
- def _on_mousewheel(self, event):
1604
- """Handle mouse wheel scrolling."""
1605
- delta = -1 * (event.delta // 120)
1606
- self.left_canvas.yview_scroll(delta, "units")
1607
- self.right_canvas.yview_scroll(delta, "units")
1608
- return "break"
1609
-
1610
- def _on_inner_configure(self, event=None):
1611
- """Update scroll region when inner frame changes."""
1612
- self.left_canvas.configure(scrollregion=self.left_canvas.bbox("all"))
1613
- self.right_canvas.configure(scrollregion=self.right_canvas.bbox("all"))
1614
-
1615
- def _on_canvas_configure(self, event=None):
1616
- """Update inner frame width when canvas resizes."""
1617
- width = self.left_canvas.winfo_width()
1618
- self.left_canvas.itemconfig(self.left_canvas_window, width=width)
1619
- self.right_canvas.itemconfig(self.right_canvas_window, width=width)
1620
-
1621
- def show_loading(self, provider: str):
1622
- """Show loading state."""
1623
- self.loading_label.configure(text=f"Fetching models from {provider}...")
1624
- self.loading_frame.grid(row=1, column=0, columnspan=2, sticky="nsew")
1625
- self.error_frame.grid_forget()
1626
-
1627
- def show_error(self, message: str, on_retry: Callable):
1628
- """Show error state."""
1629
- self.error_label.configure(text=f"❌ {message}")
1630
- self.retry_btn.configure(command=on_retry)
1631
- self.error_frame.grid(row=1, column=0, columnspan=2, sticky="nsew")
1632
- self.loading_frame.grid_forget()
1633
-
1634
- def hide_overlays(self):
1635
- """Hide loading and error overlays."""
1636
- self.loading_frame.grid_forget()
1637
- self.error_frame.grid_forget()
1638
-
1639
- def set_models(self, models: List[str], statuses: List[ModelStatus]):
1640
- """Set the models to display."""
1641
- self.models = models
1642
- self.statuses = {s.model_id: s for s in statuses}
1643
-
1644
- self._rebuild_lists()
1645
- self._update_counts()
1646
- self.hide_overlays()
1647
 
1648
- def _rebuild_lists(self):
1649
- """Rebuild both model lists."""
1650
- # Clear existing items
1651
- for item in self.left_items.values():
1652
- item.destroy()
1653
- for item in self.right_items.values():
1654
- item.destroy()
1655
- self.left_items.clear()
1656
- self.right_items.clear()
1657
-
1658
- # Create new items
1659
- for model_id in self.models:
1660
- status = self.statuses.get(
1661
- model_id,
1662
- ModelStatus(model_id=model_id, status="normal", color=NORMAL_COLOR),
1663
- )
1664
 
1665
- # Left item (plain)
1666
- left_item = ModelListItem(
1667
- self.left_inner,
1668
- status,
1669
- show_status_indicator=False,
1670
- on_click=self.on_model_click,
1671
- on_right_click=self.on_model_right_click,
1672
- )
1673
- left_item.pack(fill="x")
1674
- self.left_items[model_id] = left_item
1675
-
1676
- # Right item (with status colors)
1677
- right_item = ModelListItem(
1678
- self.right_inner,
1679
- status,
1680
- show_status_indicator=True,
1681
- on_click=self.on_model_click,
1682
- on_right_click=self.on_model_right_click,
1683
- )
1684
- right_item.pack(fill="x")
1685
- self.right_items[model_id] = right_item
1686
 
1687
- # Apply current search filter
1688
- self._apply_search_filter()
 
 
 
1689
 
1690
- def update_statuses(self, statuses: List[ModelStatus]):
1691
- """Update status display for all models."""
1692
- self.statuses = {s.model_id: s for s in statuses}
1693
 
1694
- for model_id, status in self.statuses.items():
1695
- if model_id in self.right_items:
1696
- self.right_items[model_id].update_status(status)
 
1697
 
1698
- self._update_counts()
 
1699
 
1700
- def _update_counts(self):
1701
- """Update the count labels."""
1702
- visible_count = sum(
1703
- 1
1704
- for item in self.left_items.values()
1705
- if item.winfo_viewable() or item.matches_search(self.search_query)
1706
  )
1707
- total = len(self.models)
1708
-
1709
- # Count available (not ignored)
1710
- available = sum(1 for s in self.statuses.values() if s.status != "ignored")
1711
-
1712
- self.left_count_label.configure(text=f"({total})")
1713
- self.right_count_label.configure(text=f"{available} available")
1714
 
1715
- def filter_by_search(self, query: str):
1716
- """Filter models by search query."""
1717
- self.search_query = query
1718
- self._apply_search_filter()
1719
-
1720
- def _apply_search_filter(self):
1721
- """Apply the current search filter to items."""
1722
- for model_id in self.models:
1723
- left_item = self.left_items.get(model_id)
1724
- right_item = self.right_items.get(model_id)
1725
-
1726
- if left_item and right_item:
1727
- matches = left_item.matches_search(self.search_query)
1728
- if matches:
1729
- left_item.pack(fill="x")
1730
- right_item.pack(fill="x")
1731
- else:
1732
- left_item.pack_forget()
1733
- right_item.pack_forget()
1734
 
1735
- def highlight_models_by_rule(self, rule: FilterRule):
1736
- """Highlight all models affected by a rule."""
1737
- self.clear_highlights()
1738
 
1739
- first_match = None
1740
- for model_id in rule.affected_models:
1741
- if model_id in self.left_items:
1742
- self.left_items[model_id].set_highlighted(True)
1743
- if first_match is None:
1744
- first_match = model_id
1745
- if model_id in self.right_items:
1746
- self.right_items[model_id].set_highlighted(True)
1747
 
1748
- # Scroll to first match
1749
- if first_match:
1750
- self._scroll_to_model(first_match)
1751
 
1752
- def highlight_model(self, model_id: str):
1753
- """Highlight a specific model."""
1754
- self.clear_highlights()
 
 
 
 
1755
 
1756
- if model_id in self.left_items:
1757
- self.left_items[model_id].set_highlighted(True)
1758
- if model_id in self.right_items:
1759
- self.right_items[model_id].set_highlighted(True)
1760
 
1761
  def clear_highlights(self):
1762
- """Clear all model highlights."""
1763
- for item in self.left_items.values():
1764
- item.set_highlighted(False)
1765
- for item in self.right_items.values():
1766
- item.set_highlighted(False)
1767
-
1768
- def _scroll_to_model(self, model_id: str):
1769
- """Scroll to make a model visible."""
1770
- if model_id not in self.left_items:
1771
- return
1772
-
1773
- item = self.left_items[model_id]
1774
-
1775
- # Calculate position
1776
- self.update_idletasks()
1777
- item_y = item.winfo_y()
1778
- inner_height = self.left_inner.winfo_height()
1779
- canvas_height = self.left_canvas.winfo_height()
1780
-
1781
- if inner_height > canvas_height:
1782
- # Calculate scroll fraction
1783
- scroll_pos = item_y / inner_height
1784
- scroll_pos = max(0, min(scroll_pos, 1))
1785
 
1786
- self.left_canvas.yview_moveto(scroll_pos)
1787
- self.right_canvas.yview_moveto(scroll_pos)
 
 
 
 
1788
 
1789
- def scroll_to_affected(self, affected_models: List[str]):
1790
- """Scroll to first affected model in the list."""
1791
- for model_id in self.models:
1792
- if model_id in affected_models:
1793
- self._scroll_to_model(model_id)
1794
- break
1795
 
1796
- def get_model_at_position(self, model_id: str) -> Optional[ModelStatus]:
1797
- """Get the status of a model."""
1798
- return self.statuses.get(model_id)
1799
 
1800
 
1801
  # ════════════════════════════════════════════════════════════════════════════════
@@ -1982,7 +2103,8 @@ class ModelFilterGUI(ctk.CTk):
1982
 
1983
  def _create_model_lists(self):
1984
  """Create the synchronized model list panel."""
1985
- self.model_list_panel = SyncModelListPanel(
 
1986
  self,
1987
  on_model_click=self._on_model_clicked,
1988
  on_model_right_click=self._on_model_right_clicked,
@@ -2378,12 +2500,11 @@ class ModelFilterGUI(ctk.CTk):
2378
  self.filter_engine.preview_pattern(pattern, rule_type, self.models)
2379
  )
2380
 
2381
- # Highlight affected models
2382
  if affected:
2383
- # Create temporary statuses for preview
2384
- for model_id in affected:
2385
- if model_id in self.model_list_panel.right_items:
2386
- self.model_list_panel.right_items[model_id].set_highlighted(True)
2387
 
2388
  # Scroll to first affected
2389
  self.model_list_panel.scroll_to_affected(affected)
 
149
 
150
  Handles pattern matching, rule storage, and status calculation.
151
  Tracks changes for save/discard functionality.
152
+ Uses caching for performance with large model lists.
153
  """
154
 
155
  def __init__(self):
 
161
  self._original_whitelist_patterns: Set[str] = set()
162
  self._current_provider: Optional[str] = None
163
 
164
+ # Caching for performance
165
+ self._status_cache: Dict[str, ModelStatus] = {}
166
+ self._available_count_cache: Optional[Tuple[int, int]] = None
167
+ self._cache_valid: bool = False
168
+
169
+ def _invalidate_cache(self):
170
+ """Mark cache as stale (call when rules change)."""
171
+ self._status_cache.clear()
172
+ self._available_count_cache = None
173
+ self._cache_valid = False
174
+
175
  def reset(self):
176
  """Clear all rules and reset state."""
177
  self.ignore_rules.clear()
 
180
  self._whitelist_color_index = 0
181
  self._original_ignore_patterns.clear()
182
  self._original_whitelist_patterns.clear()
183
+ self._invalidate_cache()
184
 
185
  def _get_next_ignore_color(self) -> str:
186
  """Get next color for ignore rules (cycles through palette)."""
 
209
  pattern=pattern, color=self._get_next_ignore_color(), rule_type="ignore"
210
  )
211
  self.ignore_rules.append(rule)
212
+ self._invalidate_cache()
213
  return rule
214
 
215
  def add_whitelist_rule(self, pattern: str) -> Optional[FilterRule]:
 
229
  rule_type="whitelist",
230
  )
231
  self.whitelist_rules.append(rule)
232
+ self._invalidate_cache()
233
  return rule
234
 
235
  def remove_ignore_rule(self, pattern: str) -> bool:
 
237
  for i, rule in enumerate(self.ignore_rules):
238
  if rule.pattern == pattern:
239
  self.ignore_rules.pop(i)
240
+ self._invalidate_cache()
241
  return True
242
  return False
243
 
 
246
  for i, rule in enumerate(self.whitelist_rules):
247
  if rule.pattern == pattern:
248
  self.whitelist_rules.pop(i)
249
+ self._invalidate_cache()
250
  return True
251
  return False
252
 
 
274
  # Exact match against full ID or provider model name
275
  return model_id == pattern or provider_model_name == pattern
276
 
277
+ def _compute_status(self, model_id: str) -> ModelStatus:
278
  """
279
+ Compute the status of a model based on current rules (no caching).
280
 
281
  Priority: Whitelist > Ignore > Normal
282
  """
 
305
  model_id=model_id, status="normal", color=NORMAL_COLOR, affecting_rule=None
306
  )
307
 
308
+ def get_model_status(self, model_id: str) -> ModelStatus:
309
+ """Get status for a model (uses cache if available)."""
310
+ if model_id in self._status_cache:
311
+ return self._status_cache[model_id]
312
+ return self._compute_status(model_id)
313
 
314
+ def _rebuild_cache(self, models: List[str]):
315
+ """Rebuild the entire status cache in one efficient pass."""
316
+ self._status_cache.clear()
317
+
318
+ # Reset rule counts
319
  for rule in self.ignore_rules + self.whitelist_rules:
320
  rule.affected_count = 0
321
  rule.affected_models = []
322
 
323
+ available = 0
324
  for model_id in models:
325
+ status = self._compute_status(model_id)
326
+ self._status_cache[model_id] = status
327
+
328
  if status.affecting_rule:
329
  status.affecting_rule.affected_count += 1
330
  status.affecting_rule.affected_models.append(model_id)
331
 
 
 
 
 
 
332
  if status.status != "ignored":
333
  available += 1
334
+
335
+ self._available_count_cache = (available, len(models))
336
+ self._cache_valid = True
337
+
338
+ def get_all_statuses(self, models: List[str]) -> List[ModelStatus]:
339
+ """Get status for all models (rebuilds cache if invalid)."""
340
+ if not self._cache_valid:
341
+ self._rebuild_cache(models)
342
+ return [self._status_cache.get(m, self._compute_status(m)) for m in models]
343
+
344
+ def update_affected_counts(self, models: List[str]):
345
+ """Update the affected_count and affected_models for all rules."""
346
+ # This now just ensures cache is valid - counts are updated in _rebuild_cache
347
+ if not self._cache_valid:
348
+ self._rebuild_cache(models)
349
+
350
+ def get_available_count(self, models: List[str]) -> Tuple[int, int]:
351
+ """Returns (available_count, total_count) from cache."""
352
+ if not self._cache_valid:
353
+ self._rebuild_cache(models)
354
+ return self._available_count_cache or (0, 0)
355
 
356
  def preview_pattern(
357
  self, pattern: str, rule_type: str, models: List[str]
 
1040
 
1041
 
1042
  # ════════════════════════════════════════════════════════════════════════════════
1043
+ # VIRTUAL MODEL LIST (Canvas-based for performance)
1044
  # ════════════════════════════════════════════════════════════════════════════════
1045
 
1046
+ # Constants for virtual list
1047
+ ITEM_HEIGHT = 24 # Height of each row in pixels
1048
+ INDICATOR_WIDTH = 18 # Width of status indicator
1049
 
1050
+
1051
+ class VirtualModelList:
1052
  """
1053
+ High-performance virtual list that only renders visible items.
1054
 
1055
+ Uses a raw tkinter Canvas to draw text directly rather than
1056
+ creating individual widgets per row. This reduces widget count
1057
+ from O(n) to O(visible_rows).
1058
  """
1059
 
1060
  def __init__(
1061
  self,
1062
+ parent,
1063
+ show_status_indicator: bool = False,
1064
+ on_click: Optional[Callable[[str], None]] = None,
1065
+ on_right_click: Optional[Callable[[str, any], None]] = None,
1066
  ):
1067
+ self.parent = parent
1068
+ self.show_status_indicator = show_status_indicator
 
 
 
 
 
 
 
 
1069
  self.on_click = on_click
1070
+ self.on_right_click = on_right_click
1071
 
1072
+ # Data
1073
+ self.models: List[str] = []
1074
+ self.statuses: Dict[str, ModelStatus] = {}
1075
+ self.filtered_models: List[str] = [] # Models after search filter
1076
+ self.search_query: str = ""
1077
+ self.highlighted_models: Set[str] = set()
1078
 
1079
+ # UI state
1080
+ self._hover_index: Optional[int] = None
1081
+ self._scroll_position: float = 0.0
1082
 
1083
+ # Create container frame
1084
+ self.frame = ctk.CTkFrame(parent, fg_color=BG_TERTIARY, corner_radius=6)
 
 
 
1085
 
1086
+ # Create canvas (use raw tk.Canvas for performance)
1087
+ import tkinter as tk
 
 
 
 
 
 
 
 
1088
 
1089
+ self.canvas = tk.Canvas(
1090
+ self.frame,
1091
+ bg=BG_TERTIARY,
1092
+ highlightthickness=0,
1093
+ bd=0,
 
 
1094
  )
1095
+ self.canvas.pack(side="left", fill="both", expand=True)
 
1096
 
1097
+ # Scrollbar
1098
+ self.scrollbar = ctk.CTkScrollbar(self.frame, command=self._on_scroll)
1099
+ self.scrollbar.pack(side="right", fill="y")
 
 
 
 
 
 
 
 
 
 
 
1100
 
1101
+ # Link canvas to scrollbar
1102
+ self.canvas.configure(yscrollcommand=self._on_canvas_scroll)
1103
 
1104
+ # Bind events
1105
+ self.canvas.bind("<Configure>", self._on_configure)
1106
+ self.canvas.bind("<MouseWheel>", self._on_mousewheel)
1107
+ self.canvas.bind("<Button-1>", self._on_left_click)
1108
+ self.canvas.bind("<Button-3>", self._on_right_click)
1109
+ self.canvas.bind("<Motion>", self._on_mouse_motion)
1110
+ self.canvas.bind("<Leave>", self._on_mouse_leave)
1111
 
1112
+ def grid(self, **kwargs):
1113
+ """Grid the container frame."""
1114
+ self.frame.grid(**kwargs)
1115
 
1116
+ def grid_forget(self):
1117
+ """Hide the container frame."""
1118
+ self.frame.grid_forget()
 
 
 
1119
 
1120
+ def pack(self, **kwargs):
1121
+ """Pack the container frame."""
1122
+ self.frame.pack(**kwargs)
 
 
 
 
 
 
 
 
1123
 
1124
+ def pack_forget(self):
1125
+ """Hide the container frame."""
1126
+ self.frame.pack_forget()
1127
+
1128
+ def set_models(self, models: List[str], statuses: Dict[str, ModelStatus]):
1129
+ """Set the model list and statuses."""
1130
+ self.models = models
1131
+ self.statuses = statuses
1132
+ self._apply_filter()
1133
+ self._update_scroll_region()
1134
+ self._render()
1135
+
1136
+ def update_statuses(self, statuses: Dict[str, ModelStatus]):
1137
+ """Update just the statuses (no model list change)."""
1138
+ self.statuses = statuses
1139
+ self._render()
1140
+
1141
+ def filter_by_search(self, query: str):
1142
+ """Filter models by search query."""
1143
+ self.search_query = query.lower().strip()
1144
+ self._apply_filter()
1145
+ self._update_scroll_region()
1146
+ self._render()
1147
+
1148
+ def _apply_filter(self):
1149
+ """Apply current search filter to models."""
1150
+ if not self.search_query:
1151
+ self.filtered_models = list(self.models)
1152
  else:
1153
+ self.filtered_models = [
1154
+ m for m in self.models if self.search_query in m.lower()
1155
+ ]
1156
 
1157
+ def highlight_models(self, model_ids: Set[str]):
1158
+ """Set which models should be highlighted."""
1159
+ self.highlighted_models = model_ids
1160
+ self._render()
1161
 
1162
+ def clear_highlights(self):
1163
+ """Clear all highlights."""
1164
+ self.highlighted_models.clear()
1165
+ self._render()
1166
+
1167
+ def scroll_to_model(self, model_id: str):
1168
+ """Scroll to make a model visible."""
1169
+ if model_id not in self.filtered_models:
1170
+ return
1171
 
1172
+ index = self.filtered_models.index(model_id)
1173
+ total_height = len(self.filtered_models) * ITEM_HEIGHT
1174
+ canvas_height = self.canvas.winfo_height()
1175
 
1176
+ if total_height <= canvas_height:
1177
+ return
1178
+
1179
+ # Calculate position to center the item
1180
+ item_y = index * ITEM_HEIGHT
1181
+ target_scroll = (item_y - canvas_height / 2 + ITEM_HEIGHT / 2) / total_height
1182
+ target_scroll = max(0, min(1, target_scroll))
1183
+
1184
+ self.canvas.yview_moveto(target_scroll)
1185
+
1186
+ def _update_scroll_region(self):
1187
+ """Update the scrollable region based on item count."""
1188
+ total_height = max(len(self.filtered_models) * ITEM_HEIGHT, 1)
1189
+ self.canvas.configure(scrollregion=(0, 0, 100, total_height))
1190
+
1191
+ def _on_scroll(self, *args):
1192
+ """Handle scrollbar command."""
1193
+ self.canvas.yview(*args)
1194
+ self._render()
1195
+
1196
+ def _on_canvas_scroll(self, first: float, last: float):
1197
+ """Handle canvas scroll update."""
1198
+ self.scrollbar.set(first, last)
1199
+ self._scroll_position = float(first)
1200
+
1201
+ def _on_configure(self, event=None):
1202
+ """Handle canvas resize."""
1203
+ self._update_scroll_region()
1204
+ self._render()
1205
+
1206
+ def _on_mousewheel(self, event):
1207
+ """Handle mouse wheel scrolling."""
1208
+ delta = -1 * (event.delta // 120)
1209
+ self.canvas.yview_scroll(delta, "units")
1210
+ self._render()
1211
+ return "break"
1212
+
1213
+ def _get_index_at_y(self, y: int) -> Optional[int]:
1214
+ """Get the model index at a y coordinate."""
1215
+ # Adjust for scroll position
1216
+ canvas_height = self.canvas.winfo_height()
1217
+ total_height = len(self.filtered_models) * ITEM_HEIGHT
1218
+
1219
+ if total_height == 0:
1220
+ return None
1221
+
1222
+ # Get scroll offset in pixels
1223
+ scroll_offset = self._scroll_position * total_height
1224
+
1225
+ # Calculate actual y in the virtual list
1226
+ actual_y = scroll_offset + y
1227
+
1228
+ index = int(actual_y // ITEM_HEIGHT)
1229
+
1230
+ if 0 <= index < len(self.filtered_models):
1231
+ return index
1232
+ return None
1233
+
1234
+ def _on_left_click(self, event):
1235
+ """Handle left click."""
1236
+ index = self._get_index_at_y(event.y)
1237
+ if index is not None and self.on_click:
1238
+ model_id = self.filtered_models[index]
1239
+ self.on_click(model_id)
1240
+
1241
+ def _on_right_click(self, event):
1242
+ """Handle right click."""
1243
+ index = self._get_index_at_y(event.y)
1244
+ if index is not None and self.on_right_click:
1245
+ model_id = self.filtered_models[index]
1246
+ self.on_right_click(model_id, event)
1247
+
1248
+ def _on_mouse_motion(self, event):
1249
+ """Handle mouse motion for hover effect."""
1250
+ new_hover = self._get_index_at_y(event.y)
1251
+ if new_hover != self._hover_index:
1252
+ self._hover_index = new_hover
1253
+ self._render()
1254
+
1255
+ def _on_mouse_leave(self, event):
1256
+ """Handle mouse leaving canvas."""
1257
+ if self._hover_index is not None:
1258
+ self._hover_index = None
1259
+ self._render()
1260
+
1261
+ def _render(self):
1262
+ """Render only the visible items."""
1263
+ self.canvas.delete("all")
1264
+
1265
+ if not self.filtered_models:
1266
+ # Show empty state
1267
+ canvas_height = self.canvas.winfo_height()
1268
+ self.canvas.create_text(
1269
+ self.canvas.winfo_width() // 2,
1270
+ canvas_height // 2,
1271
+ text="No models",
1272
+ fill=TEXT_MUTED,
1273
+ font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1274
+ )
1275
+ return
1276
+
1277
+ canvas_height = self.canvas.winfo_height()
1278
+ canvas_width = self.canvas.winfo_width()
1279
+ total_height = len(self.filtered_models) * ITEM_HEIGHT
1280
+
1281
+ # Calculate visible range
1282
+ scroll_offset = self._scroll_position * total_height
1283
+ first_visible = int(scroll_offset // ITEM_HEIGHT)
1284
+ visible_count = int(canvas_height // ITEM_HEIGHT) + 2 # +2 for partial rows
1285
+
1286
+ # Clamp to valid range
1287
+ first_visible = max(0, first_visible)
1288
+ last_visible = min(len(self.filtered_models), first_visible + visible_count)
1289
+
1290
+ # Draw visible items
1291
+ for i in range(first_visible, last_visible):
1292
+ model_id = self.filtered_models[i]
1293
+ status = self.statuses.get(
1294
+ model_id,
1295
+ ModelStatus(model_id=model_id, status="normal", color=NORMAL_COLOR),
1296
+ )
1297
+
1298
+ # Calculate y position relative to canvas
1299
+ y = (i * ITEM_HEIGHT) - scroll_offset
1300
+ y_center = y + ITEM_HEIGHT // 2
1301
+
1302
+ # Background for hover/highlight
1303
+ is_highlighted = model_id in self.highlighted_models
1304
+ is_hovered = i == self._hover_index
1305
+
1306
+ if is_highlighted:
1307
+ self.canvas.create_rectangle(
1308
+ 0, y, canvas_width, y + ITEM_HEIGHT, fill=HIGHLIGHT_BG, outline=""
1309
+ )
1310
+ elif is_hovered:
1311
+ self.canvas.create_rectangle(
1312
+ 0, y, canvas_width, y + ITEM_HEIGHT, fill=BG_HOVER, outline=""
1313
+ )
1314
+
1315
+ # Status indicator (for right list)
1316
+ x_offset = 8
1317
+ if self.show_status_indicator:
1318
+ indicator_text = {
1319
+ "normal": "●",
1320
+ "ignored": "✗",
1321
+ "whitelisted": "★",
1322
+ }.get(status.status, "●")
1323
+ self.canvas.create_text(
1324
+ x_offset + INDICATOR_WIDTH // 2,
1325
+ y_center,
1326
+ text=indicator_text,
1327
+ fill=status.color,
1328
+ font=(FONT_FAMILY, FONT_SIZE_SMALL),
1329
+ )
1330
+ x_offset += INDICATOR_WIDTH
1331
+
1332
+ # Model name
1333
+ text_color = status.color if self.show_status_indicator else TEXT_PRIMARY
1334
+ display_name = status.display_name
1335
+
1336
+ self.canvas.create_text(
1337
+ x_offset,
1338
+ y_center,
1339
+ text=display_name,
1340
+ fill=text_color,
1341
+ font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1342
+ anchor="w",
1343
+ )
1344
+
1345
+ def get_scroll_position(self) -> float:
1346
+ """Get current scroll position (0-1)."""
1347
+ return self._scroll_position
1348
+
1349
+ def set_scroll_position(self, pos: float):
1350
+ """Set scroll position (0-1) without triggering render (for sync)."""
1351
+ self.canvas.yview_moveto(pos)
1352
+
1353
+
1354
+ class VirtualSyncModelLists(ctk.CTkFrame):
1355
  """
1356
+ Container with two synchronized virtual model lists.
1357
 
1358
+ Left list: All fetched models (plain display)
1359
+ Right list: Same models with colored status indicators
1360
+
1361
+ Both lists scroll together.
1362
  """
1363
 
1364
  def __init__(
1365
  self,
1366
  master,
1367
+ on_model_click: Callable[[str], None],
1368
+ on_model_right_click: Callable[[str, any], None],
 
 
 
1369
  ):
1370
+ super().__init__(master, fg_color="transparent")
1371
 
1372
+ self.on_model_click = on_model_click
1373
+ self.on_model_right_click = on_model_right_click
1374
+
1375
+ self.models: List[str] = []
1376
+ self.statuses: Dict[str, ModelStatus] = {}
1377
+ self._syncing_scroll = False
1378
 
1379
  self._create_content()
1380
 
1381
  def _create_content(self):
1382
+ """Build the dual list layout."""
1383
+ # Configure grid
1384
+ self.grid_columnconfigure(0, weight=1)
1385
+ self.grid_columnconfigure(1, weight=1)
1386
+ self.grid_rowconfigure(1, weight=1)
1387
+
1388
+ # Left header
1389
+ left_header = ctk.CTkLabel(
1390
  self,
1391
+ text="All Fetched Models",
1392
  font=(FONT_FAMILY, FONT_SIZE_NORMAL, "bold"),
1393
  text_color=TEXT_PRIMARY,
1394
  )
1395
+ left_header.grid(row=0, column=0, sticky="w", padx=8, pady=(0, 5))
1396
 
1397
+ self.left_count_label = ctk.CTkLabel(
1398
+ self, text="(0)", font=(FONT_FAMILY, FONT_SIZE_SMALL), text_color=TEXT_MUTED
1399
+ )
1400
+ self.left_count_label.grid(row=0, column=0, sticky="e", padx=8, pady=(0, 5))
1401
+
1402
+ # Right header
1403
+ right_header = ctk.CTkLabel(
1404
  self,
1405
+ text="Filtered Status",
1406
+ font=(FONT_FAMILY, FONT_SIZE_NORMAL, "bold"),
1407
+ text_color=TEXT_PRIMARY,
 
1408
  )
1409
+ right_header.grid(row=0, column=1, sticky="w", padx=8, pady=(0, 5))
1410
 
1411
+ self.right_count_label = ctk.CTkLabel(
1412
+ self, text="", font=(FONT_FAMILY, FONT_SIZE_SMALL), text_color=TEXT_MUTED
 
 
 
 
 
1413
  )
1414
+ self.right_count_label.grid(row=0, column=1, sticky="e", padx=8, pady=(0, 5))
1415
 
1416
+ # Create virtual lists
1417
+ self.left_list = VirtualModelList(
1418
+ self,
1419
+ show_status_indicator=False,
1420
+ on_click=self.on_model_click,
1421
+ on_right_click=self.on_model_right_click,
1422
+ )
1423
+ self.left_list.grid(row=1, column=0, sticky="nsew", padx=(0, 5))
1424
 
1425
+ self.right_list = VirtualModelList(
1426
+ self,
1427
+ show_status_indicator=True,
1428
+ on_click=self.on_model_click,
1429
+ on_right_click=self.on_model_right_click,
1430
+ )
1431
+ self.right_list.grid(row=1, column=1, sticky="nsew", padx=(5, 0))
1432
+
1433
+ # Synchronize scrolling
1434
+ self._setup_scroll_sync()
1435
+
1436
+ # Loading state
1437
+ self.loading_frame = ctk.CTkFrame(self, fg_color=BG_TERTIARY, corner_radius=6)
1438
+ self.loading_label = ctk.CTkLabel(
1439
+ self.loading_frame,
1440
+ text="Loading...",
1441
  font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1442
+ text_color=TEXT_MUTED,
 
 
 
 
1443
  )
1444
+ self.loading_label.pack(expand=True)
 
 
1445
 
1446
+ # Error state
1447
+ self.error_frame = ctk.CTkFrame(self, fg_color=BG_TERTIARY, corner_radius=6)
1448
+ self.error_label = ctk.CTkLabel(
1449
+ self.error_frame,
1450
+ text="",
1451
+ font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1452
+ text_color=ACCENT_RED,
1453
+ )
1454
+ self.error_label.pack(expand=True, pady=20)
1455
+
1456
+ self.retry_btn = ctk.CTkButton(
1457
+ self.error_frame,
1458
+ text="Retry",
1459
  font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1460
  fg_color=ACCENT_BLUE,
1461
  hover_color="#3a8aee",
1462
+ width=100,
 
 
1463
  )
1464
+ self.retry_btn.pack()
1465
 
1466
+ def _setup_scroll_sync(self):
1467
+ """Setup synchronized scrolling between both lists."""
1468
+ # Override the scroll handlers to sync both lists
1469
+ original_left_scroll = self.left_list._on_scroll
1470
+ original_right_scroll = self.right_list._on_scroll
1471
+ original_left_wheel = self.left_list._on_mousewheel
1472
+ original_right_wheel = self.right_list._on_mousewheel
1473
 
1474
+ def sync_scroll_left(*args):
1475
+ if self._syncing_scroll:
1476
+ return
1477
+ self._syncing_scroll = True
1478
+ original_left_scroll(*args)
1479
+ # Sync to right
1480
+ pos = self.left_list.get_scroll_position()
1481
+ self.right_list.set_scroll_position(pos)
1482
+ self.right_list._render()
1483
+ self._syncing_scroll = False
1484
+
1485
+ def sync_scroll_right(*args):
1486
+ if self._syncing_scroll:
1487
+ return
1488
+ self._syncing_scroll = True
1489
+ original_right_scroll(*args)
1490
+ # Sync to left
1491
+ pos = self.right_list.get_scroll_position()
1492
+ self.left_list.set_scroll_position(pos)
1493
+ self.left_list._render()
1494
+ self._syncing_scroll = False
1495
+
1496
+ def sync_wheel_left(event):
1497
+ if self._syncing_scroll:
1498
+ return "break"
1499
+ self._syncing_scroll = True
1500
+ original_left_wheel(event)
1501
+ # Sync to right
1502
+ pos = self.left_list.get_scroll_position()
1503
+ self.right_list.set_scroll_position(pos)
1504
+ self.right_list._render()
1505
+ self._syncing_scroll = False
1506
+ return "break"
1507
+
1508
+ def sync_wheel_right(event):
1509
+ if self._syncing_scroll:
1510
+ return "break"
1511
+ self._syncing_scroll = True
1512
+ original_right_wheel(event)
1513
+ # Sync to left
1514
+ pos = self.right_list.get_scroll_position()
1515
+ self.left_list.set_scroll_position(pos)
1516
+ self.left_list._render()
1517
+ self._syncing_scroll = False
1518
+ return "break"
1519
+
1520
+ self.left_list._on_scroll = sync_scroll_left
1521
+ self.right_list._on_scroll = sync_scroll_right
1522
+ self.left_list.canvas.bind("<MouseWheel>", sync_wheel_left)
1523
+ self.right_list.canvas.bind("<MouseWheel>", sync_wheel_right)
1524
 
1525
+ def show_loading(self, provider: str):
1526
+ """Show loading state."""
1527
+ self.loading_label.configure(text=f"Fetching models from {provider}...")
1528
+ self.loading_frame.grid(row=1, column=0, columnspan=2, sticky="nsew")
1529
+ self.error_frame.grid_forget()
1530
 
1531
+ def show_error(self, message: str, on_retry: Callable):
1532
+ """Show error state."""
1533
+ self.error_label.configure(text=f"❌ {message}")
1534
+ self.retry_btn.configure(command=on_retry)
1535
+ self.error_frame.grid(row=1, column=0, columnspan=2, sticky="nsew")
1536
+ self.loading_frame.grid_forget()
1537
 
1538
+ def hide_overlays(self):
1539
+ """Hide loading and error overlays."""
1540
+ self.loading_frame.grid_forget()
1541
+ self.error_frame.grid_forget()
1542
 
1543
+ def set_models(self, models: List[str], statuses: List[ModelStatus]):
1544
+ """Set the models to display."""
1545
+ self.models = models
1546
+ self.statuses = {s.model_id: s for s in statuses}
1547
 
1548
+ self.left_list.set_models(models, self.statuses)
1549
+ self.right_list.set_models(models, self.statuses)
 
 
 
 
 
 
1550
 
1551
+ self._update_counts()
1552
+ self.hide_overlays()
 
 
 
1553
 
1554
+ def update_statuses(self, statuses: List[ModelStatus]):
1555
+ """Update status display for all models."""
1556
+ self.statuses = {s.model_id: s for s in statuses}
1557
+ self.left_list.update_statuses(self.statuses)
1558
+ self.right_list.update_statuses(self.statuses)
1559
+ self._update_counts()
1560
 
1561
+ def _update_counts(self):
1562
+ """Update the count labels."""
1563
+ total = len(self.models)
1564
+ available = sum(1 for s in self.statuses.values() if s.status != "ignored")
1565
 
1566
+ self.left_count_label.configure(text=f"({total})")
1567
+ self.right_count_label.configure(text=f"{available} available")
 
1568
 
1569
+ def filter_by_search(self, query: str):
1570
+ """Filter models by search query."""
1571
+ self.left_list.filter_by_search(query)
1572
+ self.right_list.filter_by_search(query)
 
 
 
1573
 
1574
+ def highlight_models_by_rule(self, rule: FilterRule):
1575
+ """Highlight all models affected by a rule."""
1576
+ model_set = set(rule.affected_models)
1577
+ self.left_list.highlight_models(model_set)
1578
+ self.right_list.highlight_models(model_set)
1579
 
1580
+ # Scroll to first match
1581
+ if rule.affected_models:
1582
+ self.left_list.scroll_to_model(rule.affected_models[0])
1583
+ # Sync right list scroll
1584
+ pos = self.left_list.get_scroll_position()
1585
+ self.right_list.set_scroll_position(pos)
1586
+ self.right_list._render()
1587
 
1588
+ def highlight_model(self, model_id: str):
1589
+ """Highlight a specific model."""
1590
+ model_set = {model_id}
1591
+ self.left_list.highlight_models(model_set)
1592
+ self.right_list.highlight_models(model_set)
 
1593
 
1594
+ def clear_highlights(self):
1595
+ """Clear all model highlights."""
1596
+ self.left_list.clear_highlights()
1597
+ self.right_list.clear_highlights()
1598
 
1599
+ def scroll_to_affected(self, affected_models: List[str]):
1600
+ """Scroll to first affected model."""
1601
+ if affected_models:
1602
+ self.left_list.scroll_to_model(affected_models[0])
1603
+ pos = self.left_list.get_scroll_position()
1604
+ self.right_list.set_scroll_position(pos)
1605
+ self.right_list._render()
1606
+
1607
+ def get_model_at_position(self, model_id: str) -> Optional[ModelStatus]:
1608
+ """Get the status of a model."""
1609
+ return self.statuses.get(model_id)
1610
 
1611
 
1612
  # ════════════════════════════════════════════════════════════════════════════════
1613
+ # RULE CHIP COMPONENT
1614
  # ════════════════════════════════════════════════════════════════════════════════
1615
 
1616
 
1617
+ class RuleChip(ctk.CTkFrame):
1618
  """
1619
+ Individual rule display showing pattern, affected count, and delete button.
1620
 
1621
+ The pattern text is colored with the rule's assigned color.
1622
  """
1623
 
1624
  def __init__(
1625
  self,
1626
  master,
1627
+ rule: FilterRule,
1628
+ on_delete: Callable[[str], None],
1629
+ on_click: Callable[[FilterRule], None],
 
1630
  ):
1631
+ super().__init__(
1632
+ master,
1633
+ fg_color=BG_TERTIARY,
1634
+ corner_radius=6,
1635
+ border_width=1,
1636
+ border_color=BORDER_COLOR,
1637
+ )
1638
 
1639
+ self.rule = rule
1640
+ self.on_delete = on_delete
1641
  self.on_click = on_click
 
1642
  self._is_highlighted = False
 
1643
 
1644
  self._create_content()
1645
 
1646
+ # Click binding
1647
+ self.bind("<Button-1>", self._handle_click)
 
 
 
 
 
1648
 
1649
+ def _create_content(self):
1650
+ """Build chip content."""
1651
+ # Container for horizontal layout
1652
+ content = ctk.CTkFrame(self, fg_color="transparent")
1653
+ content.pack(fill="x", padx=8, pady=6)
1654
 
1655
+ # Pattern text (colored)
1656
+ self.pattern_label = ctk.CTkLabel(
1657
+ content,
1658
+ text=self.rule.pattern,
 
 
 
 
 
 
 
 
 
 
 
1659
  font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1660
+ text_color=self.rule.color,
 
 
1661
  anchor="w",
1662
  )
1663
+ self.pattern_label.pack(side="left", fill="x", expand=True)
1664
+ self.pattern_label.bind("<Button-1>", self._handle_click)
 
1665
 
1666
+ # Affected count
1667
+ self.count_label = ctk.CTkLabel(
1668
+ content,
1669
+ text=f"({self.rule.affected_count})",
1670
+ font=(FONT_FAMILY, FONT_SIZE_SMALL),
1671
+ text_color=TEXT_MUTED,
1672
+ width=35,
1673
+ )
1674
+ self.count_label.pack(side="left", padx=(5, 5))
1675
+ self.count_label.bind("<Button-1>", self._handle_click)
1676
 
1677
+ # Delete button
1678
+ delete_btn = ctk.CTkButton(
1679
+ content,
1680
+ text="×",
1681
+ font=(FONT_FAMILY, FONT_SIZE_LARGE, "bold"),
1682
+ fg_color="transparent",
1683
+ hover_color=ACCENT_RED,
1684
+ text_color=TEXT_MUTED,
1685
+ width=24,
1686
+ height=24,
1687
+ corner_radius=4,
1688
+ command=self._handle_delete,
1689
+ )
1690
+ delete_btn.pack(side="right")
1691
+
1692
+ # Tooltip showing affected models
1693
+ self._update_tooltip()
1694
 
1695
  def _handle_click(self, event=None):
1696
+ """Handle click on rule chip."""
1697
+ self.on_click(self.rule)
 
1698
 
1699
+ def _handle_delete(self):
1700
+ """Handle delete button click."""
1701
+ self.on_delete(self.rule.pattern)
1702
+
1703
+ def update_count(self, count: int, affected_models: List[str]):
1704
+ """Update the affected count and tooltip."""
1705
+ self.rule.affected_count = count
1706
+ self.rule.affected_models = affected_models
1707
+ self.count_label.configure(text=f"({count})")
1708
+ self._update_tooltip()
1709
+
1710
+ def _update_tooltip(self):
1711
+ """Update tooltip with affected models."""
1712
+ if self.rule.affected_models:
1713
+ if len(self.rule.affected_models) <= 5:
1714
+ models_text = "\n".join(self.rule.affected_models)
1715
+ else:
1716
+ models_text = "\n".join(self.rule.affected_models[:5])
1717
+ models_text += f"\n... and {len(self.rule.affected_models) - 5} more"
1718
+ ToolTip(self, f"Matches:\n{models_text}")
 
 
 
 
 
1719
  else:
1720
+ ToolTip(self, "No models match this pattern")
1721
 
1722
  def set_highlighted(self, highlighted: bool):
1723
  """Set highlighted state."""
1724
+ self._is_highlighted = highlighted
1725
+ if highlighted:
1726
+ self.configure(border_color=self.rule.color, border_width=2)
1727
+ else:
1728
+ self.configure(border_color=BORDER_COLOR, border_width=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1729
 
 
 
 
1730
 
1731
+ # ════════════════════════════════════════════════════════════════════════════════
1732
+ # RULE PANEL COMPONENT
1733
+ # ════════════════════════════════════════════════════════════════════════════════
 
 
 
 
1734
 
 
 
1735
 
1736
+ class RulePanel(ctk.CTkFrame):
1737
+ """
1738
+ Panel containing rule chips, input field, and add button.
1739
 
1740
+ Handles adding and removing rules, with callbacks for changes.
1741
+ """
1742
 
1743
+ def __init__(
1744
+ self,
1745
+ master,
1746
+ title: str,
1747
+ rule_type: str, # 'ignore' or 'whitelist'
1748
+ on_rules_changed: Callable[[], None],
1749
+ on_rule_clicked: Callable[[FilterRule], None],
1750
+ on_input_changed: Callable[[str, str], None], # (text, rule_type)
1751
+ ):
1752
+ super().__init__(master, fg_color=BG_SECONDARY, corner_radius=8)
1753
 
1754
+ self.title = title
1755
+ self.rule_type = rule_type
1756
+ self.on_rules_changed = on_rules_changed
1757
+ self.on_rule_clicked = on_rule_clicked
1758
+ self.on_input_changed = on_input_changed
1759
+ self.rule_chips: Dict[str, RuleChip] = {}
 
1760
 
1761
+ self._create_content()
 
1762
 
1763
+ def _create_content(self):
1764
+ """Build panel content."""
1765
+ # Title
1766
+ title_label = ctk.CTkLabel(
1767
+ self,
1768
+ text=self.title,
1769
+ font=(FONT_FAMILY, FONT_SIZE_NORMAL, "bold"),
1770
+ text_color=TEXT_PRIMARY,
1771
  )
1772
+ title_label.pack(anchor="w", padx=12, pady=(12, 8))
1773
 
1774
+ # Rules container (scrollable)
1775
+ self.rules_frame = ctk.CTkScrollableFrame(
1776
+ self,
1777
+ fg_color="transparent",
1778
+ height=120,
1779
+ scrollbar_fg_color=BG_TERTIARY,
1780
+ scrollbar_button_color=BORDER_COLOR,
1781
+ )
1782
+ self.rules_frame.pack(fill="both", expand=True, padx=8, pady=(0, 8))
 
 
1783
 
1784
+ # Empty state label
1785
+ self.empty_label = ctk.CTkLabel(
1786
+ self.rules_frame,
1787
+ text="No rules configured\nAdd patterns below",
1788
+ font=(FONT_FAMILY, FONT_SIZE_SMALL),
 
1789
  text_color=TEXT_MUTED,
1790
+ justify="center",
1791
  )
1792
+ self.empty_label.pack(expand=True, pady=20)
1793
 
1794
+ # Input frame
1795
+ input_frame = ctk.CTkFrame(self, fg_color="transparent")
1796
+ input_frame.pack(fill="x", padx=8, pady=(0, 8))
1797
+
1798
+ # Pattern input
1799
+ self.input_entry = ctk.CTkEntry(
1800
+ input_frame,
1801
+ placeholder_text="pattern1, pattern2*, ...",
1802
  font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1803
+ fg_color=BG_TERTIARY,
1804
+ border_color=BORDER_COLOR,
1805
+ text_color=TEXT_PRIMARY,
1806
+ placeholder_text_color=TEXT_MUTED,
1807
+ height=36,
1808
  )
1809
+ self.input_entry.pack(side="left", fill="x", expand=True, padx=(0, 8))
1810
+ self.input_entry.bind("<Return>", self._on_add_clicked)
1811
+ self.input_entry.bind("<KeyRelease>", self._on_input_key)
1812
 
1813
+ # Add button
1814
+ add_btn = ctk.CTkButton(
1815
+ input_frame,
1816
+ text="+ Add",
1817
  font=(FONT_FAMILY, FONT_SIZE_NORMAL),
1818
  fg_color=ACCENT_BLUE,
1819
  hover_color="#3a8aee",
1820
+ width=70,
1821
+ height=36,
1822
+ command=self._on_add_clicked,
1823
  )
1824
+ add_btn.pack(side="right")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1825
 
1826
+ def _on_input_key(self, event=None):
1827
+ """Handle key release in input field - for real-time preview."""
1828
+ text = self.input_entry.get().strip()
1829
+ self.on_input_changed(text, self.rule_type)
 
 
 
 
 
 
 
 
 
 
 
 
1830
 
1831
+ def _on_add_clicked(self, event=None):
1832
+ """Handle add button click."""
1833
+ text = self.input_entry.get().strip()
1834
+ if text:
1835
+ # Parse comma-separated patterns
1836
+ patterns = [p.strip() for p in text.split(",") if p.strip()]
1837
+ if patterns:
1838
+ self.input_entry.delete(0, "end")
1839
+ for pattern in patterns:
1840
+ self._emit_add_pattern(pattern)
 
 
 
 
 
 
 
 
 
 
 
1841
 
1842
+ def _emit_add_pattern(self, pattern: str):
1843
+ """Emit request to add a pattern (handled by parent)."""
1844
+ # This will be connected to the main window's add method
1845
+ if hasattr(self, "_add_pattern_callback"):
1846
+ self._add_pattern_callback(pattern)
1847
 
1848
+ def set_add_callback(self, callback: Callable[[str], None]):
1849
+ """Set the callback for adding patterns."""
1850
+ self._add_pattern_callback = callback
1851
 
1852
+ def add_rule_chip(self, rule: FilterRule):
1853
+ """Add a rule chip to the panel."""
1854
+ if rule.pattern in self.rule_chips:
1855
+ return
1856
 
1857
+ # Hide empty label
1858
+ self.empty_label.pack_forget()
1859
 
1860
+ chip = RuleChip(
1861
+ self.rules_frame,
1862
+ rule,
1863
+ on_delete=self._on_rule_delete,
1864
+ on_click=self.on_rule_clicked,
 
1865
  )
1866
+ chip.pack(fill="x", pady=2)
1867
+ self.rule_chips[rule.pattern] = chip
 
 
 
 
 
1868
 
1869
+ def remove_rule_chip(self, pattern: str):
1870
+ """Remove a rule chip from the panel."""
1871
+ if pattern in self.rule_chips:
1872
+ self.rule_chips[pattern].destroy()
1873
+ del self.rule_chips[pattern]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1874
 
1875
+ # Show empty label if no rules
1876
+ if not self.rule_chips:
1877
+ self.empty_label.pack(expand=True, pady=20)
1878
 
1879
+ def _on_rule_delete(self, pattern: str):
1880
+ """Handle rule deletion."""
1881
+ if hasattr(self, "_delete_pattern_callback"):
1882
+ self._delete_pattern_callback(pattern)
 
 
 
 
1883
 
1884
+ def set_delete_callback(self, callback: Callable[[str], None]):
1885
+ """Set the callback for deleting patterns."""
1886
+ self._delete_pattern_callback = callback
1887
 
1888
+ def update_rule_counts(self, rules: List[FilterRule], models: List[str]):
1889
+ """Update affected counts for all rule chips."""
1890
+ for rule in rules:
1891
+ if rule.pattern in self.rule_chips:
1892
+ self.rule_chips[rule.pattern].update_count(
1893
+ rule.affected_count, rule.affected_models
1894
+ )
1895
 
1896
+ def highlight_rule(self, pattern: str):
1897
+ """Highlight a specific rule chip."""
1898
+ for p, chip in self.rule_chips.items():
1899
+ chip.set_highlighted(p == pattern)
1900
 
1901
  def clear_highlights(self):
1902
+ """Clear all rule highlights."""
1903
+ for chip in self.rule_chips.values():
1904
+ chip.set_highlighted(False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1905
 
1906
+ def clear_all(self):
1907
+ """Remove all rule chips."""
1908
+ for chip in list(self.rule_chips.values()):
1909
+ chip.destroy()
1910
+ self.rule_chips.clear()
1911
+ self.empty_label.pack(expand=True, pady=20)
1912
 
1913
+ def get_input_text(self) -> str:
1914
+ """Get current input text."""
1915
+ return self.input_entry.get().strip()
 
 
 
1916
 
1917
+ def clear_input(self):
1918
+ """Clear the input field."""
1919
+ self.input_entry.delete(0, "end")
1920
 
1921
 
1922
  # ════════════════════════════════════════════════════════════════════════════════
 
2103
 
2104
  def _create_model_lists(self):
2105
  """Create the synchronized model list panel."""
2106
+ # Use the virtual list implementation for performance
2107
+ self.model_list_panel = VirtualSyncModelLists(
2108
  self,
2109
  on_model_click=self._on_model_clicked,
2110
  on_model_right_click=self._on_model_right_clicked,
 
2500
  self.filter_engine.preview_pattern(pattern, rule_type, self.models)
2501
  )
2502
 
2503
+ # Highlight affected models using new virtual list API
2504
  if affected:
2505
+ affected_set = set(affected)
2506
+ self.model_list_panel.left_list.highlight_models(affected_set)
2507
+ self.model_list_panel.right_list.highlight_models(affected_set)
 
2508
 
2509
  # Scroll to first affected
2510
  self.model_list_panel.scroll_to_affected(affected)