hydraveil-gui/gui/v2/presenters/state/fast_registration_state.py

182 lines
7 KiB
Python

from core.services.profiles.filter_protocols import protocol_matches_profile_type, app_matches_profile_type
from gui.v2.presenters.data.location_data import get_location_display_data
from gui.v2.presenters.data.app_data import get_app_display_data
from gui.v2.presenters.data.protocol_data import get_protocol_button_data
from gui.v2.presenters.data.resolution_data import get_resolution_data
from gui.v2.presenters.data.type_data import get_profile_type_data
from gui.v2.presenters.image_tools.button_factory import create_button_image, create_resolution_button_image, blank_button, blank_button_path
from pathlib import Path
class RegistrationState:
def __init__(self, btn_path):
self.btn_path = btn_path
self.all_options = {}
self.selections = {}
self.order_map = {}
self.pixmap_cache = {}
# Maps a category to the function that validates its current selection.
self._filter_map = {
"Protocol": self.is_protocol_valid,
"Browser": self.is_app_valid,
}
# Maps a "Trigger" category to the categories it affects.
self._dependency_map = {
"Type": ["Protocol", "Browser"],
}
# Callbacks to notify the UI. Store functions here: { "Category": callback_func }
self.listeners = {}
self._load_all_data()
def register_listener(self, category, callback):
"""UI elements call this to be notified when their category changes."""
self.listeners[category] = callback
def _load_all_data(self):
"""Load the data for a given category, and then register that data"""
load_config = {
"Type": (get_profile_type_data, 0),
"Protocol": (get_protocol_button_data, 0),
"Resolution": (get_resolution_data, 0),
"Location": (get_location_display_data, 0),
"Browser": (get_app_display_data, 0),
}
for category, (loader_func, default_idx) in load_config.items():
data = loader_func(self.btn_path)
self._register_category(category, data, default_idx)
def _register_category(self, category, data, default_idx=0):
"""Setup a category with a given data set and default index"""
if isinstance(data, list):
data = {item: item for item in data}
self.all_options[category] = data
self.order_map[category] = list(data.keys())
# Default selection logic
ids = self.order_map[category]
self.selections[category] = ids[default_idx] if ids and default_idx < len(ids) else (ids[0] if ids else None)
def _ensure_valid(self, category):
"""Generic validator: resets selection to first valid option, or None if none exist."""
filter_func = self._filter_map.get(category)
if not filter_func:
return
current_val = self.selections.get(category)
# If current selection is invalid (or None), try to find a replacement
if current_val is None or not filter_func(current_val):
found_valid = False
for candidate_id in self.order_map.get(category, []):
if filter_func(candidate_id):
self.selections[category] = candidate_id
found_valid = True
break
# If no valid option is found, set to None to trigger blank image
if not found_valid:
self.selections[category] = None
def cycle_selection(self, category, direction):
ids = self.order_map.get(category, [])
if not ids:
self.selections[category] = None
self._notify(category)
return None
current_id = self.selections.get(category)
current_index = ids.index(current_id) if current_id in ids else 0
filter_func = self._filter_map.get(category)
found_valid = False
for i in range(1, len(ids) + 1):
next_index = (current_index + (i * direction)) % len(ids)
candidate_id = ids[next_index]
if filter_func is None or filter_func(candidate_id):
self.selections[category] = candidate_id
found_valid = True
break
# If we cycled through everything and found nothing valid, set to None
if not found_valid:
self.selections[category] = None
# Reactive Updates
dependents = self._dependency_map.get(category, [])
# 1. Notify the category that was just cycled
self._notify(category)
# 2. Check and notify all dependents (e.g., Type -> Browser)
for dep in dependents:
self._ensure_valid(dep)
self._notify(dep)
return self.get_current_image(category)
def _notify(self, category):
"""Triggers the UI callback for a specific category."""
if category in self.listeners:
self.listeners[category]()
def get_current_image(self, category):
"""Returns an image or a path to one depending on the category and data format"""
current_id = self.selections.get(category)
if current_id is None:
return blank_button(self.btn_path)
# Cache check
cache_key = f"{category}_{current_id}"
if cache_key in self.pixmap_cache:
return self.pixmap_cache[cache_key]
# 3. IMAGE DISPATCH: sorts based on data type
data = self.all_options[category][current_id]
# Special Case: Resolution
if category == "Resolution":
res_img = create_resolution_button_image(
btn_path=f"{self.btn_path}/resolution_template.png",
resolution_text=str(current_id)
)
self.pixmap_cache[cache_key] = res_img
return res_img
if data is None:
return blank_button(self.btn_path)
if isinstance(data, Path):
return data
if isinstance(data, dict):
pixmap = create_button_image(
first_line=data.get('name', ''),
fallback_image_path=data.get('path'),
second_line=data.get('second_line'),
existing_icon_path=data.get('existing_icon')
)
self.pixmap_cache[cache_key] = pixmap
return pixmap
else:
print("Error with Display State, invalid choice. But returning a blank image,")
return blank_button(self.btn_path)
# Validation logic, used by the generic _ensure_valid
def is_app_valid(self, app_choice):
current_type = self.selections.get("Type")
return app_matches_profile_type(app_choice=app_choice, profile_type=current_type)
def is_protocol_valid(self, protocol_id):
current_type = self.selections.get("Type")
return True if current_type is None else protocol_matches_profile_type(protocol=protocol_id, profile_type=current_type)
def get_final_choices(self):
return self.selections