Skip to content

Commit

Permalink
Merge pull request #11 from RadekVyM/dev
Browse files Browse the repository at this point in the history
SimpleShell 2.1.0
  • Loading branch information
RadekVyM authored Mar 4, 2023
2 parents 61baeb9 + a1069e0 commit e4a5c26
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 78 deletions.
Binary file added .DS_Store
Binary file not shown.
53 changes: 31 additions & 22 deletions docs/SimpleToolkit.SimpleShell/Transitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Each transition is represented by a `SimpleShellTransition` object which contain
- `Duration` - method returning duration of the transition
- `DestinationPageInFront` - method returning whether the destination page should be displayed in front of the origin page when navigating from one page to another

Each of these methods takes a object `SimpleShellTransitionArgs` as a parameter. Usefull information about currently running transition can be obtained from this object:
Each of these methods takes a `SimpleShellTransitionArgs` object as a parameter. Usefull information about currently running transition can be obtained from this object:

- `OriginPage` of type `VisualElement` - page from which the navigation is initiated
- `DestinationPage` of type `VisualElement` - destination page of the navigation
Expand All @@ -39,16 +39,6 @@ public static void SetTransition(
this Page page,
SimpleShellTransition transition)

public static void SetTransition(
this Page page,
Action<SimpleShellTransitionArgs> callback,
uint duration = 250,
Action<SimpleShellTransitionArgs> starting = null,
Action<SimpleShellTransitionArgs> finished = null,
bool destinationPageInFrontOnSwitching = true,
bool destinationPageInFrontOnPushing = true,
bool destinationPageInFrontOnPopping = true)

public static void SetTransition(
this Page page,
Action<SimpleShellTransitionArgs> callback,
Expand Down Expand Up @@ -108,8 +98,8 @@ public AppShell()
_ => true
},
duration: args => args.TransitionType switch {
SimpleShellTransitionType.Switching => 500,
_ => 350
SimpleShellTransitionType.Switching => 500u,
_ => 350u
});
}
```
Expand All @@ -128,15 +118,10 @@ public ImagePage()
InitializeComponent();

this.SetTransition(
callback: args =>
{
args.DestinationPage.Scale = args.Progress;
},
500,
finished: args =>
{
args.DestinationPage.Scale = 1;
});
callback: args => args.DestinationPage.Scale = args.Progress,
starting: args => args.DestinationPage.Scale = 0,
finished: args => args.DestinationPage.Scale = 1,
duration: args => 500u);
}
```

Expand All @@ -145,3 +130,27 @@ Output:
<p align="center">
<img width="350" src="../images/windows_transitions_2.gif">
</p>

### Combining transitions

Two transitions can be combined into one when you want to use different transitions under different conditions:

```csharp
public ImagePage()
{
InitializeComponent();

this.SetTransition(new SimpleShellTransition(
callback: args => args.DestinationPage.Scale = args.Progress,
starting: args => args.DestinationPage.Scale = 0,
finished: args => args.DestinationPage.Scale = 1,
duration: args => 500u)
.CombinedWith(
transition: SimpleShell.Current.GetTransition(),
when: args => args.TransitionType != SimpleShellTransitionType.Pushing));
}
```

`when` delegate determines when the second `transition` should be used.

> In this example, scale transition is used only when the the page is being pushed to the navigation stack, otherwise the default transition defined in shell is used.
62 changes: 30 additions & 32 deletions src/SimpleToolkit.Playground/PlaygroundAppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,37 @@ public PlaygroundAppShell()

Loaded += PlaygroundAppShellLoaded;

this.SetTransition(args =>
{
switch (args.TransitionType)
this.SetTransition(
callback: static args =>
{
case SimpleShellTransitionType.Switching:
args.OriginPage.Opacity = 1 - args.Progress;
args.DestinationPage.Opacity = args.Progress;
break;
case SimpleShellTransitionType.Pushing:
args.DestinationPage.Opacity = args.DestinationPage.Width < 0 ? 0 : 1;
#if IOS
// If I use just TranslationX, it is not applied on iOS
args.DestinationPage.Scale = 0.99 + (0.01 * args.Progress);
#endif
args.DestinationPage.TranslationX = (1 - args.Progress) * args.DestinationPage.Width;
break;
case SimpleShellTransitionType.Popping:
#if IOS
args.OriginPage.Scale = 0.99 + (0.01 * (1 - args.Progress));
#endif
args.OriginPage.TranslationX = args.Progress * args.OriginPage.Width;
break;
}
},
250,
finished: args =>
{
args.DestinationPage.TranslationX = 0;
args.OriginPage.TranslationX = 0;
args.OriginPage.Opacity = 1;
args.DestinationPage.Opacity = 1;
},
destinationPageInFrontOnPopping: false);
switch (args.TransitionType)
{
case SimpleShellTransitionType.Switching:
args.OriginPage.Opacity = 1 - args.Progress;
args.DestinationPage.Opacity = args.Progress;
break;
case SimpleShellTransitionType.Pushing:
args.DestinationPage.Opacity = args.DestinationPage.Width < 0 ? 0 : 1;
args.DestinationPage.TranslationX = (1 - args.Progress) * args.DestinationPage.Width;
break;
case SimpleShellTransitionType.Popping:
args.OriginPage.TranslationX = args.Progress * args.OriginPage.Width;
break;
}
},
duration: static args => 250u,
finished: static args =>
{
args.DestinationPage.TranslationX = 0;
args.OriginPage.TranslationX = 0;
args.OriginPage.Opacity = 1;
args.DestinationPage.Opacity = 1;
},
destinationPageInFront: static args => args.TransitionType switch
{
SimpleShellTransitionType.Popping => false,
_ => true
});
}

private void PlaygroundAppShellLoaded(object sender, EventArgs e)
Expand Down
12 changes: 6 additions & 6 deletions src/SimpleToolkit.Playground/SampleAppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public SampleAppShell()
Routing.RegisterRoute(nameof(ImagePage), typeof(ImagePage));

this.SetTransition(
callback: args =>
callback: static args =>
{
switch (args.TransitionType)
{
Expand All @@ -30,22 +30,22 @@ public SampleAppShell()
break;
}
},
finished: args =>
finished: static args =>
{
args.OriginPage.Opacity = 1;
args.OriginPage.TranslationX = 0;
args.DestinationPage.Opacity = 1;
args.DestinationPage.TranslationX = 0;
},
destinationPageInFront: args => args.TransitionType switch
destinationPageInFront: static args => args.TransitionType switch
{
SimpleShellTransitionType.Popping => false,
_ => true
},
duration: args => args.TransitionType switch
duration: static args => args.TransitionType switch
{
SimpleShellTransitionType.Switching => 500,
_ => 350
SimpleShellTransitionType.Switching => 500u,
_ => 350u
});

Loaded += PlaygroundAppShellLoaded;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

<ItemGroup Condition="'$(Configuration)'!='DEBUG'">
<PackageReference Include="SimpleToolkit.SimpleShell.Controls" Version="2.0.0-preview1" />
<PackageReference Include="SimpleToolkit.SimpleShell" Version="2.0.*" />
<PackageReference Include="SimpleToolkit.SimpleShell" Version="2.1.*" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='DEBUG'">
Expand Down
21 changes: 10 additions & 11 deletions src/SimpleToolkit.Playground/Views/Pages/ImagePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SimpleToolkit.SimpleShell.Extensions;
using SimpleToolkit.SimpleShell.Transitions;

namespace SimpleToolkit.SimpleShell.Playground.Views.Pages;

Expand All @@ -8,17 +9,15 @@ public ImagePage()
{
InitializeComponent();

this.SetTransition(
callback: args =>
{
args.DestinationPage.Scale = args.Progress;
},
500,
finished: args =>
{
args.DestinationPage.Scale = 1;
});
}
this.SetTransition(new SimpleShellTransition(
callback: static args => args.DestinationPage.Scale = args.Progress,
starting: static args => args.DestinationPage.Scale = 0,
finished: static args => args.DestinationPage.Scale = 1,
duration: static args => 500u)
.CombinedWith(
transition: SimpleShell.Current.GetTransition(),
when: static args => args.TransitionType != SimpleShellTransitionType.Pushing));
}

//protected override void OnNavigatedTo(NavigatedToEventArgs args)
//{
Expand Down
15 changes: 13 additions & 2 deletions src/SimpleToolkit.SimpleShell/Extensions/SimpleShellExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
using SimpleToolkit.SimpleShell.Transitions;
using static System.TimeZoneInfo;

namespace SimpleToolkit.SimpleShell.Extensions
{
public static class SimpleShellExtensions
{
/// <summary>
/// Sets the transition to the page.
/// Gets the page transition.
/// </summary>
/// <param name="page">Page with required transition.</param>
public static SimpleShellTransition GetTransition(this Page page)
{
return SimpleShell.GetTransition(page);
}

/// <summary>
/// Sets the page transition.
/// </summary>
/// <param name="page">Destination page of a navigation with this transition.</param>
/// <param name="transition">Transition of a navigation to the page.</param>
Expand All @@ -26,6 +34,8 @@ public static void SetTransition(this Page page, SimpleShellTransition transitio
/// <param name="destinationPageInFrontOnSwitching">Whether the destination page should be displayed in front of the origin page when switching root pages in the stack.</param>
/// <param name="destinationPageInFrontOnPushing">Whether the destination page should be displayed in front of the origin page when pushing new page to the stack.</param>
/// <param name="destinationPageInFrontOnPopping">Whether the destination page should be displayed in front of the origin page when popping existing page from the stack.</param>
[Obsolete("This extension method is deprecated. Please use other overload of this method.")]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static void SetTransition(
this Page page,
Action<SimpleShellTransitionArgs> callback,
Expand Down Expand Up @@ -94,6 +104,7 @@ public static void SetTransition(
}, duration, starting, finished, destinationPageInFront));
}


internal static IEnumerable<ShellSection> GetShellSections(this BaseShellItem baseShellItem)
{
var list = new HashSet<ShellSection>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using SimpleToolkit.SimpleShell.Transitions;

namespace SimpleToolkit.SimpleShell.Extensions
{
public static class SimpleShellTransitionExtensions
{
/// <summary>
/// Combines two page transitions.
/// </summary>
/// <param name="originalTransition">Default page transition</param>
/// <param name="transition">Page transition which should be combined with the default one</param>
/// <param name="when">When the second page transition should be used</param>
/// <returns>New page transition</returns>
public static SimpleShellTransition CombinedWith(
this SimpleShellTransition originalTransition,
SimpleShellTransition transition,
Func<SimpleShellTransitionArgs, bool> when)
{
return new SimpleShellTransition(
callback: args =>
{
if (when?.Invoke(args) ?? false)
transition?.Callback?.Invoke(args);
else
originalTransition?.Callback?.Invoke(args);
},
duration: args =>
{
if (when?.Invoke(args) ?? false)
return transition?.Duration?.Invoke(args) ?? SimpleShellTransition.DefaultDuration;
else
return originalTransition?.Duration?.Invoke(args) ?? SimpleShellTransition.DefaultDuration;
},
starting: args =>
{
if (when?.Invoke(args) ?? false)
transition?.Starting?.Invoke(args);
else
originalTransition?.Starting?.Invoke(args);
},
finished: args =>
{
if (when?.Invoke(args) ?? false)
transition?.Finished?.Invoke(args);
else
originalTransition?.Finished?.Invoke(args);
},
destinationPageInFront: args =>
{
if (when?.Invoke(args) ?? false)
return transition?.DestinationPageInFront?.Invoke(args) ?? false;
else
return originalTransition?.DestinationPageInFront?.Invoke(args) ?? false;
});
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public override void LayoutSubviews()

foreach (var subview in Subviews)
{
subview.Frame = Bounds;
// Only resize a subview when it does not match the size of the parent
if (subview.Bounds.Width != Bounds.Width || subview.Bounds.Height != Bounds.Height)
{
subview.Frame = new CoreGraphics.CGRect(subview.Frame.X, subview.Frame.Y, Bounds.Width, Bounds.Height);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ protected virtual void SyncNavigationStack(bool animated, NavigationRequestedEve
(VirtualView.CurrentItem as IShellContentController).GetOrCreateContent()
};

//LogStack(e, pageStack, VirtualView);
#if DEBUG
LogStack(e, pageStack, VirtualView);
#endif

if (e?.RequestType != NavigationRequestType.PopToRoot) // See https://github.com/dotnet/maui/pull/10653
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PackageTags>MAUI, Controls, Shell, Navigation, Transitions</PackageTags>
<Authors>RadekVyM</Authors>
<PackageOutputPath>..\..\packages</PackageOutputPath>
<VersionPrefix>2.0.1</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<Version>$(VersionPrefix)$(VersionSuffix)</Version>
<Description>Simplified implementation of .NET MAUI Shell that is part of SimpleToolkit library.</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public class SimpleShellTransition
/// <param name="destinationPageInFrontOnSwitching">Whether the destination page should be displayed in front of the origin page when switching root pages in the stack.</param>
/// <param name="destinationPageInFrontOnPushing">Whether the destination page should be displayed in front of the origin page when pushing new page to the stack.</param>
/// <param name="destinationPageInFrontOnPopping">Whether the destination page should be displayed in front of the origin page when popping existing page from the stack.</param>
[Obsolete("This constructor is deprecated. Please use other constructor.")]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public SimpleShellTransition(
Action<SimpleShellTransitionArgs> callback,
uint duration = DefaultDuration,
Expand All @@ -116,7 +118,7 @@ public SimpleShellTransition(
}

/// <summary>
/// Constructor that allows to dynamically set some properties of the transition.
/// Constructor that allows to dynamically set properties of the transition.
/// </summary>
/// <param name="callback">Callback that is called when progress of the transition changes.</param>
/// <param name="duration">Duration of the transition.</param>
Expand Down

0 comments on commit e4a5c26

Please sign in to comment.