Skip to content

Commit 790f500

Browse files
Merge pull request #45 from molsonkiko/dark_mode_support
add dark mode support, warnings for big dirs
2 parents d501926 + ba9f279 commit 790f500

File tree

10 files changed

+518
-57
lines changed

10 files changed

+518
-57
lines changed

NppNavigateTo/FormStyle.cs

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
using Kbg.NppPluginNET.PluginInfrastructure;
2+
using System;
3+
using System.Drawing;
4+
using System.Runtime.InteropServices;
5+
using System.Windows.Forms;
6+
7+
namespace NavigateTo.Plugin.Namespace
8+
{
9+
public class FormStyle
10+
{
11+
public static Color SlightlyDarkControl = Color.FromArgb(
12+
3 * SystemColors.Control.R / 4 + SystemColors.ControlDark.R / 4,
13+
3 * SystemColors.Control.G / 4 + SystemColors.ControlDark.G / 4,
14+
3 * SystemColors.Control.B / 4 + SystemColors.ControlDark.B / 4
15+
);
16+
17+
18+
/// <summary>
19+
/// Changes the background and text color of the form
20+
/// and any child forms to match the editor window.<br></br>
21+
/// Fires when the form is first opened
22+
/// and also whenever the style is changed.<br></br>
23+
/// Heavily based on CsvQuery (https://github.com/jokedst/CsvQuery)
24+
/// </summary>
25+
public static void ApplyStyle(Control ctrl, bool use_npp_style, bool isDark = false)
26+
{
27+
if (ctrl == null || ctrl.IsDisposed) return;
28+
int[] version = Main.notepad.GetNppVersion();
29+
if (version[0] < 8)
30+
use_npp_style = false; // trying to follow editor style looks weird for Notepad++ 7.3.3
31+
if (ctrl is Form form)
32+
{
33+
foreach (Form childForm in form.OwnedForms)
34+
{
35+
ApplyStyle(childForm, use_npp_style, isDark);
36+
}
37+
}
38+
Color backColor = Main.notepad.GetDefaultBackgroundColor();
39+
Color foreColor = Main.notepad.GetDefaultForegroundColor();
40+
Color InBetween = Color.FromArgb(
41+
foreColor.R / 4 + 3 * backColor.R / 4,
42+
foreColor.G / 4 + 3 * backColor.G / 4,
43+
foreColor.B / 4 + 3 * backColor.B / 4
44+
);
45+
IntPtr themePtr = Main.notepad.GetDarkModeColors();
46+
if (isDark && themePtr != IntPtr.Zero)
47+
{
48+
var theme = (DarkModeColors)Marshal.PtrToStructure(themePtr, typeof(DarkModeColors));
49+
ctrl.BackColor = NppDarkMode.BGRToColor(theme.Background);
50+
ctrl.ForeColor = NppDarkMode.BGRToColor(theme.Text);
51+
foreach (Control child in ctrl.Controls)
52+
{
53+
// this doesn't actually make disabled controls have different colors
54+
// windows forms don't make it easy for the user to choose the
55+
// color of a disabled control. See https://stackoverflow.com/questions/136129/windows-forms-how-do-you-change-the-font-color-for-a-disabled-label
56+
var textTheme = child.Enabled ? theme.Text : theme.DisabledText;
57+
if (child is GroupBox)
58+
ApplyStyle(child, use_npp_style, isDark);
59+
else if (child is Button btn)
60+
{
61+
btn.BackColor = NppDarkMode.BGRToColor(theme.SofterBackground);
62+
btn.ForeColor = NppDarkMode.BGRToColor(textTheme);
63+
}
64+
else if (child is LinkLabel llbl)
65+
{
66+
llbl.BackColor = NppDarkMode.BGRToColor(theme.PureBackground);
67+
llbl.ForeColor = NppDarkMode.BGRToColor(textTheme);
68+
llbl.LinkColor = NppDarkMode.BGRToColor(theme.LinkText);
69+
llbl.ActiveLinkColor = NppDarkMode.BGRToColor(textTheme);
70+
llbl.VisitedLinkColor = NppDarkMode.BGRToColor(theme.DarkerText);
71+
}
72+
// other common text-based controls
73+
else if (child is TextBox
74+
|| child is Label
75+
|| child is ListBox
76+
|| child is ComboBox)
77+
{
78+
child.BackColor = NppDarkMode.BGRToColor(theme.PureBackground);
79+
child.ForeColor = NppDarkMode.BGRToColor(textTheme);
80+
}
81+
else if (child is TreeView tv)
82+
{
83+
tv.BackColor = NppDarkMode.BGRToColor(theme.HotBackground);
84+
tv.ForeColor = NppDarkMode.BGRToColor(textTheme);
85+
}
86+
else if (child is DataGridView dgv)
87+
{
88+
// for reasons that I cannot fathom, everything EXCEPT the background gets correctly colored
89+
// when you first open the form in dark mode.
90+
// The background is untouched by the current style until you change to another style and then back,
91+
// at which point the background remembers what color it's supposed to be. Very odd.
92+
dgv.BackgroundColor = InBetween;
93+
dgv.ForeColor = NppDarkMode.BGRToColor(textTheme);
94+
dgv.GridColor = NppDarkMode.BGRToColor(theme.HotBackground);
95+
dgv.RowHeadersDefaultCellStyle.ForeColor = NppDarkMode.BGRToColor(textTheme);
96+
dgv.RowHeadersDefaultCellStyle.BackColor = NppDarkMode.BGRToColor(theme.PureBackground);
97+
dgv.RowsDefaultCellStyle.ForeColor = NppDarkMode.BGRToColor(textTheme);
98+
dgv.RowsDefaultCellStyle.BackColor = NppDarkMode.BGRToColor(theme.PureBackground);
99+
}
100+
else
101+
{
102+
// other controls I haven't thought of yet
103+
child.BackColor = NppDarkMode.BGRToColor(theme.SofterBackground);
104+
child.ForeColor = NppDarkMode.BGRToColor(textTheme);
105+
}
106+
}
107+
Marshal.FreeHGlobal(themePtr);
108+
return;
109+
}
110+
else if (!use_npp_style || (
111+
backColor.R > 240 &&
112+
backColor.G > 240 &&
113+
backColor.B > 240))
114+
{
115+
// if the background is basically white,
116+
// use the system defaults because they
117+
// look best on a white or nearly white background
118+
ctrl.BackColor = SystemColors.Control;
119+
ctrl.ForeColor = SystemColors.ControlText;
120+
foreach (Control child in ctrl.Controls)
121+
{
122+
if (child is GroupBox)
123+
ApplyStyle(child, use_npp_style, isDark);
124+
// controls containing text
125+
else if (child is TextBox || child is ListBox || child is ComboBox || child is TreeView)
126+
{
127+
child.BackColor = SystemColors.Window; // white background
128+
child.ForeColor = SystemColors.WindowText;
129+
}
130+
else if (child is LinkLabel llbl)
131+
{
132+
llbl.LinkColor = Color.Blue;
133+
llbl.ActiveLinkColor = Color.Red;
134+
llbl.VisitedLinkColor = Color.Purple;
135+
}
136+
else if (child is DataGridView dgv)
137+
{
138+
dgv.BackgroundColor = SystemColors.ControlDark;
139+
dgv.ForeColor = SystemColors.ControlText;
140+
dgv.GridColor = SystemColors.ControlDarkDark;
141+
dgv.RowHeadersDefaultCellStyle.ForeColor = SystemColors.WindowText;
142+
dgv.RowHeadersDefaultCellStyle.BackColor = SystemColors.Window;
143+
dgv.RowsDefaultCellStyle.ForeColor = SystemColors.ControlText;
144+
dgv.RowsDefaultCellStyle.BackColor = SystemColors.Window;
145+
}
146+
else
147+
{
148+
// buttons should be a bit darker but everything else is the same color as the background
149+
child.BackColor = (child is Button) ? SlightlyDarkControl : SystemColors.Control;
150+
child.ForeColor = SystemColors.ControlText;
151+
}
152+
}
153+
return;
154+
}
155+
// use NPP styling for non-dark-mode
156+
ctrl.BackColor = backColor;
157+
foreach (Control child in ctrl.Controls)
158+
{
159+
child.BackColor = backColor;
160+
child.ForeColor = foreColor;
161+
if (child is LinkLabel llbl)
162+
{
163+
llbl.LinkColor = foreColor;
164+
llbl.ActiveLinkColor = foreColor;
165+
llbl.VisitedLinkColor = foreColor;
166+
}
167+
else if (child is DataGridView dgv)
168+
{
169+
dgv.BackgroundColor = InBetween;
170+
dgv.ForeColor = foreColor;
171+
dgv.GridColor = foreColor;
172+
dgv.RowHeadersDefaultCellStyle.ForeColor = foreColor;
173+
dgv.RowHeadersDefaultCellStyle.BackColor = backColor;
174+
dgv.RowsDefaultCellStyle.ForeColor = foreColor;
175+
dgv.RowsDefaultCellStyle.BackColor = backColor;
176+
}
177+
}
178+
}
179+
}
180+
}

NppNavigateTo/Forms/AboutForm.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public AboutForm()
1717
{
1818
InitializeComponent();
1919
Title.Text = $"NavigateTo v{AssemblyVersionString()}";
20+
FormStyle.ApplyStyle(this, true, Main.notepad.IsDarkModeEnabled());
2021
}
2122

2223
/// <summary>

0 commit comments

Comments
 (0)