Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit 39bd4b8

Browse files
committed
apply dark mode to all forms automatically
Based on #104 Rdipardo did all the hard work here, this is just a refinement. This makes it easy for plugin makers to apply dark mode to all forms. Rather than having to individually select themes to apply for each control, sensible defaults are applied based on each control's type. This PR also allows the color changes to propagate recursively to a form's child forms, if it has any.
1 parent 4f7a4d4 commit 39bd4b8

File tree

12 files changed

+728
-62
lines changed

12 files changed

+728
-62
lines changed

Demo Plugin/NppManagedPluginDemo/Demo.cs

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using System;
33
using System.IO;
44
using System.Text;
5+
using System.Collections.Generic;
56
using System.Drawing;
7+
using System.Reflection;
68
using System.Threading;
79
using System.Windows.Forms;
810
using System.Drawing.Imaging;
@@ -38,7 +40,13 @@ public static void OnNotification(ScNotification notification)
3840
{
3941
if (notification.Header.Code == (uint)SciMsg.SCN_CHARADDED)
4042
{
41-
Kbg.Demo.Namespace.Main.doInsertHtmlCloseTag((char)notification.Character);
43+
Demo.Namespace.Main.doInsertHtmlCloseTag((char)notification.Character);
44+
}
45+
// dark mode (de-)activated
46+
if (notification.Header.Code == (uint)NppMsg.NPPN_DARKMODECHANGED)
47+
{
48+
INotepadPPGateway notepad = new NotepadPPGateway();
49+
Demo.Namespace.Main.ToggleDarkMode(Demo.Namespace.Main.frmGoToLine, notepad.IsDarkModeEnabled());
4250
}
4351
}
4452

@@ -57,7 +65,7 @@ class Main
5765
static string keyName = "doCloseTag";
5866
static bool doCloseTag = false;
5967
static string sessionFilePath = @"C:\text.session";
60-
static frmGoToLine frmGoToLine = null;
68+
static internal frmGoToLine frmGoToLine = null;
6169
static internal int idFrmGotToLine = -1;
6270

6371
// toolbar icons
@@ -179,6 +187,104 @@ static internal void PluginCleanUp()
179187
{
180188
Win32.WritePrivateProfileString(sectionName, keyName, doCloseTag ? "1" : "0", iniFilePath);
181189
}
190+
191+
/// <summary>
192+
/// Apply dark mode (or re-apply light mode) to the controls of any form.<br></br>
193+
/// This method currently supports colorizing the following types of controls:<br></br>
194+
/// - Buttons<br></br>
195+
/// - Labels<br></br>
196+
/// - LinkLabels<br></br>
197+
/// - ComboBoxes<br></br>
198+
/// - CheckBoxes<br></br>
199+
/// - ListBoxes<br></br>
200+
/// - TreeViews<br></br>
201+
/// Feel free to add more as needed.<br></br>
202+
/// TODO: Figure out best way to customize border colors of controls.
203+
/// https://stackoverflow.com/questions/1445472/how-to-change-the-form-border-color-c
204+
/// may be a lead.
205+
/// </summary>
206+
/// <param name="form">a Windows Form</param>
207+
/// <param name="isDark">is Notepad++ dark mode on?</param>
208+
static internal void ToggleDarkMode(Form form, bool isDark)
209+
{
210+
if (form == null)
211+
return;
212+
IntPtr themePtr = notepad.GetDarkModeColors();
213+
if (isDark && themePtr == IntPtr.Zero)
214+
return;
215+
var theme = (DarkModeColors)Marshal.PtrToStructure(themePtr, typeof(DarkModeColors));
216+
foreach (Form childForm in form.OwnedForms)
217+
{
218+
// allow possibility that some forms will have other child forms
219+
// JsonTools does this in a couple of places
220+
ToggleDarkMode(childForm, isDark);
221+
}
222+
if (isDark)
223+
{
224+
form.BackColor = NppDarkMode.BGRToColor(theme.Background);
225+
form.ForeColor = NppDarkMode.BGRToColor(theme.Text);
226+
}
227+
else
228+
{
229+
form.ResetForeColor();
230+
form.ResetBackColor();
231+
}
232+
foreach (Control ctrl in form.Controls)
233+
{
234+
if (isDark)
235+
{
236+
// this doesn't actually make disabled controls have different colors
237+
// windows forms don't make it easy for the user to choose the
238+
// color of a disabled control. See https://stackoverflow.com/questions/136129/windows-forms-how-do-you-change-the-font-color-for-a-disabled-label
239+
var textTheme = ctrl.Enabled ? theme.Text : theme.DisabledText;
240+
if (ctrl is Button btn)
241+
{
242+
btn.BackColor = NppDarkMode.BGRToColor(theme.SofterBackground);
243+
btn.ForeColor = NppDarkMode.BGRToColor(textTheme);
244+
}
245+
else if (ctrl is LinkLabel llbl)
246+
{
247+
llbl.BackColor = NppDarkMode.BGRToColor(theme.ErrorBackground);
248+
llbl.ForeColor = NppDarkMode.BGRToColor(theme.DarkerText);
249+
llbl.LinkColor = NppDarkMode.BGRToColor(theme.LinkText);
250+
llbl.ActiveLinkColor = NppDarkMode.BGRToColor(theme.Text);
251+
llbl.VisitedLinkColor = NppDarkMode.BGRToColor(theme.DarkerText);
252+
}
253+
// other common text-based controls
254+
else if (ctrl is TextBox
255+
|| ctrl is Label
256+
|| ctrl is ListBox
257+
|| ctrl is ComboBox)
258+
{
259+
ctrl.BackColor = NppDarkMode.BGRToColor(theme.PureBackground);
260+
ctrl.ForeColor = NppDarkMode.BGRToColor(textTheme);
261+
}
262+
else if (ctrl is TreeView tv)
263+
{
264+
tv.BackColor = NppDarkMode.BGRToColor(theme.HotBackground);
265+
tv.ForeColor = NppDarkMode.BGRToColor(textTheme);
266+
}
267+
else
268+
{
269+
// other controls I haven't thought of yet
270+
ctrl.BackColor = NppDarkMode.BGRToColor(theme.SofterBackground);
271+
ctrl.ForeColor = NppDarkMode.BGRToColor(textTheme);
272+
}
273+
}
274+
else // normal light mode
275+
{
276+
ctrl.ResetForeColor();
277+
ctrl.ResetBackColor();
278+
if (ctrl is LinkLabel llbl)
279+
{
280+
llbl.LinkColor = Color.Blue;
281+
llbl.ActiveLinkColor = Color.Red;
282+
llbl.VisitedLinkColor = Color.Purple;
283+
}
284+
}
285+
}
286+
Marshal.FreeHGlobal(themePtr);
287+
}
182288
#endregion
183289

184290
#region " Menu functions "
@@ -443,6 +549,7 @@ static void DockableDlgDemo()
443549
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_SETMENUITEMCHECK, PluginBase._funcItems.Items[idFrmGotToLine]._cmdID, 0);
444550
}
445551
}
552+
ToggleDarkMode(frmGoToLine, notepad.IsDarkModeEnabled());
446553
frmGoToLine.textBox1.Focus();
447554
}
448555

Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.Designer.cs

Lines changed: 236 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)