fix: fix various timepicker sync issue.

This commit is contained in:
rabbitism
2024-09-04 02:18:39 +08:00
parent 3742fcf27e
commit 561554f2bf
8 changed files with 116 additions and 23 deletions

View File

@@ -5,12 +5,16 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa" xmlns:u="https://irihi.tech/ursa"
xmlns:viewModels="clr-namespace:Ursa.Demo.ViewModels"
d:DesignHeight="450" d:DesignHeight="450"
d:DesignWidth="800" d:DesignWidth="800"
x:DataType="viewModels:TimePickerDemoViewModel"
mc:Ignorable="d"> mc:Ignorable="d">
<StackPanel HorizontalAlignment="Left"> <StackPanel HorizontalAlignment="Left">
<ToggleSwitch Name="needConfirm" Content="Need Confirm" /> <ToggleSwitch Name="needConfirm" Content="Need Confirm" />
<!--
<TextBlock Text="{Binding #picker.SelectedTime}" /> <TextBlock Text="{Binding #picker.SelectedTime}" />
-->
<TextBox <TextBox
Name="displayFormat" Name="displayFormat"
Width="300" Width="300"
@@ -21,6 +25,7 @@
Width="300" Width="300"
InnerLeftContent="Panel Format" InnerLeftContent="Panel Format"
Text="tt HH mm ss" /> Text="tt HH mm ss" />
<!--
<u:TimePicker <u:TimePicker
Name="picker" Name="picker"
Width="200" Width="200"
@@ -42,5 +47,19 @@
Width="300" Width="300"
DisplayFormat="{Binding #displayFormat.Text}" DisplayFormat="{Binding #displayFormat.Text}"
PanelFormat="{Binding #panelFormat.Text}" /> PanelFormat="{Binding #panelFormat.Text}" />
-->
<TextBlock Text="Binding"/>
<u:TimePicker
Width="300"
HorizontalAlignment="Left"
SelectedTime="{Binding Time}"
DisplayFormat="{Binding #displayFormat.Text}"
PanelFormat="{Binding #panelFormat.Text}" />
<u:TimeRangePicker
Width="300"
StartTime="{Binding StartTime}"
EndTime="{Binding EndTime}"
DisplayFormat="{Binding #displayFormat.Text}"
PanelFormat="{Binding #panelFormat.Text}" />
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -1,8 +1,18 @@
using CommunityToolkit.Mvvm.ComponentModel; using System;
using CommunityToolkit.Mvvm.ComponentModel;
namespace Ursa.Demo.ViewModels; namespace Ursa.Demo.ViewModels;
public class TimePickerDemoViewModel: ObservableObject public partial class TimePickerDemoViewModel: ObservableObject
{ {
[ObservableProperty] private TimeSpan? _time;
[ObservableProperty] private TimeSpan? _startTime;
[ObservableProperty] private TimeSpan? _endTime;
public TimePickerDemoViewModel()
{
Time = new TimeSpan(12, 20, 0);
StartTime = new TimeSpan(8, 21, 0);
EndTime = new TimeSpan(18, 22, 0);
}
} }

View File

@@ -193,8 +193,7 @@
<u:TimePickerPresenter <u:TimePickerPresenter
Name="{x:Static u:TimePicker.PART_Presenter}" Name="{x:Static u:TimePicker.PART_Presenter}"
NeedsConfirmation="{TemplateBinding NeedConfirmation}" NeedsConfirmation="{TemplateBinding NeedConfirmation}"
PanelFormat="{TemplateBinding PanelFormat}" PanelFormat="{TemplateBinding PanelFormat}"/>
Time="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedTime, Mode=OneWayToSource}" />
</DockPanel> </DockPanel>
</Border> </Border>
</Popup> </Popup>

View File

@@ -141,7 +141,7 @@
Grid.Column="0" Grid.Column="0"
NeedsConfirmation="{TemplateBinding NeedConfirmation}" NeedsConfirmation="{TemplateBinding NeedConfirmation}"
PanelFormat="{TemplateBinding PanelFormat}" PanelFormat="{TemplateBinding PanelFormat}"
Time="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=StartTime, Mode=OneWayToSource}" /> />
<Rectangle <Rectangle
Grid.Row="2" Grid.Row="2"
Grid.Column="1" Grid.Column="1"
@@ -155,7 +155,7 @@
Grid.Column="2" Grid.Column="2"
NeedsConfirmation="{TemplateBinding NeedConfirmation}" NeedsConfirmation="{TemplateBinding NeedConfirmation}"
PanelFormat="{TemplateBinding PanelFormat}" PanelFormat="{TemplateBinding PanelFormat}"
Time="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=EndTime, Mode=OneWayToSource}" /> />
</Grid> </Grid>
</DockPanel> </DockPanel>
</Border> </Border>

View File

@@ -0,0 +1,16 @@
using Avalonia.Interactivity;
namespace Ursa.Controls;
public class TimeChangedEventArgs:RoutedEventArgs
{
public TimeSpan? OldTime { get; }
public TimeSpan? NewTime { get; }
public TimeChangedEventArgs(TimeSpan? oldTime, TimeSpan? newTime)
{
this.OldTime = oldTime;
this.NewTime = newTime;
}
}

View File

@@ -29,6 +29,8 @@ public class TimePicker : TimePickerBase, IClearControl
public static readonly StyledProperty<string?> WatermarkProperty = AvaloniaProperty.Register<TimePicker, string?>( public static readonly StyledProperty<string?> WatermarkProperty = AvaloniaProperty.Register<TimePicker, string?>(
nameof(Watermark)); nameof(Watermark));
private bool _suppressTextPresenterEvent;
private Button? _button; private Button? _button;
private TimePickerPresenter? _presenter; private TimePickerPresenter? _presenter;
private TextBox? _textBox; private TextBox? _textBox;
@@ -44,11 +46,7 @@ public class TimePicker : TimePickerBase, IClearControl
private void OnDisplayFormatChanged(AvaloniaPropertyChangedEventArgs<string?> _) private void OnDisplayFormatChanged(AvaloniaPropertyChangedEventArgs<string?> _)
{ {
if (_textBox is null) return; if (_textBox is null) return;
var time = SelectedTime; SyncTimeToText(SelectedTime);
if (time is null) return;
var date = new DateTime( 1, 1, 1, time.Value.Hours, time.Value.Minutes, time.Value.Seconds);
var text = date.ToString(DisplayFormat);
_textBox.Text = text;
} }
public string? Watermark public string? Watermark
@@ -77,6 +75,7 @@ public class TimePicker : TimePickerBase, IClearControl
TextBox.TextChangedEvent.RemoveHandler(OnTextChanged, _textBox); TextBox.TextChangedEvent.RemoveHandler(OnTextChanged, _textBox);
PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _textBox); PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _textBox);
Button.ClickEvent.RemoveHandler(OnButtonClick, _button); Button.ClickEvent.RemoveHandler(OnButtonClick, _button);
TimePickerPresenter.SelectedTimeChangedEvent.RemoveHandler(OnPresenterTimeChanged, _presenter);
_textBox = e.NameScope.Find<TextBox>(PART_TextBox); _textBox = e.NameScope.Find<TextBox>(PART_TextBox);
e.NameScope.Find<Popup>(PartNames.PART_Popup); e.NameScope.Find<Popup>(PartNames.PART_Popup);
@@ -87,8 +86,17 @@ public class TimePicker : TimePickerBase, IClearControl
TextBox.TextChangedEvent.AddHandler(OnTextChanged, _textBox); TextBox.TextChangedEvent.AddHandler(OnTextChanged, _textBox);
PointerPressedEvent.AddHandler(OnTextBoxPointerPressed, RoutingStrategies.Tunnel, false, _textBox); PointerPressedEvent.AddHandler(OnTextBoxPointerPressed, RoutingStrategies.Tunnel, false, _textBox);
Button.ClickEvent.AddHandler(OnButtonClick, _button); Button.ClickEvent.AddHandler(OnButtonClick, _button);
TimePickerPresenter.SelectedTimeChangedEvent.AddHandler(OnPresenterTimeChanged, _presenter);
SetCurrentValue(SelectedTimeProperty, DateTime.Now.TimeOfDay); // SetCurrentValue(SelectedTimeProperty, DateTime.Now.TimeOfDay);
_presenter?.SetValue(TimePickerPresenter.TimeProperty, SelectedTime);
SyncTimeToText(SelectedTime);
}
private void OnPresenterTimeChanged(object sender, TimeChangedEventArgs e)
{
if (_suppressTextPresenterEvent) return;
SetCurrentValue(SelectedTimeProperty, e.NewTime);
} }
private void OnButtonClick(object? sender, RoutedEventArgs e) private void OnButtonClick(object? sender, RoutedEventArgs e)
@@ -104,7 +112,7 @@ public class TimePicker : TimePickerBase, IClearControl
private void OnTextBoxGetFocus(object? sender, GotFocusEventArgs e) private void OnTextBoxGetFocus(object? sender, GotFocusEventArgs e)
{ {
SetCurrentValue(IsDropdownOpenProperty, true); // SetCurrentValue(IsDropdownOpenProperty, true);
} }
protected override void OnKeyDown(KeyEventArgs e) protected override void OnKeyDown(KeyEventArgs e)
@@ -154,13 +162,20 @@ public class TimePicker : TimePickerBase, IClearControl
private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<TimeSpan?> args) private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<TimeSpan?> args)
{ {
if (_textBox is null) return; if (_textBox is null) return;
var time = args.NewValue.Value; _suppressTextPresenterEvent = true;
_presenter?.SetValue(TimePickerPresenter.TimeProperty, args.NewValue.Value);
SyncTimeToText(args.NewValue.Value);
_suppressTextPresenterEvent = false;
}
private void SyncTimeToText(TimeSpan? time)
{
if (_textBox is null) return;
if (time is null) if (time is null)
{ {
_textBox.Text = null; _textBox.Text = null;
return; return;
} }
var date = new DateTime(1, 1, 1, time.Value.Hours, time.Value.Minutes, time.Value.Seconds); var date = new DateTime(1, 1, 1, time.Value.Hours, time.Value.Minutes, time.Value.Seconds);
var text = date.ToString(DisplayFormat); var text = date.ToString(DisplayFormat);
_textBox.Text = text; _textBox.Text = text;

View File

@@ -2,6 +2,7 @@
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Metadata; using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using Irihi.Avalonia.Shared.Helpers; using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls; namespace Ursa.Controls;
@@ -83,7 +84,7 @@ public class TimePickerPresenter : TemplatedControl
public TimePickerPresenter() public TimePickerPresenter()
{ {
SetCurrentValue(TimeProperty, DateTime.Now.TimeOfDay); // SetCurrentValue(TimeProperty, DateTime.Now.TimeOfDay);
} }
public bool NeedsConfirmation public bool NeedsConfirmation
@@ -116,15 +117,26 @@ public class TimePickerPresenter : TemplatedControl
set => SetValue(PanelFormatProperty, value); set => SetValue(PanelFormatProperty, value);
} }
public event EventHandler<TimePickerSelectedValueChangedEventArgs>? SelectedTimeChanged; public static readonly RoutedEvent<TimeChangedEventArgs> SelectedTimeChangedEvent =
RoutedEvent.Register<TimePickerPresenter, TimeChangedEventArgs>(
nameof(SelectedTimeChanged), RoutingStrategies.Bubble);
public event EventHandler<TimeChangedEventArgs> SelectedTimeChanged
{
add => AddHandler(SelectedTimeChangedEvent, value);
remove => RemoveHandler(SelectedTimeChangedEvent, value);
}
private void OnTimeChanged(AvaloniaPropertyChangedEventArgs<TimeSpan?> args) private void OnTimeChanged(AvaloniaPropertyChangedEventArgs<TimeSpan?> args)
{ {
_updateFromTimeChange = true; _updateFromTimeChange = true;
UpdatePanelsFromSelectedTime(args.NewValue.Value); UpdatePanelsFromSelectedTime(args.NewValue.Value);
_updateFromTimeChange = false; _updateFromTimeChange = false;
SelectedTimeChanged?.Invoke(this, if (args.OldValue.Value != args.NewValue.Value)
new TimePickerSelectedValueChangedEventArgs(args.OldValue.Value, args.NewValue.Value)); {
RaiseEvent(new TimeChangedEventArgs(args.OldValue.Value, args.NewValue.Value)
{ RoutedEvent = SelectedTimeChangedEvent, Source = this });
}
} }
private void OnPanelFormatChanged(AvaloniaPropertyChangedEventArgs<string> args) private void OnPanelFormatChanged(AvaloniaPropertyChangedEventArgs<string> args)

View File

@@ -48,6 +48,7 @@ public class TimeRangePicker : TimePickerBase, IClearControl
private TimePickerPresenter? _startPresenter; private TimePickerPresenter? _startPresenter;
private TextBox? _startTextBox; private TextBox? _startTextBox;
private bool _suppressTextPresenterEvent;
static TimeRangePicker() static TimeRangePicker()
@@ -90,10 +91,17 @@ public class TimeRangePicker : TimePickerBase, IClearControl
} }
private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<TimeSpan?> args, bool start = true) private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<TimeSpan?> args, bool start = true)
{
SyncTimeToText(args.NewValue.Value, start);
_suppressTextPresenterEvent = true;
TimePickerPresenter.TimeProperty.SetValue(args.NewValue.Value, start ? _startPresenter : _endPresenter);
_suppressTextPresenterEvent = false;
}
private void SyncTimeToText(TimeSpan? time, bool start = true)
{ {
var textBox = start ? _startTextBox : _endTextBox; var textBox = start ? _startTextBox : _endTextBox;
if (textBox is null) return; if (textBox is null) return;
var time = args.NewValue.Value;
if (time is null) if (time is null)
{ {
textBox.Text = null; textBox.Text = null;
@@ -112,6 +120,8 @@ public class TimeRangePicker : TimePickerBase, IClearControl
GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _startTextBox, _endTextBox); GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _startTextBox, _endTextBox);
PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _startTextBox, _endTextBox); PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _startTextBox, _endTextBox);
Button.ClickEvent.RemoveHandler(OnButtonClick, _button); Button.ClickEvent.RemoveHandler(OnButtonClick, _button);
TimePickerPresenter.SelectedTimeChangedEvent.RemoveHandler(OnPresenterTimeChanged, _startPresenter,
_endPresenter);
e.NameScope.Find<Popup>(PartNames.PART_Popup); e.NameScope.Find<Popup>(PartNames.PART_Popup);
_startTextBox = e.NameScope.Find<TextBox>(PART_StartTextBox); _startTextBox = e.NameScope.Find<TextBox>(PART_StartTextBox);
@@ -124,6 +134,18 @@ public class TimeRangePicker : TimePickerBase, IClearControl
PointerPressedEvent.AddHandler(OnTextBoxPointerPressed, RoutingStrategies.Tunnel, false, _startTextBox, PointerPressedEvent.AddHandler(OnTextBoxPointerPressed, RoutingStrategies.Tunnel, false, _startTextBox,
_endTextBox); _endTextBox);
Button.ClickEvent.AddHandler(OnButtonClick, _button); Button.ClickEvent.AddHandler(OnButtonClick, _button);
TimePickerPresenter.SelectedTimeChangedEvent.AddHandler(OnPresenterTimeChanged, _startPresenter, _endPresenter);
_startPresenter?.SetValue(TimePickerPresenter.TimeProperty, StartTime);
_endPresenter?.SetValue(TimePickerPresenter.TimeProperty, EndTime);
SyncTimeToText(StartTime);
SyncTimeToText(EndTime, false);
}
private void OnPresenterTimeChanged(object sender, TimeChangedEventArgs e)
{
if (_suppressTextPresenterEvent) return;
SetCurrentValue(Equals(sender, _startPresenter) ? StartTimeProperty : EndTimeProperty, e.NewTime);
} }
private void OnButtonClick(object? sender, RoutedEventArgs e) private void OnButtonClick(object? sender, RoutedEventArgs e)