Финализация рефакторинга
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using Pf2e.Scrapper.Processors.Attributes;
|
||||
using Pf2e.Scrapper.Processors.Attributes.Enum;
|
||||
using Pf2e.Scrapper.Processors.Collection;
|
||||
using CheckBox = System.Windows.Controls.CheckBox;
|
||||
|
||||
namespace Pf2e.WPF.Start.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Логика взаимодействия для ProcessorCollectionControl.xaml
|
||||
/// </summary>
|
||||
internal partial class ProcessorCollectionControl : System.Windows.Controls.UserControl
|
||||
{
|
||||
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
||||
public ProcessorCollection Collection
|
||||
{
|
||||
get => _collection;
|
||||
set
|
||||
{
|
||||
_collection = value;
|
||||
SetContent();
|
||||
}
|
||||
}
|
||||
private ProcessorCollection _collection = null!;
|
||||
|
||||
public ProcessorCollectionControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private Dictionary<ProcessorGroupEnum, CheckBox> GroupCheckBoxes { get; } = [];
|
||||
|
||||
private Dictionary<ProcessorGroupEnum, bool?> GroupLastStats { get; } = [];
|
||||
|
||||
private Dictionary<string, CheckBox> ItemCheckBoxes { get; } = [];
|
||||
|
||||
private void SetContent()
|
||||
{
|
||||
RootPanel.Children.Clear();
|
||||
|
||||
foreach (ProcessorGroupEnum group in Enum.GetValues<ProcessorGroupEnum>())
|
||||
{
|
||||
GroupCheckBoxes[group] = new()
|
||||
{
|
||||
Content = group.ToString(),
|
||||
IsThreeState = true,
|
||||
Style = (Style)Resources["HeaderCheckbox"]!,
|
||||
};
|
||||
|
||||
GroupCheckBoxes[group].Click += GroupCheckBox_Click;
|
||||
|
||||
System.Windows.Controls.Border border = new()
|
||||
{
|
||||
Style = (Style)Resources["ElementBorder"]!,
|
||||
};
|
||||
|
||||
System.Windows.Controls.WrapPanel wrapPanel = new()
|
||||
{
|
||||
Style = (Style)Resources["ElementPanelStyle"]!,
|
||||
};
|
||||
|
||||
foreach (ProcessorRecord item in Collection.AllItems.Where(item => item.Group == group))
|
||||
{
|
||||
ItemCheckBoxes[item.Name] = new()
|
||||
{
|
||||
Content = item.Description,
|
||||
ToolTip = item.Name,
|
||||
IsChecked = item.Active,
|
||||
Style = (Style)Resources["ElementCheckBox"]!,
|
||||
};
|
||||
|
||||
ItemCheckBoxes[item.Name].Click += ItemCheckBox_Click;
|
||||
|
||||
wrapPanel.Children.Add(ItemCheckBoxes[item.Name]);
|
||||
}
|
||||
|
||||
border.Child = wrapPanel;
|
||||
|
||||
RootPanel.Children.Add(GroupCheckBoxes[group]);
|
||||
RootPanel.Children.Add(border);
|
||||
}
|
||||
|
||||
UpdateContent();
|
||||
}
|
||||
|
||||
private void UpdateContent()
|
||||
{
|
||||
GroupLastStats.Clear();
|
||||
List<string> checkedItems = [];
|
||||
|
||||
foreach ((string key, CheckBox? checkBox) in ItemCheckBoxes)
|
||||
{
|
||||
ProcessorRecord item = Collection[key];
|
||||
bool isChecked = checkBox.IsChecked ?? false;
|
||||
|
||||
if (GroupLastStats.TryGetValue(item.Group, out bool? state))
|
||||
{
|
||||
/*
|
||||
* | true | false | null
|
||||
* | Checked | Unchecked | Indeterminate
|
||||
* -------+---------+-----------+---------------
|
||||
* true | true | null | null
|
||||
* false | null | false | null
|
||||
*/
|
||||
|
||||
GroupLastStats[item.Group] =
|
||||
isChecked && state == true ? true
|
||||
: !isChecked && state == false ? false
|
||||
: null;
|
||||
}
|
||||
else
|
||||
{
|
||||
GroupLastStats[item.Group] = isChecked;
|
||||
}
|
||||
|
||||
if (isChecked)
|
||||
{
|
||||
checkedItems.Add(key);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ((ProcessorGroupEnum groupEnum, CheckBox? checkBox) in GroupCheckBoxes)
|
||||
{
|
||||
checkBox.IsChecked = GroupLastStats.GetValueOrDefault(groupEnum, null);
|
||||
}
|
||||
|
||||
Collection.FilterProcessors(checkedItems);
|
||||
}
|
||||
|
||||
private void GroupCheckBox_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
CheckBox groupCheckBox = (CheckBox)sender;
|
||||
|
||||
ProcessorGroupEnum groupEnum = GroupCheckBoxes
|
||||
.First(pair => pair.Value == groupCheckBox)
|
||||
.Key;
|
||||
|
||||
groupCheckBox.IsChecked =
|
||||
GroupLastStats[groupEnum] is null || groupCheckBox.IsChecked is not null;
|
||||
|
||||
foreach (
|
||||
(string _, CheckBox checkBox) in ItemCheckBoxes.Where(pair =>
|
||||
Collection[pair.Key].Group == groupEnum
|
||||
)
|
||||
)
|
||||
{
|
||||
checkBox.IsChecked = groupCheckBox.IsChecked;
|
||||
}
|
||||
|
||||
UpdateContent();
|
||||
}
|
||||
|
||||
private void ItemCheckBox_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
UpdateContent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user