Skip to content

Commit

Permalink
Adding i18n support to SettingsSwitchItem
Browse files Browse the repository at this point in the history
  • Loading branch information
YexuanXiao authored and timschneeb committed Jun 20, 2024
1 parent efca533 commit bcb18c1
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions GalaxyBudsClient/Interface/Controls/SettingsSwitchItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using GalaxyBudsClient.Generated.I18N;
using GalaxyBudsClient.Utils.Interface;

namespace GalaxyBudsClient.Interface.Controls;

Expand All @@ -13,38 +15,47 @@ public SettingsSwitchItem()
{
_toggle = new ToggleSwitch();
_toggle.IsCheckedChanged += (_, _) => IsChecked = _toggle.IsChecked;

Click += OnClick;
IsClickEnabled = true;
Footer = _toggle;

Loc.LanguageUpdated += OnLanguageUpdated;
OnLanguageUpdated();
}

private readonly ToggleSwitch _toggle;
public static readonly RoutedEvent<RoutedEventArgs> IsCheckedChangedEvent =

public static readonly RoutedEvent<RoutedEventArgs> IsCheckedChangedEvent =
RoutedEvent.Register<SettingsSwitchItem, RoutedEventArgs>(nameof(IsCheckedChanged), RoutingStrategies.Bubble);

public static readonly StyledProperty<bool?> IsCheckedProperty =
public static readonly StyledProperty<bool?> IsCheckedProperty =
ToggleButton.IsCheckedProperty.AddOwner<SettingsSwitchItem>();

public event EventHandler<RoutedEventArgs>? IsCheckedChanged
{
add => AddHandler(IsCheckedChangedEvent, value);
remove => RemoveHandler(IsCheckedChangedEvent, value);
}

private void OnLanguageUpdated()
{
_toggle.OnContent = Strings.On;
_toggle.OffContent = Strings.Off;
}

public bool? IsChecked
{
get => GetValue(IsCheckedProperty);
set => SetValue(IsCheckedProperty, value);
}

private void OnClick(object? sender, RoutedEventArgs e)
{
IsChecked = !IsChecked;
RaiseEvent(new RoutedEventArgs(IsCheckedChangedEvent));
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
if (change.Property == IsCheckedProperty)
Expand Down

0 comments on commit bcb18c1

Please sign in to comment.