XAML-Map-Control/MapUiTools/Shared/MapProjectionsMenuButton.cs

119 lines
3.3 KiB
C#
Raw Normal View History

2023-01-03 15:12:53 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
2022-01-11 19:42:12 +01:00
// Licensed under the Microsoft Public License (Ms-PL)
2022-01-13 20:12:49 +01:00
using System.Collections.ObjectModel;
using System.Collections.Specialized;
2021-12-05 17:16:14 +01:00
using System.Linq;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Markup;
2021-12-05 17:16:14 +01:00
#elif UWP
using Windows.UI.Xaml;
2022-01-13 20:12:49 +01:00
using Windows.UI.Xaml.Markup;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Markup;
2024-05-24 15:15:29 +02:00
#elif AVALONIA
using Avalonia.Interactivity;
using Avalonia.Metadata;
using DependencyProperty = Avalonia.AvaloniaProperty;
using FrameworkElement = Avalonia.Controls.Control;
2021-12-05 17:16:14 +01:00
#endif
2022-01-11 19:42:12 +01:00
namespace MapControl.UiTools
2021-12-05 17:16:14 +01:00
{
2024-05-22 11:25:32 +02:00
#if WPF
2022-01-13 20:12:49 +01:00
[ContentProperty(nameof(Projection))]
2024-05-24 15:15:29 +02:00
#elif UWP || WINUI
2024-05-22 11:25:32 +02:00
[ContentProperty(Name = nameof(Projection))]
2022-01-13 20:12:49 +01:00
#endif
public class MapProjectionItem
{
2024-05-24 15:15:29 +02:00
#if AVALONIA
[Content]
#endif
2022-01-19 16:43:00 +01:00
public string Projection { get; set; }
2024-05-24 15:15:29 +02:00
public string Text { get; set; }
2022-01-13 20:12:49 +01:00
}
2024-05-22 11:25:32 +02:00
#if WPF
2022-01-13 20:12:49 +01:00
[ContentProperty(nameof(MapProjections))]
2024-05-24 15:15:29 +02:00
#elif UWP || WINUI
2024-05-22 11:25:32 +02:00
[ContentProperty(Name = nameof(MapProjections))]
2022-01-13 20:12:49 +01:00
#endif
2021-12-05 17:16:14 +01:00
public class MapProjectionsMenuButton : MenuButton
{
2022-01-21 18:37:05 +01:00
private string selectedProjection;
2021-12-05 17:16:14 +01:00
public MapProjectionsMenuButton()
2022-01-11 19:42:12 +01:00
: base("\uE809")
2021-12-05 17:16:14 +01:00
{
2022-01-13 20:12:49 +01:00
((INotifyCollectionChanged)MapProjections).CollectionChanged += (s, e) => InitializeMenu();
2021-12-05 17:16:14 +01:00
}
2024-05-23 18:08:14 +02:00
public static readonly DependencyProperty MapProperty =
2024-05-23 19:21:28 +02:00
DependencyPropertyHelper.Register<MapProjectionsMenuButton, MapBase>(nameof(Map), null,
2024-05-23 18:08:14 +02:00
(button, oldValue, newValue) => button.InitializeMenu());
2021-12-05 17:16:14 +01:00
public MapBase Map
{
2022-08-06 11:04:49 +02:00
get => (MapBase)GetValue(MapProperty);
set => SetValue(MapProperty, value);
2021-12-05 17:16:14 +01:00
}
2024-05-24 15:15:29 +02:00
#if AVALONIA
[Content]
#endif
2022-01-13 20:12:49 +01:00
public Collection<MapProjectionItem> MapProjections { get; } = new ObservableCollection<MapProjectionItem>();
2021-12-05 17:16:14 +01:00
private void InitializeMenu()
{
2022-01-13 20:12:49 +01:00
if (Map != null)
2021-12-05 17:16:14 +01:00
{
var menu = CreateMenu();
2022-01-13 20:12:49 +01:00
foreach (var item in MapProjections)
2021-12-05 17:16:14 +01:00
{
2022-01-13 20:12:49 +01:00
menu.Items.Add(CreateMenuItem(item.Text, item.Projection, MapProjectionClicked));
2021-12-05 17:16:14 +01:00
}
2022-01-13 20:12:49 +01:00
var initialProjection = MapProjections.Select(p => p.Projection).FirstOrDefault();
2021-12-05 17:16:14 +01:00
if (initialProjection != null)
{
SetMapProjection(initialProjection);
}
}
}
private void MapProjectionClicked(object sender, RoutedEventArgs e)
{
var item = (FrameworkElement)sender;
2022-01-19 16:43:00 +01:00
var projection = (string)item.Tag;
2021-12-05 17:16:14 +01:00
SetMapProjection(projection);
}
2022-01-19 16:43:00 +01:00
private void SetMapProjection(string projection)
2021-12-05 17:16:14 +01:00
{
2022-01-21 18:37:05 +01:00
if (selectedProjection != projection)
{
selectedProjection = projection;
Map.MapProjection = MapProjectionFactory.Instance.GetProjection(selectedProjection);
2022-01-21 18:37:05 +01:00
}
2021-12-05 17:16:14 +01:00
2022-01-21 18:37:05 +01:00
UpdateCheckedStates();
}
private void UpdateCheckedStates()
{
2021-12-05 17:16:14 +01:00
foreach (var item in GetMenuItems())
{
2022-01-21 18:37:05 +01:00
item.IsChecked = selectedProjection == (string)item.Tag;
2021-12-05 17:16:14 +01:00
}
}
}
}