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:
117
gui/widgets/base_dialog.py
Normal file
117
gui/widgets/base_dialog.py
Normal file
@@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Base Dialog - 基础对话框类
|
||||
|
||||
所有对话框的基类,提供通用功能:
|
||||
- 模态对话框设置
|
||||
- 窗口居中显示
|
||||
- 统一的对话框生命周期管理
|
||||
"""
|
||||
import tkinter as tk
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class BaseDialog(tk.Toplevel):
|
||||
"""
|
||||
所有对话框的基类,提供通用功能
|
||||
|
||||
Usage:
|
||||
class MyDialog(BaseDialog):
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent, title="My Dialog")
|
||||
self._create_content()
|
||||
|
||||
def _create_content(self):
|
||||
# 创建对话框内容
|
||||
pass
|
||||
|
||||
dialog = MyDialog(parent)
|
||||
result = dialog.get_result()
|
||||
"""
|
||||
|
||||
def __init__(self, parent, title: str, **kwargs):
|
||||
"""
|
||||
初始化基础对话框
|
||||
|
||||
Args:
|
||||
parent: 父窗口
|
||||
title: 对话框标题
|
||||
**kwargs: 传递给 tk.Toplevel 的其他参数
|
||||
"""
|
||||
super().__init__(parent, **kwargs)
|
||||
|
||||
self.parent = parent
|
||||
self.result = None
|
||||
self.title(title)
|
||||
|
||||
# 设置为模态对话框并居中
|
||||
self._setup_modal()
|
||||
|
||||
def _setup_modal(self):
|
||||
"""设置为模态对话框并居中"""
|
||||
self.transient(self.parent)
|
||||
self.grab_set()
|
||||
self._center_window()
|
||||
|
||||
def _center_window(self):
|
||||
"""
|
||||
窗口居中显示(统一实现)
|
||||
|
||||
根据对话框大小自动居中到屏幕中央
|
||||
"""
|
||||
self.update_idletasks()
|
||||
width = self.winfo_width()
|
||||
height = self.winfo_height()
|
||||
|
||||
# 如果窗口还没有实际大小,使用默认最小值
|
||||
if width <= 1:
|
||||
width = 400
|
||||
if height <= 1:
|
||||
height = 300
|
||||
|
||||
x = (self.winfo_screenwidth() // 2) - (width // 2)
|
||||
y = (self.winfo_screenheight() // 2) - (height // 2)
|
||||
self.geometry(f'{width}x{height}+{x}+{y}')
|
||||
|
||||
def _center_on_parent(self):
|
||||
"""
|
||||
窗口居中到父窗口
|
||||
|
||||
将对话框居中显示在父窗口中央,而不是屏幕中央
|
||||
"""
|
||||
self.update_idletasks()
|
||||
self.parent.update_idletasks()
|
||||
|
||||
width = self.winfo_width()
|
||||
height = self.winfo_height()
|
||||
|
||||
# 如果窗口还没有实际大小,使用默认最小值
|
||||
if width <= 1:
|
||||
width = 400
|
||||
if height <= 1:
|
||||
height = 300
|
||||
|
||||
parent_x = self.parent.winfo_x()
|
||||
parent_y = self.parent.winfo_y()
|
||||
parent_width = self.parent.winfo_width()
|
||||
parent_height = self.parent.winfo_height()
|
||||
|
||||
x = parent_x + (parent_width - width) // 2
|
||||
y = parent_y + (parent_height - height) // 2
|
||||
self.geometry(f"{width}x{height}+{x}+{y}")
|
||||
|
||||
def get_result(self):
|
||||
"""
|
||||
获取对话框结果
|
||||
|
||||
子类应设置 self.result 来返回结果
|
||||
|
||||
Returns:
|
||||
对话框结果,类型由子类定义
|
||||
"""
|
||||
return self.result
|
||||
|
||||
def close(self):
|
||||
"""关闭对话框"""
|
||||
self.destroy()
|
||||
Reference in New Issue
Block a user