-
Notifications
You must be signed in to change notification settings - Fork 9
/
NotifyIconBuilder.cs
184 lines (151 loc) · 5.11 KB
/
NotifyIconBuilder.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ModernNotifyIcon.Theme;
namespace PowerDimmer
{
// from: https://github.com/Sharp0802/ModernNotifyIcon
public class NotifyIconBuilder
{
private ContextMenuStripBuilder StripBuilder { get; } = new();
protected NotifyIconBuilder()
{
}
public static NotifyIconBuilder Create()
{
return new NotifyIconBuilder();
}
public NotifyIconBuilder Configure(Action<ContextMenuStripBuilder> builder)
{
builder(StripBuilder);
return this;
}
public NotifyIcon Build(Icon icon)
{
return new NotifyIcon
{
Icon = icon,
ContextMenuStrip = StripBuilder.Build()
};
}
}
public class ContextMenuStripBuilder
{
public List<ToolStripItem> Items { get; } = new();
public ContextMenuStripBuilder AddItem(ToolStripItem item)
{
Items.Add(item);
return this;
}
public ContextMenuStripBuilder AddText(string text) => AddItem(new ToolStripMenuItem(text));
public ContextMenuStripBuilder AddSeparator() => AddItem(new ToolStripSeparator { Margin = new Padding(0, 2, 0, 2) });
public ContextMenuStripBuilder AddToggle(Action<ToggleGenerateOption> option)
{
var toggle = new ToolStripMenuItem();
var optionRef = new ToggleGenerateOption(toggle);
option.Invoke(optionRef);
toggle.Text = optionRef.Text;
toggle.Checked = optionRef.Checked;
toggle.Click += (_, _) => optionRef.InvokeHandlers(toggle.Checked = !toggle.Checked);
return AddItem(toggle);
}
public ContextMenuStripBuilder AddButton(Action<ButtonGenerateOption> option)
{
var button = new ToolStripMenuItem();
var optionRef = new ButtonGenerateOption(button);
option.Invoke(optionRef);
button.Text = optionRef.Text;
button.Click += (_, _) => optionRef.InvokeHandlers();
return AddItem(button);
}
public ContextMenuStripBuilder AddSubmenu(string text, Action<ContextMenuStripBuilder> option)
{
var optionRef = new ContextMenuStripBuilder();
option.Invoke(optionRef);
var button = new ToolStripMenuItem(text)
{
DropDown = optionRef.Build()
};
return AddItem(button);
}
public ThemeReferencedContextMenuStrip Build()
{
const int padding = 5;
var strip = new ThemeReferencedContextMenuStrip { Spacing = padding };
var array = Items.ToArray();
for (var i = 0; i < array.Length; ++i)
{
array[i].Padding = new Padding(0, padding, 0, padding);
array[i].Margin += new Padding(0, i == 0 ? padding : 0, 0, i == array.Length - 1 ? padding : 0);
}
strip.Items.AddRange(array);
return strip;
}
}
public class GenerateOption<T> where T : GenerateOption<T>
{
public delegate void ConfigureItemHandler(ToolStripMenuItem item);
public ToolStripMenuItem Item { get; private set; }
public GenerateOption(ToolStripMenuItem item)
{
Item = item;
}
public T ConfigureItem(ConfigureItemHandler handler)
{
handler(Item);
return (T)this;
}
}
public sealed class ButtonGenerateOption : GenerateOption<ButtonGenerateOption>
{
public string? Text { get; private set; }
public event Action? Toggled;
public ButtonGenerateOption(ToolStripMenuItem item) : base(item)
{
}
public ButtonGenerateOption SetText(string text)
{
Text = text;
return this;
}
public ButtonGenerateOption AddHandler(Action handler)
{
Toggled += handler;
return this;
}
internal void InvokeHandlers()
{
Toggled?.Invoke();
}
}
public sealed class ToggleGenerateOption : GenerateOption<ToggleGenerateOption>
{
public delegate void ToggleEventHandler(bool toggled);
public string? Text { get; private set; }
public bool Checked { get; private set; } = false;
public event ToggleEventHandler? Toggled;
public ToggleGenerateOption(ToolStripMenuItem item) : base(item)
{
}
public ToggleGenerateOption SetText(string text)
{
Text = text;
return this;
}
public ToggleGenerateOption SetChecked(bool _checked)
{
Checked = _checked;
return this;
}
public ToggleGenerateOption AddHandler(ToggleEventHandler handler)
{
Toggled += handler;
return this;
}
internal void InvokeHandlers(bool check)
{
Toggled?.Invoke(check);
}
}
}