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
This commit is contained in:
UnknownShadow200 2021-11-11 22:31:59 +11:00
parent 8ee32ba780
commit ada34d4c32
14 changed files with 58 additions and 16 deletions

View File

@ -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
{
/// <summary> MCGalaxy window icon (shared) </summary>
public static Icon WinIcon;
public static void SetIcon(Form form) {
try { form.Icon = WinIcon; } catch { }
}
/// <summary> Opens the given url in the system's default web browser </summary>
/// <remarks> Catches and logs any unhandled errors </remarks>
public static void OpenBrowser(string url) {

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -33,6 +33,10 @@ namespace MCGalaxy.Gui.Popups {
}
}
void CustomCommands_Load(object sender, EventArgs e) {
GuiUtils.SetIcon(this);
}
void CreateCommand(ICompiler engine) {
string cmdName = txtCmdName.Text.Trim();
if (cmdName.Length == 0) {

View File

@ -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";

View File

@ -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();

View File

@ -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";

View File

@ -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;
@ -39,6 +41,10 @@ namespace MCGalaxy.Gui.Popups {
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/");
}

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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 { }
}
@ -321,8 +322,6 @@ 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 { }
hasPropsForm = true;
}