Skip to content

Commit

Permalink
Implement docking system
Browse files Browse the repository at this point in the history
  • Loading branch information
acidicMercury8 committed Apr 17, 2024
1 parent f447759 commit 5e16238
Show file tree
Hide file tree
Showing 17 changed files with 761 additions and 524 deletions.
1 change: 1 addition & 0 deletions src/ImeSense.ShaderPlayground/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<Application.Styles>
<SimpleTheme />
<StyleInclude Source="avares://Dock.Avalonia/Themes/DockSimpleTheme.axaml" />
<StyleInclude Source="avares://AvaloniaEdit/Themes/Simple/AvaloniaEdit.xaml" />
</Application.Styles>
</Application>
17 changes: 13 additions & 4 deletions src/ImeSense.ShaderPlayground/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,19 @@ public override void Initialize() =>

public override void OnFrameworkInitializationCompleted() {
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
desktop.MainWindow = _serviceProvider
.GetRequiredService<MainWindow>();
desktop.MainWindow.DataContext = _serviceProvider
.GetRequiredService<MainViewModel>();
var mainViewModel = _serviceProvider.GetRequiredService<MainViewModel>();

var mainWindow = new MainWindow {
DataContext = mainViewModel,
};
mainWindow.Closing += (_, _) => {
mainViewModel.CloseLayout();
};

desktop.MainWindow = mainWindow;
desktop.Exit += (_, _) => {
mainViewModel.CloseLayout();
};
}

base.OnFrameworkInitializationCompleted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
<PackageReference Include="Avalonia.Xaml.Interactions" Version="11.0.10.4" />
<PackageReference Include="AvaloniaEdit.TextMate" Version="11.0.6" />
<PackageReference Include="AvaloniaEdit.TextMate.Grammars" Version="0.10.12" />
<PackageReference Include="Dock.Avalonia" Version="11.0.0.7" />
<PackageReference Include="Dock.Model.ReactiveUI" Version="11.0.0.7" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>

Expand Down
124 changes: 124 additions & 0 deletions src/ImeSense.ShaderPlayground/ViewModels/AppFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;

using Dock.Avalonia.Controls;
using Dock.Model.Controls;
using Dock.Model.Core;
using Dock.Model.ReactiveUI;
using Dock.Model.ReactiveUI.Controls;

using ImeSense.ShaderPlayground.ViewModels.Docks;
using ImeSense.ShaderPlayground.ViewModels.Documents;
using ImeSense.ShaderPlayground.ViewModels.Toolbars;

namespace ImeSense.ShaderPlayground.ViewModels;

public class AppFactory : Factory {
private IRootDock? _rootDock;

private IDocumentDock? _shaderDock;

private ITool? _viewportToolbar;
private ITool? _logToolbar;

public override IDocumentDock CreateDocumentDock() {
return new ShaderDocumentDock();
}

public override IRootDock CreateLayout() {
var untitledShaderViewModel = new ShaderViewModel {
Title = "Untitled",
};

var viewportViewModel = new ViewportViewModel {
Id = "Viewport",
Title = "Viewport",
};
var logViewModel = new LogViewModel {
Id = "Log",
Title = "Log",
};

// Tabs
var documentDock = new ShaderDocumentDock() {
Id = "Shaders",
Title = "Shaders",
IsCollapsable = false,
Proportion = double.NaN,
ActiveDockable = untitledShaderViewModel,
VisibleDockables = CreateList<IDockable>(untitledShaderViewModel),
CanCreateDocument = false,
};

var tools = new ProportionalDock {
Proportion = 0.5,
Orientation = Orientation.Vertical,
VisibleDockables = CreateList<IDockable>(
new ToolDock {
ActiveDockable = viewportViewModel,
VisibleDockables = CreateList<IDockable>(viewportViewModel),
Alignment = Alignment.Right,
GripMode = GripMode.Visible,
},
new ProportionalDockSplitter(),
new ToolDock {
ActiveDockable = logViewModel,
VisibleDockables = CreateList<IDockable>(logViewModel),
Alignment = Alignment.Right,
GripMode = GripMode.Visible,
}
),
};

var windowLayout = CreateRootDock();
windowLayout.Title = "Default";
windowLayout.IsCollapsable = false;

// Content
var windowLayoutContent = new ProportionalDock {
Orientation = Orientation.Horizontal,
IsCollapsable = false,
VisibleDockables = CreateList<IDockable>(
documentDock,
new ProportionalDockSplitter(),
tools
),
};
windowLayout.VisibleDockables = CreateList<IDockable>(windowLayoutContent);
windowLayout.ActiveDockable = windowLayoutContent;

// Root
var rootDock = CreateRootDock();
rootDock.IsCollapsable = false;
rootDock.VisibleDockables = CreateList<IDockable>(windowLayout);
rootDock.ActiveDockable = windowLayout;
rootDock.DefaultDockable = windowLayout;

_shaderDock = documentDock;
_rootDock = rootDock;
_viewportToolbar = viewportViewModel;
_logToolbar = logViewModel;

return rootDock;
}

public override void InitLayout(IDockable layout) {
ContextLocator = new Dictionary<string, Func<object?>> {
["Viewport"] = () => layout,
["Log"] = () => layout
};

DockableLocator = new Dictionary<string, Func<IDockable?>> {
["Root"] = () => _rootDock,
["Files"] = () => _shaderDock,
["Viewport"] = () => _viewportToolbar,
["Log"] = () => _logToolbar,
};

HostWindowLocator = new Dictionary<string, Func<IHostWindow?>> {
[nameof(IDockWindow)] = () => new HostWindow(),
};

base.InitLayout(layout);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Dock.Model.ReactiveUI.Controls;

using ImeSense.ShaderPlayground.ViewModels.Documents;

using ReactiveUI;

namespace ImeSense.ShaderPlayground.ViewModels.Docks;

public class ShaderDocumentDock : DocumentDock {
private void CreateNewShader() {
if (!CanCreateDocument) {
return;
}

var shader = new ShaderViewModel {
Title = "Shader",
};
Factory?.AddDockable(this, shader);
Factory?.SetActiveDockable(shader);
Factory?.SetFocusedDockable(this, shader);
}

public ShaderDocumentDock() {
CreateDocument = ReactiveCommand.Create(CreateNewShader);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Dock.Model.ReactiveUI.Controls;

namespace ImeSense.ShaderPlayground.ViewModels.Documents;

public class ShaderViewModel : Document {
}
52 changes: 20 additions & 32 deletions src/ImeSense.ShaderPlayground/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows.Input;
using Dock.Model.Controls;
using Dock.Model.Core;

using ReactiveUI;

Expand All @@ -7,6 +8,14 @@ namespace ImeSense.ShaderPlayground.ViewModels;
public class MainViewModel : ReactiveObject {
private readonly MenuViewModel _menuViewModel;

private readonly IFactory? _factory;
private IRootDock? _layout;

public IRootDock? Layout {
get => _layout;
set => this.RaiseAndSetIfChanged(ref _layout, value);
}

public MainViewModel(MenuViewModel menuViewModel) {
_menuViewModel = menuViewModel;

Expand All @@ -16,27 +25,24 @@ public MainViewModel(MenuViewModel menuViewModel) {

MenuContext = _menuViewModel;

Resolution = "800x600";
Frame = "60";
_factory = new AppFactory();

PlayCommand = ReactiveCommand.Create(() => {
});
StopCommand = ReactiveCommand.Create(() => {
});
SettingsCommand = ReactiveCommand.Create(() => {
});
Layout = _factory?.CreateLayout();
if (Layout is { }) {
_factory?.InitLayout(Layout);
}
}

#if DEBUG
public MainViewModel() {
_menuViewModel = null!;

MenuContext = null!;
PlayCommand = null!;
StopCommand = null!;
SettingsCommand = null!;
}
#endif

public void CloseLayout() {
Layout?.Close.Execute(null);
Layout = null;
}

private int _windowHeight;

Expand Down Expand Up @@ -74,22 +80,4 @@ public ReactiveObject MenuContext {
get => _menuContext;
init => this.RaiseAndSetIfChanged(ref _menuContext, value);
}

private string _resolution = string.Empty;

public string Resolution {
get => _resolution;
set => this.RaiseAndSetIfChanged(ref _resolution, value);
}

private string _frame = string.Empty;

public string Frame {
get => _frame;
set => this.RaiseAndSetIfChanged(ref _frame, value);
}

public ICommand PlayCommand { get; private set; }
public ICommand StopCommand { get; private set; }
public ICommand SettingsCommand { get; private set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Dock.Model.ReactiveUI.Controls;

namespace ImeSense.ShaderPlayground.ViewModels.Toolbars;

public class LogViewModel : Tool {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Windows.Input;

using Dock.Model.ReactiveUI.Controls;

using ReactiveUI;

namespace ImeSense.ShaderPlayground.ViewModels.Toolbars;

public class ViewportViewModel : Tool {
public ViewportViewModel() {
Resolution = "800x600";
Frame = "60";

PlayCommand = ReactiveCommand.Create(() => {
});
StopCommand = ReactiveCommand.Create(() => {
});
SettingsCommand = ReactiveCommand.Create(() => {
});
}

private string _resolution = string.Empty;

public string Resolution {
get => _resolution;
set => this.RaiseAndSetIfChanged(ref _resolution, value);
}

private string _frame = string.Empty;

public string Frame {
get => _frame;
set => this.RaiseAndSetIfChanged(ref _frame, value);
}

public ICommand PlayCommand { get; private set; }
public ICommand StopCommand { get; private set; }
public ICommand SettingsCommand { get; private set; }
}
Loading

0 comments on commit 5e16238

Please sign in to comment.