继续优化UI加载速度

This commit is contained in:
xinzhu.yin
2026-06-22 10:52:51 +08:00
parent 92cba114a9
commit 2d8d4119e3
7 changed files with 307 additions and 72 deletions

View File

@@ -1220,6 +1220,10 @@ def _build_ucd_resized_image(image_path: str, target_w: int, target_h: int) -> s
def refresh_ai_image_theme(self: "PQAutomationApp"):
"""刷新 AI 图片面板中的主题相关控件。"""
if getattr(self, "current_panel", None) != "ai_image":
self._ai_image_theme_dirty = True
return
self._ai_image_theme_dirty = False
if hasattr(self, "_apply_ai_image_list_style"):
self._apply_ai_image_list_style()
tip = getattr(self, "_ai_image_tooltip", None)

View File

@@ -699,9 +699,14 @@ def _on_calman_panel_shown(self: "PQAutomationApp") -> None:
"""面板切到前台时只做轻量刷新,不重绘 Matplotlib。"""
_refresh_calman_config_summary(self)
_schedule_adaptive_matrix_columns(self)
if getattr(self, "_calman_theme_dirty", False):
_redraw_calman_charts(self)
self._calman_theme_dirty = False
def _schedule_adaptive_matrix_columns(self: "PQAutomationApp") -> None:
if getattr(self, "_theme_transition", False):
return
job = getattr(self, "_calman_matrix_col_job", None)
if job is not None:
try:
@@ -1116,7 +1121,7 @@ def _adaptive_matrix_columns(self: "PQAutomationApp") -> None:
self.calman_data_tree.column(str(p), width=width, minwidth=min_w, stretch=False)
def _redraw_calman_charts(self: "PQAutomationApp") -> None:
def _redraw_calman_charts(self: "PQAutomationApp", *, sync_draw: bool = False) -> None:
"""根据 calman_results 重绘四张图和 xy 散点。"""
palette = _get_calman_palette()
if hasattr(self, "calman_fig"):
@@ -1236,12 +1241,15 @@ def _redraw_calman_charts(self: "PQAutomationApp") -> None:
a4.set_ylim(1.8, 2.8)
a4.set_xlabel("", fontsize=8)
self.calman_canvas.draw_idle()
if sync_draw or getattr(self, "_theme_transition", False):
self.calman_canvas.draw()
else:
self.calman_canvas.draw_idle()
_redraw_xy_chart(self)
_redraw_xy_chart(self, sync_draw=sync_draw)
def _redraw_xy_chart(self: "PQAutomationApp") -> None:
def _redraw_xy_chart(self: "PQAutomationApp", *, sync_draw: bool = False) -> None:
palette = _get_calman_palette()
if hasattr(self, "calman_xy_fig"):
self.calman_xy_fig.patch.set_facecolor(palette["figure_bg"])
@@ -1268,7 +1276,10 @@ def _redraw_xy_chart(self: "PQAutomationApp") -> None:
last = recs[-1]
ax.plot([last["x"]], [last["y"]], marker="o", color="#ffcc00", markersize=5)
ax.plot([last["x"], D65_X], [last["y"], D65_Y], color=_mix(palette["fg"], palette["axes_bg"], 0.4), linewidth=0.8)
self.calman_xy_canvas.draw_idle()
if sync_draw or getattr(self, "_theme_transition", False):
self.calman_xy_canvas.draw()
else:
self.calman_xy_canvas.draw_idle()
def _update_actual_strip(self: "PQAutomationApp") -> None:
@@ -1360,11 +1371,18 @@ def _refresh_metric_table(self: "PQAutomationApp") -> None:
self.calman_table_ysb.set(first, last)
def refresh_calman_theme(self: "PQAutomationApp") -> None:
def refresh_calman_theme(self: "PQAutomationApp", *, redraw_charts: bool | None = None) -> None:
"""主题切换后刷新 Calman 灰阶调试面板的表格与图表颜色。"""
if not hasattr(self, "calman_frame"):
return
if getattr(self, "current_panel", None) != "calman":
self._calman_theme_dirty = True
return
if redraw_charts is None:
redraw_charts = True
palette = _get_calman_palette()
if hasattr(self, "calman_elapsed_label"):
@@ -1388,7 +1406,11 @@ def refresh_calman_theme(self: "PQAutomationApp") -> None:
_refresh_metric_table(self)
_refresh_calman_config_summary(self)
_redraw_calman_charts(self)
if redraw_charts:
_redraw_calman_charts(self, sync_draw=getattr(self, "_theme_transition", False))
self._calman_theme_dirty = False
else:
self._calman_theme_dirty = True
class CalmanPanelMixin:

View File

@@ -172,6 +172,14 @@ def _apply_custom_result_theme(self: "PQAutomationApp"):
def refresh_custom_template_theme(self: "PQAutomationApp"):
"""刷新客户模板结果表的主题色。"""
notebook = getattr(self, "chart_notebook", None)
tab_frame = getattr(self, "custom_template_tab_frame", None)
if notebook is not None and tab_frame is not None:
try:
if str(tab_frame) not in notebook.tabs():
return
except Exception:
pass
_apply_custom_result_theme(self)

View File

@@ -869,9 +869,113 @@ def _refresh_theme_toggle_label(self: "PQAutomationApp") -> None:
self.theme_toggle_btn.configure(text="切换深色模式")
def _replot_charts_for_current_theme(self: "PQAutomationApp") -> None:
"""刷新结果区图表配色(仅同步绘制当前可见 Tab"""
if hasattr(self, "refresh_result_charts_theme"):
try:
self.refresh_result_charts_theme(sync_draw=True, visible_only=True)
return
except Exception:
pass
test_type = None
if hasattr(self, "config"):
test_type = getattr(self.config, "current_test_type", None)
snapshots = {}
if test_type and hasattr(self, "_chart_snapshots"):
snapshots = self._chart_snapshots.get(test_type) or {}
if not snapshots:
if hasattr(self, "apply_result_chart_theme"):
self.apply_result_chart_theme(sync_draw=True)
return
for chart_name, args in snapshots.items():
plot_fn = getattr(self, f"plot_{chart_name}", None)
if plot_fn:
try:
plot_fn(*args)
except Exception:
pass
def _cancel_pending_ui_jobs(self: "PQAutomationApp") -> None:
"""取消可能在主题切换后继续触发的延迟 UI 任务。"""
for attr in ("_calman_matrix_col_job", "_chart_restore_job"):
job = getattr(self, attr, None)
if job is None:
continue
try:
self.root.after_cancel(job)
except Exception:
pass
setattr(self, attr, None)
def _refresh_all_theme_widgets(self: "PQAutomationApp") -> None:
"""同步刷新主题相关控件:优先处理可见区域,隐藏面板延后。"""
_refresh_theme_toggle_label(self)
# 侧栏 / 连接指示 / 选中态 —— 始终可见,必须立即更新
for method_name in (
"refresh_tool_panel_sidebar_theme",
"refresh_connection_indicators",
):
method = getattr(self, method_name, None)
if method is not None:
try:
method()
except Exception:
pass
current_panel = getattr(self, "current_panel", None)
if current_panel == "ai_image" and hasattr(self, "refresh_ai_image_theme"):
try:
self.refresh_ai_image_theme()
except Exception:
pass
else:
self._ai_image_theme_dirty = True
if current_panel == "single_step" and hasattr(self, "refresh_single_step_theme"):
try:
self.refresh_single_step_theme()
except Exception:
pass
if current_panel == "log" and hasattr(self, "log_gui") and hasattr(
self.log_gui, "refresh_log_theme"
):
try:
self.log_gui.refresh_log_theme()
except Exception:
pass
if hasattr(self, "refresh_custom_template_theme"):
try:
self.refresh_custom_template_theme()
except Exception:
pass
if hasattr(self, "refresh_calman_theme"):
try:
self.refresh_calman_theme()
except Exception:
pass
if hasattr(self, "update_sidebar_selection"):
try:
self.update_sidebar_selection()
except Exception:
pass
_replot_charts_for_current_theme(self)
def _on_toggle_theme(self: "PQAutomationApp") -> None:
"""切换主题:重新应用 ttk 样式并刷新所有自定义样式相关的标签"""
# 在测试进行时禁止切换主题,避免影响测量稳定性
"""切换主题:保持窗口可见,同步批量刷新避免卡顿与分批变色"""
if getattr(self, "testing", False):
try:
if hasattr(self, "log_gui"):
@@ -881,67 +985,14 @@ def _on_toggle_theme(self: "PQAutomationApp") -> None:
return
from app.views.theme_manager import toggle_theme
toggle_theme()
# apply_modern_styles()
_refresh_theme_toggle_label(self)
if hasattr(self, "refresh_tool_panel_sidebar_theme"):
try:
self.refresh_tool_panel_sidebar_theme()
except Exception:
pass
if hasattr(self, "refresh_connection_indicators"):
try:
self.refresh_connection_indicators()
except Exception:
pass
if hasattr(self, "apply_result_chart_theme"):
try:
self.apply_result_chart_theme()
except Exception:
pass
if hasattr(self, "log_gui") and hasattr(self.log_gui, "refresh_log_theme"):
try:
self.log_gui.refresh_log_theme()
except Exception:
pass
if hasattr(self, "refresh_ai_image_theme"):
try:
self.refresh_ai_image_theme()
except Exception:
pass
if hasattr(self, "refresh_single_step_theme"):
try:
self.refresh_single_step_theme()
except Exception:
pass
if hasattr(self, "refresh_custom_template_theme"):
try:
self.refresh_custom_template_theme()
except Exception:
pass
if hasattr(self, "refresh_calman_theme"):
try:
self.refresh_calman_theme()
except Exception:
pass
# 同步刷新侧栏选中态(高亮样式跟随新色板)
if hasattr(self, "update_sidebar_selection"):
try:
self.update_sidebar_selection()
except Exception:
pass
# 以新的 dark_mode 值重绘当前测试类型的所有图表
if hasattr(self, "_chart_snapshots") and hasattr(self, "config"):
test_type = getattr(self.config, "current_test_type", None)
if test_type:
snapshots = self._chart_snapshots.get(test_type, {})
for chart_name, args in snapshots.items():
plot_fn = getattr(self, f"plot_{chart_name}", None)
if plot_fn:
try:
plot_fn(*args)
except Exception:
pass
_cancel_pending_ui_jobs(self)
self._theme_transition = True
try:
toggle_theme()
_refresh_all_theme_widgets(self)
finally:
self._theme_transition = False
def update_config_info_display(self: "PQAutomationApp"):