Skip to content

Commit

Permalink
Back button source generator
Browse files Browse the repository at this point in the history
  • Loading branch information
BurkusCat committed Nov 11, 2023
1 parent b9b6dc6 commit f4611aa
Show file tree
Hide file tree
Showing 37 changed files with 161 additions and 101 deletions.
38 changes: 17 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,26 +355,6 @@ Several parameter keys have been pre-defined and are using by the `Burkus.Mvvm.M

The `NavigationParameters` object exposes some handy properties `.UseAnimatedNavigation` and `.UseModalNavigation` so you can easily set or check the value of these properties.

### Handling back button presses
By default, back button presses on Android/Windows will bypass `Burkus.Mvvm.Maui` which would mean lifecycle events and parameter passing may not happen when you expect them to. You can allow `Burkus.Mvvm.Maui` to handle the back button navigation for a page by turning it into a `Burkus...Page`. For example, below a `ContentPage` is turned into a `BurkusContentPage`.

``` xml
<burkus:BurkusContentPage
...
xmlns:burkus="http://burkus.co.uk"
...>
```

``` csharp
public partial class HomePage : BurkusContentPage
```

The page types available are:
- `BurkusContentPage`
- `BurkusNavigationPage`
- `BurkusTabbedPage`
- `BurkusFlyoutPage`

## Dialog service
`IDialogService` is automatically registered by `.UseBurkusMvvm(...)`. It is a testable service that is an abstraction over [the MAUI alerts/pop-ups/prompts/action sheets](https://learn.microsoft.com/en-us/dotnet/maui/user-interface/pop-ups).
Expand All @@ -401,14 +381,30 @@ dialogService.DisplayAlert(
See the [IDialogService interface in the repository](https://github.com/BurkusCat/Burkus.Mvvm.Maui/blob/main/src/Abstractions/IDialogService.cs) for all the possible method options.
## Advanced / complexities
The below are some things of note that may help prevent issues from arising:
Below are some things of note that may help prevent issues from arising:
- The `MainPage` of the app will be automatically set to a `NavigationPage`. This means the first page you push can be a `ContentPage` rather than needing to push a `NavigationPage`. This may change in the future.
- A source generator will automatically add code overriding `Window CreateWindow(IActivationState? activationState)` in your `App.xaml.cs` class.
- Adding this package to a project will automatically import the `Burkus.Mvvm.Maui` namespace globally if you have [`ImplicitUsings`](https://devblogs.microsoft.com/dotnet/welcome-to-csharp-10/#implicit-usings) enabled in your project. You can opt out of this by including the following in your `.csproj` file:
``` xml
<Using Remove="Burkus.Mvvm.Maui" />
```

### Handling back button presses
A source generator will automatically override `bool OnBackButtonPressed()` for every `ContentPage`, `FlyoutPage`, `TabbedPage`, and `NavigationPage`. This generated source code allows `Burkus.Mvvm.Maui` to handle back button presses on Android & Windows.

To disable this for a particular page, annotate the page with the `[DisableBackButtonNavigator]` attribute like this:
```csharp
[DisableBackButtonNavigator]
public partial class FlyoutMenuPage : ContentPage
{
...
```

The source generator calls the following code, which may be useful if you need to create some custom `OnBackButtonPressed` logic:
``` csharp
return BackButtonNavigator.HandleBackButtonPressed();
```

# Roadmap 🛣️
- [View and viewmodel auto-registration](https://github.com/BurkusCat/Burkus.Mvvm.Maui/issues/4)
- [Popup pages](https://github.com/BurkusCat/Burkus.Mvvm.Maui/issues/2)
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/ChangeUsernamePage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.ChangeUsernamePage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -37,4 +37,4 @@
Text="{x:Static properties:Resources.Button_Finish}" />
</Grid>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/ChangeUsernamePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class ChangeUsernamePage : BurkusContentPage
public partial class ChangeUsernamePage : ContentPage
{
public ChangeUsernamePage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Flyouts/ContactsPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.ContactsPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -39,4 +39,4 @@
Text="{x:Static properties:Resources.Home_Button_TabbedPageDemo}" />
</VerticalStackLayout>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Flyouts/ContactsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class ContactsPage : BurkusContentPage
public partial class ContactsPage : ContentPage
{
public ContactsPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Flyouts/DemoFlyoutPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusFlyoutPage
<FlyoutPage
x:Class="DemoApp.Views.DemoFlyoutPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand All @@ -21,4 +21,4 @@
</x:Arguments>
</NavigationPage>
</FlyoutPage.Detail>
</burkus:BurkusFlyoutPage>
</FlyoutPage>
3 changes: 2 additions & 1 deletion samples/DemoApp/Views/Flyouts/DemoFlyoutPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace DemoApp.Views;

public partial class DemoFlyoutPage : BurkusFlyoutPage
[DisableBackButtonNavigator]
public partial class DemoFlyoutPage : FlyoutPage
{
public DemoFlyoutPage()
{
Expand Down
1 change: 1 addition & 0 deletions samples/DemoApp/Views/Flyouts/FlyoutMenuPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace DemoApp.Views;

[DisableBackButtonNavigator]
public partial class FlyoutMenuPage : ContentPage
{
public FlyoutMenuPage()
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Flyouts/RemindersPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.RemindersPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -28,4 +28,4 @@
</VerticalStackLayout>
</ScrollView>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Flyouts/RemindersPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class RemindersPage : BurkusContentPage
public partial class RemindersPage : ContentPage
{
public RemindersPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Flyouts/TodoPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.TodoPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand All @@ -26,4 +26,4 @@
Text="{x:Static properties:Resources.Todo_Button_PushNewPageIntoDetail}" />
</VerticalStackLayout>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Flyouts/TodoPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class TodoPage : BurkusContentPage
public partial class TodoPage : ContentPage
{
public TodoPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/HomePage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.HomePage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -72,4 +72,4 @@
Text="{x:Static properties:Resources.Home_Button_Exit}" />
</VerticalStackLayout>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/HomePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class HomePage : BurkusContentPage
public partial class HomePage : ContentPage
{
public HomePage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/LoginPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.LoginPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -54,4 +54,4 @@
</Label>
</Grid>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/LoginPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class LoginPage : BurkusContentPage
public partial class LoginPage : ContentPage
{
public LoginPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/RegisterPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.RegisterPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -30,4 +30,4 @@
Text="{x:Static properties:Resources.Button_GoBack}" />
</Grid>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/RegisterPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class RegisterPage : BurkusContentPage
public partial class RegisterPage : ContentPage
{
public RegisterPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Tabs/AlphaTabPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.AlphaTabPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -35,4 +35,4 @@
Text="{x:Static properties:Resources.Button_GoBack}" />
</Grid>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Tabs/AlphaTabPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class AlphaTabPage : BurkusContentPage
public partial class AlphaTabPage : ContentPage
{
public AlphaTabPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Tabs/BetaTabPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.BetaTabPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -35,4 +35,4 @@
Text="{x:Static properties:Resources.Button_GoBack}" />
</Grid>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Tabs/BetaTabPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class BetaTabPage : BurkusContentPage
public partial class BetaTabPage : ContentPage
{
public BetaTabPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Tabs/CharlieTabPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.CharlieTabPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -35,4 +35,4 @@
Text="{x:Static properties:Resources.Button_GoBack}" />
</Grid>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Tabs/CharlieTabPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class CharlieTabPage : BurkusContentPage
public partial class CharlieTabPage : ContentPage
{
public CharlieTabPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Tabs/DemoTabsPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusTabbedPage
<TabbedPage
x:Class="DemoApp.Views.DemoTabsPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand All @@ -13,4 +13,4 @@
<views:AlphaTabPage Title="Alpha" />
<views:BetaTabPage Title="Beta" />
<views:CharlieTabPage Title="Charlie" />
</burkus:BurkusTabbedPage>
</TabbedPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Tabs/DemoTabsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class DemoTabsPage : BurkusTabbedPage
public partial class DemoTabsPage : TabbedPage
{
public DemoTabsPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/UriTestPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<burkus:BurkusContentPage
<ContentPage
x:Class="DemoApp.Views.UriTestPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -48,4 +48,4 @@
Text="{x:Static properties:Resources.UriTest_Button_SwitchToChangeUsername}" />
</VerticalStackLayout>
</ScrollView>
</burkus:BurkusContentPage>
</ContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/UriTestPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class UriTestPage : BurkusContentPage
public partial class UriTestPage : ContentPage
{
public UriTestPage()
{
Expand Down
5 changes: 2 additions & 3 deletions src/Burkus.Mvvm.Maui.SourceGenerators/AppSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Burkus.Mvvm.Maui;

[Generator]
public class AppSourceGenerator : ISourceGenerator
internal class AppSourceGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context)
{
Expand All @@ -33,7 +33,6 @@ public void Execute(GeneratorExecutionContext context)
}

// get the MAUI program we are running in

var assembly = context.Compilation.Assembly;
var mauiProgramName = $"{assembly.Name}.MauiProgram";
var mauiProgram = context.Compilation
Expand All @@ -54,7 +53,7 @@ public void Execute(GeneratorExecutionContext context)
namespace {mauiProgram.ContainingNamespace.ToDisplayString()};
public partial class App : Application
partial class App
{{
protected override Window CreateWindow(IActivationState? activationState)
{{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.7.0" />
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
</Project>
Loading

0 comments on commit f4611aa

Please sign in to comment.