Skip to content

Commit

Permalink
Trigger textbox changes in settings on lost focus (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored Apr 18, 2024
1 parent 2582ed3 commit 5a044a4
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 16 deletions.
63 changes: 63 additions & 0 deletions LightBulb/Behaviors/LostFocusUpdateBindingBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;

namespace LightBulb.Behaviors;

public class LostFocusUpdateBindingBehavior : Behavior<TextBox>
{
public static readonly StyledProperty<string?> TextProperty = AvaloniaProperty.Register<
LostFocusUpdateBindingBehavior,
string?
>(nameof(Text), defaultBindingMode: BindingMode.TwoWay);

static LostFocusUpdateBindingBehavior()
{
TextProperty.Changed.Subscribe(args =>
{
if (args.Sender is LostFocusUpdateBindingBehavior behavior)
behavior.OnBindingValueChanged();
});
}

public string? Text
{
get => GetValue(TextProperty);
set => SetValue(TextProperty, value);
}

protected override void OnAttached()
{
if (AssociatedObject is null)
return;

AssociatedObject.LostFocus += OnLostFocus;
base.OnAttached();
}

protected override void OnDetaching()
{
if (AssociatedObject is null)
return;

AssociatedObject.LostFocus -= OnLostFocus;
base.OnDetaching();
}

private void OnLostFocus(object? sender, RoutedEventArgs e)
{
if (AssociatedObject is null)
return;

Text = AssociatedObject.Text;
}

private void OnBindingValueChanged()
{
if (AssociatedObject != null)
AssociatedObject.Text = Text;
}
}
1 change: 1 addition & 0 deletions LightBulb/LightBulb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Avalonia" Version="11.0.10" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.10" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.0.10" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.0.10.6" />
<PackageReference Include="Cogwheel" Version="2.0.4" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.0" PrivateAssets="all" />
Expand Down
43 changes: 31 additions & 12 deletions LightBulb/Views/Components/Settings/GeneralSettingsTabView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
x:Class="LightBulb.Views.Components.Settings.GeneralSettingsTabView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="clr-namespace:LightBulb.Behaviors"
xmlns:converters="clr-namespace:LightBulb.Converters"
xmlns:settings="clr-namespace:LightBulb.ViewModels.Components.Settings">
<Design.DataContext>
Expand All @@ -15,8 +16,11 @@
<TextBox
MinWidth="24"
HorizontalAlignment="Right"
DockPanel.Dock="Right"
Text="{Binding DayTemperature, Converter={x:Static converters:DoubleToStringConverter.Instance}}" />
DockPanel.Dock="Right">
<Interaction.Behaviors>
<behaviors:LostFocusUpdateBindingBehavior Text="{Binding DayTemperature, Converter={x:Static converters:DoubleToStringConverter.Instance}}" />
</Interaction.Behaviors>
</TextBox>
</DockPanel>
<Slider
Margin="0,12,0,0"
Expand All @@ -34,8 +38,11 @@
<TextBox
MinWidth="24"
HorizontalAlignment="Right"
DockPanel.Dock="Right"
Text="{Binding NightTemperature, Converter={x:Static converters:DoubleToStringConverter.Instance}}" />
DockPanel.Dock="Right">
<Interaction.Behaviors>
<behaviors:LostFocusUpdateBindingBehavior Text="{Binding NightTemperature, Converter={x:Static converters:DoubleToStringConverter.Instance}}" />
</Interaction.Behaviors>
</TextBox>
</DockPanel>
<Slider
Margin="0,12,0,0"
Expand Down Expand Up @@ -66,8 +73,11 @@
<TextBox
MinWidth="24"
HorizontalAlignment="Right"
DockPanel.Dock="Right"
Text="{Binding DayBrightness, Converter={x:Static converters:FractionToPercentageStringConverter.Instance}}" />
DockPanel.Dock="Right">
<Interaction.Behaviors>
<behaviors:LostFocusUpdateBindingBehavior Text="{Binding DayBrightness, Converter={x:Static converters:FractionToPercentageStringConverter.Instance}}" />
</Interaction.Behaviors>
</TextBox>
</DockPanel>
<Slider
Margin="0,12,0,0"
Expand Down Expand Up @@ -98,8 +108,11 @@
<TextBox
MinWidth="24"
HorizontalAlignment="Right"
DockPanel.Dock="Right"
Text="{Binding NightBrightness, Converter={x:Static converters:FractionToPercentageStringConverter.Instance}}" />
DockPanel.Dock="Right">
<Interaction.Behaviors>
<behaviors:LostFocusUpdateBindingBehavior Text="{Binding NightBrightness, Converter={x:Static converters:FractionToPercentageStringConverter.Instance}}" />
</Interaction.Behaviors>
</TextBox>
</DockPanel>
<Slider
Margin="0,12,0,0"
Expand All @@ -117,8 +130,11 @@
<TextBox
MinWidth="48"
HorizontalAlignment="Right"
DockPanel.Dock="Right"
Text="{Binding ConfigurationTransitionDuration, Converter={x:Static converters:TimeSpanToDurationStringConverter.Instance}}" />
DockPanel.Dock="Right">
<Interaction.Behaviors>
<behaviors:LostFocusUpdateBindingBehavior Text="{Binding ConfigurationTransitionDuration, Converter={x:Static converters:TimeSpanToDurationStringConverter.Instance}}" />
</Interaction.Behaviors>
</TextBox>
</DockPanel>
<Slider
Margin="0,12,0,0"
Expand All @@ -134,8 +150,11 @@
<TextBox
MinWidth="24"
HorizontalAlignment="Right"
DockPanel.Dock="Right"
Text="{Binding ConfigurationTransitionOffset, Converter={x:Static converters:FractionToPercentageStringConverter.Instance}}" />
DockPanel.Dock="Right">
<Interaction.Behaviors>
<behaviors:LostFocusUpdateBindingBehavior Text="{Binding ConfigurationTransitionOffset, Converter={x:Static converters:FractionToPercentageStringConverter.Instance}}" />
</Interaction.Behaviors>
</TextBox>
</DockPanel>
<Slider
Margin="0,12,0,0"
Expand Down
15 changes: 11 additions & 4 deletions LightBulb/Views/Components/Settings/LocationSettingsTabView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
x:Class="LightBulb.Views.Components.Settings.LocationSettingsTabView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="clr-namespace:LightBulb.Behaviors"
xmlns:converters="clr-namespace:LightBulb.Converters"
xmlns:materialIcons="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
xmlns:settings="clr-namespace:LightBulb.ViewModels.Components.Settings">
Expand Down Expand Up @@ -37,8 +38,11 @@
<TextBox
MinWidth="24"
HorizontalAlignment="Right"
DockPanel.Dock="Right"
Text="{Binding ManualSunrise, Converter={x:Static converters:TimeOnlyToStringConverter.Instance}}" />
DockPanel.Dock="Right">
<Interaction.Behaviors>
<behaviors:LostFocusUpdateBindingBehavior Text="{Binding ManualSunrise, Converter={x:Static converters:TimeOnlyToStringConverter.Instance}}" />
</Interaction.Behaviors>
</TextBox>
</DockPanel>
<Slider
Margin="0,12,0,0"
Expand All @@ -54,8 +58,11 @@
<TextBox
MinWidth="24"
HorizontalAlignment="Right"
DockPanel.Dock="Right"
Text="{Binding ManualSunset, Converter={x:Static converters:TimeOnlyToStringConverter.Instance}}" />
DockPanel.Dock="Right">
<Interaction.Behaviors>
<behaviors:LostFocusUpdateBindingBehavior Text="{Binding ManualSunset, Converter={x:Static converters:TimeOnlyToStringConverter.Instance}}" />
</Interaction.Behaviors>
</TextBox>
</DockPanel>
<Slider
Margin="0,12,0,0"
Expand Down

0 comments on commit 5a044a4

Please sign in to comment.