-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginThemingUtil.cs
143 lines (119 loc) · 4.57 KB
/
PluginThemingUtil.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
/////////////////////////////////////////////////////////////////////////////////
//
// Soft Proofing Effect Plugin for Paint.NET
//
// This software is provided under the MIT License:
// Copyright (C) 2017-2018 Nicholas Hayes
//
// See LICENSE.txt for complete licensing and attribution information.
//
/////////////////////////////////////////////////////////////////////////////////
using PaintDotNet;
using PaintDotNet.Effects;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace SoftProofing
{
internal static class PluginThemingUtil
{
// Paint.NET added theming support for plug-ins in 4.20.
private static readonly Version PluginThemingMinVersion = new Version("4.20");
private static MethodInfo useAppThemeSetter = null;
private static bool initAppThemeSetter = false;
public static void EnableEffectDialogTheme(EffectConfigDialog dialog)
{
try
{
Version pdnVersion = dialog.Services.GetService<PaintDotNet.AppModel.IAppInfoService>().AppVersion;
if (pdnVersion >= PluginThemingMinVersion)
{
if (!initAppThemeSetter)
{
initAppThemeSetter = true;
PropertyInfo propertyInfo = typeof(EffectConfigDialog).GetProperty("UseAppThemeColors");
if (propertyInfo != null)
{
useAppThemeSetter = propertyInfo.GetSetMethod();
}
}
if (useAppThemeSetter != null)
{
useAppThemeSetter.Invoke(dialog, new object[] { true });
}
}
}
catch
{
// Ignore any exceptions that are thrown when trying to enable the dialog theming.
// The dialog should be shown to the user even if theming could not be enabled.
}
}
public static void UpdateControlBackColor(Control root)
{
Color backColor = root.BackColor;
Stack<Control> stack = new Stack<Control>();
stack.Push(root);
while (stack.Count > 0)
{
Control parent = stack.Pop();
var controls = parent.Controls;
for (int i = 0; i < controls.Count; i++)
{
Control control = controls[i];
if (control is Button button)
{
// Reset the BackColor of all Button controls.
button.UseVisualStyleBackColor = true;
}
else
{
// Update the BackColor for all child controls as some controls
// do not change the BackColor when the parent control does.
control.BackColor = backColor;
if (control.HasChildren)
{
stack.Push(control);
}
}
}
}
}
public static void UpdateControlForeColor(Control root)
{
Color foreColor = root.ForeColor;
Stack<Control> stack = new Stack<Control>();
stack.Push(root);
while (stack.Count > 0)
{
Control parent = stack.Pop();
var controls = parent.Controls;
for (int i = 0; i < controls.Count; i++)
{
Control control = controls[i];
if (control is Button button)
{
// Reset the ForeColor of all Button controls.
button.ForeColor = SystemColors.ControlText;
}
else if (control is LinkLabel link)
{
link.LinkColor = foreColor;
}
else
{
// Update the ForeColor for all child controls as some controls
// do not change the ForeColor when the parent control does.
control.ForeColor = foreColor;
if (control.HasChildren)
{
stack.Push(control);
}
}
}
}
}
}
}