Skip to content

Commit

Permalink
apply dark mode to all forms automatically
Browse files Browse the repository at this point in the history
Based on kbilsted#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.
  • Loading branch information
molsonkiko committed Jun 20, 2023
1 parent 4f7a4d4 commit 39bd4b8
Show file tree
Hide file tree
Showing 12 changed files with 728 additions and 62 deletions.
111 changes: 109 additions & 2 deletions Demo Plugin/NppManagedPluginDemo/Demo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Imaging;
Expand Down Expand Up @@ -38,7 +40,13 @@ public static void OnNotification(ScNotification notification)
{
if (notification.Header.Code == (uint)SciMsg.SCN_CHARADDED)
{
Kbg.Demo.Namespace.Main.doInsertHtmlCloseTag((char)notification.Character);
Demo.Namespace.Main.doInsertHtmlCloseTag((char)notification.Character);
}
// dark mode (de-)activated
if (notification.Header.Code == (uint)NppMsg.NPPN_DARKMODECHANGED)
{
INotepadPPGateway notepad = new NotepadPPGateway();
Demo.Namespace.Main.ToggleDarkMode(Demo.Namespace.Main.frmGoToLine, notepad.IsDarkModeEnabled());
}
}

Expand All @@ -57,7 +65,7 @@ class Main
static string keyName = "doCloseTag";
static bool doCloseTag = false;
static string sessionFilePath = @"C:\text.session";
static frmGoToLine frmGoToLine = null;
static internal frmGoToLine frmGoToLine = null;
static internal int idFrmGotToLine = -1;

// toolbar icons
Expand Down Expand Up @@ -179,6 +187,104 @@ static internal void PluginCleanUp()
{
Win32.WritePrivateProfileString(sectionName, keyName, doCloseTag ? "1" : "0", iniFilePath);
}

/// <summary>
/// Apply dark mode (or re-apply light mode) to the controls of any form.<br></br>
/// This method currently supports colorizing the following types of controls:<br></br>
/// - Buttons<br></br>
/// - Labels<br></br>
/// - LinkLabels<br></br>
/// - ComboBoxes<br></br>
/// - CheckBoxes<br></br>
/// - ListBoxes<br></br>
/// - TreeViews<br></br>
/// Feel free to add more as needed.<br></br>
/// TODO: Figure out best way to customize border colors of controls.
/// https://stackoverflow.com/questions/1445472/how-to-change-the-form-border-color-c
/// may be a lead.
/// </summary>
/// <param name="form">a Windows Form</param>
/// <param name="isDark">is Notepad++ dark mode on?</param>
static internal void ToggleDarkMode(Form form, bool isDark)
{
if (form == null)
return;
IntPtr themePtr = notepad.GetDarkModeColors();
if (isDark && themePtr == IntPtr.Zero)
return;
var theme = (DarkModeColors)Marshal.PtrToStructure(themePtr, typeof(DarkModeColors));
foreach (Form childForm in form.OwnedForms)
{
// allow possibility that some forms will have other child forms
// JsonTools does this in a couple of places
ToggleDarkMode(childForm, isDark);
}
if (isDark)
{
form.BackColor = NppDarkMode.BGRToColor(theme.Background);
form.ForeColor = NppDarkMode.BGRToColor(theme.Text);
}
else
{
form.ResetForeColor();
form.ResetBackColor();
}
foreach (Control ctrl in form.Controls)
{
if (isDark)
{
// this doesn't actually make disabled controls have different colors
// windows forms don't make it easy for the user to choose the
// color of a disabled control. See https://stackoverflow.com/questions/136129/windows-forms-how-do-you-change-the-font-color-for-a-disabled-label
var textTheme = ctrl.Enabled ? theme.Text : theme.DisabledText;
if (ctrl is Button btn)
{
btn.BackColor = NppDarkMode.BGRToColor(theme.SofterBackground);
btn.ForeColor = NppDarkMode.BGRToColor(textTheme);
}
else if (ctrl is LinkLabel llbl)
{
llbl.BackColor = NppDarkMode.BGRToColor(theme.ErrorBackground);
llbl.ForeColor = NppDarkMode.BGRToColor(theme.DarkerText);
llbl.LinkColor = NppDarkMode.BGRToColor(theme.LinkText);
llbl.ActiveLinkColor = NppDarkMode.BGRToColor(theme.Text);
llbl.VisitedLinkColor = NppDarkMode.BGRToColor(theme.DarkerText);
}
// other common text-based controls
else if (ctrl is TextBox
|| ctrl is Label
|| ctrl is ListBox
|| ctrl is ComboBox)
{
ctrl.BackColor = NppDarkMode.BGRToColor(theme.PureBackground);
ctrl.ForeColor = NppDarkMode.BGRToColor(textTheme);
}
else if (ctrl is TreeView tv)
{
tv.BackColor = NppDarkMode.BGRToColor(theme.HotBackground);
tv.ForeColor = NppDarkMode.BGRToColor(textTheme);
}
else
{
// other controls I haven't thought of yet
ctrl.BackColor = NppDarkMode.BGRToColor(theme.SofterBackground);
ctrl.ForeColor = NppDarkMode.BGRToColor(textTheme);
}
}
else // normal light mode
{
ctrl.ResetForeColor();
ctrl.ResetBackColor();
if (ctrl is LinkLabel llbl)
{
llbl.LinkColor = Color.Blue;
llbl.ActiveLinkColor = Color.Red;
llbl.VisitedLinkColor = Color.Purple;
}
}
}
Marshal.FreeHGlobal(themePtr);
}
#endregion

#region " Menu functions "
Expand Down Expand Up @@ -443,6 +549,7 @@ static void DockableDlgDemo()
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_SETMENUITEMCHECK, PluginBase._funcItems.Items[idFrmGotToLine]._cmdID, 0);
}
}
ToggleDarkMode(frmGoToLine, notepad.IsDarkModeEnabled());
frmGoToLine.textBox1.Focus();
}

Expand Down
236 changes: 236 additions & 0 deletions Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 39bd4b8

Please sign in to comment.