Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
ability to add document extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Mar 11, 2024
1 parent 1b3a2e1 commit 1c0439d
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:OneWare.Essentials.Controls"
xmlns:converters="clr-namespace:OneWare.Essentials.Converters"
xmlns:models="clr-namespace:OneWare.Essentials.Models">
<Design.PreviewWith>
<controls:DocumentUiExtensionCollection />
</Design.PreviewWith>

<Style Selector="controls|DocumentUiExtensionCollection">
<!-- Set Defaults -->
<Setter Property="Template">
<ControlTemplate>
<ItemsControl ItemsSource="{TemplateBinding Extensions}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel HorizontalAlignment="{TemplateBinding HorizontalAlignment}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="models:DocumentUiExtension">
<ContentControl>
<ContentControl.Content>
<MultiBinding Converter="{x:Static converters:SharedConverters.DocumentUiExtensionConverter}">
<Binding Path="$parent[controls:DocumentUiExtensionCollection].File"/>
<Binding Path="."/>
</MultiBinding>
</ContentControl.Content>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ControlTemplate>
</Setter>
</Style>
</Styles>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Avalonia;
using Avalonia.Controls.Primitives;
using Avalonia.Layout;
using OneWare.Essentials.Models;

namespace OneWare.Essentials.Controls;

public class DocumentUiExtensionCollection : TemplatedControl
{
public static readonly StyledProperty<IFile?> FileProperty =
AvaloniaProperty.Register<DocumentUiExtensionCollection, IFile?>(nameof(File));

public static readonly StyledProperty<ObservableCollection<DocumentUiExtension>?> ExtensionsProperty =
AvaloniaProperty.Register<DocumentUiExtensionCollection, ObservableCollection<DocumentUiExtension>?>(nameof(Extensions));

public static readonly StyledProperty<Orientation> OrientationProperty =
AvaloniaProperty.Register<DocumentUiExtensionCollection, Orientation>(nameof(Orientation));

protected override Type StyleKeyOverride => typeof(DocumentUiExtensionCollection);

public IFile? File
{
get => GetValue(FileProperty);
set => SetValue(FileProperty, value);
}

public ObservableCollection<DocumentUiExtension>? Extensions
{
get => GetValue(ExtensionsProperty);
set => SetValue(ExtensionsProperty, value);
}

public Orientation Orientation
{
get => GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
}
1 change: 1 addition & 0 deletions src/OneWare.Essentials/Controls/SharedControls.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
</Styles.Resources>

<StyleInclude Source="avares://OneWare.Essentials/Controls/UiExtensionCollection.axaml" />
<StyleInclude Source="avares://OneWare.Essentials/Controls/DocumentUiExtensionCollection.axaml" />
</Styles>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ItemsControl ItemsSource="{TemplateBinding Extensions}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="{TemplateBinding HorizontalAlignment}"/>
<StackPanel Orientation="{TemplateBinding Orientation}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
Expand Down
24 changes: 9 additions & 15 deletions src/OneWare.Essentials/Controls/UiExtensionCollection.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,30 @@
using System.Collections.Specialized;
using Avalonia;
using Avalonia.Controls.Primitives;
using Avalonia.Layout;
using OneWare.Essentials.Models;

namespace OneWare.Essentials.Controls;

public class UiExtensionCollection : TemplatedControl
{
public static readonly StyledProperty<ObservableCollection<UiExtension>?> ExtensionsProperty =
AvaloniaProperty.Register<UiExtensionCollection, ObservableCollection<UiExtension>?>(nameof(UiExtensionCollection));
AvaloniaProperty.Register<UiExtensionCollection, ObservableCollection<UiExtension>?>(nameof(Extensions));

public static readonly StyledProperty<Orientation> OrientationProperty =
AvaloniaProperty.Register<UiExtensionCollection, Orientation>(nameof(Orientation));

protected override Type StyleKeyOverride => typeof(UiExtensionCollection);

public ObservableCollection<UiExtension>? Extensions
{
get => GetValue(ExtensionsProperty);
set
{
if(Extensions != null)
Extensions.CollectionChanged -= OnExtensionsChanged;

SetValue(ExtensionsProperty, value);

if (value == null)
return;

value.CollectionChanged += OnExtensionsChanged;
}
set => SetValue(ExtensionsProperty, value);
}

private void OnExtensionsChanged(object? sender, NotifyCollectionChangedEventArgs e)
public Orientation Orientation
{

get => GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
}
26 changes: 26 additions & 0 deletions src/OneWare.Essentials/Converters/DocumentUiExtensionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Data.Converters;
using OneWare.Essentials.Models;

namespace OneWare.Essentials.Converters;

public class DocumentUiExtensionConverter : IMultiValueConverter
{
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{
if (values.Count != 2) return null;

var file = values[0] as IFile;
var uiExtension = values[1] as DocumentUiExtension;

if(file == null || uiExtension == null) return null;

if (Activator.CreateInstance(uiExtension.Type) is Control control)
{
control.DataContext = uiExtension.CreateDataContext(file);
return control;
}
return null;
}
}
1 change: 1 addition & 0 deletions src/OneWare.Essentials/Converters/SharedConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static readonly IValueConverter
public static readonly IValueConverter
BoolToScrollBarVisibilityConverter = new BoolToScrollBarVisibilityConverter();
public static readonly IValueConverter UiExtensionToControlConverter = new UiExtensionToControlConverter();
public static readonly IMultiValueConverter DocumentUiExtensionConverter = new DocumentUiExtensionConverter();
public static readonly IValueConverter ComparisonConverter = new ComparisonConverter();
public static readonly IValueConverter EnumToStringConverter = new EnumToStringConverter();
public static readonly IValueConverter FileOpacityConverter = new FileOpacityConverter();
Expand Down
8 changes: 8 additions & 0 deletions src/OneWare.Essentials/Models/DocumentUiExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OneWare.Essentials.Models;

public class DocumentUiExtension(Type type, Func<IFile, object?> createDataContext)
{
public Type Type { get; } = type;

public Func<IFile, object?> CreateDataContext { get; } = createDataContext;
}
7 changes: 7 additions & 0 deletions src/OneWare.Essentials/OneWare.Essentials.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@
<ItemGroup>
<None Include="readme.md" Pack="true" PackagePath=""/>
</ItemGroup>

<ItemGroup>
<Compile Update="Controls\DocumentUiExtensionCollection.axaml.cs">
<DependentUpon>DocumentUiExtensionCollection.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/OneWare.Essentials/Services/IDockService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using Avalonia.Controls;
using Dock.Model.Core;
Expand All @@ -18,6 +19,10 @@ public interface IDockService : INotifyPropertyChanged

public void RegisterDocumentView<T>(params string[] extensions) where T : IExtendedDocument;

public ObservableCollection<DocumentUiExtension> GetDocumentViewExtensions(string key);

public void RegisterDocumentViewExtension<T>(string key, Func<IFile,object?> createDataContext);

public void RegisterLayoutExtension<T>(DockShowLocation location);

public void Show<T>(DockShowLocation location = DockShowLocation.Window) where T : IDockable;
Expand Down

0 comments on commit 1c0439d

Please sign in to comment.