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

Integrate Npp's dark mode APIs #104

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions Demo Plugin/NppManagedPluginDemo/Demo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public static void OnNotification(ScNotification notification)
{
Kbg.Demo.Namespace.Main.doInsertHtmlCloseTag((char)notification.Character);
}

// dark mode (de-)activated
if (notification.Header.Code == (uint)NppMsg.NPPN_DARKMODECHANGED)
{
INotepadPPGateway notepad = new NotepadPPGateway();
Kbg.Demo.Namespace.Main.ToggleDarkMode(notepad.IsDarkModeEnabled());
}
}

internal static string PluginName { get { return Kbg.Demo.Namespace.Main.PluginName; }}
Expand Down Expand Up @@ -177,6 +184,33 @@ static internal void PluginCleanUp()
{
Win32.WritePrivateProfileString(sectionName, keyName, doCloseTag ? "1" : "0", iniFilePath);
}

static internal void ToggleDarkMode(bool isDark)
{
if (frmGoToLine != null)
{
if (isDark)
{
IntPtr theme_ptr = notepad.GetDarkModeColors();
if (theme_ptr != IntPtr.Zero)
{
var theme = (DarkModeColors)Marshal.PtrToStructure(theme_ptr, typeof(DarkModeColors));
frmGoToLine.label1.BackColor = NppDarkMode.BGRToColor(theme.PureBackground);
frmGoToLine.label1.ForeColor = NppDarkMode.BGRToColor(theme.Text);
frmGoToLine.button1.BackColor = NppDarkMode.BGRToColor(theme.SofterBackground);
frmGoToLine.button1.ForeColor = NppDarkMode.BGRToColor(theme.Text);
Marshal.FreeHGlobal(theme_ptr);
}
}
else
{
frmGoToLine.label1.ResetBackColor();
frmGoToLine.label1.ResetForeColor();
frmGoToLine.button1.ResetBackColor();
frmGoToLine.button1.ResetForeColor();
}
}
}
#endregion

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

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

3 changes: 3 additions & 0 deletions Demo Plugin/NppManagedPluginDemo/NppManagedPluginDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<Compile Include="..\..\Visual Studio Project Template C#\PluginInfrastructure\ClikeStringArray.cs">
<Link>PluginInfrastructure\ClikeStringArray.cs</Link>
</Compile>
<Compile Include="..\..\Visual Studio Project Template C#\PluginInfrastructure\DarkMode.cs">
<Link>PluginInfrastructure\DarkMode.cs</Link>
</Compile>
<Compile Include="..\..\Visual Studio Project Template C#\PluginInfrastructure\NotepadPPGateway.cs">
<Link>PluginInfrastructure\NotepadPPGateway.cs</Link>
</Compile>
Expand Down
1 change: 1 addition & 0 deletions Visual Studio Project Template C#/$projectname$.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="PluginInfrastructure\Win32.cs" />
<Compile Include="Main.cs" />
<Compile Include="PluginInfrastructure\GatewayDomain.cs" />
<Compile Include="PluginInfrastructure\DarkMode.cs" />
<Compile Include="PluginInfrastructure\NotepadPPGateway.cs" />
<Compile Include="PluginInfrastructure\ScintillaGateway.cs" />
<Compile Include="PluginInfrastructure\IScintillaGateway.cs" />
Expand Down
56 changes: 56 additions & 0 deletions Visual Studio Project Template C#/PluginInfrastructure/DarkMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace Kbg.NppPluginNET.PluginInfrastructure
{
/// <summary>
/// Holds the BGR values of the active dark mode theme.
/// <see href "https://github.com/notepad-plus-plus/notepad-plus-plus/blob/master/PowerEditor/src/NppDarkMode.h"/>
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DarkModeColors
{
public int Background;
public int SofterBackground;
public int HotBackground;
public int PureBackground;
public int ErrorBackground;
public int Text;
public int DarkerText;
public int DisabledText;
public int LinkText;
public int Edge;
public int HotEdge;
public int DisabledEdge;
}

/// <summary>
/// Extends <see cref="NotepadPPGateway"/> with methods implementing Npp's dark mode API.
/// </summary>
public partial class NotepadPPGateway : INotepadPPGateway
{
public IntPtr GetDarkModeColors()
{
DarkModeColors darkModeColors = new DarkModeColors();
IntPtr _cbSize = new IntPtr(Marshal.SizeOf(darkModeColors));
IntPtr _ptrDarkModeColors = Marshal.AllocHGlobal(_cbSize);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETDARKMODECOLORS, _cbSize, _ptrDarkModeColors);
return _ptrDarkModeColors;
}

public bool IsDarkModeEnabled()
{
IntPtr result = Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_ISDARKMODEENABLED, Unused, Unused);
return ((int)result == 1);
}
}

static class NppDarkMode
{
public static Color BGRToColor(int bgr)
{
return Color.FromArgb((bgr & 0xFF), ((bgr >> 8) & 0xFF), ((bgr >> 16) & 0xFF));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum NppTbMsg : uint
DWS_ICONTAB = 0x00000001, // Icon for tabs are available
DWS_ICONBAR = 0x00000002, // Icon for icon bar are available (currently not supported)
DWS_ADDINFO = 0x00000004, // Additional information are in use
DWS_USEOWNDARKMODE = 0x00000008, // Use plugin's own dark mode
DWS_PARAMSALL = (DWS_ICONTAB | DWS_ICONBAR | DWS_ADDINFO),

// default docking values for first call of plugin
Expand Down
23 changes: 23 additions & 0 deletions Visual Studio Project Template C#/PluginInfrastructure/Msgs_h.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,21 @@ public enum NppMsg : uint
/// </summary>
NPPM_ADDTOOLBARICON_FORDARKMODE = Constants.NPPMSG + 101,

/// <summary>
/// bool NPPM_ISDARKMODEENABLED(0, 0)
/// Returns true when Notepad++ Dark Mode is enabled, false when it is not.
/// <see href="https://github.com/notepad-plus-plus/notepad-plus-plus/commit/1eb5b10e41d7ab92b60aa32b28d4fe7739d15b53"/>
/// </summary>
NPPM_ISDARKMODEENABLED = (Constants.NPPMSG + 107),

/// <summary>
/// bool NPPM_GETDARKMODECOLORS (size_t cbSize, NppDarkMode::Colors* returnColors)
/// - cbSize must be filled with sizeof(NppDarkMode::Colors).
/// - returnColors must be a pre-allocated NppDarkMode::Colors struct.
/// Returns true when successful, false otherwise.
/// </summary>
NPPM_GETDARKMODECOLORS = (Constants.NPPMSG + 108),

RUNCOMMAND_USER = Constants.WM_USER + 3000,
NPPM_GETFULLCURRENTPATH = RUNCOMMAND_USER + FULL_CURRENT_PATH,
NPPM_GETCURRENTDIRECTORY = RUNCOMMAND_USER + CURRENT_DIRECTORY,
Expand Down Expand Up @@ -805,6 +820,14 @@ public enum NppMsg : uint
/// </summary>
NPPN_FILEDELETED = NPPN_FIRST + 26,

/// <summary>
/// To notify plugins that Dark Mode was enabled/disabled
/// scnNotification->nmhdr.code = NPPN_DARKMODECHANGED;
/// scnNotification->nmhdr.hwndFrom = hwndNpp;
/// scnNotification->nmhdr.idFrom = 0;
/// </summary>
NPPN_DARKMODECHANGED = (NPPN_FIRST + 27)

/* --Autogenerated -- end of section automatically generated from notepad-plus-plus\PowerEditor\src\MISC\PluginsManager\Notepad_plus_msgs.h * */
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface INotepadPPGateway

void AddToolbarIcon(int funcItemsIndex, toolbarIcons icon);
void AddToolbarIcon(int funcItemsIndex, Bitmap icon);
bool IsDarkModeEnabled();
IntPtr GetDarkModeColors();
string GetNppPath();
string GetPluginConfigPath();
string GetCurrentFilePath();
Expand All @@ -25,7 +27,7 @@ public interface INotepadPPGateway
/// This class holds helpers for sending messages defined in the Msgs_h.cs file. It is at the moment
/// incomplete. Please help fill in the blanks.
/// </summary>
public class NotepadPPGateway : INotepadPPGateway
public partial class NotepadPPGateway : INotepadPPGateway
{
private const int Unused = 0;

Expand Down