fix: Gather individual win settings on dispatcher thread

In practice, window-states live on their respective dispatcher threads.
Window settings factories shouldn't be expected to be thread-safe.
This commit is contained in:
John Colvin committed 2022-08-05 13:41:37 -04:00
1 parent 41742359e9
commit c46d97646a
5 files changed
+60 -33

No files matched your search

@@ -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();
@@ -8,13 +8,11 @@ using System.Threading.Tasks;
namespace mbts.ui.tests {
class TestSettingsCollector : ISettingsCollector {
public IEnumerable<shell.WindowState> get_window_state() {
yield return new shell.WindowState();
}
public Task<List<shell.WindowState>> get_window_state() =>
Task.FromResult(new List<shell.WindowState> { new() });
public shell.WindowState get_window_state(IShellWindow window) {
return new shell.WindowState();
}
public Task<shell.WindowState> get_window_state(IShellWindow window) =>
Task.FromResult(new shell.WindowState());
public IEnumerable<shell.ModuleState> get_module_state() {
yield return new shell.ModuleState();
@@ -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<WindowState> get_window_state();
WindowState get_window_state(IShellWindow window);
Task<List<WindowState>> get_window_state();
Task<WindowState> get_window_state(IShellWindow window);
IEnumerable<ModuleState> get_module_state();
WindowPosition get_launcher_state();
}
@@ -32,18 +34,24 @@ namespace mbts.ui.settings {
[Import]
public ObservableCollection<IShellModule> modules { get; set; }
[Import]
public IShellThreadFactory thread_factory { get; set; }
IWindowsInterop _interop => _wm;
public IEnumerable<WindowState> get_window_state() {
foreach (var window in _wm.list_windows()) {
public async Task<List<WindowState>> get_window_state() {
var windows = _wm.list_windows();
var rv = new List<WindowState>(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<ModuleState> 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<WindowState> 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));
}
@@ -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<Action> _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();
}
}
}
@@ -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<WorkspaceViewModel> Workspaces { get; private set; }
public event Action<string> WorkspaceLoaded;
WorkspaceDefinition get_workspace() {
async Task<WorkspaceDefinition> 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<WorkspaceDefinition> get_workspace_async() {
WorkspaceDefinition workspace = new WorkspaceDefinition();
workspace.windows = new List<WindowState>();
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();
}
}