From 57758ed975913c003de9ee198ffee97ccc5f0f6f Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Fri, 24 May 2024 15:15:29 +0200 Subject: [PATCH] Added MapUiTools.Avalonia --- MBTiles/WPF/MBTiles.WPF.csproj | 4 +- MBTiles/WinUI/MBTiles.WinUI.csproj | 8 +- MapProjections/WPF/MapProjections.WPF.csproj | 4 +- .../WinUI/MapProjections.WinUI.csproj | 8 +- .../Avalonia/MapUiTools.Avalonia.csproj | 33 +++++++ MapUiTools/Avalonia/MenuButton.Avalonia.cs | 93 +++++++++++++++++++ MapUiTools/Shared/MapLayersMenuButton.cs | 31 +++++-- MapUiTools/Shared/MapProjectionsMenuButton.cs | 18 +++- MapUiTools/WinUI/MapUiTools.WinUI.csproj | 6 +- MapUiTools/WinUI/MenuButton.WinUI.cs | 8 +- 10 files changed, 182 insertions(+), 31 deletions(-) create mode 100644 MapUiTools/Avalonia/MapUiTools.Avalonia.csproj create mode 100644 MapUiTools/Avalonia/MenuButton.Avalonia.cs diff --git a/MBTiles/WPF/MBTiles.WPF.csproj b/MBTiles/WPF/MBTiles.WPF.csproj index 23800f8a..e10bb1a3 100644 --- a/MBTiles/WPF/MBTiles.WPF.csproj +++ b/MBTiles/WPF/MBTiles.WPF.csproj @@ -25,10 +25,10 @@ - + - + diff --git a/MBTiles/WinUI/MBTiles.WinUI.csproj b/MBTiles/WinUI/MBTiles.WinUI.csproj index ed931555..4610086f 100644 --- a/MBTiles/WinUI/MBTiles.WinUI.csproj +++ b/MBTiles/WinUI/MBTiles.WinUI.csproj @@ -26,13 +26,13 @@ + + + + - - - - diff --git a/MapProjections/WPF/MapProjections.WPF.csproj b/MapProjections/WPF/MapProjections.WPF.csproj index 4f905b0e..bfbefa2e 100644 --- a/MapProjections/WPF/MapProjections.WPF.csproj +++ b/MapProjections/WPF/MapProjections.WPF.csproj @@ -29,10 +29,10 @@ - + - + diff --git a/MapProjections/WinUI/MapProjections.WinUI.csproj b/MapProjections/WinUI/MapProjections.WinUI.csproj index d4de18c2..98cb911c 100644 --- a/MapProjections/WinUI/MapProjections.WinUI.csproj +++ b/MapProjections/WinUI/MapProjections.WinUI.csproj @@ -26,13 +26,13 @@ + + + + - - - - diff --git a/MapUiTools/Avalonia/MapUiTools.Avalonia.csproj b/MapUiTools/Avalonia/MapUiTools.Avalonia.csproj new file mode 100644 index 00000000..56dcd03f --- /dev/null +++ b/MapUiTools/Avalonia/MapUiTools.Avalonia.csproj @@ -0,0 +1,33 @@ + + + net6.0 + disable + true + MapControl + XAML Map Control UI Tools Library for WPF + XAML Map Control + 10.0.0 + Clemens Fischer + Copyright © 2024 Clemens Fischer + true + ..\..\MapControl.snk + false + AVALONIA + + + + + + + + + + + + + + + + + + diff --git a/MapUiTools/Avalonia/MenuButton.Avalonia.cs b/MapUiTools/Avalonia/MenuButton.Avalonia.cs new file mode 100644 index 00000000..e0a2c1c2 --- /dev/null +++ b/MapUiTools/Avalonia/MenuButton.Avalonia.cs @@ -0,0 +1,93 @@ +// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control +// Copyright © 2024 Clemens Fischer +// Licensed under the Microsoft Public License (Ms-PL) + +using Avalonia; +using Avalonia.Controls; +using Avalonia.Interactivity; +using Avalonia.Media; +using Avalonia.Styling; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace MapControl +{ + public class ToggleMenuFlyoutItem : MenuItem + { + internal static readonly FontFamily SymbolFont = new FontFamily("Segoe MDL2 Assets"); + + private readonly StackPanel header; + private readonly TextBlock icon; + private bool isChecked; + + public ToggleMenuFlyoutItem(string text, object item, EventHandler click) + { + icon = new TextBlock + { + FontFamily = SymbolFont, + Width = 20, + VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center + }; + + header = new StackPanel { Orientation = Avalonia.Layout.Orientation.Horizontal }; + header.Children.Add(icon); + header.Children.Add(new TextBlock { Text = text }); + + Header = header; + Tag = item; + + Click += click; + } + + protected override Type StyleKeyOverride => typeof(MenuItem); + + public bool IsChecked + { + get => isChecked; + set + { + isChecked = value; + icon.Text = isChecked ? "\uE73E" : ""; // CheckMark + } + } + } + + public class MenuButton : Button + { + protected MenuButton(string icon) + { + var style = new Style(); + style.Setters.Add(new Setter(TextBlock.FontFamilyProperty, ToggleMenuFlyoutItem.SymbolFont)); + style.Setters.Add(new Setter(TextBlock.FontSizeProperty, 20d)); + style.Setters.Add(new Setter(PaddingProperty, new Thickness(8))); + Styles.Add(style); + + Content = icon; + } + + protected override Type StyleKeyOverride => typeof(Button); + + protected MenuFlyout CreateMenu() + { + var menu = new MenuFlyout(); + Flyout = menu; + return menu; + } + + protected IEnumerable GetMenuItems() + { + return ((MenuFlyout)Flyout).Items.OfType(); + } + + protected static MenuItem CreateMenuItem(string text, object item, EventHandler click) + { + return new ToggleMenuFlyoutItem(text, item, click); + } + + protected static Separator CreateSeparator() + { + return new Separator(); + } + } +} diff --git a/MapUiTools/Shared/MapLayersMenuButton.cs b/MapUiTools/Shared/MapLayersMenuButton.cs index ea457e32..c6644045 100644 --- a/MapUiTools/Shared/MapLayersMenuButton.cs +++ b/MapUiTools/Shared/MapLayersMenuButton.cs @@ -15,32 +15,42 @@ using Windows.UI.Xaml.Markup; #elif WINUI using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Markup; +#elif AVALONIA +using Avalonia.Interactivity; +using Avalonia.Metadata; +using DependencyProperty = Avalonia.AvaloniaProperty; +using FrameworkElement = Avalonia.Controls.Control; #endif namespace MapControl.UiTools { #if WPF [ContentProperty(nameof(Layer))] -#else +#elif UWP || WINUI [ContentProperty(Name = nameof(Layer))] #endif public class MapLayerItem { - public string Text { get; set; } - public UIElement Layer { get; set; } - public Func LayerFactory { get; set; } +#if AVALONIA + [Content] +#endif + public FrameworkElement Layer { get; set; } - public UIElement GetLayer() => Layer ?? (Layer = LayerFactory?.Invoke()); + public string Text { get; set; } + + public Func LayerFactory { get; set; } + + public FrameworkElement GetLayer() => Layer ?? (Layer = LayerFactory?.Invoke()); } #if WPF [ContentProperty(nameof(MapLayers))] -#else +#elif UWP || WINUI [ContentProperty(Name = nameof(MapLayers))] #endif public class MapLayersMenuButton : MenuButton { - private UIElement selectedLayer; + private FrameworkElement selectedLayer; public MapLayersMenuButton() : base("\uE81E") @@ -59,6 +69,9 @@ namespace MapControl.UiTools set => SetValue(MapProperty, value); } +#if AVALONIA + [Content] +#endif public Collection MapLayers { get; } = new ObservableCollection(); public Collection MapOverlays { get; } = new ObservableCollection(); @@ -112,7 +125,7 @@ namespace MapControl.UiTools ToggleMapOverlay(mapLayerItem.GetLayer()); } - private void SetMapLayer(UIElement layer) + private void SetMapLayer(FrameworkElement layer) { if (selectedLayer != layer) { @@ -123,7 +136,7 @@ namespace MapControl.UiTools UpdateCheckedStates(); } - private void ToggleMapOverlay(UIElement layer) + private void ToggleMapOverlay(FrameworkElement layer) { if (Map.Children.Contains(layer)) { diff --git a/MapUiTools/Shared/MapProjectionsMenuButton.cs b/MapUiTools/Shared/MapProjectionsMenuButton.cs index da193113..bb2301b5 100644 --- a/MapUiTools/Shared/MapProjectionsMenuButton.cs +++ b/MapUiTools/Shared/MapProjectionsMenuButton.cs @@ -14,24 +14,33 @@ using Windows.UI.Xaml.Markup; #elif WINUI using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Markup; +#elif AVALONIA +using Avalonia.Interactivity; +using Avalonia.Metadata; +using DependencyProperty = Avalonia.AvaloniaProperty; +using FrameworkElement = Avalonia.Controls.Control; #endif namespace MapControl.UiTools { #if WPF [ContentProperty(nameof(Projection))] -#else + #elif UWP || WINUI [ContentProperty(Name = nameof(Projection))] #endif public class MapProjectionItem { - public string Text { get; set; } +#if AVALONIA + [Content] +#endif public string Projection { get; set; } + + public string Text { get; set; } } #if WPF [ContentProperty(nameof(MapProjections))] -#else +#elif UWP || WINUI [ContentProperty(Name = nameof(MapProjections))] #endif public class MapProjectionsMenuButton : MenuButton @@ -54,6 +63,9 @@ namespace MapControl.UiTools set => SetValue(MapProperty, value); } +#if AVALONIA + [Content] +#endif public Collection MapProjections { get; } = new ObservableCollection(); private void InitializeMenu() diff --git a/MapUiTools/WinUI/MapUiTools.WinUI.csproj b/MapUiTools/WinUI/MapUiTools.WinUI.csproj index 0f3ebe8f..7e69df47 100644 --- a/MapUiTools/WinUI/MapUiTools.WinUI.csproj +++ b/MapUiTools/WinUI/MapUiTools.WinUI.csproj @@ -25,11 +25,11 @@ - - + - + + diff --git a/MapUiTools/WinUI/MenuButton.WinUI.cs b/MapUiTools/WinUI/MenuButton.WinUI.cs index 84395102..999d1650 100644 --- a/MapUiTools/WinUI/MenuButton.WinUI.cs +++ b/MapUiTools/WinUI/MenuButton.WinUI.cs @@ -4,12 +4,12 @@ using System.Collections.Generic; using System.Linq; -#if WINUI -using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Controls; -#elif UWP +#if UWP using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +#elif WINUI +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; #endif namespace MapControl.UiTools