""" 降级拖拽检测器 当平台特定检测器不可用时的降级方案。 """ import os import time import threading from typing import List from .base_detector import BaseDragDetector class FallbackDragDetector(BaseDragDetector): """降级拖拽检测器""" def __init__(self, supported_formats: List[str] = None): """ 初始化降级检测器 Args: supported_formats: 支持的文件格式列表 """ super().__init__(supported_formats) self._watch_directory = os.path.expanduser("~/Desktop") # 监控桌面 self._known_files = set() self._scan_interval = 0.5 # 扫描间隔(秒) # 初始化已知文件列表 self._scan_known_files() def _scan_known_files(self): """扫描已知文件""" self._known_files.clear() if os.path.exists(self._watch_directory): for filename in os.listdir(self._watch_directory): if self._is_supported_format(filename): filepath = os.path.join(self._watch_directory, filename) self._known_files.add(filepath) def _monitor_loop(self): """监控循环 - 通过文件系统变化检测""" while self.is_running: try: if os.path.exists(self._watch_directory): current_files = set() for filename in os.listdir(self._watch_directory): if self._is_supported_format(filename): filepath = os.path.join(self._watch_directory, filename) current_files.add(filepath) # 检测新文件 new_files = current_files - self._known_files for filepath in new_files: # 检查文件是否最近创建(1秒内) try: file_time = os.path.getmtime(filepath) if time.time() - file_time < 1.0: self.add_dropped_file(filepath) except OSError: pass self._known_files = current_files except Exception as e: print(f"降级检测器错误: {e}") time.sleep(self._scan_interval) def is_supported(self) -> bool: """降级检测器总是支持""" return True def set_watch_directory(self, directory: str): """ 设置监控目录 Args: directory: 要监控的目录路径 """ if os.path.exists(directory): self._watch_directory = directory self._scan_known_files()