-
Notifications
You must be signed in to change notification settings - Fork 120
Orientation option for Segmented #747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
25106dc
4d35ca1
99738b1
c4e0ae3
093c4b2
9924e89
00de4bc
fccfc84
db22dd1
aa68ff5
fe533f5
4b3fb0e
05c7684
bb11f05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace CommunityToolkit.WinUI.Controls; | ||
|
|
||
| public partial class Segmented | ||
| { | ||
| /// <summary> | ||
| /// The backing <see cref="DependencyProperty"/> for the <see cref="Orientation"/> property. | ||
| /// </summary> | ||
| public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register( | ||
| nameof(Orientation), | ||
| typeof(Orientation), | ||
| typeof(Segmented), | ||
| new PropertyMetadata(Orientation.Horizontal, (d, e) => ((Segmented)d).OnOrientationChanged())); | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the orientation. | ||
| /// </summary> | ||
| public Orientation Orientation | ||
| { | ||
| get => (Orientation)GetValue(OrientationProperty); | ||
| set => SetValue(OrientationProperty, value); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ public Segmented() | |
| this.DefaultStyleKey = typeof(Segmented); | ||
|
|
||
| RegisterPropertyChangedCallback(SelectedIndexProperty, OnSelectedIndexChanged); | ||
| RegisterPropertyChangedCallback(OrientationProperty, OnSelectedIndexChanged); | ||
|
Comment on lines
24
to
+25
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to extract the logic in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, wait a minute. That's supposed to call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. Because I moved the callback to the DependencyProperty declaration. This line is entirely unnecessary. |
||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
|
|
@@ -154,4 +155,15 @@ private void OnSelectedIndexChanged(DependencyObject sender, DependencyProperty | |
| _internalSelectedIndex = SelectedIndex; | ||
| } | ||
| } | ||
|
|
||
| private void OnOrientationChanged() | ||
| { | ||
| for (int i = 0; i < Items.Count; i++) | ||
| { | ||
| if (ContainerFromIndex(i) is SegmentedItem item) | ||
| { | ||
| item.UpdateOrientation(Orientation); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name
OnPropertyChangedis less specific because it's being called from multiple places, but it needs to be disambiguated between the other properties and property changed callbacks here.I suggest removing the second unused param from this method (discarding it w/
DependencyPropertyChangedEventArgs _in a delegate instead) and naming the method based on what it does, rather than naming based on where it's called from.In fact, since all it's doing is calling
InvalidateMeasure()on the first paramDependencyObject dcast toEqualPanel, we could probably just inline delegate that call without splitting into a new method.Which solution we choose depends on whether we expect code to accumulate in these callback bodies. Seems like a straightforward
.InvalidateMeasure()on whichever dependency property container called it, so I'm leaning towards the second delegate-only option.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does it need to be disambiguated? Every property actually defined on
EqualPaneluses this callback, and the only reasonOnAlignmentChangedis different is because it required a different delegate by nature of being a property of a parent type.