-
-
Notifications
You must be signed in to change notification settings - Fork 940
fix(controls): Tooltips, context menu, and comboboxes drop shadow #1472
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a644fad
Add files via upload
Nuklon 2dd3c46
Update ContextMenu.xaml
Nuklon 5da7f41
Update MenuItem.xaml
Nuklon e632561
Update ToolTip.xaml
Nuklon e106738
Update EffectThicknessDecorator.cs
Nuklon 3895251
Update ComboBox.xaml
Nuklon 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
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,191 @@ | ||
| // 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; | ||
| using System.Windows.Controls.Primitives; | ||
|
|
||
| namespace Wpf.Ui.Controls; | ||
|
|
||
| public class EffectThicknessDecorator : Decorator | ||
| { | ||
| public static readonly DependencyProperty ThicknessProperty = | ||
| DependencyProperty.Register(nameof(Thickness), typeof(Thickness), typeof(EffectThicknessDecorator), new PropertyMetadata(new Thickness(35), OnThicknessChanged)); | ||
|
|
||
| public static readonly DependencyProperty AnimationDelayProperty = | ||
| DependencyProperty.Register(nameof(AnimationDelay), typeof(TimeSpan), typeof(EffectThicknessDecorator), new PropertyMetadata(TimeSpan.Zero)); | ||
|
|
||
| public static readonly DependencyProperty AnimationElementProperty = | ||
| DependencyProperty.Register(nameof(AnimationElement), typeof(UIElement), typeof(EffectThicknessDecorator), new PropertyMetadata(default(UIElement))); | ||
|
|
||
| private PopupContainer? _popupContainer; | ||
|
|
||
| public EffectThicknessDecorator() | ||
| { | ||
| SizeChanged += (_, _) => UpdateLayout(); | ||
| } | ||
|
|
||
| public TimeSpan AnimationDelay | ||
| { | ||
| get { return (TimeSpan)GetValue(AnimationDelayProperty); } | ||
| set { SetValue(AnimationDelayProperty, value); } | ||
| } | ||
|
|
||
| public UIElement? AnimationElement | ||
| { | ||
| get { return (UIElement?)GetValue(AnimationElementProperty); } | ||
| set { SetValue(AnimationElementProperty, value); } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the thickness of the effect around the containing element. | ||
| /// </summary> | ||
| public Thickness Thickness | ||
| { | ||
| get { return (Thickness)GetValue(ThicknessProperty); } | ||
| set { SetValue(ThicknessProperty, value); } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override int VisualChildrenCount => 1; | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override void OnVisualParentChanged(DependencyObject oldParent) | ||
| { | ||
| base.OnVisualParentChanged(oldParent); | ||
|
|
||
| if (IsInitialized) | ||
| { | ||
| SetPopupContainer(); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override Visual GetVisualChild(int index) | ||
| { | ||
| // Only 1 child... | ||
| return Child; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override void OnInitialized(EventArgs e) | ||
| { | ||
| base.OnInitialized(e); | ||
|
|
||
| SetPopupContainer(); | ||
| } | ||
|
|
||
| private void SetPopupContainer() | ||
| { | ||
| PopupContainer? popupContainer = null; | ||
|
|
||
| switch (VisualParent) | ||
| { | ||
| case ContextMenu contextMenu: | ||
| popupContainer = new PopupContainer(contextMenu); | ||
| break; | ||
| case ToolTip toolTip: | ||
| popupContainer = new PopupContainer(toolTip); | ||
| break; | ||
| default: | ||
| if (GetParentPopup(this) is { } parentPopup) | ||
| { | ||
| popupContainer = new PopupContainer(parentPopup); | ||
| } | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| if (popupContainer == null || _popupContainer?.FrameworkElement == popupContainer.FrameworkElement) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| popupContainer.Opened += (_, _) => | ||
| { | ||
| if (AnimationElement is { Effect: { } effect } animationElement && AnimationDelay.Ticks > 0) | ||
| { | ||
| animationElement.Effect = null; | ||
|
|
||
| Task.Delay(AnimationDelay).ContinueWith(_ => Dispatcher.Invoke(() => animationElement.Effect = effect)); | ||
| } | ||
| }; | ||
|
|
||
| _popupContainer = popupContainer; | ||
| ApplyMargin(); | ||
| } | ||
|
|
||
| private static Popup? GetParentPopup(FrameworkElement element) | ||
| { | ||
| while (true) | ||
| { | ||
| switch (element.Parent) | ||
| { | ||
| case Popup popup: | ||
| return popup; | ||
| case FrameworkElement frameworkElement: | ||
| element = frameworkElement; | ||
| continue; | ||
| } | ||
|
|
||
| if (VisualTreeHelper.GetParent(element) is FrameworkElement parent) | ||
| { | ||
| element = parent; | ||
| continue; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static void OnThicknessChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
| { | ||
| if (d is EffectThicknessDecorator decorator) | ||
| { | ||
| decorator.ApplyMargin(); | ||
| } | ||
| } | ||
|
|
||
| private void ApplyMargin() | ||
| { | ||
| _popupContainer?.SetMargin(Thickness); | ||
| } | ||
|
|
||
| private class PopupContainer | ||
| { | ||
| private readonly ContextMenu? _contextMenu; | ||
| private readonly Popup? _popup; | ||
| private readonly ToolTip? _toolTip; | ||
|
|
||
| public PopupContainer(ContextMenu contextMenu) | ||
| { | ||
| _contextMenu = contextMenu; | ||
| contextMenu.Opened += (sender, args) => Opened?.Invoke(sender, args); | ||
| } | ||
|
|
||
| public PopupContainer(ToolTip toolTip) | ||
| { | ||
| _toolTip = toolTip; | ||
| toolTip.Opened += (sender, args) => Opened?.Invoke(sender, args); | ||
| } | ||
|
|
||
| public PopupContainer(Popup popup) | ||
| { | ||
| _popup = popup; | ||
| popup.Opened += (sender, args) => Opened?.Invoke(sender, args); | ||
| } | ||
|
|
||
| public event EventHandler Opened; | ||
|
|
||
| public FrameworkElement? FrameworkElement => _contextMenu ?? _toolTip ?? _popup?.Child as FrameworkElement; | ||
|
|
||
| public void SetMargin(Thickness margin) | ||
| { | ||
| if (FrameworkElement is { } frameworkElement) | ||
| { | ||
| frameworkElement.Margin = margin; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
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.
Consider giving this class a doc block (explaining the purpose) that effectively repeats this part from the PR: