feat: Theme toggler
This commit is contained in:
82
src/Ursa/Controls/Buttons/ButtonGroup.cs
Normal file
82
src/Ursa/Controls/Buttons/ButtonGroup.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System.Net.Http.Headers;
|
||||
using Avalonia;
|
||||
using Avalonia.Collections;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Data;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Metadata;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class ButtonGroup: ItemsControl
|
||||
{
|
||||
public static readonly StyledProperty<IBinding?> CommandBindingProperty = AvaloniaProperty.Register<ButtonGroup, IBinding?>(
|
||||
nameof(CommandBinding));
|
||||
|
||||
[AssignBinding]
|
||||
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
||||
public IBinding? CommandBinding
|
||||
{
|
||||
get => GetValue(CommandBindingProperty);
|
||||
set => SetValue(CommandBindingProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IBinding?> CommandParameterBindingProperty = AvaloniaProperty.Register<ButtonGroup, IBinding?>(
|
||||
nameof(CommandParameterBinding));
|
||||
|
||||
[AssignBinding]
|
||||
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
||||
public IBinding? CommandParameterBinding
|
||||
{
|
||||
get => GetValue(CommandParameterBindingProperty);
|
||||
set => SetValue(CommandParameterBindingProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IBinding?> ContentBindingProperty = AvaloniaProperty.Register<ButtonGroup, IBinding?>(
|
||||
nameof(ContentBinding));
|
||||
|
||||
[AssignBinding]
|
||||
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
||||
public IBinding? ContentBinding
|
||||
{
|
||||
get => GetValue(ContentBindingProperty);
|
||||
set => SetValue(ContentBindingProperty, value);
|
||||
}
|
||||
|
||||
|
||||
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
|
||||
{
|
||||
recycleKey = null;
|
||||
return item is not Button;
|
||||
}
|
||||
|
||||
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
|
||||
{
|
||||
return new Button();
|
||||
}
|
||||
|
||||
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
|
||||
{
|
||||
base.PrepareContainerForItemOverride(container, item, index);
|
||||
if(container is Button button)
|
||||
{
|
||||
if ( CommandBinding is not null)
|
||||
{
|
||||
button[!Button.CommandProperty] = CommandBinding;
|
||||
}
|
||||
if ( CommandParameterBinding is not null)
|
||||
{
|
||||
button[!Button.CommandParameterProperty] = CommandParameterBinding;
|
||||
}
|
||||
if ( ContentBinding is not null)
|
||||
{
|
||||
button[!Button.ContentProperty] = ContentBinding;
|
||||
}
|
||||
if (ItemTemplate is not null)
|
||||
{
|
||||
button.ContentTemplate = ItemTemplate;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
91
src/Ursa/Controls/Buttons/IconButton.cs
Normal file
91
src/Ursa/Controls/Buttons/IconButton.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Layout;
|
||||
using Ursa.Common;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
[PseudoClasses(PC_Right, PC_Left, PC_Top, PC_Bottom, PC_Empty)]
|
||||
public class IconButton: Button
|
||||
{
|
||||
public const string PC_Right = ":right";
|
||||
public const string PC_Left = ":left";
|
||||
public const string PC_Top = ":top";
|
||||
public const string PC_Bottom = ":bottom";
|
||||
public const string PC_Empty = ":empty";
|
||||
|
||||
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<IconButton, object?>(
|
||||
nameof(Icon));
|
||||
|
||||
public object? Icon
|
||||
{
|
||||
get => GetValue(IconProperty);
|
||||
set => SetValue(IconProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IDataTemplate?> IconTemplateProperty = AvaloniaProperty.Register<IconButton, IDataTemplate?>(
|
||||
nameof(IconTemplate));
|
||||
|
||||
public IDataTemplate? IconTemplate
|
||||
{
|
||||
get => GetValue(IconTemplateProperty);
|
||||
set => SetValue(IconTemplateProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<bool> IsLoadingProperty = AvaloniaProperty.Register<IconButton, bool>(
|
||||
nameof(IsLoading));
|
||||
|
||||
public bool IsLoading
|
||||
{
|
||||
get => GetValue(IsLoadingProperty);
|
||||
set => SetValue(IsLoadingProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<Position> IconPlacementProperty = AvaloniaProperty.Register<IconButton, Position>(
|
||||
nameof(IconPlacement), defaultValue: Position.Left);
|
||||
|
||||
public Position IconPlacement
|
||||
{
|
||||
get => GetValue(IconPlacementProperty);
|
||||
set => SetValue(IconPlacementProperty, value);
|
||||
}
|
||||
|
||||
static IconButton()
|
||||
{
|
||||
IconPlacementProperty.Changed.AddClassHandler<IconButton, Position>((o, e) =>
|
||||
{
|
||||
o.SetPlacement(e.NewValue.Value, o.Icon);
|
||||
});
|
||||
IconProperty.Changed.AddClassHandler<IconButton, object?>((o, e) =>
|
||||
{
|
||||
o.SetPlacement(o.IconPlacement, e.NewValue.Value);
|
||||
});
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
SetPlacement(IconPlacement, Icon);
|
||||
}
|
||||
|
||||
private void SetPlacement(Position placement, object? icon)
|
||||
{
|
||||
if (icon is null)
|
||||
{
|
||||
PseudoClasses.Set(PC_Empty, true);
|
||||
PseudoClasses.Set(PC_Left, false);
|
||||
PseudoClasses.Set(PC_Right, false);
|
||||
PseudoClasses.Set(PC_Top, false);
|
||||
PseudoClasses.Set(PC_Bottom, false);
|
||||
return;
|
||||
}
|
||||
PseudoClasses.Set(PC_Empty, false);
|
||||
PseudoClasses.Set(PC_Left, placement == Position.Left);
|
||||
PseudoClasses.Set(PC_Right, placement == Position.Right);
|
||||
PseudoClasses.Set(PC_Top, placement == Position.Top);
|
||||
PseudoClasses.Set(PC_Bottom, placement == Position.Bottom);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user