From 9b5ec390c8c4cd6b1f2e9031225019c5d6450072 Mon Sep 17 00:00:00 2001
From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Date: Fri, 23 Aug 2024 03:02:10 +0300
Subject: [PATCH] Use native `UpdateSourceTrigger=LostFocus` instead of the
behavior workaround
---
.../LostFocusUpdateBindingBehavior.cs | 65 -------------------
LightBulb/LightBulb.csproj | 1 -
.../Settings/GeneralSettingsTabView.axaml | 43 ++++--------
.../Settings/LocationSettingsTabView.axaml | 15 ++---
4 files changed, 16 insertions(+), 108 deletions(-)
delete mode 100644 LightBulb/Behaviors/LostFocusUpdateBindingBehavior.cs
diff --git a/LightBulb/Behaviors/LostFocusUpdateBindingBehavior.cs b/LightBulb/Behaviors/LostFocusUpdateBindingBehavior.cs
deleted file mode 100644
index dc42250..0000000
--- a/LightBulb/Behaviors/LostFocusUpdateBindingBehavior.cs
+++ /dev/null
@@ -1,65 +0,0 @@
-using System;
-using System.Diagnostics.CodeAnalysis;
-using Avalonia;
-using Avalonia.Controls;
-using Avalonia.Data;
-using Avalonia.Interactivity;
-using Avalonia.Xaml.Interactivity;
-
-namespace LightBulb.Behaviors;
-
-public class LostFocusUpdateBindingBehavior : Behavior
-{
- public static readonly StyledProperty 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);
- }
-
- [RequiresUnreferencedCode("Inherited")]
- 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 is not null)
- AssociatedObject.Text = Text;
- }
-}
diff --git a/LightBulb/LightBulb.csproj b/LightBulb/LightBulb.csproj
index 7ace79c..26a8cbb 100644
--- a/LightBulb/LightBulb.csproj
+++ b/LightBulb/LightBulb.csproj
@@ -21,7 +21,6 @@
-
diff --git a/LightBulb/Views/Components/Settings/GeneralSettingsTabView.axaml b/LightBulb/Views/Components/Settings/GeneralSettingsTabView.axaml
index 58b6bde..ac7dc7e 100644
--- a/LightBulb/Views/Components/Settings/GeneralSettingsTabView.axaml
+++ b/LightBulb/Views/Components/Settings/GeneralSettingsTabView.axaml
@@ -2,7 +2,6 @@
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"
x:DataType="settings:GeneralSettingsTabViewModel">
@@ -14,11 +13,8 @@
MinWidth="24"
HorizontalAlignment="Right"
DockPanel.Dock="Right"
- Theme="{DynamicResource CompactTextBox}">
-
-
-
-
+ Text="{Binding DayTemperature, Converter={x:Static converters:DoubleToStringConverter.Instance}, UpdateSourceTrigger=LostFocus}"
+ Theme="{DynamicResource CompactTextBox}" />
-
-
-
-
+ Text="{Binding NightTemperature, Converter={x:Static converters:DoubleToStringConverter.Instance}, UpdateSourceTrigger=LostFocus}"
+ Theme="{DynamicResource CompactTextBox}" />
-
-
-
-
+ Text="{Binding DayBrightness, Converter={x:Static converters:FractionToPercentageStringConverter.Instance}, UpdateSourceTrigger=LostFocus}"
+ Theme="{DynamicResource CompactTextBox}" />
-
-
-
-
+ Text="{Binding NightBrightness, Converter={x:Static converters:FractionToPercentageStringConverter.Instance}, UpdateSourceTrigger=LostFocus}"
+ Theme="{DynamicResource CompactTextBox}" />
-
-
-
-
+ Text="{Binding ConfigurationTransitionDuration, Converter={x:Static converters:TimeSpanToDurationStringConverter.Instance}, UpdateSourceTrigger=LostFocus}"
+ Theme="{DynamicResource CompactTextBox}" />
-
-
-
-
+ Text="{Binding ConfigurationTransitionOffset, Converter={x:Static converters:FractionToPercentageStringConverter.Instance}, UpdateSourceTrigger=LostFocus}"
+ Theme="{DynamicResource CompactTextBox}" />
-
-
-
-
+ Text="{Binding ManualSunrise, Converter={x:Static converters:TimeOnlyToStringConverter.Instance}, UpdateSourceTrigger=LostFocus}"
+ Theme="{DynamicResource CompactTextBox}" />
-
-
-
-
+ Text="{Binding ManualSunset, Converter={x:Static converters:TimeOnlyToStringConverter.Instance}, UpdateSourceTrigger=LostFocus}"
+ Theme="{DynamicResource CompactTextBox}" />