From ada34d4c32c9c2c278256ac27d3543161753d6d8 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 11 Nov 2021 22:31:59 +1100 Subject: [PATCH] GUI: Fix icon not showing in popup dialogs on Mono This took a long time to figure out because 1) for testing purposes I set Icon before the window handle had been created 2) When the window handle was later created, mono would not actually apply the custom icon since the border style is FixedDialog (see CreateHandle in Form.cs) 3) I later set Icon again in multiple other places. However, this was essentially a noop because Mono checked if the new icon was the same as the same previously assigned icon and do nothing in that case. However, the assumption in step 3) was incorrect because the previously assigned icon had not actually been applied to the window. Unfortunately this meant the correct Icon never showed at all. So the correct solution to this is to only assign Icon once in the Load event of forms. I spent way too much time on this --- GUI/GuiUtils.cs | 8 ++++++++ GUI/Popups/ColorSelector.Designer.cs | 1 + GUI/Popups/ColorSelector.cs | 10 ++++++++-- GUI/Popups/CustomCommands.Designer.cs | 1 + GUI/Popups/CustomCommands.cs | 4 ++++ GUI/Popups/EditText.Designer.cs | 1 + GUI/Popups/EditText.cs | 10 ++++++++-- GUI/Popups/PortTools.Designer.cs | 1 + GUI/Popups/PortTools.cs | 10 ++++++++-- GUI/Popups/TokenSelector.Designer.cs | 1 + GUI/Popups/TokenSelector.cs | 10 ++++++++-- GUI/PropertyWindow/PropertyWindow.Commands.cs | 2 +- GUI/PropertyWindow/PropertyWindow.cs | 10 ++++++---- GUI/Window/Window.cs | 5 ++--- 14 files changed, 58 insertions(+), 16 deletions(-) diff --git a/GUI/GuiUtils.cs b/GUI/GuiUtils.cs index d021692d2..79abba895 100644 --- a/GUI/GuiUtils.cs +++ b/GUI/GuiUtils.cs @@ -17,6 +17,7 @@ */ using System; using System.Diagnostics; +using System.Drawing; using System.Windows.Forms; namespace MCGalaxy.Gui @@ -49,6 +50,13 @@ namespace MCGalaxy.Gui public static class GuiUtils { + /// MCGalaxy window icon (shared) + public static Icon WinIcon; + + public static void SetIcon(Form form) { + try { form.Icon = WinIcon; } catch { } + } + /// Opens the given url in the system's default web browser /// Catches and logs any unhandled errors public static void OpenBrowser(string url) { diff --git a/GUI/Popups/ColorSelector.Designer.cs b/GUI/Popups/ColorSelector.Designer.cs index ebb07a258..6b35604f3 100644 --- a/GUI/Popups/ColorSelector.Designer.cs +++ b/GUI/Popups/ColorSelector.Designer.cs @@ -29,6 +29,7 @@ this.Controls.Add(this.btnCancel); this.Font = new System.Drawing.Font("Calibri", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; + this.Load += new System.EventHandler(this.ColorSelector_Load); this.MaximizeBox = false; this.MinimizeBox = false; this.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); diff --git a/GUI/Popups/ColorSelector.cs b/GUI/Popups/ColorSelector.cs index 24714fbdb..668597333 100644 --- a/GUI/Popups/ColorSelector.cs +++ b/GUI/Popups/ColorSelector.cs @@ -4,8 +4,10 @@ using System.Drawing; using System; using MCGalaxy; -namespace MCGalaxy.Gui.Popups { - internal sealed partial class ColorSelector : Form { +namespace MCGalaxy.Gui.Popups +{ + internal sealed partial class ColorSelector : Form + { public char ColorCode; internal static Color LookupColor(char colCode, out Color textCol) { @@ -46,6 +48,10 @@ namespace MCGalaxy.Gui.Popups { ResumeLayout(false); } + void ColorSelector_Load(object sender, EventArgs e) { + GuiUtils.SetIcon(this); + } + const int btnWidth = 130, btnHeight = 40, btnsPerCol = 8; int index = 0; diff --git a/GUI/Popups/CustomCommands.Designer.cs b/GUI/Popups/CustomCommands.Designer.cs index 2404ad4da..6043466c4 100644 --- a/GUI/Popups/CustomCommands.Designer.cs +++ b/GUI/Popups/CustomCommands.Designer.cs @@ -150,6 +150,7 @@ this.Controls.Add(this.btnUnload); this.Font = new System.Drawing.Font("Calibri", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; + this.Load += new System.EventHandler(this.CustomCommands_Load); this.MaximizeBox = false; this.MinimizeBox = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; diff --git a/GUI/Popups/CustomCommands.cs b/GUI/Popups/CustomCommands.cs index 7d84d5ecb..11b8f5a8f 100644 --- a/GUI/Popups/CustomCommands.cs +++ b/GUI/Popups/CustomCommands.cs @@ -32,6 +32,10 @@ namespace MCGalaxy.Gui.Popups { if (!Command.IsCore(cmd)) lstCommands.Items.Add(cmd.name); } } + + void CustomCommands_Load(object sender, EventArgs e) { + GuiUtils.SetIcon(this); + } void CreateCommand(ICompiler engine) { string cmdName = txtCmdName.Text.Trim(); diff --git a/GUI/Popups/EditText.Designer.cs b/GUI/Popups/EditText.Designer.cs index fa2c64372..e796a8c2a 100644 --- a/GUI/Popups/EditText.Designer.cs +++ b/GUI/Popups/EditText.Designer.cs @@ -97,6 +97,7 @@ namespace MCGalaxy.Gui.Popups this.Controls.Add(this.cmbList); this.Font = new System.Drawing.Font("Calibri", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; + this.Load += new System.EventHandler(this.EditText_Load); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "EditText"; diff --git a/GUI/Popups/EditText.cs b/GUI/Popups/EditText.cs index 3e7f6c024..563d25218 100644 --- a/GUI/Popups/EditText.cs +++ b/GUI/Popups/EditText.cs @@ -20,8 +20,10 @@ using System.IO; using System.Windows.Forms; using MCGalaxy.Util; -namespace MCGalaxy.Gui.Popups { - public partial class EditText : Form { +namespace MCGalaxy.Gui.Popups +{ + public partial class EditText : Form + { TextFile curFile; public EditText() { @@ -32,6 +34,10 @@ namespace MCGalaxy.Gui.Popups { cmbList.Text = "Select file.."; } + void EditText_Load(object sender, EventArgs e) { + GuiUtils.SetIcon(this); + } + void cmbList_SelectedIndexChanged(object sender, EventArgs e) { if (cmbList.SelectedIndex == -1) return; TrySaveChanges(); diff --git a/GUI/Popups/PortTools.Designer.cs b/GUI/Popups/PortTools.Designer.cs index 1b6ce4b3b..a72b160ae 100644 --- a/GUI/Popups/PortTools.Designer.cs +++ b/GUI/Popups/PortTools.Designer.cs @@ -121,6 +121,7 @@ namespace MCGalaxy.Gui.Popups { this.Controls.Add(this.linkManually); this.Controls.Add(this.linkHelpForward); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; + this.Load += new System.EventHandler(this.PortTools_Load); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "PortTools"; diff --git a/GUI/Popups/PortTools.cs b/GUI/Popups/PortTools.cs index a698a9017..b2f028013 100644 --- a/GUI/Popups/PortTools.cs +++ b/GUI/Popups/PortTools.cs @@ -23,8 +23,10 @@ using System.Net.Sockets; using System.Windows.Forms; using MCGalaxy.Core; -namespace MCGalaxy.Gui.Popups { - public partial class PortTools : Form { +namespace MCGalaxy.Gui.Popups +{ + public partial class PortTools : Form + { readonly BackgroundWorker worker; int port; @@ -38,6 +40,10 @@ namespace MCGalaxy.Gui.Popups { this.port = port; btnForward.Text = "Forward " + port; } + + void PortTools_Load(object sender, EventArgs e) { + GuiUtils.SetIcon(this); + } void linkManually_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { GuiUtils.OpenBrowser("https://www.canyouseeme.org/"); diff --git a/GUI/Popups/TokenSelector.Designer.cs b/GUI/Popups/TokenSelector.Designer.cs index 1183b1e2a..0fcc120e7 100644 --- a/GUI/Popups/TokenSelector.Designer.cs +++ b/GUI/Popups/TokenSelector.Designer.cs @@ -31,6 +31,7 @@ this.Controls.Add(this.btnCancel); this.Font = new System.Drawing.Font("Calibri", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; + this.Load += new System.EventHandler(this.TokenSelector_Load); this.MaximizeBox = false; this.MinimizeBox = false; this.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); diff --git a/GUI/Popups/TokenSelector.cs b/GUI/Popups/TokenSelector.cs index 1178c4f3b..0a061a6d9 100644 --- a/GUI/Popups/TokenSelector.cs +++ b/GUI/Popups/TokenSelector.cs @@ -3,8 +3,10 @@ using System; using System.Drawing; using System.Windows.Forms; -namespace MCGalaxy.Gui.Popups { - internal sealed partial class TokenSelector : Form { +namespace MCGalaxy.Gui.Popups +{ + internal sealed partial class TokenSelector : Form + { public string Token; public TokenSelector(string title) { @@ -23,6 +25,10 @@ namespace MCGalaxy.Gui.Popups { ResumeLayout(false); } + void TokenSelector_Load(object sender, EventArgs e) { + GuiUtils.SetIcon(this); + } + const int btnWidth = 110, btnHeight = 40, btnsPerCol = 9; int index = 0; diff --git a/GUI/PropertyWindow/PropertyWindow.Commands.cs b/GUI/PropertyWindow/PropertyWindow.Commands.cs index fd9acd25d..fe156e01b 100644 --- a/GUI/PropertyWindow/PropertyWindow.Commands.cs +++ b/GUI/PropertyWindow/PropertyWindow.Commands.cs @@ -125,7 +125,7 @@ namespace MCGalaxy.Gui { void cmd_btnCustom_Click(object sender, EventArgs e) { using (CustomCommands form = new CustomCommands()) { - form.ShowDialog(); + form.ShowDialog(); } } diff --git a/GUI/PropertyWindow/PropertyWindow.cs b/GUI/PropertyWindow/PropertyWindow.cs index b0bbb6235..e22180821 100644 --- a/GUI/PropertyWindow/PropertyWindow.cs +++ b/GUI/PropertyWindow/PropertyWindow.cs @@ -20,10 +20,11 @@ using MCGalaxy.Eco; using MCGalaxy.Events.GameEvents; using MCGalaxy.Games; -namespace MCGalaxy.Gui { - public partial class PropertyWindow : Form { +namespace MCGalaxy.Gui +{ + public partial class PropertyWindow : Form + { ZombieProperties zsSettings = new ZombieProperties(); - internal Icon _icon; public PropertyWindow() { InitializeComponent(); @@ -35,7 +36,8 @@ namespace MCGalaxy.Gui { void PropertyWindow_Load(object sender, EventArgs e) { // try to use same icon as main window - try { Icon = _icon; } catch { } + // must be done in OnLoad, otherwise icon doesn't show on Mono + GuiUtils.SetIcon(this); OnMapsChangedEvent.Register(HandleMapsChanged, Priority.Low); OnStateChangedEvent.Register(HandleStateChanged, Priority.Low); diff --git a/GUI/Window/Window.cs b/GUI/Window/Window.cs index 2af84fce5..a0e9b00e6 100644 --- a/GUI/Window/Window.cs +++ b/GUI/Window/Window.cs @@ -102,6 +102,7 @@ Trying to mix two versions is unsupported - you may experience issues"; try { ComponentResourceManager resources = new ComponentResourceManager(typeof(Window)); Icon = (Icon)(resources.GetObject("$this.Icon")); + GuiUtils.WinIcon = Icon; } catch { } } @@ -320,9 +321,7 @@ Trying to mix two versions is unsupported - you may experience issues"; void btnProperties_Click(object sender, EventArgs e) { if (!hasPropsForm) { - propsForm = new PropertyWindow(); - // just doing 'propForms.Icon = Icon;' doesn't show on Mono - try { propsForm._icon = Icon; } catch { } + propsForm = new PropertyWindow(); hasPropsForm = true; }