Skip to content

Commit

Permalink
Merge pull request #293 from sirdoombox/main
Browse files Browse the repository at this point in the history
Remove dependency on ReactiveUI
  • Loading branch information
kikipoulet authored Oct 2, 2024
2 parents 46f5ae4 + 974f7b8 commit fdfe1be
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 263 deletions.
29 changes: 11 additions & 18 deletions SukiUI/Controls/Hosts/SukiToastHost.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
Expand Down Expand Up @@ -41,28 +38,30 @@ public ToastLocation Position
set => SetValue(PositionProperty, value);
}

private IDisposable? _subscriptions;

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
_subscriptions = this.GetObservable(PositionProperty)
.Do(OnPositionChanged)
.Select(_ => Unit.Default).ObserveOn(new AvaloniaSynchronizationContext())
.Subscribe();
OnPositionChanged(Position);
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == PositionProperty && change.NewValue is ToastLocation loc)
OnPositionChanged(loc);
}

private void OnPositionChanged(ToastLocation obj)
private void OnPositionChanged(ToastLocation newLoc)
{
HorizontalAlignment = Position switch
HorizontalAlignment = newLoc switch
{
ToastLocation.BottomRight => HorizontalAlignment.Right,
ToastLocation.BottomLeft => HorizontalAlignment.Left,
ToastLocation.TopRight => HorizontalAlignment.Right,
ToastLocation.TopLeft => HorizontalAlignment.Left,
_ => throw new ArgumentOutOfRangeException()
};
VerticalAlignment = Position switch
VerticalAlignment = newLoc switch
{
ToastLocation.BottomRight => VerticalAlignment.Bottom,
ToastLocation.BottomLeft => VerticalAlignment.Bottom,
Expand All @@ -71,12 +70,6 @@ private void OnPositionChanged(ToastLocation obj)
_ => throw new ArgumentOutOfRangeException()
};
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
_subscriptions?.Dispose();
}

private static void OnManagerPropertyChanged(AvaloniaObject sender,
AvaloniaPropertyChangedEventArgs propChanged)
Expand Down
9 changes: 4 additions & 5 deletions SukiUI/Controls/PropertyGrid/PropertyGrid.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using System;
using System.ComponentModel;
using System.Reactive.Linq;

// ReSharper disable once CheckNamespace
namespace SukiUI.Controls
{
public partial class PropertyGrid : UserControl
{
static PropertyGrid()
{
ItemProperty.Changed.Subscribe(OnItemChanged);
ItemProperty.Changed.AddClassHandler<PropertyGrid>((_, args) => OnItemChanged(args));
}

public PropertyGrid()
Expand Down Expand Up @@ -40,8 +39,8 @@ public InstanceViewModel? Instance
get { return GetValue(InstanceProperty); }
set { SetValue(InstanceProperty, value); }
}

private static void OnItemChanged(AvaloniaPropertyChangedEventArgs e)
private static void OnItemChanged( AvaloniaPropertyChangedEventArgs e)
{
if (e.Sender is PropertyGrid propertyGrid)
{
Expand Down
48 changes: 19 additions & 29 deletions SukiUI/Controls/SettingsLayout.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;
using DynamicData;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Layout;

namespace SukiUI.Controls;

Expand All @@ -32,11 +30,6 @@ public SettingsLayout()
InitializeComponent();
}

protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnAttachedToLogicalTree(e);
}

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
Expand All @@ -55,11 +48,10 @@ private void InitializeComponent()
private ObservableCollection<SettingsLayoutItem> _items;

public static readonly DirectProperty<SettingsLayout, ObservableCollection<SettingsLayoutItem>> StepsProperty =
AvaloniaProperty.RegisterDirect<SettingsLayout, ObservableCollection<SettingsLayoutItem>>(nameof(Items), l => l.Items,
(numpicker, v) =>
{
numpicker.Items = v;
}, defaultBindingMode: BindingMode.TwoWay, enableDataValidation: true);
AvaloniaProperty.RegisterDirect<SettingsLayout, ObservableCollection<SettingsLayoutItem>>(nameof(Items),
l => l.Items,
(numpicker, v) => { numpicker.Items = v; }, defaultBindingMode: BindingMode.TwoWay,
enableDataValidation: true);

public ObservableCollection<SettingsLayoutItem> Items
{
Expand Down Expand Up @@ -110,7 +102,7 @@ private void UpdateItems()
var summaryButton = new RadioButton()
{
Content = new TextBlock() { Text = settingsLayoutItem.Header, FontSize = 17 },
Classes = { new string[] { "MenuChip" } }
Classes = { "MenuChip" }
};
summaryButton.Click += async (sender, args) =>
{
Expand All @@ -119,7 +111,7 @@ private void UpdateItems()
var x = border.TranslatePoint(new Point(), stackItems);

if (x.HasValue)
await AnimateScroll(x.Value.Y); // myScroll.Offset = new Vector(0, x.Value.Y);
await AnimateScroll(x.Value.Y); // myScroll.Offset = new Vector(0, x.Value.Y);
};
radios.Add(summaryButton);
stackSummary.Children.Add(summaryButton);
Expand All @@ -139,35 +131,28 @@ private void UpdateItems()
};
}

private Mutex mut = new Mutex();
private Mutex mut = new Mutex();

private double LastDesiredSize = -1;

private async void DockPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{

var stack = this.GetTemplateChildren().First(n => n.Name == "StackSummary");
var desiredSize = e.NewSize.Width > 1100 ? 400 : 0;
if(LastDesiredSize == desiredSize)

if (LastDesiredSize == desiredSize)
return;

LastDesiredSize = desiredSize;

if (stack.Width != desiredSize && (stack.Width == 0 || stack.Width == 400))
if (stack.Width != desiredSize && (stack.Width == 0 || stack.Width == 400))
stack.Animate<double>(WidthProperty, stack.Width, desiredSize, TimeSpan.FromMilliseconds(800));


}

private bool isAnimatingWidth = false;
private bool isAnimatingMargin = false;
private bool isAnimatingScroll = false;





private async Task AnimateScroll(double desiredScroll)
{
isAnimatingScroll = true;
Expand All @@ -189,7 +174,14 @@ private async Task AnimateScroll(double desiredScroll)
},
new KeyFrame()
{
Setters = { new Setter { Property = ScrollViewer.OffsetProperty, Value = new Vector(myscroll.Offset.X, desiredScroll -30) } },
Setters =
{
new Setter
{
Property = ScrollViewer.OffsetProperty,
Value = new Vector(myscroll.Offset.X, desiredScroll - 30)
}
},
KeyTime = TimeSpan.FromMilliseconds(800)
}
}
Expand All @@ -203,6 +195,4 @@ private async Task AnimateScroll(double desiredScroll)

await Task.WhenAll(animationTask, abortTask);
}


}
24 changes: 5 additions & 19 deletions SukiUI/Controls/Stepper.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
Expand Down Expand Up @@ -48,7 +46,6 @@ public IEnumerable? Steps
}

private Grid? _grid;
private IDisposable? _subscriptionDisposables;

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
Expand All @@ -59,19 +56,14 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
}

_grid = grid;
StepsChangedHandler(Steps);
}

protected override void OnLoaded(RoutedEventArgs e)
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
var indexObs = this.GetObservable(IndexProperty)
.Do(_ => StepsChangedHandler(Steps))
.Select(_ => Unit.Default);
_subscriptionDisposables = this.GetObservable(StepsProperty)
.Do(_ => StepsChangedHandler(Steps))
.Select(_ => Unit.Default)
.Merge(indexObs)
.ObserveOn(new AvaloniaSynchronizationContext())
.Subscribe();
base.OnPropertyChanged(change);
if (change.Property == IndexProperty || change.Property == StepsProperty)
StepsChangedHandler(Steps);
}

private void StepsChangedHandler(IEnumerable? newSteps)
Expand Down Expand Up @@ -217,12 +209,6 @@ private void AddStep(object step, int index, Grid grid, int stepCount)
grid.Children.Add(griditem);
}

protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
_subscriptionDisposables?.Dispose();
}

#endregion

#region StepperAlternateStyle
Expand Down
61 changes: 25 additions & 36 deletions SukiUI/Controls/SukiBackground.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Reactive;
using System.Reactive.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Threading;
using SukiUI.Enums;
using SukiUI.Utilities;
using SukiUI.Utilities.Effects;

namespace SukiUI.Controls
Expand Down Expand Up @@ -98,39 +95,36 @@ public bool ForceSoftwareRendering
}

private readonly EffectBackgroundDraw _draw;
private readonly IDisposable _observables;

public SukiBackground()
{
IsHitTestVisible = false;
_draw = new EffectBackgroundDraw(new Rect(0, 0, Bounds.Width, Bounds.Height));
var forceSwRenderingObs = this.GetObservable(ForceSoftwareRenderingProperty)
.Do(enabled => _draw.ForceSoftwareRendering = enabled)
.Select(_ => Unit.Default);
var transEnabledObs = this.GetObservable(TransitionsEnabledProperty)
.Do(enabled => _draw.TransitionsEnabled = enabled)
.Select(_ => Unit.Default)
.Merge(forceSwRenderingObs);
var transTime = this.GetObservable(TransitionTimeProperty)
.Do(time => _draw.TransitionTime = time)
.Select(_ => Unit.Default)
.Merge(transEnabledObs);
var animObs = this.GetObservable(AnimationEnabledProperty)
.Do(enabled => _draw.AnimationEnabled = enabled)
.Select(_ => Unit.Default)
.Merge(transTime);
var bgStyleObs = this.GetObservable(StyleProperty)
.Select(_ => Unit.Default)
.Merge(animObs);
var bgShaderFileObs = this.GetObservable(ShaderFileProperty)
.Select(_ => Unit.Default)
.Merge(bgStyleObs);
var bgShaderCodeObs = this.GetObservable(ShaderCodeProperty)
.Select(_ => Unit.Default)
.Merge(bgShaderFileObs)
.Do(_ => HandleBackgroundStyleChanges())
.ObserveOn(new AvaloniaSynchronizationContext());
_observables = bgShaderCodeObs.Subscribe();
}

protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
_draw.ForceSoftwareRendering = ForceSoftwareRendering;
_draw.TransitionsEnabled = TransitionsEnabled;
_draw.TransitionTime = TransitionTime;
_draw.AnimationEnabled = AnimationEnabled;
HandleBackgroundStyleChanges();
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ForceSoftwareRenderingProperty && change.NewValue is bool forceSoftwareRendering)
_draw.ForceSoftwareRendering = forceSoftwareRendering;
else if(change.Property == TransitionsEnabledProperty && change.NewValue is bool transitionEnabled)
_draw.TransitionsEnabled = transitionEnabled;
else if(change.Property == TransitionTimeProperty && change.NewValue is double transitionTime)
_draw.TransitionTime = transitionTime;
else if(change.Property == AnimationEnabledProperty && change.NewValue is bool animationEnabled)
_draw.AnimationEnabled = animationEnabled;
else if(change.Property == StyleProperty || change.Property == ShaderFileProperty || change.Property == ShaderCodeProperty)
HandleBackgroundStyleChanges();
}

public override void Render(DrawingContext context)
Expand All @@ -149,10 +143,5 @@ private void HandleBackgroundStyleChanges()
else
_draw.Effect = SukiEffect.FromEmbeddedResource(Style.ToString());
}

protected override void OnUnloaded(RoutedEventArgs e)
{
_observables.Dispose();
}
}
}
Loading

0 comments on commit fdfe1be

Please sign in to comment.