Files
ReminderOrganizer/CategoryWindow.xaml.cs

148 lines
5.9 KiB
C#
Raw Permalink Normal View History

#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ReminderOrganizer.Core;
using ReminderOrganizer.Models;
using ReminderOrganizer.Services;
namespace ReminderOrganizer
{
/// <summary>Edit anniversary categories (name, icon, color, text). Works on copies until "Save".</summary>
public partial class CategoryWindow : Window
{
private readonly ObservableCollection<Category> _items;
private readonly Func<string, int> _usage;
private readonly ILocalizationService _loc;
public IReadOnlyList<string> Glyphs => Category.Glyphs;
public IReadOnlyList<string> Palette => Folder.Palette;
/// <summary>The edited list (valid after DialogResult == true).</summary>
public List<Category> Result => _items.ToList();
public CategoryWindow(IEnumerable<Category> categories, bool darkMode, Func<string, int> usageCount)
{
InitializeComponent();
DialogAnimation.Attach(this);
Theme.Apply(Resources, darkMode);
_loc = (ILocalizationService)ServiceLocator.Services.GetService(typeof(ILocalizationService))!;
_usage = usageCount;
_items = new ObservableCollection<Category>(categories.Select(c => c.Clone()));
foreach (var c in _items) c.PropertyChanged += OnCategoryChanged;
Rows.ItemsSource = _items;
DataContext = new LocalizedTexts(_loc);
}
/// <summary>Indexer for {Binding [Key]} in this window.</summary>
private sealed class LocalizedTexts
{
private readonly ILocalizationService _loc;
public LocalizedTexts(ILocalizationService loc) => _loc = loc;
public string this[string key] => _loc.GetString(key);
}
private string Example(Category c)
{
var culture = ReminderTask.DisplayCulture?.Invoke() ?? System.Globalization.CultureInfo.CurrentCulture;
return c.FormatYears(25, culture, _loc.GetString);
}
private void Example_Loaded(object sender, RoutedEventArgs e)
{
if (sender is TextBlock tb && tb.Tag is Category c) tb.Text = Example(c);
}
private void OnCategoryChanged(object? sender, PropertyChangedEventArgs e)
{
// keep the "example" column live while typing
if (sender is not Category c) return;
foreach (var tb in FindVisualChildren<TextBlock>(Rows))
if (ReferenceEquals(tb.Tag, c)) tb.Text = Example(c);
}
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject root) where T : DependencyObject
{
for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(root); i++)
{
var child = System.Windows.Media.VisualTreeHelper.GetChild(root, i);
if (child is T t) yield return t;
foreach (var d in FindVisualChildren<T>(child)) yield return d;
}
}
private void Add_Click(object sender, RoutedEventArgs e)
{
var c = new Category
{
Name = _loc.GetString("NewCategory"),
Glyph = Category.Glyphs[_items.Count % Category.Glyphs.Length],
Color = Folder.Palette[_items.Count % Folder.Palette.Length],
Format = _loc.GetString("CatJubileeFormat")
};
c.PropertyChanged += OnCategoryChanged;
_items.Add(c);
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
if (sender is not FrameworkElement { Tag: Category c }) return;
if (_items.Count <= 1) return;
int used = _usage(c.Id);
if (used > 0 && MessageBox.Show(this, string.Format(_loc.GetString("ConfirmDeleteCategory"), c.Name, used),
_loc.GetString("AppTitle"), MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
return;
_items.Remove(c);
}
private void Save_Click(object sender, RoutedEventArgs e)
{
foreach (var c in _items)
{
c.Name = string.IsNullOrWhiteSpace(c.Name) ? _loc.GetString("NewCategory") : c.Name.Trim();
if (string.IsNullOrWhiteSpace(c.Format)) c.Format = "{0}";
}
DialogResult = true;
}
private void Cancel_Click(object sender, RoutedEventArgs e) => DialogResult = false;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape) DialogResult = false;
}
/// <summary>The picker buttons live in a popup; walk up (visual, then logical) to the category row.</summary>
private static Category? CategoryOf(object sender)
{
DependencyObject? d = sender as DependencyObject;
while (d != null)
{
if (d is FrameworkElement { DataContext: Category c }) return c;
d = System.Windows.Media.VisualTreeHelper.GetParent(d) ?? LogicalTreeHelper.GetParent(d);
}
return null;
}
private void PickGlyph_Click(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement { Tag: string glyph } && CategoryOf(sender) is { } c) c.Glyph = glyph;
}
private void PickColor_Click(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement { Tag: string color } && CategoryOf(sender) is { } c) c.Color = color;
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && e.OriginalSource is not TextBox) DragMove();
}
}
}