From ac2b2d042a0141068f0472940e37093e30ee42a5 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 8 Aug 2022 13:44:19 -0400 Subject: [PATCH] refactor: Split WorkspaceProtos up into 1 class/file --- .../Model/Settings/ISavedWindowFactory.cs | 6 +- .../Model/Settings/ISettingsCollector.cs | 18 ++--- .../spark/Model/Settings/ModuleSettings.cs | 31 ++++++++ .../spark/Model/Settings/WindowPosition.cs | 71 +++++++++++++++++ .../spark/Model/Settings/WindowSettings.cs | 45 +++++++++++ .../spark/Model/Settings/WorkspaceProtos.cs | 77 ------------------- .../spark/Modules/Bookmarks/BookmarksModel.cs | 14 ++-- .../Tests/ViewTests/TestSavedWindowFactory.cs | 2 +- .../Tests/ViewTests/TestSettingsCollector.cs | 12 +-- .../IShellWindowMigrationRule.cs | 2 +- .../view/Shell/Workspaces/WorkspacesModel.cs | 12 +-- 11 files changed, 180 insertions(+), 110 deletions(-) create mode 100644 src/client/spark/Model/Settings/ModuleSettings.cs create mode 100644 src/client/spark/Model/Settings/WindowPosition.cs create mode 100644 src/client/spark/Model/Settings/WindowSettings.cs delete mode 100644 src/client/spark/Model/Settings/WorkspaceProtos.cs diff --git a/src/client/spark/Model/Settings/ISavedWindowFactory.cs b/src/client/spark/Model/Settings/ISavedWindowFactory.cs index fc8573b3e..9fc470015 100644 --- a/src/client/spark/Model/Settings/ISavedWindowFactory.cs +++ b/src/client/spark/Model/Settings/ISavedWindowFactory.cs @@ -13,7 +13,7 @@ using System.Threading.Tasks; namespace mbts.ui.settings { public interface ISavedWindowFactory { - Task restore(WindowState window); + Task restore(WindowSettings window); } [Export(typeof(ISavedWindowFactory))] @@ -28,7 +28,7 @@ namespace mbts.ui.settings { [Import] public ObservableCollection modules { get; set; } - public Task restore(WindowState window) { + public Task restore(WindowSettings window) { Log.Ui.info("Loading window '{0}'".fmt(window.title)); bool changed; int count = 0; @@ -49,7 +49,7 @@ namespace mbts.ui.settings { /// ex: If change the data format twice from A to B to C then we would write 2 IShellWindowMigration instances. /// Starting in state A one of the _compat tests changes to state B. Now we need to re-run /// the migrations because state B will match a different rule and be converted to state C. - private bool apply_compatability_migrations(ref WindowState window) { + private bool apply_compatability_migrations(ref WindowSettings window) { foreach (var test in _compat) { try { if (test.apply(ref window)) { diff --git a/src/client/spark/Model/Settings/ISettingsCollector.cs b/src/client/spark/Model/Settings/ISettingsCollector.cs index 58ea01e41..19437f15e 100644 --- a/src/client/spark/Model/Settings/ISettingsCollector.cs +++ b/src/client/spark/Model/Settings/ISettingsCollector.cs @@ -14,9 +14,9 @@ using System.Windows.Threading; namespace mbts.ui.settings { public interface ISettingsCollector { // Get the save state for every open window - Task> get_window_state(); - Task get_window_state(IShellWindow window); - IEnumerable get_module_state(); + Task> get_window_state(); + Task get_window_state(IShellWindow window); + IEnumerable get_module_state(); WindowPosition get_launcher_state(); } @@ -39,9 +39,9 @@ namespace mbts.ui.settings { IWindowsInterop _interop => _wm; - public async Task> get_window_state() { + public async Task> get_window_state() { var windows = _wm.list_windows(); - var rv = new List(windows.Count); + 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)) { @@ -54,12 +54,12 @@ namespace mbts.ui.settings { return rv; } - public IEnumerable get_module_state() { + public IEnumerable get_module_state() { //Note: I'm assuming that the list of modules is stable and I don't have to protect against it changing here foreach (var module in modules.OfTypeSafe()) { MemoryStream out_stream = new MemoryStream(); module.get_state(out_stream); - yield return new ModuleState() { + yield return new ModuleSettings() { name = module.key(), app_data = out_stream.ToArray(), }; @@ -70,7 +70,7 @@ namespace mbts.ui.settings { return _launcher.get_position(); } - public async Task get_window_state(IShellWindow window) { + public async Task get_window_state(IShellWindow window) { try { if (window == null || window.Owner == null) return null; Dispatcher dispatcher = null; @@ -89,7 +89,7 @@ namespace mbts.ui.settings { } var pos = _interop.GetPosition(window); - var state = new WindowState() { + var state = new WindowSettings() { owner = window.Owner.key(), app_data = dispatcher is null ? serialize() : (await dispatcher.InvokeAsync(serialize)), diff --git a/src/client/spark/Model/Settings/ModuleSettings.cs b/src/client/spark/Model/Settings/ModuleSettings.cs new file mode 100644 index 000000000..cfdbbbe35 --- /dev/null +++ b/src/client/spark/Model/Settings/ModuleSettings.cs @@ -0,0 +1,31 @@ +#nullable enable +namespace mbts.ui.shell { + using ProtoBuf; + + using Spark.Core.Protobuf; + + using System.Collections.Generic; + + [ProtoContract] + public class ModuleSettings : IProtobuf { + [ProtoMember(1)] + public string? name { get; set; } + + [ProtoMember(2)] + public byte[]? app_data { get; set; } + + public bool Equals(ModuleSettings? other) => + other is not null && + name == other.name && + app_data.safe_sequence_equals(other.app_data); + + public override bool Equals(object? obj) => + Equals(obj as ModuleSettings); + + public override int GetHashCode() => + GetType().GetHashCode(); + + public ModuleSettings Clone() => + ProtobufSerializationHelper.clone(this); + } +} diff --git a/src/client/spark/Model/Settings/WindowPosition.cs b/src/client/spark/Model/Settings/WindowPosition.cs new file mode 100644 index 000000000..70e24518d --- /dev/null +++ b/src/client/spark/Model/Settings/WindowPosition.cs @@ -0,0 +1,71 @@ +#nullable enable +namespace mbts.ui.shell { + using ProtoBuf; + + using Spark.Core.Protobuf; + + using System; + using System.ComponentModel; + + [ProtoContract] + public class WindowPosition : IProtobuf { + [ProtoMember(3)] + public int width { get; set; } + + [ProtoMember(4)] + public int height { get; set; } + + [ProtoMember(5)] + public int left { get; set; } + + [ProtoMember(6)] + public int top { get; set; } + + [ProtoMember(7)] + public bool is_maximized { get; set; } + + [ProtoMember(8)] + public string? host_id { get; set; } + + [ProtoMember(9)] + public int tab_index { get; set; } + + [ProtoMember(100)] + public bool horizontal { get; set; } + + [ProtoMember(101), DefaultValue(true)] + public bool show_text { get; set; } = true; + + [ProtoMember(102), DefaultValue(true)] + public bool always_on_top { get; set; } = true; + + [ProtoMember(104)] + public string? custom_title { get; set; } + + public bool Equals(WindowPosition? other) => + other is not null && + width == other.width && + height == other.height && + left == other.left && + top == other.top && + is_maximized == other.is_maximized && + host_id == other.host_id && + tab_index == other.tab_index && + horizontal == other.horizontal && + show_text == other.show_text && + always_on_top == other.always_on_top && + custom_title == other.custom_title; + + public override bool Equals(object? obj) => + Equals(obj as WindowPosition); + + public override int GetHashCode() => + GetType().GetHashCode(); + + public WindowPosition Clone() => + ProtobufSerializationHelper.clone(this); + + object ICloneable.Clone() => + Clone(); + } +} diff --git a/src/client/spark/Model/Settings/WindowSettings.cs b/src/client/spark/Model/Settings/WindowSettings.cs new file mode 100644 index 000000000..8a20d4770 --- /dev/null +++ b/src/client/spark/Model/Settings/WindowSettings.cs @@ -0,0 +1,45 @@ +#nullable enable +namespace mbts.ui.shell { + using bts.utils; + + using ProtoBuf; + + using Spark.Core.Protobuf; + + using System; + using System.Collections.Generic; + + [ProtoContract] + public class WindowSettings : IProtobuf { + [ProtoMember(1)] + public string? owner { get; set; } + + [ProtoMember(2)] + public byte[]? app_data { get; set; } + + [ProtoMember(8)] + public WindowPosition? position { get; set; } + + [ProtoMember(9)] + public string? title { get; set; } + + public bool Equals(WindowSettings? other) => + other is not null && + owner == other.owner && + app_data.safe_sequence_equals(other.app_data) && + position.safe_equals(other.position) && + title == other.title; + + public override bool Equals(object? obj) => + Equals(obj as WindowSettings); + + public override int GetHashCode() => + GetType().GetHashCode(); + + public WindowSettings Clone() => + ProtobufSerializationHelper.clone(this); + + object ICloneable.Clone() => + Clone(); + } +} diff --git a/src/client/spark/Model/Settings/WorkspaceProtos.cs b/src/client/spark/Model/Settings/WorkspaceProtos.cs deleted file mode 100644 index 8abb8018c..000000000 --- a/src/client/spark/Model/Settings/WorkspaceProtos.cs +++ /dev/null @@ -1,77 +0,0 @@ -using ProtoBuf; - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace mbts.ui.shell { - public enum DockState { - FLOATING = 1, - TOP = 2, - BOTTOM = 3, - LEFT = 4, - RIGHT = 5, - } - - [ProtoContract] - public class WindowPosition { - [ProtoMember(3)] - public int width; - [ProtoMember(4)] - public int height; - [ProtoMember(5)] - public int left; - [ProtoMember(6)] - public int top; - [ProtoMember(7)] - public bool is_maximized; - [ProtoMember(8)] - public string host_id; - [ProtoMember(9)] - public int tab_index; - - [ProtoMember(100), DefaultValue(false)] - public bool horizontal = false; - [ProtoMember(101), DefaultValue(true)] - public bool show_text = true; - [ProtoMember(102), DefaultValue(true)] - public bool always_on_top = true; - //[ProtoMember(103)] //used - - [ProtoMember(104)] - public string custom_title; - } - - [ProtoContract] - public class WindowState { - [ProtoMember(1)] - public string owner; - [ProtoMember(2)] - public byte[] app_data; - - //optional bool is_open = 3 [default = true]; - //optional bool is_favorite = 4; - //optional string title = 5; - - //optional string id = 6; - // optional bool is_loaded = 7 [default = false]; - [ProtoMember(8)] - public WindowPosition position; - - [ProtoMember(9)] - public string title; // for debugging - } - - [ProtoContract] - public class ModuleState { - [ProtoMember(1)] - public string name; - [ProtoMember(2)] - public byte[] app_data; - } - - -} diff --git a/src/client/spark/Modules/Bookmarks/BookmarksModel.cs b/src/client/spark/Modules/Bookmarks/BookmarksModel.cs index 2ee83fe05..f5dc024d6 100644 --- a/src/client/spark/Modules/Bookmarks/BookmarksModel.cs +++ b/src/client/spark/Modules/Bookmarks/BookmarksModel.cs @@ -8,16 +8,16 @@ namespace mbts.ui.shell { public interface IBookmarks { Task save_bookmark(IShellWindow window); - void save_bookmark(WindowState window); - void remove(WindowState window); + void save_bookmark(WindowSettings window); + void remove(WindowSettings window); void clear(); - IEnumerable bookmarks { get; } + IEnumerable bookmarks { get; } } [Export(typeof(IBookmarks))] public class BookmarksModel : IBookmarks { readonly ISettingsCollector _settings_collector; - readonly Dictionary _bookmarks = new Dictionary(); + readonly Dictionary _bookmarks = new(); [ImportingConstructor] public BookmarksModel(ISettingsCollector settings) { @@ -31,17 +31,17 @@ namespace mbts.ui.shell { } } - public void save_bookmark(WindowState window) => + public void save_bookmark(WindowSettings window) => _bookmarks[window.title] = window; public void clear() { _bookmarks.Clear(); } - public void remove(WindowState window) { + public void remove(WindowSettings window) { _bookmarks.Remove(window.title); } - public IEnumerable bookmarks => _bookmarks.Values; + public IEnumerable bookmarks => _bookmarks.Values; } } diff --git a/src/client/spark/Tests/ViewTests/TestSavedWindowFactory.cs b/src/client/spark/Tests/ViewTests/TestSavedWindowFactory.cs index f87658d4e..33052460a 100644 --- a/src/client/spark/Tests/ViewTests/TestSavedWindowFactory.cs +++ b/src/client/spark/Tests/ViewTests/TestSavedWindowFactory.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace mbts.ui.tests { class TestSavedWindowFactory : ISavedWindowFactory { - public Task restore(shell.WindowState window) { + public Task restore(shell.WindowSettings window) { TaskCompletionSource tcs = new TaskCompletionSource(); tcs.SetResult(null); return tcs.Task; diff --git a/src/client/spark/Tests/ViewTests/TestSettingsCollector.cs b/src/client/spark/Tests/ViewTests/TestSettingsCollector.cs index 8afa5deaf..61e637f9b 100644 --- a/src/client/spark/Tests/ViewTests/TestSettingsCollector.cs +++ b/src/client/spark/Tests/ViewTests/TestSettingsCollector.cs @@ -8,14 +8,14 @@ using System.Threading.Tasks; namespace mbts.ui.tests { class TestSettingsCollector : ISettingsCollector { - public Task> get_window_state() => - Task.FromResult(new List { new() }); + public Task> get_window_state() => + Task.FromResult(new List { new() }); - public Task get_window_state(IShellWindow window) => - Task.FromResult(new shell.WindowState()); + public Task get_window_state(IShellWindow window) => + Task.FromResult(new shell.WindowSettings()); - public IEnumerable get_module_state() { - yield return new shell.ModuleState(); + public IEnumerable get_module_state() { + yield return new shell.ModuleSettings(); } public shell.WindowPosition get_launcher_state() { diff --git a/src/client/spark/core/ShellInterface/IShellWindowMigrationRule.cs b/src/client/spark/core/ShellInterface/IShellWindowMigrationRule.cs index 580b9fc07..c5f0a868d 100644 --- a/src/client/spark/core/ShellInterface/IShellWindowMigrationRule.cs +++ b/src/client/spark/core/ShellInterface/IShellWindowMigrationRule.cs @@ -4,6 +4,6 @@ using System; namespace mbts.ui { public interface IShellWindowMigrationRule { - bool apply(ref WindowState state); + bool apply(ref WindowSettings state); } } diff --git a/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs b/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs index 8375ae265..a1cb2c19a 100644 --- a/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs +++ b/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs @@ -197,12 +197,12 @@ namespace mbts.ui.shell { async Task get_workspace_async() { WorkspaceDefinition workspace = new WorkspaceDefinition(); - workspace.windows = new List(); + workspace.windows = new List(); foreach (var w in await _settings_collector.get_window_state()) { workspace.windows.Add(w); await Task.Yield(); } - workspace.modules = new List(); + workspace.modules = new List(); foreach (var m in _settings_collector.get_module_state()) { workspace.modules.Add(m); await Task.Yield(); @@ -260,7 +260,7 @@ namespace mbts.ui.shell { } } if (def.windows != null) { - async Task restore(WindowState window) { + async Task restore(WindowSettings window) { var w = await _window_restore.restore(window); if (!_closing) { await _wm.add(w); @@ -303,14 +303,14 @@ namespace mbts.ui.shell { [ProtoContract(Name = "WorkspaceDefinition")] public class WorkspaceDefinition { [ProtoMember(2, IsRequired = false)] - public List windows { get; set; } + public List windows { get; set; } // named windows which are not currently open [ProtoMember(3)] - public List named_windows { get; set; } + public List named_windows { get; set; } [ProtoMember(9, IsRequired = false)] - public List modules { get; set; } + public List modules { get; set; } [ProtoMember(8, IsRequired = false)] public WindowPosition launcher_state { get; set; }