79 lines
3.4 KiB
Python
79 lines
3.4 KiB
Python
import logging
|
|
import os
|
|
import sys
|
|
|
|
# Import components from other model files
|
|
from model.constants import STOP_KEY, AHK_TEMPLATE_REL_PATH_FROM_MODEL_DIR, AHK_EXECUTABLE_FALLBACK_PATHS, AUTOHOTKEY_KEY_MAP
|
|
from model.app_logger import message_queue # Assuming app_logger provides the message_queue and handles initial logging setup
|
|
from model.ahk_process_manager import AHKProcessManager
|
|
from model.window_manager import WindowManager
|
|
from pynput.keyboard import Key, Listener # Still needed here for STOP_KEY in on_press_listener callback directly
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class KeyPressModel:
|
|
def __init__(self):
|
|
self.current_target_window_title = ""
|
|
self.current_key_to_press_str = '+'
|
|
self._ahk_stopped_callback = None # Callback to Controller when AHK script stops externally
|
|
|
|
# Instantiate sub-components that manage specific areas of the model's responsibility
|
|
self.ahk_manager = AHKProcessManager(
|
|
ahk_template_rel_path=AHK_TEMPLATE_REL_PATH_FROM_MODEL_DIR,
|
|
ahk_executable_fallback_paths=AHK_EXECUTABLE_FALLBACK_PATHS,
|
|
autohotkey_key_map=AUTOHOTKEY_KEY_MAP,
|
|
stop_key=STOP_KEY,
|
|
message_queue=message_queue # Pass the central message queue
|
|
)
|
|
self.window_manager = WindowManager()
|
|
|
|
# Register callback for AHK process stopping
|
|
self.ahk_manager.register_ahk_stopped_callback(self._ahk_process_stopped_internally)
|
|
|
|
logger.info("Model: KeyPressModel Initialized. Sub-components loaded.")
|
|
|
|
def _ahk_process_stopped_internally(self):
|
|
"""Internal callback from AHKProcessManager when AHK script stops."""
|
|
logger.info("Model: AHK process reported termination.")
|
|
if self._ahk_stopped_callback:
|
|
self._ahk_stopped_callback()
|
|
|
|
def register_ahk_stopped_callback(self, callback_func):
|
|
"""Registers a callback function to be called when the AHK script stops externally."""
|
|
self._ahk_stopped_callback = callback_func
|
|
logger.info("Model: AHK stopped callback registered by Controller.")
|
|
|
|
def get_window_titles(self):
|
|
"""Retrieves a sorted list of active window titles from WindowManager."""
|
|
return self.window_manager.get_window_titles()
|
|
|
|
def set_target_window(self, title):
|
|
"""Sets the target window title for AHK operations in AHKProcessManager."""
|
|
self.current_target_window_title = title
|
|
self.ahk_manager.set_target_window(title)
|
|
logger.info(f"Model: Target window set to: '{self.current_target_window_title}' in AHK manager.")
|
|
|
|
def set_key_to_press(self, key_str):
|
|
"""Sets the key string to be pressed by AHK in AHKProcessManager."""
|
|
self.current_key_to_press_str = key_str
|
|
self.ahk_manager.set_key_to_press(key_str)
|
|
logger.info(f"Model: Key to press set to: '{self.current_key_to_press_str}' in AHK manager.")
|
|
|
|
def get_message_queue(self):
|
|
"""Returns the central message queue."""
|
|
return message_queue
|
|
|
|
def start_autohotkey_script(self):
|
|
"""Initiates the AHK script launch via AHKProcessManager."""
|
|
return self.ahk_manager.start_script()
|
|
|
|
def stop_autohotkey_script(self):
|
|
"""Terminates the AHK script via AHKProcessManager."""
|
|
self.ahk_manager.stop_script()
|
|
|
|
def start_pynput_listener_thread(self):
|
|
"""Starts the pynput keyboard listener thread via AHKProcessManager."""
|
|
self.ahk_manager.start_pynput_listener()
|
|
|