Repository navigation
Expand file tree
/
Copy pathLayoutManagerControl.xaml.cs
More file actions
164 lines (139 loc) · 5.43 KB
/
Copy pathLayoutManagerControl.xaml.cs
File metadata and controls
164 lines (139 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using TypeFix.Models;
namespace TypeFix
{
public partial class LayoutManagerControl : System.Windows.Controls.UserControl
{
public static readonly DependencyProperty ShowManagementProperty =
DependencyProperty.Register(
nameof(ShowManagement),
typeof(bool),
typeof(LayoutManagerControl),
new PropertyMetadata(true, OnShowManagementChanged));
private LayoutCatalog? _catalog;
private bool _refreshing;
public LayoutManagerControl()
{
InitializeComponent();
ApplyManagementVisibility();
}
public bool ShowManagement
{
get => (bool)GetValue(ShowManagementProperty);
set => SetValue(ShowManagementProperty, value);
}
public Action<string, bool>? ReportStatus { get; set; }
public void Attach(LayoutCatalog catalog)
{
if (_catalog != null)
_catalog.Changed -= OnCatalogChanged;
_catalog = catalog;
_catalog.Changed += OnCatalogChanged;
Refresh();
}
public void Refresh()
{
if (_catalog == null)
return;
_refreshing = true;
try
{
LayoutCombo.ItemsSource = _catalog.Profiles
.Select(profile => new LayoutChoice(_catalog.Label(profile), profile.Id))
.ToList();
LayoutCombo.SelectedValue = _catalog.ActiveId;
}
finally
{
_refreshing = false;
}
}
private void OnCatalogChanged()
{
Dispatcher.BeginInvoke(Refresh);
}
private static void OnShowManagementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is LayoutManagerControl control)
control.ApplyManagementVisibility();
}
private void ApplyManagementVisibility()
{
if (ManagementRoot == null)
return;
ManagementRoot.Visibility = ShowManagement ? Visibility.Visible : Visibility.Collapsed;
}
private void LayoutCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_refreshing || _catalog == null)
return;
if (LayoutCombo.SelectedValue is not string id || id == _catalog.ActiveId)
return;
_catalog.SetActive(id);
Report(UiLanguage.Format("StatusLayoutActivated", _catalog.Label(_catalog.Active)), false);
}
private void ImportButton_Click(object sender, RoutedEventArgs e)
{
if (_catalog == null)
return;
var dialog = new Microsoft.Win32.OpenFileDialog
{
Filter = UiLanguage.Get("LayoutFileFilter"),
Multiselect = true,
CheckFileExists = true
};
if (dialog.ShowDialog(Window.GetWindow(this)) != true)
return;
var results = _catalog.ImportPaths(dialog.FileNames);
int imported = results.Count(r => r.Status == LayoutImportStatus.Imported);
int duplicates = results.Count(r => r.Status == LayoutImportStatus.Duplicate);
int invalid = results.Count(r => r.Status == LayoutImportStatus.Invalid);
if (imported > 0 && invalid > 0)
Report(UiLanguage.Format("StatusLayoutImportPartial", imported, invalid), false);
else if (imported == 1)
Report(UiLanguage.Format("StatusLayoutImported", results.First(r => r.Status == LayoutImportStatus.Imported).Name), false);
else if (imported > 1)
Report(UiLanguage.Format("StatusLayoutImportedMany", imported, _catalog.Label(_catalog.Active)), false);
else if (duplicates > 0 && invalid == 0)
Report(UiLanguage.Format("StatusLayoutDuplicate", results.First(r => r.Status == LayoutImportStatus.Duplicate).Name), false);
else
Report(UiLanguage.Get("StatusLayoutImportFailed"), true);
}
private void ExportButton_Click(object sender, RoutedEventArgs e)
{
if (_catalog == null)
return;
var dialog = new Microsoft.Win32.SaveFileDialog
{
Filter = UiLanguage.Get("LayoutZipFilter"),
FileName = "TypeFix-layouts.zip",
AddExtension = true,
DefaultExt = ".zip",
OverwritePrompt = true
};
if (dialog.ShowDialog(Window.GetWindow(this)) != true)
return;
if (_catalog.ExportAll(dialog.FileName))
Report(UiLanguage.Format("StatusLayoutExported", _catalog.Profiles.Count), false);
else
Report(UiLanguage.Get("StatusLayoutExportFailed"), true);
}
private void Report(string message, bool isError)
{
ReportStatus?.Invoke(message, isError);
}
private sealed class LayoutChoice
{
public LayoutChoice(string label, string id)
{
Label = label;
Id = id;
}
public string Label { get; }
public string Id { get; }
}
}
}