Files
Wino-Mail/Wino.Mail/Behaviors/BindableCommandBarBehavior.cs

194 lines
6.1 KiB
C#
Raw Normal View History

2024-04-26 01:47:46 +02:00
using System.Collections;
2024-04-18 01:44:37 +02:00
using System.Collections.Specialized;
using System.Windows.Input;
2024-04-26 01:47:46 +02:00
using Microsoft.Xaml.Interactivity;
2024-04-18 01:44:37 +02:00
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Wino.Controls;
using Wino.Core.Domain.Models.Menus;
using Wino.Core.UWP.Controls;
2024-04-18 01:44:37 +02:00
using Wino.Helpers;
2025-02-16 11:54:23 +01:00
namespace Wino.Behaviors;
public class BindableCommandBarBehavior : Behavior<CommandBar>
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
public static readonly DependencyProperty PrimaryCommandsProperty = DependencyProperty.Register(
"PrimaryCommands", typeof(object), typeof(BindableCommandBarBehavior),
new PropertyMetadata(null, UpdateCommands));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public ICommand ItemClickedCommand
{
get { return (ICommand)GetValue(ItemClickedCommandProperty); }
set { SetValue(ItemClickedCommandProperty, value); }
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public static readonly DependencyProperty ItemClickedCommandProperty = DependencyProperty.Register(nameof(ItemClickedCommand), typeof(ICommand), typeof(BindableCommandBarBehavior), new PropertyMetadata(null));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public object PrimaryCommands
{
get { return GetValue(PrimaryCommandsProperty); }
set { SetValue(PrimaryCommandsProperty, value); }
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
protected override void OnDetaching()
{
base.OnDetaching();
2025-02-16 11:54:23 +01:00
AttachChanges(false);
2025-02-16 11:54:23 +01:00
if (PrimaryCommands is IEnumerable enumerable)
{
foreach (var item in enumerable)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
if (item is ButtonBase button)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
button.Click -= Button_Click;
2024-04-18 01:44:37 +02:00
}
}
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private void UpdatePrimaryCommands()
{
if (AssociatedObject == null)
return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (PrimaryCommands == null)
return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (AssociatedObject.PrimaryCommands is IEnumerable enumerableObjects)
{
foreach (var item in enumerableObjects)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
if (item is ButtonBase button)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
button.Click -= Button_Click;
2024-04-18 01:44:37 +02:00
}
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (AssociatedObject.SecondaryCommands is IEnumerable secondaryObject)
{
foreach (var item in secondaryObject)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
if (item is ButtonBase button)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
button.Click -= Button_Click;
2024-04-18 01:44:37 +02:00
}
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
AssociatedObject.PrimaryCommands.Clear();
AssociatedObject.SecondaryCommands.Clear();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (!(PrimaryCommands is IEnumerable enumerable)) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
foreach (var command in enumerable)
{
if (command is MailOperationMenuItem mailOperationMenuItem)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
ICommandBarElement menuItem = null;
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
if (mailOperationMenuItem.Operation == Core.Domain.Enums.MailOperation.Seperator)
{
menuItem = new AppBarSeparator();
}
else
{
var label = XamlHelpers.GetOperationString(mailOperationMenuItem.Operation);
menuItem = new AppBarButton
{
2025-02-16 11:54:23 +01:00
Icon = new WinoFontIcon() { Glyph = ControlConstants.WinoIconFontDictionary[XamlHelpers.GetWinoIconGlyph(mailOperationMenuItem.Operation)] },
Label = label,
LabelPosition = string.IsNullOrWhiteSpace(label) ? CommandBarLabelPosition.Collapsed : CommandBarLabelPosition.Default,
DataContext = mailOperationMenuItem,
};
ToolTip toolTip = new ToolTip
{
2025-02-16 11:54:23 +01:00
Content = label
};
ToolTipService.SetToolTip((DependencyObject)menuItem, toolTip);
((AppBarButton)menuItem).Click -= Button_Click;
((AppBarButton)menuItem).Click += Button_Click;
2025-02-16 11:35:43 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (mailOperationMenuItem.IsSecondaryMenuPreferred)
{
AssociatedObject.SecondaryCommands.Add(menuItem);
}
else
{
AssociatedObject.PrimaryCommands.Add(menuItem);
}
}
2024-04-26 01:47:46 +02:00
2025-02-16 11:54:23 +01:00
//if (dependencyObject is ICommandBarElement icommandBarElement)
//{
// if (dependencyObject is ButtonBase button)
// {
// button.Click -= Button_Click;
// button.Click += Button_Click;
// }
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// if (command is MailOperationMenuItem mailOperationMenuItem)
// {
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// }
//}
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private void Button_Click(object sender, RoutedEventArgs e)
{
ItemClickedCommand?.Execute(((ButtonBase)sender).DataContext);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
protected override void OnAttached()
{
base.OnAttached();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
AttachChanges(true);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
UpdatePrimaryCommands();
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private void PrimaryCommandsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdatePrimaryCommands();
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
private static void UpdateCommands(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
if (!(dependencyObject is BindableCommandBarBehavior behavior)) return;
2025-02-16 11:54:23 +01:00
if (dependencyPropertyChangedEventArgs.OldValue is INotifyCollectionChanged oldList)
{
oldList.CollectionChanged -= behavior.PrimaryCommandsCollectionChanged;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
behavior.AttachChanges(true);
behavior.UpdatePrimaryCommands();
}
private void AttachChanges(bool register)
{
if (PrimaryCommands is null) return;
2025-02-16 11:54:23 +01:00
if (PrimaryCommands is INotifyCollectionChanged collectionChanged)
{
if (register)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
collectionChanged.CollectionChanged -= PrimaryCommandsCollectionChanged;
collectionChanged.CollectionChanged += PrimaryCommandsCollectionChanged;
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
else
collectionChanged.CollectionChanged -= PrimaryCommandsCollectionChanged;
2024-04-18 01:44:37 +02:00
}
}
}