-
-
Notifications
You must be signed in to change notification settings - Fork 550
Revert "Upgrade iNKORE.UI.WPF.Modern and refactor scroll logic" #4201
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
+264
−13
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
253 changes: 253 additions & 0 deletions
253
Flow.Launcher/Resources/Controls/CustomScrollViewerEx.cs
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,253 @@ | ||
| using iNKORE.UI.WPF.Modern.Controls; | ||
| using iNKORE.UI.WPF.Modern.Controls.Helpers; | ||
| using iNKORE.UI.WPF.Modern.Controls.Primitives; | ||
| using System; | ||
| using System.Windows; | ||
| using System.Windows.Controls; | ||
| using System.Windows.Input; | ||
|
|
||
| namespace Flow.Launcher.Resources.Controls | ||
| { | ||
| // TODO: Use IsScrollAnimationEnabled property in future: https://github.com/iNKORE-NET/UI.WPF.Modern/pull/347 | ||
| public class CustomScrollViewerEx : ScrollViewer | ||
| { | ||
| private double LastVerticalLocation = 0; | ||
| private double LastHorizontalLocation = 0; | ||
|
|
||
| public CustomScrollViewerEx() | ||
| { | ||
| Loaded += OnLoaded; | ||
| var valueSource = DependencyPropertyHelper.GetValueSource(this, AutoPanningMode.IsEnabledProperty).BaseValueSource; | ||
| if (valueSource == BaseValueSource.Default) | ||
| { | ||
| AutoPanningMode.SetIsEnabled(this, true); | ||
| } | ||
| } | ||
|
|
||
| #region Orientation | ||
|
|
||
| public static readonly DependencyProperty OrientationProperty = | ||
| DependencyProperty.Register( | ||
| nameof(Orientation), | ||
| typeof(Orientation), | ||
| typeof(CustomScrollViewerEx), | ||
| new PropertyMetadata(Orientation.Vertical)); | ||
|
|
||
| public Orientation Orientation | ||
| { | ||
| get => (Orientation)GetValue(OrientationProperty); | ||
| set => SetValue(OrientationProperty, value); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region AutoHideScrollBars | ||
|
|
||
| public static readonly DependencyProperty AutoHideScrollBarsProperty = | ||
| ScrollViewerHelper.AutoHideScrollBarsProperty | ||
| .AddOwner( | ||
| typeof(CustomScrollViewerEx), | ||
| new PropertyMetadata(true, OnAutoHideScrollBarsChanged)); | ||
|
|
||
| public bool AutoHideScrollBars | ||
| { | ||
| get => (bool)GetValue(AutoHideScrollBarsProperty); | ||
| set => SetValue(AutoHideScrollBarsProperty, value); | ||
| } | ||
|
|
||
| private static void OnAutoHideScrollBarsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
| { | ||
| if (d is CustomScrollViewerEx sv) | ||
| { | ||
| sv.UpdateVisualState(); | ||
| } | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| private void OnLoaded(object sender, RoutedEventArgs e) | ||
| { | ||
| LastVerticalLocation = VerticalOffset; | ||
| LastHorizontalLocation = HorizontalOffset; | ||
| UpdateVisualState(false); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void OnInitialized(EventArgs e) | ||
| { | ||
| base.OnInitialized(e); | ||
|
|
||
| if (Style == null && ReadLocalValue(StyleProperty) == DependencyProperty.UnsetValue) | ||
| { | ||
| SetResourceReference(StyleProperty, typeof(ScrollViewer)); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void OnMouseWheel(MouseWheelEventArgs e) | ||
| { | ||
| var Direction = GetDirection(); | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ScrollViewerBehavior.SetIsAnimating(this, true); | ||
|
|
||
| if (Direction == Orientation.Vertical) | ||
| { | ||
| if (ScrollableHeight > 0) | ||
| { | ||
| e.Handled = true; | ||
| } | ||
|
|
||
| var WheelChange = e.Delta * (ViewportHeight / 1.5) / ActualHeight; | ||
| var newOffset = LastVerticalLocation - WheelChange; | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (newOffset < 0) | ||
| { | ||
| newOffset = 0; | ||
| } | ||
|
|
||
| if (newOffset > ScrollableHeight) | ||
| { | ||
| newOffset = ScrollableHeight; | ||
| } | ||
|
|
||
| if (newOffset == LastVerticalLocation) | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return; | ||
| } | ||
|
|
||
| ScrollToVerticalOffset(LastVerticalLocation); | ||
|
|
||
| ScrollToValue(newOffset, Direction); | ||
| LastVerticalLocation = newOffset; | ||
| } | ||
| else | ||
| { | ||
| if (ScrollableWidth > 0) | ||
| { | ||
| e.Handled = true; | ||
| } | ||
|
|
||
| var WheelChange = e.Delta * (ViewportWidth / 1.5) / ActualWidth; | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var newOffset = LastHorizontalLocation - WheelChange; | ||
|
|
||
| if (newOffset < 0) | ||
| { | ||
| newOffset = 0; | ||
| } | ||
|
|
||
| if (newOffset > ScrollableWidth) | ||
| { | ||
| newOffset = ScrollableWidth; | ||
| } | ||
|
|
||
| if (newOffset == LastHorizontalLocation) | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return; | ||
| } | ||
|
|
||
| ScrollToHorizontalOffset(LastHorizontalLocation); | ||
|
|
||
| ScrollToValue(newOffset, Direction); | ||
| LastHorizontalLocation = newOffset; | ||
| } | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void OnScrollChanged(ScrollChangedEventArgs e) | ||
| { | ||
| base.OnScrollChanged(e); | ||
| if (!ScrollViewerBehavior.GetIsAnimating(this)) | ||
| { | ||
| LastVerticalLocation = VerticalOffset; | ||
| LastHorizontalLocation = HorizontalOffset; | ||
| } | ||
| } | ||
|
|
||
| private Orientation GetDirection() | ||
| { | ||
| var isShiftDown = Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift); | ||
|
|
||
| if (Orientation == Orientation.Horizontal) | ||
| { | ||
| return isShiftDown ? Orientation.Vertical : Orientation.Horizontal; | ||
| } | ||
| else | ||
| { | ||
| return isShiftDown ? Orientation.Horizontal : Orientation.Vertical; | ||
| } | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Causes the <see cref="ScrollViewerEx"/> to load a new view into the viewport using the specified offsets and zoom factor. | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| /// <param name="horizontalOffset">A value between 0 and <see cref="ScrollViewer.ScrollableWidth"/> that specifies the distance the content should be scrolled horizontally.</param> | ||
| /// <param name="verticalOffset">A value between 0 and <see cref="ScrollViewer.ScrollableHeight"/> that specifies the distance the content should be scrolled vertically.</param> | ||
| /// <param name="zoomFactor">A value between MinZoomFactor and MaxZoomFactor that specifies the required target ZoomFactor.</param> | ||
| /// <returns><see langword="true"/> if the view is changed; otherwise, <see langword="false"/>.</returns> | ||
| public bool ChangeView(double? horizontalOffset, double? verticalOffset, float? zoomFactor) | ||
| { | ||
| return ChangeView(horizontalOffset, verticalOffset, zoomFactor, false); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Causes the <see cref="ScrollViewerEx"/> to load a new view into the viewport using the specified offsets and zoom factor, and optionally disables scrolling animation. | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| /// <param name="horizontalOffset">A value between 0 and <see cref="ScrollViewer.ScrollableWidth"/> that specifies the distance the content should be scrolled horizontally.</param> | ||
| /// <param name="verticalOffset">A value between 0 and <see cref="ScrollViewer.ScrollableHeight"/> that specifies the distance the content should be scrolled vertically.</param> | ||
| /// <param name="zoomFactor">A value between MinZoomFactor and MaxZoomFactor that specifies the required target ZoomFactor.</param> | ||
| /// <param name="disableAnimation"><see langword="true"/> to disable zoom/pan animations while changing the view; otherwise, <see langword="false"/>. The default is false.</param> | ||
| /// <returns><see langword="true"/> if the view is changed; otherwise, <see langword="false"/>.</returns> | ||
| public bool ChangeView(double? horizontalOffset, double? verticalOffset, float? zoomFactor, bool disableAnimation) | ||
| { | ||
| if (disableAnimation) | ||
| { | ||
| if (horizontalOffset.HasValue) | ||
| { | ||
| ScrollToHorizontalOffset(horizontalOffset.Value); | ||
| } | ||
|
|
||
| if (verticalOffset.HasValue) | ||
| { | ||
| ScrollToVerticalOffset(verticalOffset.Value); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| if (horizontalOffset.HasValue) | ||
| { | ||
| ScrollToHorizontalOffset(LastHorizontalLocation); | ||
| ScrollToValue(Math.Min(ScrollableWidth, horizontalOffset.Value), Orientation.Horizontal); | ||
| LastHorizontalLocation = horizontalOffset.Value; | ||
| } | ||
|
|
||
| if (verticalOffset.HasValue) | ||
| { | ||
| ScrollToVerticalOffset(LastVerticalLocation); | ||
| ScrollToValue(Math.Min(ScrollableHeight, verticalOffset.Value), Orientation.Vertical); | ||
| LastVerticalLocation = verticalOffset.Value; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private void ScrollToValue(double value, Orientation Direction) | ||
| { | ||
| if (Direction == Orientation.Vertical) | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| ScrollToVerticalOffset(value); | ||
| } | ||
| else | ||
| { | ||
| ScrollToHorizontalOffset(value); | ||
| } | ||
|
|
||
| ScrollViewerBehavior.SetIsAnimating(this, false); | ||
| } | ||
|
|
||
| private void UpdateVisualState(bool useTransitions = true) | ||
| { | ||
| var stateName = AutoHideScrollBars ? "NoIndicator" : "MouseIndicator"; | ||
| VisualStateManager.GoToState(this, stateName, useTransitions); | ||
| } | ||
| } | ||
| } | ||
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.