Skip to content
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

Implement enhanced object and list item editors #1301

Merged
merged 6 commits into from
Mar 16, 2025
Merged
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
866 changes: 436 additions & 430 deletions src/Beutl.Language/Strings.Designer.cs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/Beutl.Language/Strings.ja.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1391,4 +1391,7 @@
<data name="EditDrawable" xml:space="preserve">
<value>描画オブジェクトを編集</value>
</data>
<data name="Item" xml:space="preserve">
<value>アイテム</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/Beutl.Language/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1386,4 +1386,7 @@
<data name="EditDrawable" xml:space="preserve">
<value>Edit drawable</value>
</data>
<data name="Item" xml:space="preserve">
<value>Item</value>
</data>
</root>
20 changes: 14 additions & 6 deletions src/Beutl/Services/PropertyEditorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,23 @@ public static (IPropertyAdapter[] Properties, PropertyEditorExtension Extension)
return Activator.CreateInstance(type, s) as BaseEditorViewModel;
}

private static Control? CreateNavigationButton(IPropertyAdapter s)
private static Control? CreateCoreObjectEditor(IPropertyAdapter s)
{
Type controlType = typeof(NavigateButton<>);
Type controlType = typeof(CoreObjectEditor<>);
controlType = controlType.MakeGenericType(s.PropertyType);
return Activator.CreateInstance(controlType) as Control;
}

private static BaseEditorViewModel? CreateNavigationButtonViewModel(IPropertyAdapter s)
private static IListItemEditor? CreateCoreObjectListItemEditor(IPropertyAdapter s)
{
Type viewModelType = typeof(NavigationButtonViewModel<>);
Type controlType = typeof(CoreObjectListItemEditor<>);
controlType = controlType.MakeGenericType(s.PropertyType);
return Activator.CreateInstance(controlType) as IListItemEditor;
}

private static BaseEditorViewModel? CreateCoreObjectEditorViewModel(IPropertyAdapter s)
{
Type viewModelType = typeof(CoreObjectEditorViewModel<>);
viewModelType = viewModelType.MakeGenericType(s.PropertyType);
return Activator.CreateInstance(viewModelType, s) as BaseEditorViewModel;
}
Expand Down Expand Up @@ -136,7 +143,8 @@ private record struct ListItemEditor(Func<IPropertyAdapter, IListItemEditor?> Cr
{ typeof(PathSegment), new(_ => new PathOperationListItemEditor(), s => new PathOperationEditorViewModel(s.ToTyped<PathSegment?>())) },
{ typeof(PathFigure), new(_ => new PathFigureListItemEditor(), s => new PathFigureEditorViewModel(s.ToTyped<PathFigure>())) },
{ typeof(ISoundEffect), new(_ => new SoundEffectListItemEditor(), s => new SoundEffectEditorViewModel(s.ToTyped<ISoundEffect?>())) },
{ typeof(ITransform), new(_ => new TransformListItemEditor(), s => new TransformEditorViewModel(s.ToTyped<ITransform?>())) }
{ typeof(ITransform), new(_ => new TransformListItemEditor(), s => new TransformEditorViewModel(s.ToTyped<ITransform?>())) },
{ typeof(ICoreObject), new(CreateCoreObjectListItemEditor, CreateCoreObjectEditorViewModel) }
};

private static readonly Dictionary<int, Editor> s_editorsOverride =
Expand Down Expand Up @@ -202,7 +210,7 @@ private record struct ListItemEditor(Func<IPropertyAdapter, IListItemEditor?> Cr
new(typeof(GradientStops), new(_ => new GradientStopsEditor(), s => new GradientStopsEditorViewModel(s.ToTyped<GradientStops>()))),
new(typeof(DisplacementMapTransform), new(_ => new DisplacementMapTransformEditor(), s => new DisplacementMapTransformEditorViewModel(s.ToTyped<DisplacementMapTransform?>()))),
new(typeof(IList), new(CreateListEditor, CreateListEditorViewModel)),
new(typeof(ICoreObject), new(CreateNavigationButton, CreateNavigationButtonViewModel)),
new(typeof(ICoreObject), new(CreateCoreObjectEditor, CreateCoreObjectEditorViewModel)),
new(typeof(IParsable<>), new(CreateParsableEditor, CreateParsableEditorViewModel)),
}.ToFrozenDictionary();

Expand Down
132 changes: 132 additions & 0 deletions src/Beutl/ViewModels/Editors/CoreObjectEditorViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System.Text.Json.Nodes;
using Reactive.Bindings;

namespace Beutl.ViewModels.Editors;

public interface ICoreObjectEditorViewModel
{
string Header { get; }

ReadOnlyReactivePropertySlim<PropertiesEditorViewModel?> Properties { get; }

ReactivePropertySlim<bool> IsExpanded { get; }

ReadOnlyReactivePropertySlim<bool> CanEdit { get; }

ReadOnlyReactivePropertySlim<bool> IsNull { get; }

ReadOnlyReactivePropertySlim<bool> IsNotSetAndCanWrite { get; }

bool CanWrite { get; }
}

public sealed class CoreObjectEditorViewModel<T> : BaseEditorViewModel<T>, ICoreObjectEditorViewModel
where T : ICoreObject
{
public CoreObjectEditorViewModel(IPropertyAdapter<T> property)
: base(property)
{
CanWrite = !property.IsReadOnly;

IsNull = property.GetObservable()
.Select(x => x == null)
.ToReadOnlyReactivePropertySlim()
.DisposeWith(Disposables);

IsNotSetAndCanWrite = IsNull.Select(x => x && CanWrite)
.ToReadOnlyReactivePropertySlim()
.DisposeWith(Disposables);

Value = property.GetObservable()
.ToReadOnlyReactivePropertySlim()
.DisposeWith(Disposables);

Properties = Value
.Select(x => x != null ? new PropertiesEditorViewModel(x, m => m.Browsable) : null)
.Do(AcceptProperties)
.ToReadOnlyReactivePropertySlim()
.DisposeWith(Disposables);
}

public ReadOnlyReactivePropertySlim<T?> Value { get; }

public ReadOnlyReactivePropertySlim<PropertiesEditorViewModel?> Properties { get; }

public ReactivePropertySlim<bool> IsExpanded { get; } = new();

public ReadOnlyReactivePropertySlim<bool> IsNull { get; }

public ReadOnlyReactivePropertySlim<bool> IsNotSetAndCanWrite { get; }

public bool CanWrite { get; }

private void AcceptProperties(PropertiesEditorViewModel? obj)
{
if (obj != null)
{
var visitor = new Visitor(this);
foreach (IPropertyEditorContext item in obj.Properties)
{
item.Accept(visitor);
}
}
}

public override void Accept(IPropertyEditorContextVisitor visitor)
{
base.Accept(visitor);
if (visitor is IServiceProvider)
{
AcceptProperties(Properties.Value);
}
}

public override void ReadFromJson(JsonObject json)
{
base.ReadFromJson(json);
try
{
if (json.TryGetPropertyValue(nameof(IsExpanded), out JsonNode? isExpandedNode)
&& isExpandedNode is JsonValue isExpanded)
{
IsExpanded.Value = (bool)isExpanded;
}

Properties.Value?.ReadFromJson(json);
}
catch
{
}
}

public override void WriteToJson(JsonObject json)
{
base.WriteToJson(json);
try
{
json[nameof(IsExpanded)] = IsExpanded.Value;
Properties.Value?.WriteToJson(json);
}
catch
{
}
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Properties.Value?.Dispose();
}

private sealed record Visitor(CoreObjectEditorViewModel<T> Obj) : IServiceProvider, IPropertyEditorContextVisitor
{
public object? GetService(Type serviceType)
{
return Obj.GetService(serviceType);
}

public void Visit(IPropertyEditorContext context)
{
}
}
}
58 changes: 58 additions & 0 deletions src/Beutl/Views/Editors/CoreObjectEditor.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<UserControl x:Class="Beutl.Views.Editors.CoreObjectEditor"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:icons="using:FluentIcons.FluentAvalonia"
xmlns:lang="using:Beutl.Language"
xmlns:local="using:Beutl.Views.Editors"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="using:FluentAvalonia.UI.Controls"
xmlns:vm="using:Beutl.ViewModels.Editors"
d:DesignWidth="300"
x:CompileBindings="True"
x:DataType="vm:ICoreObjectEditorViewModel"
mc:Ignorable="d">
<Grid RowDefinitions="Auto,Auto">
<ToggleButton x:Name="expandToggle"
Margin="8,4"
IsChecked="{Binding IsExpanded.Value}"
Theme="{DynamicResource PropertyEditorMiniExpanderToggleButton}">
<ToggleButton.ContextFlyout>
<ui:FAMenuFlyout>
<ui:MenuFlyoutItem Click="NewClick"
FontWeight="Normal"
IconSource="New"
Text="{x:Static lang:Strings.Change}" />
<ui:MenuFlyoutItem Click="SetNullClick"
FontWeight="Normal"
Text="Null" />
<ui:MenuFlyoutItem Click="Navigate_Click"
FontWeight="Normal"
Text="Open in tab" />
</ui:FAMenuFlyout>
</ToggleButton.ContextFlyout>
<ToggleButton.Tag>
<Button Padding="0"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Classes="size-24x24"
Click="NewClick"
Theme="{StaticResource TransparentButton}">
<icons:SymbolIcon Symbol="New" />
</Button>
</ToggleButton.Tag>
<TextBlock FontWeight="DemiBold" Text="{Binding Header}" />
</ToggleButton>

<Panel x:Name="content"
Grid.Row="1"
MinHeight="32">
<TextBlock IsVisible="{Binding IsNull.Value}" Text="{x:Static lang:Message.Property_is_unset}" />

<local:PropertiesEditor Margin="16,0,0,0"
Padding="0"
DataContext="{Binding Properties.Value}"
IsVisible="{ReflectionBinding #root.DataContext.Properties.Value, Converter={x:Static ObjectConverters.IsNotNull}}" />
</Panel>
</Grid>
</UserControl>
Loading
Loading