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:
@@ -10,6 +10,8 @@ import tkinter as tk
|
||||
from tkinter import ttk, scrolledtext
|
||||
from typing import Optional, Callable
|
||||
from datetime import datetime
|
||||
from gui.log_config import LogTheme
|
||||
from gui.widgets.base_dialog import BaseDialog
|
||||
|
||||
# 尝试导入 tkinterweb 和 markdown2
|
||||
try:
|
||||
@@ -25,7 +27,7 @@ except ImportError:
|
||||
HAS_MARKDOWN2 = False
|
||||
|
||||
|
||||
class DeleteProgressWindow:
|
||||
class DeleteProgressWindow(BaseDialog):
|
||||
"""删除进度窗口"""
|
||||
|
||||
def __init__(
|
||||
@@ -46,40 +48,51 @@ class DeleteProgressWindow:
|
||||
dryrun: 是否为预览模式
|
||||
on_cancel: 取消回调函数
|
||||
"""
|
||||
self.parent = parent
|
||||
self.on_cancel = on_cancel
|
||||
self.cancelled = False
|
||||
self.managers = managers
|
||||
self.dryrun = dryrun
|
||||
self.main_frame = None
|
||||
self.progress_frame = None
|
||||
self.progress_var = None
|
||||
self.progress_label = None
|
||||
self.progress_bar = None
|
||||
self.log_frame = None
|
||||
self.log_text = None
|
||||
self.report_frame = None
|
||||
self.report_html = None
|
||||
self.report_text = None
|
||||
self.cancel_button = None
|
||||
self.close_button = None
|
||||
|
||||
# 创建窗口
|
||||
self.window = tk.Toplevel(parent)
|
||||
self.window.title(title)
|
||||
self.window.resizable(True, True)
|
||||
self.window.transient(parent)
|
||||
# 调用父类初始化(会自动设置为模态并居中)
|
||||
super().__init__(parent, title)
|
||||
|
||||
# 设置窗口大小
|
||||
self.window.geometry("700x600")
|
||||
# 设置可调整大小
|
||||
self.resizable(True, True)
|
||||
|
||||
# 创建内容
|
||||
self._create_widgets()
|
||||
|
||||
# 居中显示
|
||||
self._center()
|
||||
# 设置固定大小并重新居中
|
||||
self._set_fixed_size(700, 600)
|
||||
|
||||
def _center(self):
|
||||
"""将窗口居中显示"""
|
||||
self.window.update_idletasks()
|
||||
width = 700
|
||||
height = 600
|
||||
x = (self.window.winfo_screenwidth() // 2) - (width // 2)
|
||||
y = (self.window.winfo_screenheight() // 2) - (height // 2)
|
||||
self.window.geometry(f"{width}x{height}+{x}+{y}")
|
||||
def _set_fixed_size(self, width: int, height: int):
|
||||
"""
|
||||
设置固定大小并重新居中
|
||||
|
||||
Args:
|
||||
width: 宽度
|
||||
height: 高度
|
||||
"""
|
||||
self.update_idletasks()
|
||||
self.geometry(f"{width}x{height}")
|
||||
self._center_window()
|
||||
|
||||
def _create_widgets(self):
|
||||
"""创建窗口组件"""
|
||||
# 主容器
|
||||
self.main_frame = ttk.Frame(self.window, padding=10)
|
||||
self.main_frame = ttk.Frame(self, padding=10)
|
||||
self.main_frame.pack(fill=tk.BOTH, expand=True)
|
||||
|
||||
# 信息区域
|
||||
@@ -124,11 +137,11 @@ class DeleteProgressWindow:
|
||||
)
|
||||
self.log_text.pack(fill=tk.BOTH, expand=True)
|
||||
|
||||
# 配置日志标签颜色
|
||||
self.log_text.tag_configure('info', foreground='black')
|
||||
self.log_text.tag_configure('success', foreground='green')
|
||||
self.log_text.tag_configure('warning', foreground='orange')
|
||||
self.log_text.tag_configure('error', foreground='red')
|
||||
# 配置日志标签颜色(使用统一主题)
|
||||
self.log_text.tag_configure('info', foreground=LogTheme.get_color('INFO'))
|
||||
self.log_text.tag_configure('success', foreground=LogTheme.get_color('SUCCESS'))
|
||||
self.log_text.tag_configure('warning', foreground=LogTheme.get_color('WARNING'))
|
||||
self.log_text.tag_configure('error', foreground=LogTheme.get_color('ERROR'))
|
||||
|
||||
# 报告区域(完成后显示)- 初始隐藏
|
||||
self.report_frame = ttk.LabelFrame(self.main_frame, text="执行报告", padding=5)
|
||||
@@ -191,7 +204,7 @@ class DeleteProgressWindow:
|
||||
self.progress_var.set(message)
|
||||
else:
|
||||
self.progress_var.set(message)
|
||||
self.window.update_idletasks()
|
||||
self.update_idletasks()
|
||||
|
||||
def append_log(self, message: str, level: str = "info"):
|
||||
"""
|
||||
@@ -208,7 +221,7 @@ class DeleteProgressWindow:
|
||||
self.log_text.insert(tk.END, log_entry, level)
|
||||
self.log_text.see(tk.END)
|
||||
self.log_text.config(state=tk.DISABLED)
|
||||
self.window.update_idletasks()
|
||||
self.update_idletasks()
|
||||
|
||||
def show_report(self, markdown_content: str):
|
||||
"""
|
||||
@@ -242,7 +255,7 @@ class DeleteProgressWindow:
|
||||
self.report_text.config(state=tk.DISABLED)
|
||||
|
||||
# 更新标题
|
||||
self.window.title("执行报告")
|
||||
self.title("执行报告")
|
||||
|
||||
# 隐藏取消按钮,显示关闭按钮
|
||||
self.cancel_button.pack_forget()
|
||||
@@ -435,7 +448,7 @@ class DeleteProgressWindow:
|
||||
|
||||
def close(self):
|
||||
"""关闭窗口"""
|
||||
self.window.destroy()
|
||||
self.destroy()
|
||||
|
||||
def is_cancelled(self) -> bool:
|
||||
"""检查是否已取消"""
|
||||
@@ -444,4 +457,4 @@ class DeleteProgressWindow:
|
||||
def set_completed(self):
|
||||
"""设置为完成状态"""
|
||||
self.cancel_button.pack_forget()
|
||||
self.close_button.pack(side=tk.RIGHT)
|
||||
self.close_button.pack(side=tk.RIGHT)
|
||||
|
||||
Reference in New Issue
Block a user