refactor: Split WorkspaceProtos up into 1 class/file

This commit is contained in:
John Colvin committed 2022-08-08 16:11:25 -04:00
1 parent 829d2cdbf6
commit ac2b2d042a
11 files changed
+180 -110

No files matched your search

@@ -13,7 +13,7 @@ using System.Threading.Tasks;
namespace mbts.ui.settings {
public interface ISavedWindowFactory {
Task<IShellWindow> restore(WindowState window);
Task<IShellWindow> restore(WindowSettings window);
}
[Export(typeof(ISavedWindowFactory))]
@@ -28,7 +28,7 @@ namespace mbts.ui.settings {
[Import]
public ObservableCollection<IShellModule> modules { get; set; }
public Task<IShellWindow> restore(WindowState window) {
public Task<IShellWindow> 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)) {
@@ -14,9 +14,9 @@ using System.Windows.Threading;
namespace mbts.ui.settings {
public interface ISettingsCollector {
// Get the save state for every open window
Task<List<WindowState>> get_window_state();
Task<WindowState> get_window_state(IShellWindow window);
IEnumerable<ModuleState> get_module_state();
Task<List<WindowSettings>> get_window_state();
Task<WindowSettings> get_window_state(IShellWindow window);
IEnumerable<ModuleSettings> get_module_state();
WindowPosition get_launcher_state();
}
@@ -39,9 +39,9 @@ namespace mbts.ui.settings {
IWindowsInterop _interop => _wm;
public async Task<List<WindowState>> get_window_state() {
public async Task<List<WindowSettings>> get_window_state() {
var windows = _wm.list_windows();
var rv = new List<WindowState>(windows.Count);
var rv = new List<WindowSettings>(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<ModuleState> get_module_state() {
public IEnumerable<ModuleSettings> 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<IPersistShellModule>()) {
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<WindowState> get_window_state(IShellWindow window) {
public async Task<WindowSettings> 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)),
@@ -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<ModuleSettings> {
[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);
}
}
@@ -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<WindowPosition> {
[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();
}
}
@@ -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<WindowSettings> {
[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();
}
}
@@ -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;
}
}
@@ -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<WindowState> bookmarks { get; }
IEnumerable<WindowSettings> bookmarks { get; }
}
[Export(typeof(IBookmarks))]
public class BookmarksModel : IBookmarks {
readonly ISettingsCollector _settings_collector;
readonly Dictionary<string, WindowState> _bookmarks = new Dictionary<string, WindowState>();
readonly Dictionary<string, WindowSettings> _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<WindowState> bookmarks => _bookmarks.Values;
public IEnumerable<WindowSettings> bookmarks => _bookmarks.Values;
}
}
@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace mbts.ui.tests {
class TestSavedWindowFactory : ISavedWindowFactory {
public Task<IShellWindow> restore(shell.WindowState window) {
public Task<IShellWindow> restore(shell.WindowSettings window) {
TaskCompletionSource<IShellWindow> tcs = new TaskCompletionSource<IShellWindow>();
tcs.SetResult(null);
return tcs.Task;
@@ -8,14 +8,14 @@ using System.Threading.Tasks;
namespace mbts.ui.tests {
class TestSettingsCollector : ISettingsCollector {
public Task<List<shell.WindowState>> get_window_state() =>
Task.FromResult(new List<shell.WindowState> { new() });
public Task<List<shell.WindowSettings>> get_window_state() =>
Task.FromResult(new List<shell.WindowSettings> { new() });
public Task<shell.WindowState> get_window_state(IShellWindow window) =>
Task.FromResult(new shell.WindowState());
public Task<shell.WindowSettings> get_window_state(IShellWindow window) =>
Task.FromResult(new shell.WindowSettings());
public IEnumerable<shell.ModuleState> get_module_state() {
yield return new shell.ModuleState();
public IEnumerable<shell.ModuleSettings> get_module_state() {
yield return new shell.ModuleSettings();
}
public shell.WindowPosition get_launcher_state() {
@@ -4,6 +4,6 @@ using System;
namespace mbts.ui {
public interface IShellWindowMigrationRule {
bool apply(ref WindowState state);
bool apply(ref WindowSettings state);
}
}
@@ -197,12 +197,12 @@ namespace mbts.ui.shell {
async Task<WorkspaceDefinition> get_workspace_async() {
WorkspaceDefinition workspace = new WorkspaceDefinition();
workspace.windows = new List<WindowState>();
workspace.windows = new List<WindowSettings>();
foreach (var w in await _settings_collector.get_window_state()) {
workspace.windows.Add(w);
await Task.Yield();
}
workspace.modules = new List<ModuleState>();
workspace.modules = new List<ModuleSettings>();
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<WindowState> windows { get; set; }
public List<WindowSettings> windows { get; set; }
// named windows which are not currently open
[ProtoMember(3)]
public List<WindowState> named_windows { get; set; }
public List<WindowSettings> named_windows { get; set; }
[ProtoMember(9, IsRequired = false)]
public List<ModuleState> modules { get; set; }
public List<ModuleSettings> modules { get; set; }
[ProtoMember(8, IsRequired = false)]
public WindowPosition launcher_state { get; set; }