Repository navigation
Expand file tree
/
Copy pathSettingsShell.cs
More file actions
257 lines (237 loc) · 12.4 KB
/
Copy pathSettingsShell.cs
File metadata and controls
257 lines (237 loc) · 12.4 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
// A compact navigation shell for the many taskbar controls. Pages are built once;
// switching pages does not recreate controls or restart any taskbar integration.
internal sealed class SettingsShell : UserControl
{
readonly Panel host = new() { Dock = DockStyle.Fill, BackColor = Ui.Background };
readonly FlowLayoutPanel navigation = new()
{
Dock = DockStyle.Fill, FlowDirection = FlowDirection.TopDown, WrapContents = false,
AutoScroll = true, BackColor = Ui.Sidebar
};
readonly Dictionary<string, (Control Page, NavButton Button)> pages = new(StringComparer.Ordinal);
string? selectedKey;
public ComboBox LanguageChoice { get; } = new ModernCombo { FieldColor = Ui.SidebarHover, ForeColor = Color.White, BackColor = Ui.SidebarHover };
// Always reachable from every page; Program wires the click to Taskbar.RestartExplorer.
public NavButton RestartExplorerButton { get; } = new("") { Text = Localization.T("Restart Explorer", "Explorer neu starten") };
// Program wires the click to the Help page's reset, which asks before changing anything.
public NavButton ResetButton { get; } = new("") { Text = Localization.T("Reset everything…", "Alles zurücksetzen…") };
readonly ToolTip tips = new();
int S(int value) => LogicalToDeviceUnits(value);
public SettingsShell()
{
Dock = DockStyle.Fill;
BackColor = Ui.Background;
navigation.Padding = new Padding(S(12), S(6), S(12), S(6));
var layout = new TableLayoutPanel { Dock = DockStyle.Fill, ColumnCount = 2, RowCount = 1, Margin = Padding.Empty, Padding = Padding.Empty };
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, S(248)));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
Controls.Add(layout);
var sidebar = new TableLayoutPanel { Dock = DockStyle.Fill, ColumnCount = 1, RowCount = 3, BackColor = Ui.Sidebar, Margin = Padding.Empty };
sidebar.RowStyles.Add(new RowStyle(SizeType.Absolute, S(104)));
sidebar.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
sidebar.RowStyles.Add(new RowStyle(SizeType.Absolute, S(156)));
layout.Controls.Add(sidebar, 0, 0);
layout.Controls.Add(host, 1, 0);
var brand = new Panel { Dock = DockStyle.Fill, BackColor = Ui.Sidebar, Margin = Padding.Empty };
brand.Paint += (_, e) => PaintBrand(e.Graphics);
sidebar.Controls.Add(brand, 0, 0);
sidebar.Controls.Add(navigation, 0, 1);
var language = new Panel { Dock = DockStyle.Fill, BackColor = Ui.Sidebar, Margin = Padding.Empty };
RestartExplorerButton.Bounds = new Rectangle(S(12), S(6), S(224), S(42));
// Set on hover so the tip follows a language switch.
RestartExplorerButton.MouseEnter += (_, _) => tips.SetToolTip(RestartExplorerButton, Localization.T(
"Reloads the taskbar and winBar's helper. Open File Explorer windows close.",
"Lädt die Taskleiste und den winBar-Helfer neu. Offene Explorer-Fenster werden geschlossen."));
// Hairline that separates the page list from the two maintenance actions.
language.Paint += (_, e) =>
{
using var line = new Pen(Ui.SidebarHover);
e.Graphics.DrawLine(line, S(22), 0, language.Width - S(22), 0);
};
language.Controls.Add(RestartExplorerButton);
ResetButton.Bounds = new Rectangle(S(12), S(54), S(224), S(42));
language.Controls.Add(ResetButton);
// No caption: the language names explain the field, and the space goes to the reset button.
LanguageChoice.AccessibleName = Localization.T("Language", "Sprache");
LanguageChoice.Items.AddRange(["English", "Deutsch", "Schwiizerdütsch", "System"]);
LanguageChoice.Location = new Point(S(18), S(106));
LanguageChoice.Width = S(212);
language.Controls.Add(LanguageChoice);
sidebar.Controls.Add(language, 0, 2);
}
void PaintBrand(Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
var mark = new Rectangle(S(22), S(32), S(40), S(40));
using (var shape = Ui.Rounded(mark, S(11)))
using (var fill = new LinearGradientBrush(mark, Color.FromArgb(84, 132, 255), Color.FromArgb(139, 92, 246), 45f))
g.FillPath(fill, shape);
// A tiny taskbar inside the logo: a frosted bar with three app dots.
var bar = new RectangleF(mark.X + S(7), mark.Bottom - S(15), mark.Width - S(14), S(8));
using (var shape = Ui.Rounded(bar, S(4)))
using (var glass = new SolidBrush(Color.FromArgb(200, 255, 255, 255)))
g.FillPath(glass, shape);
using (var dot = new SolidBrush(Color.FromArgb(96, 112, 255)))
for (int i = 0; i < 3; i++) g.FillEllipse(dot, bar.X + S(5) + i * S(6), bar.Y + S(2), S(4), S(4));
using var title = new Font("Segoe UI Semibold", 15f, FontStyle.Bold);
TextRenderer.DrawText(g, "winBar", title, new Point(mark.Right + S(10), mark.Y - S(4)), Color.White, TextFormatFlags.NoPadding);
TextRenderer.DrawText(g, Localization.T("A quieter taskbar", "Mehr Ruhe"), Font,
new Point(mark.Right + S(11), mark.Y + S(22)), Ui.SidebarText, TextFormatFlags.NoPadding);
}
static string Glyph(string key) => key switch
{
"appearance" => "",
"taskbar" => "",
"motion" => "",
"clock" => "",
"apps" => "",
"right" => "",
"layouts" => "",
"system" => "",
"updates" => "",
_ => ""
};
public TableLayoutPanel AddPage(string key, string englishTitle, string germanTitle,
string englishDescription, string germanDescription)
{
if (pages.TryGetValue(key, out var existing))
return (TableLayoutPanel)existing.Page.Controls[1];
var (navigationEnglish, navigationGerman) = key switch
{
"clock" => ("Clock", "Uhr"),
"apps" => ("Apps", "Apps"),
"system" => ("Help", "Hilfe"),
_ => (englishTitle, germanTitle)
};
var button = new NavButton(Glyph(key))
{
Text = Localization.T(navigationEnglish, navigationGerman),
Size = new Size(S(224), S(42)), Margin = new Padding(0, 0, 0, S(3)), TabStop = true
};
button.Click += (_, _) => ShowPage(key);
navigation.Controls.Add(button);
var page = new TableLayoutPanel { Dock = DockStyle.Fill, ColumnCount = 1, RowCount = 2, Margin = Padding.Empty, BackColor = Ui.Background };
page.RowStyles.Add(new RowStyle(SizeType.Absolute, S(92)));
page.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
var header = new Panel { Dock = DockStyle.Fill, BackColor = Ui.Background, Margin = Padding.Empty };
header.Controls.Add(new Label
{
Text = Localization.T(englishTitle, germanTitle),
ForeColor = Ui.Ink, Font = new Font("Segoe UI Semibold", 20, FontStyle.Bold),
AutoSize = true, Location = new Point(S(20), S(18))
});
header.Controls.Add(new Label
{
Text = Localization.T(englishDescription, germanDescription),
ForeColor = Ui.Muted, AutoSize = true, MaximumSize = new Size(S(700), 0), Location = new Point(S(23), S(60))
});
page.Controls.Add(header, 0, 0);
var content = new BufferedTableLayoutPanel
{
Dock = DockStyle.Fill, ColumnCount = 2, AutoScroll = true, Margin = Padding.Empty,
Padding = new Padding(S(20), S(14), S(20), S(30)), BackColor = Ui.Background, ForeColor = Ui.Ink
};
content.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 38));
content.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 62));
content.Paint += (_, e) => PaintCards(content, e.Graphics);
page.Controls.Add(content, 0, 1);
page.Visible = false;
host.Controls.Add(page);
pages.Add(key, (page, button));
if (selectedKey == null) ShowPage(key);
return content;
}
// Controls between two section titles share one white card. Titles, theme cards and
// illustrations are tagged Ui.OutsideCard and sit directly on the page background.
void PaintCards(TableLayoutPanel content, Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle? group = null;
void Flush()
{
if (group is not { } r) return;
var card = new RectangleF(content.Padding.Left + S(3), r.Top - S(12), content.ClientSize.Width - content.Padding.Horizontal - S(6) - 1, r.Height + S(24));
using var shape = Ui.Rounded(card, S(10));
using var fill = new SolidBrush(Ui.Card);
using var border = new Pen(Ui.CardBorder);
// Two faint offset copies read as a soft drop shadow without a blur pass.
for (int depth = 2; depth >= 1; depth--)
{
using var shadowShape = Ui.Rounded(new RectangleF(card.X, card.Y + S(depth), card.Width, card.Height), S(10));
using var shadow = new SolidBrush(Color.FromArgb(depth == 2 ? 7 : 12, 22, 32, 51));
g.FillPath(shadow, shadowShape);
}
g.FillPath(fill, shape);
g.DrawPath(border, shape);
group = null;
}
foreach (Control control in content.Controls)
{
if (!control.Visible) continue;
if (Equals(control.Tag, Ui.OutsideCard)) { Flush(); continue; }
group = group is { } r ? Rectangle.Union(r, control.Bounds) : control.Bounds;
}
Flush();
}
[DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr window, int message, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")] static extern bool RedrawWindow(IntPtr window, IntPtr update, IntPtr region, uint flags);
public void ShowPage(string key)
{
if (!pages.TryGetValue(key, out var selected)) return;
selectedKey = key;
// Freeze drawing while one page hides and the next one lays out, then paint the
// finished page once. Without this, each control appears separately and the page flickers.
bool freeze = host.IsHandleCreated && host.Visible;
if (freeze) SendMessage(host.Handle, 0x000B, IntPtr.Zero, IntPtr.Zero); // WM_SETREDRAW off
try
{
foreach (var (id, value) in pages)
{
value.Page.Visible = id == key;
value.Button.Active = id == key;
}
selected.Page.BringToFront();
}
finally
{
if (freeze)
{
SendMessage(host.Handle, 0x000B, (IntPtr)1, IntPtr.Zero); // WM_SETREDRAW on
RedrawWindow(host.Handle, IntPtr.Zero, IntPtr.Zero, 0x0001 | 0x0004 | 0x0080 | 0x0100); // INVALIDATE | ERASE | ALLCHILDREN | UPDATENOW
}
else selected.Page.Invalidate(true);
}
}
}
// The settings pages paint their white cards behind the controls. Double buffering keeps
// those cards from flickering while the page scrolls or the window is resized.
internal sealed class BufferedTableLayoutPanel : TableLayoutPanel
{
readonly Dictionary<Control, int> heights = [];
public BufferedTableLayoutPanel()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
}
// Pages are built while hidden, so wrapping toggles first measure their text at a narrow
// width and become very tall. When the page gets its real width they shrink inside this
// layout pass, and WinForms drops that nested layout request: rows and the scroll range kept
// the old heights, leaving a long empty area below the last card. Lay out again until the
// child heights are stable (normally one extra pass, none when nothing changed).
protected override void OnLayout(LayoutEventArgs e)
{
for (int pass = 0; pass < 4; pass++)
{
heights.Clear();
foreach (Control control in Controls) heights[control] = control.Height;
base.OnLayout(e);
bool changed = false;
foreach (Control control in Controls)
if (!heights.TryGetValue(control, out int before) || before != control.Height) { changed = true; break; }
if (!changed) break;
}
heights.Clear();
Invalidate();
}
}