diff --git a/src/client/spark/Modules/Bookmarks/BookmarksModel.cs b/src/client/spark/Modules/Bookmarks/BookmarksModel.cs index a4992e412..2ee83fe05 100644 --- a/src/client/spark/Modules/Bookmarks/BookmarksModel.cs +++ b/src/client/spark/Modules/Bookmarks/BookmarksModel.cs @@ -2,11 +2,12 @@ using System.Collections.Generic; using System.Composition; +using System.Threading.Tasks; namespace mbts.ui.shell { public interface IBookmarks { - void save_bookmark(IShellWindow window); + Task save_bookmark(IShellWindow window); void save_bookmark(WindowState window); void remove(WindowState window); void clear(); @@ -22,15 +23,16 @@ namespace mbts.ui.shell { public BookmarksModel(ISettingsCollector settings) { _settings_collector = settings; } - public void save_bookmark(IShellWindow window) { - var state = _settings_collector.get_window_state(window); + + public async Task save_bookmark(IShellWindow window) { + var state = await _settings_collector.get_window_state(window); if (state != null) { _bookmarks[state.title] = state; } } - public void save_bookmark(WindowState window) { + + public void save_bookmark(WindowState window) => _bookmarks[window.title] = window; - } public void clear() { _bookmarks.Clear(); diff --git a/src/client/spark/Tests/ViewTests/TestSettingsCollector.cs b/src/client/spark/Tests/ViewTests/TestSettingsCollector.cs index 12b265de5..8afa5deaf 100644 --- a/src/client/spark/Tests/ViewTests/TestSettingsCollector.cs +++ b/src/client/spark/Tests/ViewTests/TestSettingsCollector.cs @@ -8,13 +8,11 @@ using System.Threading.Tasks; namespace mbts.ui.tests { class TestSettingsCollector : ISettingsCollector { - public IEnumerable get_window_state() { - yield return new shell.WindowState(); - } + public Task> get_window_state() => + Task.FromResult(new List { new() }); - public shell.WindowState get_window_state(IShellWindow window) { - return new shell.WindowState(); - } + public Task get_window_state(IShellWindow window) => + Task.FromResult(new shell.WindowState()); public IEnumerable get_module_state() { yield return new shell.ModuleState(); diff --git a/src/client/spark/core/Settings/ISettingsCollector.cs b/src/client/spark/core/Settings/ISettingsCollector.cs index bea670d37..5a8e8955f 100644 --- a/src/client/spark/core/Settings/ISettingsCollector.cs +++ b/src/client/spark/core/Settings/ISettingsCollector.cs @@ -8,12 +8,14 @@ using System.Collections.ObjectModel; using System.Composition; using System.IO; using System.Linq; +using System.Threading.Tasks; +using System.Windows.Threading; namespace mbts.ui.settings { public interface ISettingsCollector { // Get the save state for every open window - IEnumerable get_window_state(); - WindowState get_window_state(IShellWindow window); + Task> get_window_state(); + Task get_window_state(IShellWindow window); IEnumerable get_module_state(); WindowPosition get_launcher_state(); } @@ -32,18 +34,24 @@ namespace mbts.ui.settings { [Import] public ObservableCollection modules { get; set; } + [Import] + public IShellThreadFactory thread_factory { get; set; } + IWindowsInterop _interop => _wm; - public IEnumerable get_window_state() { - foreach (var window in _wm.list_windows()) { + public async Task> get_window_state() { + var windows = _wm.list_windows(); + var rv = new List(windows.Count); + foreach (var window in windows) { // Check that the window is still open since we might be iterating asyncronously if (_wm.contains(window)) { - var state = get_window_state(window); + var state = await get_window_state(window); if (state != null) { - yield return state; + rv.Add(state); } } } + return rv; } public IEnumerable get_module_state() { @@ -62,20 +70,39 @@ namespace mbts.ui.settings { return _launcher.get_position(); } - public WindowState get_window_state(IShellWindow window) { + public async Task get_window_state(IShellWindow window) { try { if (window == null || window.Owner == null) return null; - MemoryStream out_stream = new MemoryStream(); - window.GetState(out_stream); + Dispatcher dispatcher = null; + if (window is ISlowWindow) { + var tf = thread_factory; + if (tf is not null) { + var thread_id = (window as ISharedWindow)?.thread_id; + dispatcher = tf.get_dispatcher(thread_id); + if (dispatcher is null) { + Log.Gen.error( + $"Unable to get window state for '{window.Title}.' " + + $"Didn't find dispatcher for thread {thread_id}."); + return null; + } + } + } var pos = _interop.GetPosition(window); var state = new WindowState() { owner = window.Owner.key(), - app_data = out_stream.ToArray(), + app_data = dispatcher is null ? + (await dispatcher.InvokeAsync(serialize)) : serialize(), position = pos, title = string.IsNullOrEmpty(pos.custom_title) ? window.Title : pos.custom_title }; return state; + + byte[] serialize() { + using var ms = new MemoryStream(); + window.GetState(ms); + return ms.ToArray(); + } } catch (Exception e) { Log.Ui.error("Error saving window state for type {0}.\n {1}".fmt(window.GetType(), e.Message)); } diff --git a/src/client/spark/view/Shell/FloatingWindow.xaml.cs b/src/client/spark/view/Shell/FloatingWindow.xaml.cs index b2c2be337..8e89e64e1 100644 --- a/src/client/spark/view/Shell/FloatingWindow.xaml.cs +++ b/src/client/spark/view/Shell/FloatingWindow.xaml.cs @@ -174,7 +174,7 @@ namespace mbts.ui.shell { return await await vm.Dispatcher.check_invoke_async(mcs.MeasureContentSize); } - Action _create_undo_action(ShellWindowVM document) { + async Task _create_undo_action(ShellWindowVM document) { if (document is null) { throw new ArgumentNullException(nameof(document)); } @@ -182,7 +182,7 @@ namespace mbts.ui.shell { return null; } - var state = _window_settings.get_window_state(document.Model); + var state = await _window_settings.get_window_state(document.Model); if (state is null) { return null; } @@ -199,15 +199,15 @@ namespace mbts.ui.shell { }; } - void _tab_closing(object sender, ShellWindowVM document) { + async void _tab_closing(object sender, ShellWindowVM document) { if (sender != this) { - var undo = _create_undo_action(document); + var undo = await _create_undo_action(document); if (undo is not null) { _recently_closed_windows.Push(undo); } } if (document.HasCustomTitle) { - _bookmark.save_bookmark(document.Model); + _bookmark.save_bookmark(document.Model).ignore(); } } } diff --git a/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs b/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs index 5cee3d04d..0c0d4cb5d 100644 --- a/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs +++ b/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs @@ -138,7 +138,7 @@ namespace mbts.ui.shell { async void _launcher_closing(CancelEventArgs obj) { _closing = true; - var workspace = get_workspace(); + var workspace = await get_workspace(); var task = _private.set(WORKSPACE_NAMESPACE, ACTIVE_WORKSPACE_NAME, workspace); await _wm.clear(); task.Wait(TimeSpan.FromSeconds(90)); @@ -186,9 +186,9 @@ namespace mbts.ui.shell { public ObservableCollection Workspaces { get; private set; } public event Action WorkspaceLoaded; - WorkspaceDefinition get_workspace() { + async Task get_workspace() { WorkspaceDefinition workspace = new WorkspaceDefinition(); - workspace.windows = _settings_collector.get_window_state().ToList(); + workspace.windows = await _settings_collector.get_window_state(); workspace.modules = _settings_collector.get_module_state().ToList(); workspace.launcher_state = _settings_collector.get_launcher_state(); workspace.named_windows = _bookmarks.bookmarks.ToList(); @@ -198,7 +198,7 @@ namespace mbts.ui.shell { async Task get_workspace_async() { WorkspaceDefinition workspace = new WorkspaceDefinition(); workspace.windows = new List(); - foreach (var w in _settings_collector.get_window_state()) { + foreach (var w in await _settings_collector.get_window_state()) { workspace.windows.Add(w); await Task.Yield(); } @@ -212,8 +212,8 @@ namespace mbts.ui.shell { return workspace; } - public void save_workspace(string name, bool persist = true, bool ispublic = false) { - WorkspaceDefinition workspace = get_workspace(); + public async void save_workspace(string name, bool persist = true, bool ispublic = false) { + WorkspaceDefinition workspace = await get_workspace(); workspace.is_shared = ispublic; workspace.last_save_lldatetime = LLDateTime.now().value; if (ispublic) { name += SHARED_MONIKER; } @@ -226,7 +226,7 @@ namespace mbts.ui.shell { Workspaces.Add(new WorkspaceViewModel(this, name, workspace)); if (persist) { - (ispublic ? _public : _private).set(WORKSPACE_NAMESPACE, name, workspace); + (ispublic ? _public : _private).set(WORKSPACE_NAMESPACE, name, workspace).ignore(); } }