Skip to content

Commit

Permalink
Fix TimePicker breaking change, when using pre-11.2 styles
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkatz6 committed Nov 15, 2024
1 parent 5c1492d commit 331d73e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 44 deletions.
58 changes: 41 additions & 17 deletions src/Avalonia.Controls/DateTimePickers/TimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,13 @@ private void SetGrid()
return;

var use24HourClock = ClockIdentifier == "24HourClock";
var canUseSeconds = _secondText is not null && _fourthPickerHost is not null && _thirdSplitter is not null;

var columnsD = new ColumnDefinitions();
columnsD.Add(new ColumnDefinition(GridLength.Star));
columnsD.Add(new ColumnDefinition(GridLength.Auto));
columnsD.Add(new ColumnDefinition(GridLength.Star));
if (UseSeconds)
if (canUseSeconds && UseSeconds)
{
columnsD.Add(new ColumnDefinition(GridLength.Auto));
columnsD.Add(new ColumnDefinition(GridLength.Star));
Expand All @@ -274,30 +275,45 @@ private void SetGrid()
columnsD.Add(new ColumnDefinition(GridLength.Auto));
columnsD.Add(new ColumnDefinition(GridLength.Star));
}

_contentGrid.ColumnDefinitions = columnsD;

_thirdPickerHost!.IsVisible = UseSeconds;
_secondSplitter!.IsVisible = UseSeconds;

_fourthPickerHost!.IsVisible = !use24HourClock;
_thirdSplitter!.IsVisible = !use24HourClock;
_contentGrid.ColumnDefinitions = columnsD;

var amPmColumn = (UseSeconds) ? 6 : 4;
if (canUseSeconds)
{
_thirdPickerHost!.IsVisible = UseSeconds;
_secondSplitter!.IsVisible = UseSeconds;
_fourthPickerHost!.IsVisible = !use24HourClock;
_thirdSplitter!.IsVisible = !use24HourClock;
}
else
{
_thirdPickerHost!.IsVisible = !use24HourClock;
_secondSplitter!.IsVisible = !use24HourClock;
}

Grid.SetColumn(_firstPickerHost!, 0);
Grid.SetColumn(_secondPickerHost!, 2);
Grid.SetColumn(_thirdPickerHost!, UseSeconds ? 4 : 0);
Grid.SetColumn(_fourthPickerHost, use24HourClock ? 0 : amPmColumn);

Grid.SetColumn(_firstSplitter!, 1);
Grid.SetColumn(_secondSplitter!, UseSeconds ? 3 : 0);
Grid.SetColumn(_thirdSplitter, use24HourClock ? 0 : amPmColumn-1);
if (canUseSeconds)
{
var amPmColumn = (UseSeconds) ? 6 : 4;
Grid.SetColumn(_thirdPickerHost!, UseSeconds ? 4 : 0);
Grid.SetColumn(_fourthPickerHost!, use24HourClock ? 0 : amPmColumn);
Grid.SetColumn(_firstSplitter!, 1);
Grid.SetColumn(_secondSplitter!, UseSeconds ? 3 : 0);
Grid.SetColumn(_thirdSplitter!, use24HourClock ? 0 : amPmColumn-1);
}
else
{
Grid.SetColumn(_thirdPickerHost, use24HourClock ? 0 : 4);
Grid.SetColumn(_firstSplitter!, 1);
Grid.SetColumn(_secondSplitter, use24HourClock ? 0 : 3);
}
}

private void SetSelectedTimeText()
{
if (_hourText == null || _minuteText == null || _secondText == null || _periodText == null)
if (_hourText == null || _minuteText == null || _periodText == null)
return;

var time = SelectedTime;
Expand All @@ -314,7 +330,11 @@ private void SetSelectedTimeText()

_hourText.Text = newTime.ToString("%h");
_minuteText.Text = newTime.ToString("mm");
_secondText.Text = newTime.ToString("ss");
if (_secondText is not null)
{
_secondText.Text = newTime.ToString("ss");
}

PseudoClasses.Set(":hasnotime", false);

_periodText.Text = time.Value.Hours >= 12 ? TimeUtils.GetPMDesignator() : TimeUtils.GetAMDesignator();
Expand All @@ -324,7 +344,11 @@ private void SetSelectedTimeText()
// By clearing local value, we reset text property to the value from the template.
_hourText.ClearValue(TextBlock.TextProperty);
_minuteText.ClearValue(TextBlock.TextProperty);
_secondText.ClearValue(TextBlock.TextProperty);
if (_secondText is not null)
{
_secondText.ClearValue(TextBlock.TextProperty);
}

PseudoClasses.Set(":hasnotime", true);

_periodText.Text = DateTime.Now.Hour >= 12 ? TimeUtils.GetPMDesignator() : TimeUtils.GetAMDesignator();
Expand Down
78 changes: 51 additions & 27 deletions src/Avalonia.Controls/DateTimePickers/TimePickerPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)

_pickerContainer = e.NameScope.Get<Grid>("PART_PickerContainer");
_periodHost = e.NameScope.Get<Panel>("PART_PeriodHost");
_secondHost = e.NameScope.Get<Panel>("PART_SecondHost");
_secondHost = e.NameScope.Find<Panel>("PART_SecondHost");

_hourSelector = e.NameScope.Get<DateTimePickerPanel>("PART_HourSelector");
_minuteSelector = e.NameScope.Get<DateTimePickerPanel>("PART_MinuteSelector");
_secondSelector = e.NameScope.Get<DateTimePickerPanel>("PART_SecondSelector");
_secondSelector = e.NameScope.Find<DateTimePickerPanel>("PART_SecondSelector");
_periodSelector = e.NameScope.Get<DateTimePickerPanel>("PART_PeriodSelector");

_spacer2 = e.NameScope.Get<Rectangle>("PART_SecondSpacer");
_spacer3 = e.NameScope.Get<Rectangle>("PART_ThirdSpacer");
_spacer3 = e.NameScope.Find<Rectangle>("PART_ThirdSpacer");

_acceptButton = e.NameScope.Get<Button>("PART_AcceptButton");
_acceptButton.Click += OnAcceptButtonClicked;
Expand Down Expand Up @@ -234,7 +234,7 @@ protected override void OnConfirmed()
{
var hr = _hourSelector!.SelectedValue;
var min = _minuteSelector!.SelectedValue;
var sec = _secondSelector!.SelectedValue;
var sec = _secondSelector?.SelectedValue ?? 0;
var per = _periodSelector!.SelectedValue;

if (ClockIdentifier == "12HourClock")
Expand Down Expand Up @@ -265,12 +265,15 @@ private void InitPicker()
_minuteSelector.Increment = MinuteIncrement;
_minuteSelector.ItemFormat = "mm";
_minuteSelector.SelectedValue = Time.Minutes;

_secondSelector!.MaximumValue = 59;
_secondSelector.MinimumValue = 0;
_secondSelector.Increment = SecondIncrement;
_secondSelector.ItemFormat = "ss";
_secondSelector.SelectedValue = Time.Seconds;

if (_secondSelector is not null)
{
_secondSelector.MaximumValue = 59;
_secondSelector.MinimumValue = 0;
_secondSelector.Increment = SecondIncrement;
_secondSelector.ItemFormat = "ss";
_secondSelector.SelectedValue = Time.Seconds;
}

_periodSelector!.MaximumValue = 1;
_periodSelector.MinimumValue = 0;
Expand All @@ -282,26 +285,47 @@ private void InitPicker()

private void SetGrid()
{
bool use24HourClock = ClockIdentifier == "24HourClock";

var columnsD = "*, Auto, *";
if (UseSeconds) columnsD += ", Auto *";
if (!use24HourClock) columnsD += ", Auto *";
var use24HourClock = ClockIdentifier == "24HourClock";
var canUseSeconds = _secondHost is not null && _spacer3 is not null;

_pickerContainer!.ColumnDefinitions = new ColumnDefinitions(columnsD);
var columnsD = new ColumnDefinitions();
columnsD.Add(new ColumnDefinition(GridLength.Star));
columnsD.Add(new ColumnDefinition(GridLength.Auto));
columnsD.Add(new ColumnDefinition(GridLength.Star));
if (canUseSeconds && UseSeconds)
{
columnsD.Add(new ColumnDefinition(GridLength.Auto));
columnsD.Add(new ColumnDefinition(GridLength.Star));
}
if (!use24HourClock)
{
columnsD.Add(new ColumnDefinition(GridLength.Auto));
columnsD.Add(new ColumnDefinition(GridLength.Star));
}

_spacer2!.IsVisible = UseSeconds;
_secondHost!.IsVisible = UseSeconds;
_pickerContainer!.ColumnDefinitions = columnsD;

_spacer3!.IsVisible = !use24HourClock;
_periodHost!.IsVisible = !use24HourClock;

var amPmColumn = (UseSeconds) ? 6 : 4;

Grid.SetColumn(_spacer2, UseSeconds ? 3 : 0);
Grid.SetColumn(_secondHost, UseSeconds ? 4 : 0);
Grid.SetColumn(_spacer3, use24HourClock ? 0 : amPmColumn-1);
Grid.SetColumn(_periodHost, use24HourClock ? 0 : amPmColumn);
if (canUseSeconds)
{
_spacer2!.IsVisible = UseSeconds;
_secondHost!.IsVisible = UseSeconds;
_spacer3!.IsVisible = !use24HourClock;
_periodHost!.IsVisible = !use24HourClock;

var amPmColumn = (UseSeconds) ? 6 : 4;

Grid.SetColumn(_spacer2, UseSeconds ? 3 : 0);
Grid.SetColumn(_secondHost, UseSeconds ? 4 : 0);
Grid.SetColumn(_spacer3, use24HourClock ? 0 : amPmColumn-1);
Grid.SetColumn(_periodHost, use24HourClock ? 0 : amPmColumn);
}
else
{
_spacer2!.IsVisible = !use24HourClock;
_periodHost!.IsVisible = !use24HourClock;
Grid.SetColumn(_spacer2, use24HourClock ? 0 : 3);
Grid.SetColumn(_periodHost, use24HourClock ? 0 : 4);
}
}

private void OnDismissButtonClicked(object? sender, RoutedEventArgs e)
Expand Down

0 comments on commit 331d73e

Please sign in to comment.