Skip to content

Refactoring data binding to DI #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions AvaloniaVisualBasic/App.axaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:avaloniaVisualBasic="clr-namespace:AvaloniaVisualBasic"
xmlns:vb="clr-namespace:AvaloniaVisualBasic"
xmlns:common="clr-namespace:Classic.CommonControls;assembly=Classic.CommonControls.Avalonia"
x:Class="AvaloniaVisualBasic.App"
RequestedThemeVariant="{x:Static ClassicTheme.Standard}">

<Application.Resources>
<ResourceDictionary>
<!--Creates a shared resource of type Composition and with key "Composition",
which will be further used as a data context in the views.-->
<vb:Composition x:Key="Composition" />

<ResourceDictionary.MergedDictionaries>
<ResourceInclude Source="Controls/MDI/MDIWindow.axaml" />
<ResourceInclude Source="Controls/MDI/MDIHost.axaml" />
Expand Down Expand Up @@ -34,6 +38,6 @@
</Application.Styles>

<Application.DataTemplates>
<avaloniaVisualBasic:ViewLocator />
<vb:ViewLocator />
</Application.DataTemplates>
</Application>
31 changes: 11 additions & 20 deletions AvaloniaVisualBasic/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Threading.Tasks;
using Avalonia;
Expand All @@ -12,6 +13,7 @@
using AvaloniaVisualBasic.VisualDesigner;
using Classic.Avalonia.Theme;
using Classic.CommonControls.Dialogs;
using CommunityToolkit.Mvvm.ComponentModel;
using R3;

namespace AvaloniaVisualBasic;
Expand All @@ -27,9 +29,6 @@ public override void Initialize()

public override void OnFrameworkInitializationCompleted()
{
var rootViewModel = new DISetup().Root;
Static.RootViewModel = rootViewModel;

if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
// Line below is needed to remove Avalonia data validation.
Expand All @@ -39,19 +38,17 @@ public override void OnFrameworkInitializationCompleted()
if (Static.ForceSingleView)
{
Static.SingleView = true;
Static.MainView = new MainView
{
DataContext = rootViewModel
};

desktop.MainWindow = new ClassicWindow()
Static.MainView = new MainView();
desktop.MainWindow = new ClassicWindow
{
Content = Static.MainView
};

rootViewModel.ObservePropertyChanged(x => x.Title)
.Subscribe(title => desktop.MainWindow.Title = title);

if (desktop.MainWindow.DataContext is MainViewViewModel rootViewModel)
{
rootViewModel.ObservePropertyChanged(x => x.Title)
.Subscribe(title => desktop.MainWindow.Title = title);
}
#if DEBUG
desktop.MainWindow.AttachDevTools();
#endif
Expand All @@ -60,10 +57,7 @@ public override void OnFrameworkInitializationCompleted()
}
else
{
var mainWindow = new MainWindow
{
DataContext = rootViewModel
};
var mainWindow = new MainWindow();
desktop.MainWindow = mainWindow;
Static.SingleView = false;
Static.MainView = mainWindow.MainView;
Expand All @@ -72,10 +66,7 @@ public override void OnFrameworkInitializationCompleted()
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
{
Static.SingleView = true;
singleViewPlatform.MainView = Static.MainView = new MainView
{
DataContext = rootViewModel
};
singleViewPlatform.MainView = Static.MainView = new MainView();
Static.MainView.WindowInitialized();
}

Expand Down
2 changes: 1 addition & 1 deletion AvaloniaVisualBasic/AvaloniaVisualBasic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="Classic.Avalonia.Theme.DataGrid" Version="$(ClassicAvaloniaVersion)"/>
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="$(AvaloniaVersion)"/>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
<PackageReference Include="Pure.DI" Version="2.1.37">
<PackageReference Include="Pure.DI" Version="2.1.70">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
37 changes: 37 additions & 0 deletions AvaloniaVisualBasic/Composition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Pure.DI;
using System.Diagnostics;
using AvaloniaVisualBasic.IDE;
using AvaloniaVisualBasic.Projects;
using AvaloniaVisualBasic.Tools;
using AvaloniaVisualBasic.VisualDesigner;
using static Pure.DI.Lifetime;
using static Pure.DI.RootKinds;
using MdiWindowManager = AvaloniaVisualBasic.IDE.MdiWindowManager;

namespace AvaloniaVisualBasic;

public partial class Composition
{
[Conditional("DI")]
static void Setup() =>
DI.Setup()
.DefaultLifetime(Singleton)
.Bind().To<ToolBoxToolViewModel>()
.Bind().To<PropertiesToolViewModel>()
.Bind().To<ProjectToolViewModel>()
.Bind().To<FormLayoutToolViewModel>()
.Bind().To<ImmediateToolViewModel>()
.Bind().To<LocalsToolViewModel>()
.Bind().To<WatchesToolViewModel>()
.Bind().To<ColorPaletteToolViewModel>()
.Bind().To<MdiWindowManager>()
.Bind().To<WindowManager>()
.Bind().To<ProjectManager>()
.Bind().To<EditorService>()
.Bind().To<MainViewViewModel.DockFactory>()
.Bind().To<EventBus>()
.Bind().To<ProjectRunnerService>()
.Bind().To<ProjectService>()
.Bind().To<FocusedProjectUtil>()
.Root<MainViewViewModel>(nameof(Root), kind: Virtual);
}
5 changes: 3 additions & 2 deletions AvaloniaVisualBasic/Controls/Properties/PropertyBox.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
xmlns:commonControls="clr-namespace:Classic.CommonControls;assembly=Classic.CommonControls.Avalonia"
xmlns:builtinTypes="clr-namespace:AvaloniaVisualBasic.Runtime.BuiltinTypes;assembly=AvaloniaVisualBasic.Runtime"
xmlns:converters="clr-namespace:AvaloniaVisualBasic.Converters"
xmlns:avaloniaVisualBasic="clr-namespace:AvaloniaVisualBasic"
x:ClassModifier="internal">
<Design.PreviewWith>
<StackPanel Orientation="Vertical" Height="200" Width="300">
<controls:PropertyFontBox Font="" FontName="" />
<controls:PropertyFontBox Font="" FontName="" WindowManager="{Binding Source={StaticResource Composition}, DataType=avaloniaVisualBasic:Composition, Path=Root.WindowManager}" />
<controls:PropertyEnumBox PropertyType="{x:Type builtinTypes:BorderStyles}" />
<controls:PropertyColorBox
VerticalAlignment="Top"
Expand Down Expand Up @@ -43,7 +44,7 @@
</Setter>
<Setter Property="FontTemplate">
<DataTemplate>
<controls:PropertyFontBox Font="{Binding $parent[controls:PropertyBox].Object, Mode=TwoWay}" />
<controls:PropertyFontBox Font="{Binding $parent[controls:PropertyBox].Object, Mode=TwoWay}" WindowManager="{Binding Source={StaticResource Composition}, DataType=avaloniaVisualBasic:Composition, Path=Root.WindowManager}" />
</DataTemplate>
</Setter>
<Setter Property="StringListTemplate">
Expand Down
10 changes: 9 additions & 1 deletion AvaloniaVisualBasic/Controls/Properties/PropertyFontBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Interactivity;
using AvaloniaVisualBasic.IDE;
using AvaloniaVisualBasic.Runtime.BuiltinTypes;
using AvaloniaVisualBasic.Utils;
using Classic.CommonControls.Dialogs;
Expand All @@ -14,6 +15,7 @@ public class PropertyFontBox : TemplatedControl
{
public static readonly StyledProperty<VBFont> FontProperty = AvaloniaProperty.Register<PropertyFontBox, VBFont>("Font", defaultBindingMode: BindingMode.TwoWay, defaultValue: VBFont.Default);
public static readonly DirectProperty<PropertyFontBox, string?> FontNameProperty = AvaloniaProperty.RegisterDirect<PropertyFontBox, string?>("FontName", o => o.FontName);
public static readonly StyledProperty<WindowManager> WindowManagerProperty = AvaloniaProperty.Register<PropertyFontBox, WindowManager>("WindowManager");

public string? FontName
{
Expand All @@ -26,6 +28,12 @@ public VBFont Font
set => SetValue(FontProperty, value);
}

public WindowManager WindowManager
{
get => GetValue(WindowManagerProperty);
set => SetValue(WindowManagerProperty, value);
}

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
Expand All @@ -37,7 +45,7 @@ private void OnButtonClick(object? sender, RoutedEventArgs e)
{
async Task OpenFontWindow()
{
var result = await Static.RootViewModel.WindowManager.ShowFontDialog(new FontDialogResult(Font.FontFamily, Font.Style, Font.Weight, Font.Size));
var result = await WindowManager.ShowFontDialog(new FontDialogResult(Font.FontFamily, Font.Style, Font.Weight, Font.Size));
if (result != null)
{
SetCurrentValue(FontProperty, new VBFont(result.Family, (int)result.Size, result.Weight, result.Style));
Expand Down
38 changes: 0 additions & 38 deletions AvaloniaVisualBasic/DISetup.cs

This file was deleted.

10 changes: 10 additions & 0 deletions AvaloniaVisualBasic/DesignTimeComposition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace AvaloniaVisualBasic;

public class DesignTimeComposition: Composition
{
private readonly Lazy<MainViewViewModel> designTimeRoot = new();

public override MainViewViewModel Root => designTimeRoot.Value;
}
Loading