2
2
using System ;
3
3
using System . IO ;
4
4
using System . Text ;
5
+ using System . Collections . Generic ;
5
6
using System . Drawing ;
7
+ using System . Reflection ;
6
8
using System . Threading ;
7
9
using System . Windows . Forms ;
8
10
using System . Drawing . Imaging ;
@@ -38,7 +40,13 @@ public static void OnNotification(ScNotification notification)
38
40
{
39
41
if ( notification . Header . Code == ( uint ) SciMsg . SCN_CHARADDED )
40
42
{
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 ( ) ) ;
42
50
}
43
51
}
44
52
@@ -57,7 +65,7 @@ class Main
57
65
static string keyName = "doCloseTag" ;
58
66
static bool doCloseTag = false ;
59
67
static string sessionFilePath = @"C:\text.session" ;
60
- static frmGoToLine frmGoToLine = null ;
68
+ static internal frmGoToLine frmGoToLine = null ;
61
69
static internal int idFrmGotToLine = - 1 ;
62
70
63
71
// toolbar icons
@@ -179,6 +187,104 @@ static internal void PluginCleanUp()
179
187
{
180
188
Win32 . WritePrivateProfileString ( sectionName , keyName , doCloseTag ? "1" : "0" , iniFilePath ) ;
181
189
}
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
+ }
182
288
#endregion
183
289
184
290
#region " Menu functions "
@@ -443,6 +549,7 @@ static void DockableDlgDemo()
443
549
Win32 . SendMessage ( PluginBase . nppData . _nppHandle , ( uint ) NppMsg . NPPM_SETMENUITEMCHECK , PluginBase . _funcItems . Items [ idFrmGotToLine ] . _cmdID , 0 ) ;
444
550
}
445
551
}
552
+ ToggleDarkMode ( frmGoToLine , notepad . IsDarkModeEnabled ( ) ) ;
446
553
frmGoToLine . textBox1 . Focus ( ) ;
447
554
}
448
555
0 commit comments