Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit f084cbd

Browse files
[Enhancement] Add Scale support for SideMenuView (#1011)
* SideMenu scaling #1007 * Added easing
1 parent ff2b942 commit f084cbd

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

samples/XCT.Sample/Pages/Views/SideMenuViewPage.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<BoxView
3636
xct:SideMenuView.Position="LeftMenu"
3737
xct:SideMenuView.MenuWidthPercentage=".5"
38+
xct:SideMenuView.MainViewScaleFactor=".95"
3839
BackgroundColor="Orange"/>
3940

4041
<!-- RightMenu -->

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/SideMenuView/SideMenuView.shared.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public static readonly BindableProperty MenuWidthPercentageProperty
8787
public static readonly BindableProperty MenuGestureEnabledProperty
8888
= BindableProperty.CreateAttached(nameof(GetMenuGestureEnabled), typeof(bool), typeof(SideMenuView), true);
8989

90+
public static readonly BindableProperty MainViewScaleFactorProperty
91+
= BindableProperty.CreateAttached(nameof(GetMainViewScaleFactor), typeof(double), typeof(SideMenuView), 1.0);
92+
9093
public SideMenuView()
9194
{
9295
#region Required work-around to prevent linker from removing the platform-specific implementation
@@ -157,6 +160,12 @@ public static bool GetMenuGestureEnabled(BindableObject bindable)
157160
public static void SetMenuGestureEnabled(BindableObject bindable, bool value)
158161
=> bindable.SetValue(MenuGestureEnabledProperty, value);
159162

163+
public static double GetMainViewScaleFactor(BindableObject bindable)
164+
=> (double)bindable.GetValue(MainViewScaleFactorProperty);
165+
166+
public static void SetMainViewScaleFactor(BindableObject bindable, double value)
167+
=> bindable.SetValue(MainViewScaleFactorProperty, value);
168+
160169
internal void OnPanUpdated(object sender, PanUpdatedEventArgs e)
161170
{
162171
var shift = e.TotalX;
@@ -364,26 +373,34 @@ SideMenuState ResolveSwipeState(bool isRightSwipe)
364373
return isRightSwipe ? left : right;
365374
}
366375

367-
bool TryUpdateShift(double sift, bool shouldUpdatePreviousShift, bool shouldCheckMenuGestureEnabled)
376+
bool TryUpdateShift(double shift, bool shouldUpdatePreviousShift, bool shouldCheckMenuGestureEnabled)
368377
{
369-
SetActiveView(sift >= 0);
378+
SetActiveView(shift >= 0);
370379
if (activeMenu == null)
371380
return false;
372381

373382
if (shouldCheckMenuGestureEnabled && !GetMenuGestureEnabled(activeMenu))
374383
return false;
375384

376-
sift = Sign(sift) * Min(Abs(sift), activeMenu.Width);
377-
if (Abs(Shift - sift) <= double.Epsilon)
385+
var activeMenuWidth = activeMenu.Width;
386+
var mainViewWidth = mainView.Width;
387+
388+
shift = Sign(shift) * Min(Abs(shift), activeMenuWidth);
389+
if (Abs(Shift - shift) <= double.Epsilon)
378390
return false;
379391

380-
Shift = sift;
381-
SetCurrentGestureState(sift);
392+
Shift = shift;
393+
SetCurrentGestureState(shift);
382394
if (shouldUpdatePreviousShift)
383-
previousShift = sift;
395+
previousShift = shift;
384396

385-
mainView.TranslationX = sift;
386-
overlayView.TranslationX = sift;
397+
using (mainView.Batch())
398+
{
399+
var scale = 1 - ((1 - GetMainViewScaleFactor(activeMenu)) * animationEasing.Ease(shift / activeMenuWidth));
400+
mainView.Scale = scale;
401+
mainView.TranslationX = shift - (Sign(shift) * mainViewWidth * 0.5 * (1 - scale));
402+
}
403+
overlayView.TranslationX = shift;
387404
return true;
388405
}
389406

0 commit comments

Comments
 (0)