From 8d8f5e755525916412186587ee04129a1052f001 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Thu, 2 Feb 2023 18:45:56 +0100 Subject: [PATCH] feat: add main window docking --- src/PacketLogger/App.axaml | 3 +- .../Models/Packets/DummyPacketProvider.cs | 79 ++++++++++++ src/PacketLogger/ViewModels/DockFactory.cs | 115 ++++++++++++++++++ .../ViewModels/MainWindowViewModel.cs | 43 +++++++ src/PacketLogger/Views/MainWindow.axaml | 52 ++++++++ src/PacketLogger/Views/MainWindow.axaml.cs | 22 ++++ 6 files changed, 313 insertions(+), 1 deletion(-) create mode 100644 src/PacketLogger/Models/Packets/DummyPacketProvider.cs create mode 100644 src/PacketLogger/ViewModels/DockFactory.cs create mode 100644 src/PacketLogger/ViewModels/MainWindowViewModel.cs create mode 100644 src/PacketLogger/Views/MainWindow.axaml create mode 100644 src/PacketLogger/Views/MainWindow.axaml.cs diff --git a/src/PacketLogger/App.axaml b/src/PacketLogger/App.axaml index 82da4bf..56caa41 100644 --- a/src/PacketLogger/App.axaml +++ b/src/PacketLogger/App.axaml @@ -8,6 +8,7 @@ - + + \ No newline at end of file diff --git a/src/PacketLogger/Models/Packets/DummyPacketProvider.cs b/src/PacketLogger/Models/Packets/DummyPacketProvider.cs new file mode 100644 index 0000000..3606e46 --- /dev/null +++ b/src/PacketLogger/Models/Packets/DummyPacketProvider.cs @@ -0,0 +1,79 @@ +// +// DummyPacketProvider.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Threading.Tasks; +using DynamicData; +using DynamicData.Binding; +using NosSmooth.PacketSerializer.Abstractions.Attributes; +using PacketLogger.Models.Filters; +using ReactiveUI; +using Remora.Results; + +namespace PacketLogger.Models.Packets; + +/// +public class DummyPacketProvider : IPacketProvider, IDisposable +{ + /// + /// Initializes a new instance of the class. + /// + public DummyPacketProvider() + { + var index = 0; + Packets = new SourceList(); + Packets.Add(new PacketInfo(index++, DateTime.Now, PacketSource.Client, "#cl")); + Packets.Add(new PacketInfo(index++, DateTime.Now, PacketSource.Client, "cl")); + for (var i = 0; i < 1000; i++) + { + Packets.Add + (new PacketInfo(index++, DateTime.Now.AddSeconds(-1000 + i), PacketSource.Client, "walk 10 10")); + Packets.Add + (new PacketInfo(index++, DateTime.Now.AddSeconds(-1000 + i), PacketSource.Server, "mv 1 50 52 123 123 89012390812 189023 182309 1823 189023 901283 091823 091823 901823 901283 091283 019283901283 901283 901 2831290 812390128390128213908139012839012839012390128390128938120938 1290 3190 adsadf")); + Packets.Add + (new PacketInfo(index++, DateTime.Now.AddSeconds(-1000 + i), PacketSource.Client, "walk 12 14")); + Packets.Add + (new PacketInfo(index++, DateTime.Now.AddSeconds(-1000 + i), PacketSource.Server, "mv 1 48 43")); + } + } + + /// + public event PropertyChangedEventHandler? PropertyChanged; + + /// + public bool IsOpen => false; + + /// + public SourceList Packets { get; } + + /// + public Task Open() + { + throw new System.NotImplementedException(); + } + + /// + public Task Close() + { + throw new System.NotImplementedException(); + } + + /// + public void Clear() + { + Packets.Clear(); + } + + /// + public void Dispose() + { + Packets.Dispose(); + } +} \ No newline at end of file diff --git a/src/PacketLogger/ViewModels/DockFactory.cs b/src/PacketLogger/ViewModels/DockFactory.cs new file mode 100644 index 0000000..4ba94c5 --- /dev/null +++ b/src/PacketLogger/ViewModels/DockFactory.cs @@ -0,0 +1,115 @@ +// +// DockFactory.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using Dock.Avalonia.Controls; +using Dock.Model.Controls; +using Dock.Model.Core; +using Dock.Model.Mvvm; +using Dock.Model.Mvvm.Controls; +using PacketLogger.Models; +using ReactiveUI; + +namespace PacketLogger.ViewModels; + +/// +/// A factory for root dock. +/// +public class DockFactory : Factory, IDisposable +{ + private NostaleProcesses _processes = new(); + + /// + public override IDocumentDock CreateDocumentDock() + { + var documentDock = new DocumentDock(); + documentDock.CreateDocument = ReactiveCommand.Create + ( + () => + { + if (!documentDock.CanCreateDocument) + { + return; + } + + var index = documentDock.VisibleDockables?.Count + 1; + var document = new PacketLogDocumentViewModel(_processes) + { Id = $"New tab {index}", Title = $"New tab {index}" }; + + AddDockable(documentDock, document); + SetActiveDockable(document); + SetFocusedDockable(documentDock, document); + } + ); + return documentDock; + } + + private readonly object _context; + private IRootDock? _rootDock; + private IDocumentDock? _documentDock; + + /// + /// Initializes a new instance of the class. + /// + /// The context. + public DockFactory(object context) + { + _context = context; + } + + /// + public override IRootDock CreateLayout() + { + var initialTab = new PacketLogDocumentViewModel(_processes) + { Id = $"New tab", Title = $"New tab" }; + var documentDock = CreateDocumentDock(); + documentDock.IsCollapsable = false; + documentDock.ActiveDockable = initialTab; + documentDock.VisibleDockables = CreateList(initialTab); + documentDock.CanCreateDocument = true; + + var rootDock = CreateRootDock(); + + rootDock.IsCollapsable = false; + rootDock.ActiveDockable = documentDock; + rootDock.DefaultDockable = documentDock; + rootDock.VisibleDockables = CreateList(documentDock); + + _documentDock = documentDock; + _rootDock = rootDock; + + return rootDock; + } + + /// + public override void InitLayout(IDockable layout) + { + ContextLocator = new Dictionary> + { + ["Home"] = () => layout + }; + + DockableLocator = new Dictionary> + { + ["Root"] = () => _rootDock, + ["Documents"] = () => _documentDock + }; + + HostWindowLocator = new Dictionary> + { + [nameof(IDockWindow)] = () => new HostWindow() + }; + + base.InitLayout(layout); + } + + /// + public void Dispose() + { + _processes.Dispose(); + } +} \ No newline at end of file diff --git a/src/PacketLogger/ViewModels/MainWindowViewModel.cs b/src/PacketLogger/ViewModels/MainWindowViewModel.cs new file mode 100644 index 0000000..2db0b96 --- /dev/null +++ b/src/PacketLogger/ViewModels/MainWindowViewModel.cs @@ -0,0 +1,43 @@ +// +// MainWindowViewModel.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.ObjectModel; +using System.ComponentModel; +using Dock.Model.Controls; +using Dock.Model.Core; +using PacketLogger.Models; +using PacketLogger.Models.Packets; + +namespace PacketLogger.ViewModels; + +/// +public class MainWindowViewModel : ViewModelBase, INotifyPropertyChanged +{ + private readonly IFactory? _factory; + + /// + /// Initializes a new instance of the class. + /// + public MainWindowViewModel() + { + _factory = new DockFactory(this); + + Layout = _factory?.CreateLayout(); + if (Layout is { }) + { + _factory?.InitLayout(Layout); + if (Layout is { } root) + { + root.Navigate.Execute("Home"); + } + } + } + + /// + /// Gets or sets the layout. + /// + public IRootDock? Layout { get; set; } +} diff --git a/src/PacketLogger/Views/MainWindow.axaml b/src/PacketLogger/Views/MainWindow.axaml new file mode 100644 index 0000000..d4d0817 --- /dev/null +++ b/src/PacketLogger/Views/MainWindow.axaml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/PacketLogger/Views/MainWindow.axaml.cs b/src/PacketLogger/Views/MainWindow.axaml.cs new file mode 100644 index 0000000..ca94145 --- /dev/null +++ b/src/PacketLogger/Views/MainWindow.axaml.cs @@ -0,0 +1,22 @@ +// +// MainWindow.axaml.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Avalonia.Controls; +using PropertyChanged; + +namespace PacketLogger.Views; + +[DoNotNotify] +public partial class MainWindow : Window +{ + /// + /// Initializes a new instance of the class. + /// + public MainWindow() + { + InitializeComponent(); + } +} \ No newline at end of file -- 2.49.0