From 16d35161bfad02813a2a1ffe48713d395416d0ca Mon Sep 17 00:00:00 2001 From: molsonkiko <46202915+molsonkiko@users.noreply.github.com> Date: Mon, 19 Jun 2023 20:53:51 -0700 Subject: [PATCH] Fix csproj, improve dark mode toggling 1. Add automatic handling of GroupBoxes, DataGridViews 2. Fix problems with .csproj caused by rebase --- Demo Plugin/NppManagedPluginDemo/Demo.cs | 92 +++++++++++----- .../Forms/DarkModeTestForm.Designer.cs | 101 ++++++++++++++---- .../Forms/DarkModeTestForm.cs | 7 +- .../Forms/DarkModeTestForm.resx | 20 +++- .../NppManagedPluginDemo.csproj | 13 +++ 5 files changed, 183 insertions(+), 50 deletions(-) diff --git a/Demo Plugin/NppManagedPluginDemo/Demo.cs b/Demo Plugin/NppManagedPluginDemo/Demo.cs index c975eb4..07e39cd 100644 --- a/Demo Plugin/NppManagedPluginDemo/Demo.cs +++ b/Demo Plugin/NppManagedPluginDemo/Demo.cs @@ -205,44 +205,56 @@ static internal void PluginCleanUp() /// </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) + static internal void ToggleDarkMode(Control ctrl, bool isDark) { - if (form == null) + if (ctrl == 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) + if (ctrl is Form form) { - // allow possibility that some forms will have other child forms - // JsonTools does this in a couple of places - ToggleDarkMode(childForm, isDark); + 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); + ctrl.BackColor = NppDarkMode.BGRToColor(theme.Background); + ctrl.ForeColor = NppDarkMode.BGRToColor(theme.Text); } else { - form.ResetForeColor(); - form.ResetBackColor(); + ctrl.ResetForeColor(); + ctrl.ResetBackColor(); } - foreach (Control ctrl in form.Controls) + foreach (Control child in ctrl.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) + var textTheme = child.Enabled ? theme.Text : theme.DisabledText; + Color foreColor = NppDarkMode.BGRToColor(textTheme); + Color backColor = NppDarkMode.BGRToColor(theme.PureBackground); + Color InBetween = Color.FromArgb( + foreColor.R / 4 + 3 * backColor.R / 4, + foreColor.G / 4 + 3 * backColor.G / 4, + foreColor.B / 4 + 3 * backColor.B / 4 + ); + if (child is GroupBox) + ToggleDarkMode(child, isDark); + else if (child is Button btn) { btn.BackColor = NppDarkMode.BGRToColor(theme.SofterBackground); - btn.ForeColor = NppDarkMode.BGRToColor(textTheme); + btn.ForeColor = foreColor; } - else if (ctrl is LinkLabel llbl) + else if (child is LinkLabel llbl) { llbl.BackColor = NppDarkMode.BGRToColor(theme.ErrorBackground); llbl.ForeColor = NppDarkMode.BGRToColor(theme.DarkerText); @@ -251,36 +263,60 @@ static internal void ToggleDarkMode(Form form, bool isDark) 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) + else if (child is TextBox + || child is Label + || child is ListBox + || child is ComboBox) { - ctrl.BackColor = NppDarkMode.BGRToColor(theme.PureBackground); - ctrl.ForeColor = NppDarkMode.BGRToColor(textTheme); + child.BackColor = backColor; + child.ForeColor = foreColor; } - else if (ctrl is TreeView tv) + else if (child is TreeView tv) { tv.BackColor = NppDarkMode.BGRToColor(theme.HotBackground); - tv.ForeColor = NppDarkMode.BGRToColor(textTheme); + tv.ForeColor = foreColor; + } + else if (child is DataGridView dgv) + { + dgv.EnableHeadersVisualStyles = false; + dgv.BackgroundColor = InBetween; + dgv.ForeColor = foreColor; + dgv.GridColor = foreColor; + dgv.ColumnHeadersDefaultCellStyle.ForeColor = foreColor; + dgv.ColumnHeadersDefaultCellStyle.BackColor = backColor; + dgv.RowHeadersDefaultCellStyle.ForeColor = foreColor; + dgv.RowHeadersDefaultCellStyle.BackColor = backColor; + dgv.RowsDefaultCellStyle.ForeColor = foreColor; + dgv.RowsDefaultCellStyle.BackColor = backColor; } else { // other controls I haven't thought of yet - ctrl.BackColor = NppDarkMode.BGRToColor(theme.SofterBackground); - ctrl.ForeColor = NppDarkMode.BGRToColor(textTheme); + child.BackColor = NppDarkMode.BGRToColor(theme.SofterBackground); + child.ForeColor = foreColor; } } else // normal light mode { - ctrl.ResetForeColor(); - ctrl.ResetBackColor(); - if (ctrl is LinkLabel llbl) + child.ResetForeColor(); + child.ResetBackColor(); + if (child is GroupBox) + ToggleDarkMode(child, isDark); + if (child is LinkLabel llbl) { llbl.LinkColor = Color.Blue; llbl.ActiveLinkColor = Color.Red; llbl.VisitedLinkColor = Color.Purple; } + else if (child is DataGridView dgv) + { + dgv.EnableHeadersVisualStyles = true; + dgv.BackgroundColor = SystemColors.ControlDark; + dgv.ForeColor = SystemColors.ControlText; + dgv.GridColor = SystemColors.ControlLight; + dgv.RowsDefaultCellStyle.ForeColor = SystemColors.ControlText; + dgv.RowsDefaultCellStyle.BackColor = SystemColors.Window; + } } } Marshal.FreeHGlobal(themePtr); diff --git a/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.Designer.cs b/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.Designer.cs index e223178..62be11c 100644 --- a/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.Designer.cs +++ b/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.Designer.cs @@ -45,20 +45,27 @@ private void InitializeComponent() System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DarkModeTestForm)); this.button1 = new System.Windows.Forms.Button(); this.treeView1 = new System.Windows.Forms.TreeView(); + this.imageList1 = new System.Windows.Forms.ImageList(this.components); this.listBox1 = new System.Windows.Forms.ListBox(); this.Title = new System.Windows.Forms.Label(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.linkLabel1 = new System.Windows.Forms.LinkLabel(); this.label1 = new System.Windows.Forms.Label(); this.checkBox1 = new System.Windows.Forms.CheckBox(); - this.imageList1 = new System.Windows.Forms.ImageList(this.components); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); + this.dataGridView1 = new System.Windows.Forms.DataGridView(); + this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); + this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // button1 // - this.button1.Location = new System.Drawing.Point(217, 301); + this.button1.Location = new System.Drawing.Point(217, 385); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; @@ -99,6 +106,14 @@ private void InitializeComponent() this.treeView1.Size = new System.Drawing.Size(178, 135); this.treeView1.TabIndex = 1; // + // imageList1 + // + this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); + this.imageList1.TransparentColor = System.Drawing.Color.Transparent; + this.imageList1.Images.SetKeyName(0, "star.png"); + this.imageList1.Images.SetKeyName(1, "star_black.ico"); + this.imageList1.Images.SetKeyName(2, "star_white.ico"); + // // listBox1 // this.listBox1.FormattingEnabled = true; @@ -134,7 +149,7 @@ private void InitializeComponent() "is", "a", "comboBox"}); - this.comboBox1.Location = new System.Drawing.Point(217, 98); + this.comboBox1.Location = new System.Drawing.Point(17, 27); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(121, 24); this.comboBox1.TabIndex = 4; @@ -143,7 +158,7 @@ private void InitializeComponent() // this.linkLabel1.AutoSize = true; this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(0, 10); - this.linkLabel1.Location = new System.Drawing.Point(376, 275); + this.linkLabel1.Location = new System.Drawing.Point(376, 359); this.linkLabel1.Name = "linkLabel1"; this.linkLabel1.Size = new System.Drawing.Size(145, 49); this.linkLabel1.TabIndex = 5; @@ -155,7 +170,7 @@ private void InitializeComponent() // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(36, 308); + this.label1.Location = new System.Drawing.Point(36, 392); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(105, 16); this.label1.TabIndex = 6; @@ -164,24 +179,16 @@ private void InitializeComponent() // checkBox1 // this.checkBox1.AutoSize = true; - this.checkBox1.Location = new System.Drawing.Point(179, 265); + this.checkBox1.Location = new System.Drawing.Point(179, 349); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(173, 20); this.checkBox1.TabIndex = 7; this.checkBox1.Text = "checkBox1 looks good?"; this.checkBox1.UseVisualStyleBackColor = true; // - // imageList1 - // - this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); - this.imageList1.TransparentColor = System.Drawing.Color.Transparent; - this.imageList1.Images.SetKeyName(0, "star.png"); - this.imageList1.Images.SetKeyName(1, "star_black.ico"); - this.imageList1.Images.SetKeyName(2, "star_white.ico"); - // // textBox1 // - this.textBox1.Location = new System.Drawing.Point(217, 190); + this.textBox1.Location = new System.Drawing.Point(17, 119); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(121, 23); @@ -191,29 +198,78 @@ private void InitializeComponent() // textBox2 // this.textBox2.Enabled = false; - this.textBox2.Location = new System.Drawing.Point(217, 144); + this.textBox2.Location = new System.Drawing.Point(17, 73); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(121, 22); this.textBox2.TabIndex = 9; this.textBox2.Text = "disabled textbox"; // + // dataGridView1 + // + this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.Column1, + this.Column2, + this.Column3}); + this.dataGridView1.Location = new System.Drawing.Point(12, 241); + this.dataGridView1.Name = "dataGridView1"; + this.dataGridView1.RowHeadersWidth = 51; + this.dataGridView1.RowTemplate.Height = 24; + this.dataGridView1.Size = new System.Drawing.Size(470, 84); + this.dataGridView1.TabIndex = 10; + // + // Column1 + // + this.Column1.HeaderText = "Column1"; + this.Column1.MinimumWidth = 6; + this.Column1.Name = "Column1"; + this.Column1.Width = 90; + // + // Column2 + // + this.Column2.HeaderText = "This is a DataGridView"; + this.Column2.MinimumWidth = 6; + this.Column2.Name = "Column2"; + this.Column2.Width = 200; + // + // Column3 + // + this.Column3.HeaderText = "Column3"; + this.Column3.MinimumWidth = 6; + this.Column3.Name = "Column3"; + this.Column3.Width = 90; + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.textBox2); + this.groupBox1.Controls.Add(this.textBox1); + this.groupBox1.Controls.Add(this.comboBox1); + this.groupBox1.Location = new System.Drawing.Point(208, 77); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(144, 158); + this.groupBox1.TabIndex = 11; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "groupBox1"; + // // DarkModeTestForm // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(577, 358); - this.Controls.Add(this.textBox2); - this.Controls.Add(this.textBox1); + this.ClientSize = new System.Drawing.Size(577, 429); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.dataGridView1); this.Controls.Add(this.checkBox1); this.Controls.Add(this.label1); this.Controls.Add(this.linkLabel1); - this.Controls.Add(this.comboBox1); this.Controls.Add(this.Title); this.Controls.Add(this.listBox1); this.Controls.Add(this.treeView1); this.Controls.Add(this.button1); this.Name = "DarkModeTestForm"; this.Text = "DarkModeTestForm"; + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -232,5 +288,10 @@ private void InitializeComponent() private System.Windows.Forms.ImageList imageList1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; + private System.Windows.Forms.DataGridView dataGridView1; + private System.Windows.Forms.DataGridViewTextBoxColumn Column1; + private System.Windows.Forms.DataGridViewTextBoxColumn Column2; + private System.Windows.Forms.DataGridViewTextBoxColumn Column3; + private System.Windows.Forms.GroupBox groupBox1; } } \ No newline at end of file diff --git a/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.cs b/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.cs index a0aeb39..95a166f 100644 --- a/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.cs +++ b/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.ComponentModel; -using System.Data; using System.Drawing; using System.Linq; using System.Text; @@ -18,6 +17,12 @@ public DarkModeTestForm() var notepad = new NotepadPPGateway(); Main.ToggleDarkMode(this, notepad.IsDarkModeEnabled()); comboBox1.SelectedIndex = 0; + DataGridViewRow row = new DataGridViewRow(); + row.CreateCells(dataGridView1); + row.Cells[0].Value = "Value1"; + row.Cells[1].Value = "Should look pretty"; + row.Cells[2].Value = "Value3"; + dataGridView1.Rows.Add(row); } private void LinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) diff --git a/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.resx b/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.resx index 811e322..9914dc2 100644 --- a/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.resx +++ b/Demo Plugin/NppManagedPluginDemo/Forms/DarkModeTestForm.resx @@ -125,7 +125,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAADw - CgAAAk1TRnQBSQFMAgEBAwEAAQgBAAEIAQABEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo + CgAAAk1TRnQBSQFMAgEBAwEAARABAAEQAQABEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5 AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA @@ -174,4 +174,22 @@ AgABgAEBAccB8wHHAfMCAAHgAQcB4AEHAeABBwIAAfABDwH4AR8B+AEfAgAL </value> </data> + <metadata name="Column1.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>True</value> + </metadata> + <metadata name="Column2.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>True</value> + </metadata> + <metadata name="Column3.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>True</value> + </metadata> + <metadata name="Column1.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>True</value> + </metadata> + <metadata name="Column2.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>True</value> + </metadata> + <metadata name="Column3.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>True</value> + </metadata> </root> \ No newline at end of file diff --git a/Demo Plugin/NppManagedPluginDemo/NppManagedPluginDemo.csproj b/Demo Plugin/NppManagedPluginDemo/NppManagedPluginDemo.csproj index 77e3ad2..55e9cc4 100644 --- a/Demo Plugin/NppManagedPluginDemo/NppManagedPluginDemo.csproj +++ b/Demo Plugin/NppManagedPluginDemo/NppManagedPluginDemo.csproj @@ -117,12 +117,21 @@ <Compile Include="..\..\Visual Studio Project Template C#\PluginInfrastructure\NanInf.cs"> <Link>PluginInfrastructure\NanInf.cs</Link> </Compile> + <Compile Include="..\..\Visual Studio Project Template C#\PluginInfrastructure\DarkMode.cs"> + <Link>PluginInfrastructure\DarkMode.cs</Link> + </Compile> <Compile Include="Forms\frmGoToLine.cs"> <SubType>Form</SubType> </Compile> <Compile Include="Forms\frmGoToLine.designer.cs"> <DependentUpon>frmGoToLine.cs</DependentUpon> </Compile> + <Compile Include="Forms\DarkModeTestForm.cs"> + <SubType>Form</SubType> + </Compile> + <Compile Include="Forms\DarkModeTestForm.designer.cs"> + <DependentUpon>DarkModeTestForm.cs</DependentUpon> + </Compile> <Compile Include="Demo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\Resources.Designer.cs"> @@ -144,6 +153,10 @@ <DependentUpon>frmGoToLine.cs</DependentUpon> <SubType>Designer</SubType> </EmbeddedResource> + <EmbeddedResource Include="Forms\DarkModeTestForm.resx"> + <DependentUpon>DarkModeTestForm.cs</DependentUpon> + <SubType>Designer</SubType> + </EmbeddedResource> <EmbeddedResource Include="Properties\Resources.resx"> <SubType>Designer</SubType> <Generator>ResXFileCodeGenerator</Generator>