Enhance reader styles exports and settings

This commit is contained in:
shenw
2026-06-14 09:51:16 +08:00
parent 586d384da3
commit 63a07b00b8
22 changed files with 3572 additions and 202 deletions
+205 -12
View File
@@ -3,28 +3,41 @@ from __future__ import annotations
import os
import sys
import traceback
import zipfile
from pathlib import Path
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
os.environ.setdefault("QTWEBENGINE_CHROMIUM_FLAGS", "--disable-gpu --no-sandbox")
os.environ.setdefault(
"QTWEBENGINE_CHROMIUM_FLAGS",
"--disable-gpu --disable-gpu-compositing --disable-software-rasterizer --no-sandbox --log-level=3",
)
os.environ.setdefault(
"QT_LOGGING_RULES",
"qt.multimedia.ffmpeg.*=false;qt.multimedia.*.debug=false;qt.multimedia.*.info=false",
)
os.environ.setdefault("QTWEBENGINE_DISABLE_SANDBOX", "1")
PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))
from PySide6.QtCore import QEventLoop, QTimer, QUrl, Qt
from PySide6.QtCore import QEventLoop, QSettings, QTimer, QUrl, Qt
from PySide6.QtGui import QTextCursor
from PySide6.QtWebEngineCore import QWebEnginePage
from PySide6.QtWidgets import QApplication, QMessageBox, QToolBar
from PySide6.QtWidgets import QApplication, QMessageBox, QPushButton, QToolBar
from mdhelper.main_window import MainWindow, ReaderPage, ReaderView, VideoPlayerWidget
from mdhelper.markdown_editor import MarkdownEditorDialog
from mdhelper.settings_dialog import AppPreferences, SettingsDialog
from mdhelper.style_dialog import DocumentStyleEditorDialog
def main() -> int:
project_root = PROJECT_ROOT
app = QApplication.instance() or QApplication([sys.argv[0]])
settings = QSettings("MDHelper", "MDHelper")
settings.setValue("reader/document_file_groups", '["markdown"]')
settings.setValue("reader/wheel_scroll_speed", 1.0)
window = MainWindow(project_root=project_root, initial_path=project_root / "sample.md")
window.show()
@@ -48,6 +61,21 @@ def main() -> int:
checks.append("project_markdown_files")
if any(entry.path == (project_root / "requirements.txt").resolve() for entry in empty_window.entries):
checks.append("project_hidden_unsupported_files")
original_file_groups = empty_window.document_file_groups
empty_window.document_file_groups = ("markdown", "python", "cpp")
empty_window._refresh_document_entries(force=True)
code_suffixes = {".py", ".c", ".cc", ".cxx", ".cpp", ".h", ".hh", ".hxx", ".hpp"}
if not any(entry.path.suffix.lower() in code_suffixes for entry in empty_window.entries):
checks.append("project_optional_code_files")
empty_window.open_document(project_root / "sample.md", add_history=False)
empty_window.document_file_groups = ("python",)
empty_window._refresh_document_entries(force=True)
if any(entry.path.suffix.lower() in {".md", ".markdown"} for entry in empty_window.entries):
checks.append("project_file_type_filter_entries")
if (project_root / "sample.md").resolve() in empty_window.path_to_item:
checks.append("project_file_type_filter_tree")
empty_window.document_file_groups = original_file_groups
empty_window._refresh_document_entries(force=True)
tree_root = empty_window.tree.topLevelItem(0)
if tree_root is None or not tree_root.isExpanded():
checks.append("file_tree_root_expanded")
@@ -124,6 +152,13 @@ def main() -> int:
checks.append("current_file")
if window.tabs.count() < 1:
checks.append("tabs")
if not window.tabs.isMovable():
checks.append("tabs_movable")
tab_bar = window.tabs.tabBar()
if tab_bar.expanding():
checks.append("tabs_non_expanding")
if hasattr(tab_bar, "changeCurrentOnDrag") and tab_bar.changeCurrentOnDrag():
checks.append("tabs_no_drag_switch")
if window._welcome_tab_index() >= 0:
checks.append("welcome_closed_for_document")
if window.windowIcon().isNull():
@@ -132,26 +167,72 @@ def main() -> int:
checks.append("app_icon")
if not isinstance(window.tabs.currentWidget(), ReaderView):
checks.append("reader_view")
if abs(window.wheel_scroll_speed - 1.0) > 0.01:
checks.append("wheel_scroll_speed_default")
window.wheel_scroll_speed = 1.7
window._apply_wheel_scroll_speed_to_tabs()
current_reader = window.tabs.currentWidget()
if isinstance(current_reader, ReaderView) and abs(float(current_reader.property("wheel_scroll_speed") or 0) - 1.7) > 0.01:
checks.append("wheel_scroll_speed_tab_property")
if window.status_label.objectName() != "statusPathLabel":
checks.append("status_label_theme_name")
if hasattr(window, "find_box"):
checks.append("toolbar_find_box_visible")
if not all(
hasattr(window, name)
for name in ("panel_open_folder_button", "find_current_button", "find_previous_button", "find_next_button")
for name in (
"panel_open_folder_button",
"panel_refresh_button",
"find_current_button",
"find_previous_button",
"find_next_button",
)
):
checks.append("panel_find_buttons")
elif window.panel_open_folder_button.icon().isNull():
checks.append("panel_open_folder_icon")
elif [window.find_current_button.text(), window.find_previous_button.text(), window.find_next_button.text()] != [
"搜索",
"上一个",
"下一个",
]:
checks.append("panel_find_button_order")
elif window.panel_open_folder_button.icon().isNull() or window.panel_refresh_button.icon().isNull():
checks.append("panel_icon_buttons")
elif any(
button.text()
for button in (
window.find_current_button,
window.find_previous_button,
window.find_next_button,
)
):
checks.append("panel_find_button_text")
elif any(
button.icon().isNull()
for button in (
window.find_current_button,
window.find_previous_button,
window.find_next_button,
)
):
checks.append("panel_find_button_icons")
expected_search_height = max(22, round(window.fontMetrics().height() * 1.5))
if abs(window.search_box.height() - expected_search_height) > 4:
checks.append("panel_search_box_height")
settings_dialog = SettingsDialog(
AppPreferences(
theme=window.theme,
theme_color=window.theme_color,
theme_background=window.theme_background,
font_scale=window.font_scale,
wheel_scroll_speed=1.4,
auto_refresh=window.auto_refresh_enabled,
auto_refresh_mode=window.auto_refresh_mode,
auto_refresh_delay_ms=window.auto_refresh_delay_ms,
non_preview_action=window.non_preview_action,
document_file_groups=window.document_file_groups,
),
window,
)
if abs(settings_dialog.wheel_scroll_speed_spin.value() - 1.4) > 0.01:
checks.append("settings_wheel_scroll_speed")
settings_dialog.wheel_scroll_speed_spin.setValue(2.2)
if abs(settings_dialog.preferences().wheel_scroll_speed - 2.2) > 0.01:
checks.append("settings_wheel_scroll_speed_value")
settings_dialog.close()
if window.findChildren(QToolBar):
checks.append("main_toolbar_removed")
if hasattr(window, "toolbar_font_label") or hasattr(window, "toolbar_font_size_label"):
@@ -160,6 +241,10 @@ def main() -> int:
checks.append("export_pdf_action")
if not window.export_html_action.isEnabled():
checks.append("export_html_action")
if not window.export_docx_action.isEnabled():
checks.append("export_docx_action")
if not window.export_mhtml_action.isEnabled():
checks.append("export_mhtml_action")
if hasattr(window, "theme_action"):
checks.append("theme_toolbar_action")
if hasattr(window, "home_action"):
@@ -174,6 +259,80 @@ def main() -> int:
if not hasattr(window, "theme_menu"):
checks.append("theme_menu")
menu_titles = [action.text() for action in window.menuBar().actions()]
if "样式" not in menu_titles or not hasattr(window, "style_menu"):
checks.append("style_menu")
style_values = {
str(action.data())
for action in window.document_style_group.actions()
if action.data() is not None
}
if not {"default", "compact", "serif", "wide", "focus"}.issubset(style_values):
checks.append("style_builtin_options")
style_dialog = DocumentStyleEditorDialog(
{
"name": "图形样式",
"rules": {
"paragraph": {
"font_size": 18,
"line_height_value": 1.6,
"line_height_unit": "multiple",
"first_line_indent_chars": 2,
"space_after_value": 6,
"space_after_unit": "pt",
}
},
"extra_css": ".reader strong { color: #0057b8; }",
},
parent=window,
)
style_dialog.apply_theme(window.theme_color)
style_payload = style_dialog.style_payload()
if (
"font-size: 18px" not in str(style_payload.get("css"))
or "text-indent: 2em" not in str(style_payload.get("css"))
or "margin-bottom: 6pt" not in str(style_payload.get("css"))
):
checks.append("style_editor_payload")
if hasattr(style_dialog, "use_chinese_font_checkbox") or hasattr(style_dialog, "use_english_font_checkbox"):
checks.append("style_font_checkboxes_removed")
if style_dialog.chinese_font_combo.maximumWidth() > 315 or style_dialog.english_font_combo.maximumWidth() > 315:
checks.append("style_font_combo_width")
if style_dialog.findChild(QPushButton, "styleResetButton").maximumWidth() > 128:
checks.append("style_reset_button_width")
if "QTabWidget::pane" not in style_dialog.styleSheet() or "border: 1px solid" not in style_dialog.styleSheet():
checks.append("style_dialog_theme")
style_dialog.close()
previous_style = window.document_style_id
window._set_document_style("compact")
if window.document_style_id != "compact" or "max-width: 1120px" not in window._document_style_css():
checks.append("style_apply_builtin")
if window.delete_custom_style_action.isEnabled():
checks.append("style_delete_builtin_enabled")
style_test_path = project_root / "style_smoke.mdhelper-style"
try:
window._write_document_style_file(style_test_path, "compact")
imported_style_id = window._import_document_style_file(style_test_path)
if imported_style_id not in window.custom_document_styles:
checks.append("style_import_file")
window.document_style_id = imported_style_id
window._build_style_menu()
if not window.delete_custom_style_action.isEnabled():
checks.append("style_delete_custom_disabled")
exported_payload = window._style_payload_for_id(imported_style_id)
if exported_payload.get("name") != "紧凑参考" or "max-width: 1120px" not in str(exported_payload.get("css")):
checks.append("style_payload")
original_question = QMessageBox.question
QMessageBox.question = lambda *args, **kwargs: QMessageBox.StandardButton.Yes
try:
window.delete_current_custom_document_style()
finally:
QMessageBox.question = original_question
if imported_style_id in window.custom_document_styles or window.document_style_id != "default":
checks.append("style_delete_custom")
finally:
style_test_path.unlink(missing_ok=True)
window.document_style_id = previous_style
window._build_style_menu()
if "关于" not in menu_titles or menu_titles.index("关于") <= menu_titles.index("主题"):
checks.append("about_menu_position")
if window.about_action not in window.about_menu.actions():
@@ -371,6 +530,35 @@ def main() -> int:
pdf_test_path.unlink(missing_ok=True)
except OSError:
pass
docx_test_path = project_root / "__gui_smoke_export.docx"
try:
if window.export_docx_to_path(docx_test_path) is None:
checks.append("export_docx_result")
elif not docx_test_path.exists():
checks.append("export_docx_write")
else:
with zipfile.ZipFile(docx_test_path) as package:
if "word/document.xml" not in package.namelist():
checks.append("export_docx_package")
finally:
try:
docx_test_path.unlink(missing_ok=True)
except OSError:
pass
mhtml_test_path = project_root / "__gui_smoke_export.mhtml"
try:
if window.export_mhtml_to_path(mhtml_test_path) is None:
checks.append("export_mhtml_result")
mhtml_loop = QEventLoop()
QTimer.singleShot(3000, mhtml_loop.quit)
mhtml_loop.exec()
if not mhtml_test_path.exists() or mhtml_test_path.stat().st_size <= 0:
checks.append("export_mhtml_write")
finally:
try:
mhtml_test_path.unlink(missing_ok=True)
except OSError:
pass
window._remember_external_application(project_root / "media" / "demo.mp4", str(project_root / "fake_player.exe"))
if ".mp4" not in window.external_applications:
checks.append("external_app_remember")
@@ -634,6 +822,8 @@ def main() -> int:
checks.append("config_theme_background")
if snapshot.get("reader", {}).get("document_tree_label_mode") not in {"title", "filename"}:
checks.append("config_tree_label_mode")
if snapshot.get("reader", {}).get("wheel_scroll_speed") is None:
checks.append("config_wheel_scroll_speed")
window._apply_configuration(
{
"reader": {
@@ -641,6 +831,7 @@ def main() -> int:
"theme_background": "peony",
"math_engine": "katex",
"font_scale": 1.2,
"wheel_scroll_speed": 1.8,
"document_font_family": "Microsoft YaHei UI",
"document_font_size": 18,
"document_tree_label_mode": "filename",
@@ -659,6 +850,8 @@ def main() -> int:
checks.append("config_apply_theme_background")
if window.document_font_size != 18 or window.document_font_family != "Microsoft YaHei UI":
checks.append("config_apply_document_font")
if abs(window.wheel_scroll_speed - 1.8) > 0.01:
checks.append("config_apply_wheel_scroll_speed")
if window.document_tree_label_mode != "filename":
checks.append("config_apply_tree_label_mode")
if window.ui_font_family != "Microsoft YaHei UI" or window.ui_font_point_size != 10: