using C4IT.FASD.Base; using F4SD_AdaptableIcon.Enums; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Runtime.InteropServices.WindowsRuntime; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using static C4IT.Logging.cLogManager; namespace FasdDesktopUi.Basics.UserControls { public partial class SelectionPill : UserControl { #region PillText public string PillText { get { return (string)GetValue(PillTextProperty); } set { SetValue(PillTextProperty, value); } } public static readonly DependencyProperty PillTextProperty = DependencyProperty.Register("PillText", typeof(string), typeof(SelectionPill), new PropertyMetadata(string.Empty)); #endregion #region Icons private static void SelectedIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { try { if (!(d is SelectionPill me)) return; if (e.NewValue is null) return; switch (e.NewValue) { case enumInternIcons _: me.SelectedMaterialIcon = null; me.SelectedGif = null; break; case MaterialIcons.MaterialIconType _: me.SelectedInternIcon = null; me.SelectedGif = null; break; case enumInternGif _: me.SelectedInternIcon = null; me.SelectedMaterialIcon = null; break; } if (me.SelectedInternIcon is null && me.SelectedMaterialIcon is null && me.SelectedGif is null) me.PillIcon.Visibility = Visibility.Collapsed; else me.PillIcon.Visibility = Visibility.Visible; } catch (Exception E) { LogException(E); } } public enumInternIcons? SelectedInternIcon { get { return (enumInternIcons?)GetValue(SelectedInternIconProperty); } set { SetValue(SelectedInternIconProperty, value); } } public static readonly DependencyProperty SelectedInternIconProperty = DependencyProperty.Register("SelectedInternIcon", typeof(enumInternIcons?), typeof(SelectionPill), new PropertyMetadata(null, new PropertyChangedCallback(SelectedIconChanged))); public MaterialIcons.MaterialIconType? SelectedMaterialIcon { get { return (MaterialIcons.MaterialIconType?)GetValue(SelectedMaterialIconProperty); } set { SetValue(SelectedMaterialIconProperty, value); } } public static readonly DependencyProperty SelectedMaterialIconProperty = DependencyProperty.Register("SelectedMaterialIcon", typeof(MaterialIcons.MaterialIconType?), typeof(SelectionPill), new PropertyMetadata(null, new PropertyChangedCallback(SelectedIconChanged))); public enumInternGif? SelectedGif { get { return (enumInternGif?)GetValue(SelectedGifProperty); } set { SetValue(SelectedGifProperty, value); } } public static readonly DependencyProperty SelectedGifProperty = DependencyProperty.Register("SelectedGif", typeof(enumInternGif?), typeof(SelectionPill), new PropertyMetadata(null, new PropertyChangedCallback(SelectedIconChanged))); #endregion private static List allPills = new List(); // Dependency Property für die Gruppenzugehörigkeit public static readonly DependencyProperty GroupNameProperty = DependencyProperty.Register("GroupName", typeof(string), typeof(SelectionPill), new PropertyMetadata("")); public string GroupName { get { return (string)GetValue(GroupNameProperty); } set { SetValue(GroupNameProperty, value); } } // Dependency Property für den Schalter "selectOne" public static readonly DependencyProperty SelectOneProperty = DependencyProperty.Register("SelectOne", typeof(bool), typeof(SelectionPill), new PropertyMetadata(false)); public bool SelectOne { get { return (bool)GetValue(SelectOneProperty); } set { SetValue(SelectOneProperty, value); } } #region IsSelected public event EventHandler IsSelectedChanged; private static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { try { if (d is SelectionPill pill) { pill.IsSelectedChanged?.Invoke(pill, EventArgs.Empty); } } catch (Exception E) { LogException(E); } } public bool IsSelected { get { return (bool)GetValue(IsSelectedProperty); } set { SetValue(IsSelectedProperty, value); } } public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(SelectionPill), new PropertyMetadata(false, new PropertyChangedCallback(OnIsSelectedChanged))); #endregion public SelectionPill() { InitializeComponent(); allPills.Add(this); } public static void RemovePill(SelectionPill pill) { if (allPills.Contains(pill)) { allPills.Remove(pill); } } private void DeselectOtherPillsInGroup() { if (!string.IsNullOrEmpty(GroupName) && SelectOne && IsSelected) { var pillsInGroup = allPills.Where(pill => pill.GroupName == this.GroupName && pill != this); foreach (var pill in pillsInGroup) { pill.IsSelected = false; } } } #region PillBorder_Click private void PillBorder_Click() { if (!SelectOne || string.IsNullOrEmpty(GroupName)) { IsSelected = !IsSelected; } else if (!IsSelected) // Nur den Zustand ändern und andere deselektieren, wenn nicht bereits ausgewählt { IsSelected = true; DeselectOtherPillsInGroup(); // Deselect other pills in the group } var _h = Dispatcher.Invoke(async () => { DoubleAnimation doubleAnimation = new DoubleAnimation(0.7, TimeSpan.FromSeconds(0.1)) { AutoReverse = true }; PillBorder.BeginAnimation(OpacityProperty, doubleAnimation); await Task.Delay(205); PillBorder.BeginAnimation(OpacityProperty, null); }); } private void PillBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { PillBorder_Click(); } private void PillBorder_TouchDown(object sender, TouchEventArgs e) { PillBorder_Click(); } #endregion } }