-
-
Notifications
You must be signed in to change notification settings - Fork 930
fix(controls): Fix Separator layout and add Orientation attached prop… #1630
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // This Source Code Form is subject to the terms of the MIT License. | ||
| // If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
| // Copyright (C) Leszek Pomianowski and WPF UI Contributors. | ||
| // All Rights Reserved. | ||
|
|
||
| using System.Windows.Controls; | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
| namespace Wpf.Ui.Controls; | ||
|
|
||
| /// <summary> | ||
| /// Provides <c>Orientation</c> property for Separator | ||
| /// </summary> | ||
| /// <example> | ||
| /// <code lang="xml"> | ||
| /// <Separator controls:SeparatorAttached.Orientation="Vertical" /> | ||
| /// </code> | ||
| /// </example> | ||
| public static class SeparatorAttached | ||
| { | ||
| /// <summary> | ||
| /// Resource key for <see cref="Separator"/> horizontal margin. | ||
| /// </summary> | ||
| public const string HorizontalMarginKey = "DefaultSeparatorHorizontalMargin"; | ||
|
|
||
| /// <summary> | ||
| /// Resource key for <see cref="Separator"/> vertical margin. | ||
| /// </summary> | ||
| public const string VerticalMarginKey = "DefaultSeparatorVerticalMargin"; | ||
|
|
||
| public static readonly DependencyProperty OrientationProperty = | ||
| DependencyProperty.RegisterAttached( | ||
| "Orientation", | ||
| typeof(Orientation), | ||
| typeof(SeparatorAttached), | ||
| new FrameworkPropertyMetadata(Orientation.Horizontal, OnOrientationChanged) | ||
| ); | ||
|
|
||
| /// <summary>Helper for getting <see cref="OrientationProperty"/> from <paramref name="obj"/>.</summary> | ||
| /// <param name="obj"><see cref="DependencyObject"/> to read <see cref="OrientationProperty"/> from.</param> | ||
| /// <returns>Orientation property value.</returns> | ||
| [AttachedPropertyBrowsableForType(typeof(Separator))] | ||
| public static Orientation GetOrientation(DependencyObject obj) | ||
| { | ||
| return (Orientation)obj.GetValue(OrientationProperty); | ||
| } | ||
|
|
||
| /// <summary>Helper for setting <see cref="OrientationProperty"/> on <paramref name="obj"/>.</summary> | ||
| /// <param name="obj"><see cref="DependencyObject"/> to set <see cref="OrientationProperty"/> on.</param> | ||
| /// <param name="value">Orientation property value.</param> | ||
| public static void SetOrientation(DependencyObject obj, Orientation value) | ||
| { | ||
| obj.SetValue(OrientationProperty, value); | ||
| } | ||
|
|
||
| private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
apachezy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (d is not FrameworkElement element) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Thickness currentMargin = element.Margin; | ||
| Orientation newOrientation = (Orientation)e.NewValue; | ||
|
|
||
| // Smart margin logic: | ||
| // 1. When switching orientation, if Margin still equals the horizontal default, | ||
| // we assume the user hasn't customized it and apply the new orientation's default. | ||
| // 2. Subsequent orientation switches only adjust the margin if it still matches | ||
| // the previous orientation's default. | ||
| // 3. If the user coincidentally sets Margin to a value that matches one default, | ||
| // switching to the other orientation will still apply the correct default. | ||
| // 4. Any non‑default value set by the user is treated as intentional and preserved. | ||
| if (newOrientation == Orientation.Vertical) | ||
| { | ||
| var horizontalMargin = element.TryFindResource(HorizontalMarginKey) as Thickness?; | ||
| if (currentMargin == horizontalMargin) | ||
| { | ||
| element.SetResourceReference(FrameworkElement.MarginProperty, VerticalMarginKey); | ||
| } | ||
| } | ||
| else if (newOrientation == Orientation.Horizontal) | ||
| { | ||
| var verticalMargin = element.TryFindResource(VerticalMarginKey) as Thickness?; | ||
| if (currentMargin == verticalMargin) | ||
| { | ||
| element.SetResourceReference(FrameworkElement.MarginProperty, HorizontalMarginKey); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.