refactor: implement UI code optimization and refactoring

This commit implements the comprehensive UI code refactoring plan to improve
code quality, reduce duplication, and enhance maintainability.

Phase 1: High Priority Improvements
- Create BaseDialog class to eliminate window centering code duplication
  across ProgressDialog, DeleteProgressWindow, LoginDialog, and
  UserSelectionDialog (~30 lines of duplicate code removed)
- Unify log color configuration with LogTheme class in gui/log_config.py
- Create permission check decorators (@require_admin, @require_permission,
  @require_user_type, @handle_errors)
- Update all dialog classes to use unified patterns

Phase 2: Architecture Improvements
- Create input validation framework (ValidationResult, Validator,
  ValidatedWidget classes)
- Implement StateManager with observer pattern for component state sharing
- Create unified ErrorHandler for consistent error handling
- Extract CheckboxTreeview into reusable component

New Modules:
- gui/widgets/base_dialog.py - Base dialog class with modal setup and centering
- gui/utils/decorators.py - Permission and error handling decorators
- gui/utils/error_handler.py - Unified error handling with user-friendly messages
- gui/utils/state_manager.py - State management with observer pattern
- gui/utils/validators.py - Input validation framework
- gui/material_validation/checkbox_treeview.py - Reusable checkbox table

Modified Files:
- gui/log_config.py - Added LogTheme class for centralized styling
- gui/widgets/log_text.py - Use LogTheme.COLORS
- gui/widgets/progress_dialog.py - Inherit from BaseDialog
- gui/widgets/delete_progress_window.py - Inherit from BaseDialog, use LogTheme
- gui/widgets/__init__.py - Add new exports, optional imports
- gui/login_dialog.py - Use unified centering pattern
- gui/user_selection_dialog.py - Use unified centering pattern
- gui/material_validation_tab.py - Import CheckboxTreeview from new module

Benefits:
- Reduced code duplication by ~200 lines
- Improved maintainability through centralized configuration
- Better abstraction with 5 new reusable base classes
- Enhanced type safety with type hints
- Future-proof theming support via LogTheme

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka Server
2026-02-26 18:40:26 +08:00
parent 3a9c6f0978
commit f715cb97e3
16 changed files with 1735 additions and 313 deletions

View File

@@ -5,12 +5,96 @@ GUI 日志配置模块
统一配置 GUI 应用和控制台的日志输出
"""
import logging
from typing import Dict
from tkinter import font as tk_font
# 日志格式配置
LOG_FORMAT = '%(asctime)s [%(levelname)s] %(message)s'
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
class LogTheme:
"""
统一的日志主题配置
提供颜色、字体等样式配置,支持未来的主题切换(如暗色模式)
"""
# 颜色方案
COLORS = {
'INFO': '#000000', # 黑色
'SUCCESS': '#008000', # 绿色
'WARNING': '#FF8C00', # 深橙色
'ERROR': '#FF0000', # 红色
'DEBUG': '#808080', # 灰色
}
# 字体配置
FONTS = {
'DEFAULT': ('TkDefaultFont', 9),
'HEADER': ('TkDefaultFont', 10, 'bold'),
'MONOSPACE': ('Consolas', 9),
'MONOSPACE_SMALL': ('Consolas', 8),
}
# 背景颜色(用于未来的暗色模式支持)
BACKGROUNDS = {
'LIGHT': '#FFFFFF',
'DARK': '#1E1E1E',
}
# 当前主题
_current_theme = 'LIGHT'
@classmethod
def get_color(cls, level: str) -> str:
"""
获取指定日志级别的颜色
Args:
level: 日志级别 (INFO, SUCCESS, WARNING, ERROR, DEBUG)
Returns:
颜色值(十六进制字符串)
"""
return cls.COLORS.get(level.upper(), '#000000')
@classmethod
def get_font(cls, font_name: str = 'DEFAULT'):
"""
获取指定字体配置
Args:
font_name: 字体名称 (DEFAULT, HEADER, MONOSPACE, MONOSPACE_SMALL)
Returns:
字体配置元组
"""
return cls.FONTS.get(font_name, cls.FONTS['DEFAULT'])
@classmethod
def set_theme(cls, theme: str):
"""
设置主题LIGHT 或 DARK
Args:
theme: 主题名称
"""
if theme.upper() in ['LIGHT', 'DARK']:
cls._current_theme = theme.upper()
@classmethod
def get_background(cls) -> str:
"""
获取当前主题的背景颜色
Returns:
背景颜色值
"""
return cls.BACKGROUNDS.get(cls._current_theme, '#FFFFFF')
def setup_gui_logging(level=logging.INFO):
"""
初始化 GUI 应用的日志配置