fix: restore only the directory at startup and load tabs on request
This commit is contained in:
+18
-9
@@ -82,12 +82,12 @@ def main():
|
||||
finally:
|
||||
window.close()
|
||||
app.processEvents()
|
||||
# Restore an editor and a project preset before showing the native window.
|
||||
# Restore only the directory/preset, even when old settings saved an editor.
|
||||
from mdhelper.workspace_state import WorkspaceStore, TabState
|
||||
from mdhelper.project_presets import ProjectPresetStore, capture_preset
|
||||
from mdhelper.preferences import ReaderPreferences
|
||||
from mdhelper.export_settings import PageSettings
|
||||
from mdhelper.markdown_editor import MarkdownEditorDialog
|
||||
settings.setValue('workspace/restore', True)
|
||||
WorkspaceStore(settings).save(root, [TabState(str(note), editing=True)], 0)
|
||||
ProjectPresetStore(settings).bind(root, capture_preset('Startup preset',
|
||||
ReaderPreferences(document_font_size=22, font_scale=22/16), PageSettings(header='Project header')))
|
||||
@@ -95,19 +95,28 @@ def main():
|
||||
restored.prepare_reader_before_show()
|
||||
events = WindowEvents(restored)
|
||||
try:
|
||||
editor = restored.tabs.currentWidget()
|
||||
assert isinstance(editor, MarkdownEditorDialog)
|
||||
parent = editor.preview.parent()
|
||||
host = restored.tabs.currentWidget()
|
||||
reader = host.reader_view
|
||||
assert restored.current_file is None and host.property('is_welcome')
|
||||
assert getattr(host,'workspace_state',None) is None
|
||||
parent = reader.parent()
|
||||
restored.show()
|
||||
native_id = int(restored.winId())
|
||||
settled = []
|
||||
QTimer.singleShot(1500, lambda: settled.append(True))
|
||||
wait(lambda: bool(settled))
|
||||
wait(lambda: bool(settled) and not restored._startup_scan_pending)
|
||||
assert restored.document_font_size == 22 and restored._saved_page_settings().header == 'Project header'
|
||||
assert int(restored.winId()) == native_id and restored.tabs.currentWidget() is editor
|
||||
assert editor.preview.parent() is parent
|
||||
assert restored.current_file is None and restored.tabs.count()==1
|
||||
assert int(restored.winId()) == native_id and restored.tabs.currentWidget() is host
|
||||
assert reader.parent() is parent and host.stack.currentWidget() is host.welcome_view
|
||||
assert events.events.count(QEvent.Type.Show) == 1 and QEvent.Type.Hide not in events.events, events.events
|
||||
print('Restored editor and project preset passed: one show, no hide, stable native window and preview parent.')
|
||||
startup_events = list(events.events)
|
||||
loaded=[];reader.loadFinished.connect(loaded.append)
|
||||
restored.open_document(note)
|
||||
wait(lambda:bool(loaded) and loaded[-1])
|
||||
assert restored.tabs.currentWidget() is host and host.stack.currentWidget() is reader
|
||||
assert int(restored.winId())==native_id and events.events==startup_events
|
||||
print('Directory-only restore and manual file open passed: no document tabs at startup, one show, no hide, stable reader host.')
|
||||
finally:
|
||||
restored.close()
|
||||
app.processEvents()
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
from pathlib import Path
|
||||
import time
|
||||
import pytest
|
||||
from PySide6.QtCore import QSettings
|
||||
from mdhelper import main_window
|
||||
from mdhelper.markdown_editor import MarkdownEditorDialog
|
||||
@@ -6,6 +8,70 @@ from mdhelper.tab_controller import document_path
|
||||
from mdhelper.workspace_state import TabState, WorkspaceStore
|
||||
|
||||
|
||||
def wait_until(app, condition):
|
||||
deadline=time.monotonic()+10
|
||||
while not condition() and time.monotonic()<deadline:
|
||||
app.processEvents();time.sleep(.01)
|
||||
assert condition()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('legacy_restore', [None, True, False])
|
||||
@pytest.mark.parametrize('deferred', [False, True])
|
||||
def test_startup_restores_directory_without_any_document_tabs(qt_application,tmp_path,monkeypatch,legacy_restore,deferred):
|
||||
settings=QSettings(str(tmp_path/'settings.ini'),QSettings.Format.IniFormat)
|
||||
monkeypatch.setattr(main_window,'QSettings',lambda *_:settings)
|
||||
note=tmp_path/'history.md';note.write_text('# History\n\nSaved document text',encoding='utf8')
|
||||
settings.setValue('project/last_root',str(tmp_path))
|
||||
if legacy_restore is not None:settings.setValue('workspace/restore',legacy_restore)
|
||||
store=WorkspaceStore(settings)
|
||||
store.save(tmp_path,[TabState(str(note),editing=True)],0)
|
||||
window=main_window.MainWindow(tmp_path,defer_startup=deferred)
|
||||
try:
|
||||
window.prepare_reader_before_show()
|
||||
host=window.tabs.currentWidget()
|
||||
reader=host.reader_view
|
||||
window.show()
|
||||
wait_until(qt_application,lambda:not window._startup_scan_pending)
|
||||
assert window.has_project and window.project_root==tmp_path.resolve()
|
||||
assert [entry.path for entry in window.entries]==[note]
|
||||
assert window.current_file is None and window.windowTitle()=='MDHelper'
|
||||
assert window.tabs.count()==1 and window.tabs.currentWidget() is host
|
||||
assert host.property('is_welcome') and document_path(host) is None
|
||||
assert getattr(host,'workspace_state',None) is None
|
||||
assert host.stack.currentWidget() is host.welcome_view
|
||||
assert not window.restore_workspace_action.isCheckable()
|
||||
assert settings.value('project/last_root')==str(tmp_path)
|
||||
# A manual open uses the prepared host and displays content immediately
|
||||
# after loading; it does not need the user to close a placeholder tab.
|
||||
loaded=[];reader.loadFinished.connect(loaded.append)
|
||||
window.open_document(note)
|
||||
wait_until(qt_application,lambda:bool(loaded) and loaded[-1])
|
||||
assert window.tabs.count()==1 and window.tabs.currentWidget() is host
|
||||
assert host.reader_view is reader and host.stack.currentWidget() is reader
|
||||
text=[];reader.page().toPlainText(text.append)
|
||||
wait_until(qt_application,lambda:bool(text))
|
||||
assert 'Saved document text' in text[0]
|
||||
assert not window.restore_workspace_action.isEnabled()
|
||||
finally:window.close()
|
||||
|
||||
|
||||
def test_directory_only_session_preserves_manual_restore_history(qt_application,tmp_path,monkeypatch):
|
||||
settings=QSettings(str(tmp_path/'settings.ini'),QSettings.Format.IniFormat)
|
||||
monkeypatch.setattr(main_window,'QSettings',lambda *_:settings)
|
||||
note=tmp_path/'history.md';note.write_text('# History',encoding='utf8')
|
||||
store=WorkspaceStore(settings);store.save(tmp_path,[TabState(str(note))],0)
|
||||
saved=settings.value(store.key(tmp_path))
|
||||
window=main_window.MainWindow(tmp_path)
|
||||
window.prepare_reader_before_show()
|
||||
window.close()
|
||||
assert settings.value(store.key(tmp_path))==saved
|
||||
window=main_window.MainWindow(tmp_path)
|
||||
try:
|
||||
window.close_project_folder()
|
||||
assert settings.value(store.key(tmp_path))==saved
|
||||
finally:window.close()
|
||||
|
||||
|
||||
def test_workspace_validation_filters_deleted_and_outside_files(tmp_path):
|
||||
settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat)
|
||||
root = tmp_path / 'project'; root.mkdir()
|
||||
@@ -19,20 +85,30 @@ def test_workspace_validation_filters_deleted_and_outside_files(tmp_path):
|
||||
assert store.load(root) == ([], 0)
|
||||
|
||||
|
||||
def test_restore_tab_order_editor_mode_and_lazy_background(qt_application, tmp_path, monkeypatch):
|
||||
@pytest.mark.parametrize('active', [0, 1, 2])
|
||||
def test_restore_tab_order_editor_mode_and_lazy_background(qt_application, tmp_path, monkeypatch, active):
|
||||
settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat)
|
||||
monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings)
|
||||
paths = [tmp_path / f'{n}.md' for n in range(3)]
|
||||
for path in paths: path.write_text('# Example\n\nText', encoding='utf-8')
|
||||
WorkspaceStore(settings).save(tmp_path, [TabState(str(paths[0]), True, 'source', False, 4),
|
||||
TabState(str(paths[1])), TabState(str(paths[2]))], 1)
|
||||
TabState(str(paths[1])), TabState(str(paths[2]))], active)
|
||||
window = main_window.MainWindow(tmp_path, defer_startup=True)
|
||||
try:
|
||||
seed = window.tabs.currentWidget()
|
||||
window.prepare_reader_before_show()
|
||||
assert window.tabs.currentIndex() == 1 and window.tabs.widget(1) is seed
|
||||
assert window.tabs.count()==1 and window.current_file is None
|
||||
window.restore_workspace_action.trigger()
|
||||
assert window.tabs.currentIndex() == active
|
||||
assert [document_path(window.tabs.widget(i)) for i in range(3)] == paths
|
||||
assert window.tabs.widget(0).reader_view is None and window.tabs.widget(2).reader_view is None
|
||||
for index in range(3):
|
||||
if index != active:assert window.tabs.widget(index).reader_view is None
|
||||
if active != 0:
|
||||
assert window.tabs.widget(active) is seed
|
||||
wait_until(qt_application,lambda:seed.stack.currentWidget() is seed.reader_view)
|
||||
text=[];seed.reader_view.page().toPlainText(text.append)
|
||||
wait_until(qt_application,lambda:bool(text))
|
||||
assert 'Example' in text[0]
|
||||
window.tabs.setCurrentIndex(0)
|
||||
editor = window.tabs.currentWidget()
|
||||
assert isinstance(editor, MarkdownEditorDialog)
|
||||
|
||||
Reference in New Issue
Block a user