Skip to content

Commit

Permalink
feat: Support MenuBar (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper authored Nov 10, 2024
1 parent cd9430f commit 3644af3
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
1 change: 1 addition & 0 deletions samples/ControlGallery/AppShell.razor
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
@if (DeviceInfo.Platform == DevicePlatform.WinUI || DeviceInfo.Platform == DevicePlatform.MacCatalyst)
{
<ContextMenuPage />
<MenuBarPage />
}
</Shell>

Expand Down
57 changes: 57 additions & 0 deletions samples/ControlGallery/Views/Menus/MenuBarPage.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<ContentPage Title="Menu Bar">
<MenuBarItems>
<MenuBarItem Text="File">
<MenuFlyoutItem Text="Save" OnClick="Save">
<KeyboardAccelerators>
<KeyboardAccelerator Key="S" Modifiers="KeyboardAcceleratorModifiers.Ctrl" />
</KeyboardAccelerators>
</MenuFlyoutItem>

<MenuFlyoutItem Text="Exit" OnClick="Exit">
<KeyboardAccelerators>
<KeyboardAccelerator Key="Q" Modifiers="KeyboardAcceleratorModifiers.Ctrl" />
</KeyboardAccelerators>
</MenuFlyoutItem>
</MenuBarItem>

<MenuBarItem Text="Update">
<MenuFlyoutSubItem Text="Set Value">
<MenuFlyoutItem Text="1" OnClick="() => SetValue(1)" />
<MenuFlyoutItem Text="2" OnClick="() => SetValue(2)" />
<MenuFlyoutItem Text="3" OnClick="() => SetValue(3)" />
</MenuFlyoutSubItem>
</MenuBarItem>

</MenuBarItems>

<ChildContent>
<VerticalStackLayout>
<Label>Selected value: @value</Label>
@if (_saved)
{
<Label>Values are saved.</Label>
}
</VerticalStackLayout>
</ChildContent>
</ContentPage>

@code {
bool _saved;
int? value;

void SetValue(int i)
{
value = i;
_saved = false;
}

void Save()
{
_saved = true;
}

void Exit()
{
Application.Current.Quit();
}
}
62 changes: 62 additions & 0 deletions src/BlazorBindings.Maui/Elements/MenuBar.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// <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 MenuBar : Element
{
static MenuBar()
{
RegisterAdditionalHandlers();
}

[Parameter] public bool? IsEnabled { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }

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

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

protected override void HandleParameter(string name, object value)
{
switch (name)
{
case nameof(IsEnabled):
if (!Equals(IsEnabled, value))
{
IsEnabled = (bool?)value;
NativeControl.IsEnabled = IsEnabled ?? (bool)MC.MenuBar.IsEnabledProperty.DefaultValue;
}
break;
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.MenuBar, IMenuBarItem>(builder, sequence++, ChildContent, x => x);
}

static partial void RegisterAdditionalHandlers();
}
}
78 changes: 78 additions & 0 deletions src/BlazorBindings.Maui/Elements/MenuBarItem.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// <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 MenuBarItem : BaseMenuItem
{
static MenuBarItem()
{
RegisterAdditionalHandlers();
}

[Parameter] public bool? IsEnabled { get; set; }
[Parameter] public int? Priority { get; set; }
[Parameter] public string Text { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }

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

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

protected override void HandleParameter(string name, object value)
{
switch (name)
{
case nameof(IsEnabled):
if (!Equals(IsEnabled, value))
{
IsEnabled = (bool?)value;
NativeControl.IsEnabled = IsEnabled ?? (bool)MC.MenuBarItem.IsEnabledProperty.DefaultValue;
}
break;
case nameof(Priority):
if (!Equals(Priority, value))
{
Priority = (int?)value;
NativeControl.Priority = Priority ?? default;
}
break;
case nameof(Text):
if (!Equals(Text, value))
{
Text = (string)value;
NativeControl.Text = Text;
}
break;
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.MenuBarItem, IMenuElement>(builder, sequence++, ChildContent, x => x);
}

static partial void RegisterAdditionalHandlers();
}
}
2 changes: 2 additions & 0 deletions src/BlazorBindings.Maui/Properties/AttributeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@

[assembly: GenerateComponent(typeof(FlyoutBase))]
[assembly: GenerateComponent(typeof(KeyboardAccelerator))]
[assembly: GenerateComponent(typeof(MenuBar))]
[assembly: GenerateComponent(typeof(MenuBarItem))]
[assembly: GenerateComponent(typeof(MenuFlyout))]
[assembly: GenerateComponent(typeof(MenuFlyoutItem))]
[assembly: GenerateComponent(typeof(MenuFlyoutSeparator))]
Expand Down

0 comments on commit 3644af3

Please sign in to comment.