Skip to content

Commit

Permalink
feat: Support ContextFlyout (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper authored Nov 10, 2024
1 parent 5b310be commit cd9430f
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 135 deletions.
12 changes: 9 additions & 3 deletions samples/ControlGallery/AppShell.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@using ControlGallery.Views.PresentData
@using ControlGallery.Views.SetValues
@using ControlGallery.Views.Shell
@using ControlGallery.Views.Menus

<Shell BackgroundColor="_backgroundColor"
ForegroundColor="_foregroundColor"
Expand Down Expand Up @@ -62,9 +63,9 @@
<ShellContent Title="Shell Properties">
<ContentTemplate>
<ShellPropertiesPage @bind-ShellBackgroundColor="_backgroundColor"
@bind-ShellForegroundColor="_foregroundColor"
@bind-ShellUnselectedColor="_unselectedColor"
@bind-ShellTabBarTitleColor="_tabBarTitleColor" />
@bind-ShellForegroundColor="_foregroundColor"
@bind-ShellUnselectedColor="_unselectedColor"
@bind-ShellTabBarTitleColor="_tabBarTitleColor" />
</ContentTemplate>
</ShellContent>

Expand All @@ -81,6 +82,11 @@
<GestureEvents />
<GestureRecognizersPage />
<ErrorBoundariesPage />

@if (DeviceInfo.Platform == DevicePlatform.WinUI || DeviceInfo.Platform == DevicePlatform.MacCatalyst)
{
<ContextMenuPage />
}
</Shell>

@code {
Expand Down
30 changes: 30 additions & 0 deletions samples/ControlGallery/Views/Menus/ContextMenuPage.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<ContentPage Title="Context Menu" BackgroundColor="_selectedColor">
<VerticalStackLayout>
<Label Text="Right click to open context menu">
<ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Black" OnClick="() => SelectColor(Colors.Black)" />

<MenuFlyoutSubItem Text="Light">
<MenuFlyoutItem Text="Blue" OnClick="() => SelectColor(Colors.LightBlue)" />
<MenuFlyoutItem Text="Coral" OnClick="() => SelectColor(Colors.LightCoral)" />
<MenuFlyoutItem Text="Cyan" OnClick="() => SelectColor(Colors.LightCyan)" />
</MenuFlyoutSubItem>

<MenuFlyoutSubItem Text="Dark">
<MenuFlyoutItem Text="Blue" OnClick="() => SelectColor(Colors.DarkBlue)" />
<MenuFlyoutItem Text="DarkMagenta" OnClick="() => SelectColor(Colors.DarkMagenta)" />
<MenuFlyoutItem Text="Cyan" OnClick="() => SelectColor(Colors.DarkCyan)" />
</MenuFlyoutSubItem>
</MenuFlyout>
</ContextFlyout>
</Label>
</VerticalStackLayout>

</ContentPage>

@code {
Color? _selectedColor = null;

void SelectColor(Color color) => _selectedColor = color;
}

This file was deleted.

28 changes: 28 additions & 0 deletions src/BlazorBindings.Maui/Elements/Element.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Components.Rendering;
using MC = Microsoft.Maui.Controls;

namespace BlazorBindings.Maui.Elements;

public partial class Element
{
[Parameter] public RenderFragment ContextFlyout { get; set; }

protected override bool HandleAdditionalParameter(string name, object value)
{
if (name == nameof(ContextFlyout))
{
ContextFlyout = (RenderFragment)value;
return true;
}

return base.HandleAdditionalParameter(name, value);
}

protected override void RenderAdditionalPartialElementContent(RenderTreeBuilder builder, ref int sequence)
{
base.RenderAdditionalPartialElementContent(builder, ref sequence);

RenderTreeBuilderHelper.AddContentProperty<MC.Element>(builder, sequence++, ContextFlyout, (x, value) =>
MC.FlyoutBase.SetContextFlyout(x, (MC.FlyoutBase)value));
}
}
30 changes: 30 additions & 0 deletions src/BlazorBindings.Maui/Elements/FlyoutBase.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

using BlazorBindings.Core;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;

#pragma warning disable MBB001

namespace BlazorBindings.Maui.Elements
{
public abstract partial class FlyoutBase : Element
{
static FlyoutBase()
{
RegisterAdditionalHandlers();
}

public new MC.FlyoutBase NativeControl => (MC.FlyoutBase)((BindableObject)this).NativeControl;



static partial void RegisterAdditionalHandlers();
}
}
68 changes: 68 additions & 0 deletions src/BlazorBindings.Maui/Elements/KeyboardAccelerator.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

using BlazorBindings.Core;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using Microsoft.Maui;
using System.Threading.Tasks;

#pragma warning disable MBB001

namespace BlazorBindings.Maui.Elements
{
/// <summary>
/// Represents a shortcut key for a <see cref="T:Microsoft.Maui.Controls.MenuFlyoutItem" />.
/// </summary>
public partial class KeyboardAccelerator : BindableObject
{
static KeyboardAccelerator()
{
RegisterAdditionalHandlers();
}

/// <summary>
/// Identifies the key for the keyboard accelerator.
/// </summary>
[Parameter] public string Key { get; set; }
/// <summary>
/// Identifies the modifiers for the keyboard accelerator.
/// </summary>
[Parameter] public KeyboardAcceleratorModifiers? Modifiers { get; set; }

public new MC.KeyboardAccelerator NativeControl => (MC.KeyboardAccelerator)((BindableObject)this).NativeControl;

protected override MC.KeyboardAccelerator CreateNativeElement() => new();

protected override void HandleParameter(string name, object value)
{
switch (name)
{
case nameof(Key):
if (!Equals(Key, value))
{
Key = (string)value;
NativeControl.Key = Key;
}
break;
case nameof(Modifiers):
if (!Equals(Modifiers, value))
{
Modifiers = (KeyboardAcceleratorModifiers?)value;
NativeControl.Modifiers = Modifiers ?? (KeyboardAcceleratorModifiers)MC.KeyboardAccelerator.ModifiersProperty.DefaultValue;
}
break;

default:
base.HandleParameter(name, value);
break;
}
}

static partial void RegisterAdditionalHandlers();
}
}
54 changes: 54 additions & 0 deletions src/BlazorBindings.Maui/Elements/MenuFlyout.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

using BlazorBindings.Core;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.Maui;
using System.Threading.Tasks;

#pragma warning disable MBB001

namespace BlazorBindings.Maui.Elements
{
public partial class MenuFlyout : FlyoutBase
{
static MenuFlyout()
{
RegisterAdditionalHandlers();
}

[Parameter] public RenderFragment ChildContent { get; set; }

public new MC.MenuFlyout NativeControl => (MC.MenuFlyout)((BindableObject)this).NativeControl;

protected override MC.MenuFlyout CreateNativeElement() => new();

protected override void HandleParameter(string name, object value)
{
switch (name)
{
case nameof(ChildContent):
ChildContent = (RenderFragment)value;
break;

default:
base.HandleParameter(name, value);
break;
}
}

protected override void RenderAdditionalElementContent(RenderTreeBuilder builder, ref int sequence)
{
base.RenderAdditionalElementContent(builder, ref sequence);
RenderTreeBuilderHelper.AddListContentProperty<MC.MenuFlyout, IMenuElement>(builder, sequence++, ChildContent, x => x);
}

static partial void RegisterAdditionalHandlers();
}
}
Loading

0 comments on commit cd9430f

Please sign in to comment.