Skip to content

Commit

Permalink
Merge pull request #133 from FmgLib/132-keyboardaccelerator-support
Browse files Browse the repository at this point in the history
132 keyboardaccelerator support
  • Loading branch information
gonultasmf authored Sep 27, 2024
2 parents f2353a2 + 35a2786 commit 91b1a89
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 18 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,6 @@ new Grid()
.Text("Paste")
.OnClicked(e => Console.WriteLine("Paste")),
new MenuFlyoutSubItem()
.Text("Background color")
{
new MenuFlyoutItem()
.Text("Blue")
Expand All @@ -585,6 +584,7 @@ new Grid()
.Text("Black")
.OnClicked(e => grid.BackgroundColor = Colors.Black)
}
.Text("Background color")
}
)
)
Expand All @@ -602,30 +602,40 @@ public class MenuPage : ContentPage
this.MenuBarItems(new MenuBarItem[]
{
new MenuBarItem()
.Text("My Menu")
{
new MenuFlyoutItem()
.Text("Exit")
.OnClicked(e => Application.Current.Quit()),
},
}
.Text("My Menu"),
new MenuBarItem()
.Text("Edit")
{
new MenuFlyoutItem()
.Text("Copy")
.OnClicked(e => Console.WriteLine("Copy")),
.OnClicked(e => Console.WriteLine("Copy"))
.KeyboardAccelerators(
new KeyboardAccelerator()
.Key("C")
.Modifiers(KeyboardAcceleratorModifiers.Ctrl)
),
new MenuFlyoutItem()
.Text("Paste")
.OnClicked(e => Console.WriteLine("Paste")),
},
.OnClicked(e => Console.WriteLine("Paste"))
.KeyboardAccelerators(
new KeyboardAccelerator()
.Key("V")
.Modifiers(KeyboardAcceleratorModifiers.Ctrl)
),
}
.Text("Edit"),
new MenuBarItem()
.Text("Theme")
{
new MenuFlyoutItem()
.Text("Blue")
.OnClicked(e => this.BackgroundColor = Colors.Blue),
...
}
.Text("Theme")
});

...
Expand Down
2 changes: 1 addition & 1 deletion sample/GeneratedExam/MyClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public class MyBarcodeGeneratorView { }
public class MyCameraView { }


[MauiMarkup(typeof(SkiaSharp.Extended.UI.Controls.SKLottieView), typeof(ZXing.Net.Maui.Controls.CameraBarcodeReaderView))]
[MauiMarkup(typeof(SkiaSharp.Extended.UI.Controls.SKLottieView), typeof(ZXing.Net.Maui.Controls.CameraBarcodeReaderView),typeof(KeyboardAccelerator))]
public class MySkLottieView { }
88 changes: 88 additions & 0 deletions src/FmgLib.MauiMarkup/Extensions/KeyboardAcceleratorExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace FmgLib.MauiMarkup;

public static partial class KeyboardAcceleratorExtension
{
public static T Key<T>(this T self,
string key)
where T : Microsoft.Maui.Controls.KeyboardAccelerator
{
self.SetValue(Microsoft.Maui.Controls.KeyboardAccelerator.KeyProperty, key);
return self;
}

public static T Key<T>(this T self,
Func<string> configure)
where T : Microsoft.Maui.Controls.KeyboardAccelerator
{
var key = configure();
self.SetValue(Microsoft.Maui.Controls.KeyboardAccelerator.KeyProperty, key);
return self;
}

public static T Key<T>(this T self, Func<PropertyContext<string>, IPropertyBuilder<string>> configure)
where T : Microsoft.Maui.Controls.KeyboardAccelerator
{
var context = new PropertyContext<string>(self, Microsoft.Maui.Controls.KeyboardAccelerator.KeyProperty);
configure(context).Build();
return self;
}

public static SettersContext<T> Key<T>(this SettersContext<T> self,
string key)
where T : Microsoft.Maui.Controls.KeyboardAccelerator
{
self.XamlSetters.Add(new Setter { Property = Microsoft.Maui.Controls.KeyboardAccelerator.KeyProperty, Value = key });
return self;
}

public static SettersContext<T> Key<T>(this SettersContext<T> self, Func<PropertySettersContext<string>, IPropertySettersBuilder<string>> configure)
where T : Microsoft.Maui.Controls.KeyboardAccelerator
{
var context = new PropertySettersContext<string>(self.XamlSetters, Microsoft.Maui.Controls.KeyboardAccelerator.KeyProperty);
configure(context).Build();
return self;
}

public static T Modifiers<T>(this T self,
Microsoft.Maui.KeyboardAcceleratorModifiers modifiers)
where T : Microsoft.Maui.Controls.KeyboardAccelerator
{
self.SetValue(Microsoft.Maui.Controls.KeyboardAccelerator.ModifiersProperty, modifiers);
return self;
}

public static T Modifiers<T>(this T self,
Func<Microsoft.Maui.KeyboardAcceleratorModifiers> configure)
where T : Microsoft.Maui.Controls.KeyboardAccelerator
{
var modifiers = configure();
self.SetValue(Microsoft.Maui.Controls.KeyboardAccelerator.ModifiersProperty, modifiers);
return self;
}

public static T Modifiers<T>(this T self, Func<PropertyContext<Microsoft.Maui.KeyboardAcceleratorModifiers>, IPropertyBuilder<Microsoft.Maui.KeyboardAcceleratorModifiers>> configure)
where T : Microsoft.Maui.Controls.KeyboardAccelerator
{
var context = new PropertyContext<Microsoft.Maui.KeyboardAcceleratorModifiers>(self, Microsoft.Maui.Controls.KeyboardAccelerator.ModifiersProperty);
configure(context).Build();
return self;
}

public static SettersContext<T> Modifiers<T>(this SettersContext<T> self,
Microsoft.Maui.KeyboardAcceleratorModifiers modifiers)
where T : Microsoft.Maui.Controls.KeyboardAccelerator
{
self.XamlSetters.Add(new Setter { Property = Microsoft.Maui.Controls.KeyboardAccelerator.ModifiersProperty, Value = modifiers });
return self;
}

public static SettersContext<T> Modifiers<T>(this SettersContext<T> self, Func<PropertySettersContext<Microsoft.Maui.KeyboardAcceleratorModifiers>, IPropertySettersBuilder<Microsoft.Maui.KeyboardAcceleratorModifiers>> configure)
where T : Microsoft.Maui.Controls.KeyboardAccelerator
{
var context = new PropertySettersContext<Microsoft.Maui.KeyboardAcceleratorModifiers>(self.XamlSetters, Microsoft.Maui.Controls.KeyboardAccelerator.ModifiersProperty);
configure(context).Build();
return self;
}

}

33 changes: 33 additions & 0 deletions src/FmgLib.MauiMarkup/Extensions/MenuFlyoutItemExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace FmgLib.MauiMarkup;

public static partial class MenuFlyoutItemExtension
{
public static T KeyboardAccelerators<T>(this T self,
IList<Microsoft.Maui.Controls.KeyboardAccelerator> keyboardAccelerators)
where T : Microsoft.Maui.Controls.MenuFlyoutItem
{
foreach (var item in keyboardAccelerators)
self.KeyboardAccelerators.Add(item);
return self;
}

public static T KeyboardAccelerators<T>(this T self,
params Microsoft.Maui.Controls.KeyboardAccelerator[] keyboardAccelerators)
where T : Microsoft.Maui.Controls.MenuFlyoutItem
{
foreach (var item in keyboardAccelerators)
self.KeyboardAccelerators.Add(item);
return self;
}

public static T KeyboardAccelerators<T>(this T self,
Func<Microsoft.Maui.Controls.KeyboardAccelerator[]> configure)
where T : Microsoft.Maui.Controls.MenuFlyoutItem
{
var keyboardAccelerators = configure();
foreach (var item in keyboardAccelerators)
self.KeyboardAccelerators.Add(item);
return self;
}

}
26 changes: 18 additions & 8 deletions src/FmgLib.MauiMarkup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,6 @@ new Grid()
.Text("Paste")
.OnClicked(e => Console.WriteLine("Paste")),
new MenuFlyoutSubItem()
.Text("Background color")
{
new MenuFlyoutItem()
.Text("Blue")
Expand All @@ -585,6 +584,7 @@ new Grid()
.Text("Black")
.OnClicked(e => grid.BackgroundColor = Colors.Black)
}
.Text("Background color")
}
)
)
Expand All @@ -602,30 +602,40 @@ public class MenuPage : ContentPage
this.MenuBarItems(new MenuBarItem[]
{
new MenuBarItem()
.Text("My Menu")
{
new MenuFlyoutItem()
.Text("Exit")
.OnClicked(e => Application.Current.Quit()),
},
}
.Text("My Menu"),
new MenuBarItem()
.Text("Edit")
{
new MenuFlyoutItem()
.Text("Copy")
.OnClicked(e => Console.WriteLine("Copy")),
.OnClicked(e => Console.WriteLine("Copy"))
.KeyboardAccelerators(
new KeyboardAccelerator()
.Key("C")
.Modifiers(KeyboardAcceleratorModifiers.Ctrl)
),
new MenuFlyoutItem()
.Text("Paste")
.OnClicked(e => Console.WriteLine("Paste")),
},
.OnClicked(e => Console.WriteLine("Paste"))
.KeyboardAccelerators(
new KeyboardAccelerator()
.Key("V")
.Modifiers(KeyboardAcceleratorModifiers.Ctrl)
),
}
.Text("Edit"),
new MenuBarItem()
.Text("Theme")
{
new MenuFlyoutItem()
.Text("Blue")
.OnClicked(e => this.BackgroundColor = Colors.Blue),
...
}
.Text("Theme")
});

...
Expand Down
3 changes: 2 additions & 1 deletion tests/FmgLib.MauiMarkup.Generator.Test/IntegrationTesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ namespace ConsoleApp1;
//[MauiMarkup(typeof(SwipeItem), typeof(DateEdit), typeof(CalendarHeaderAppearance), typeof(CalendarDayCellAppearance), typeof(CalendarSelectableCellAppearance))]
//[MauiMarkup(typeof(DateEditActualAppearance), typeof(MultilineEdit), typeof(SimpleButton), typeof(DXStackLayout), typeof(DXLayoutBase))]
//[MauiMarkup(typeof(CardsView), typeof(VisualStateGroup))]
[MauiMarkup(typeof(SKLottieView),typeof(SKFileLottieImageSource), typeof(SKAnimatedSurfaceView))]
//[MauiMarkup(typeof(SKLottieView),typeof(SKFileLottieImageSource), typeof(SKAnimatedSurfaceView))]
[MauiMarkup(typeof(KeyboardAccelerator))]
public class FileName
{ }";

Expand Down

0 comments on commit 91b1a89

Please sign in to comment.