From 4d29054aa47f2dc482c57c81618375aa2092180f Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 26 May 2022 21:21:51 -0500 Subject: [PATCH] Fix some mono UI bugs. The DataGridView with a binding source is super buggy in mono and crashes easily. But if you are really careful, you can sometimes avoid the crashes. --- KeeAgent/UI/EntryPanel.cs | 26 +++++++++++++++++++++++++- KeeAgent/UI/HostKeysDialog.cs | 12 ++++++++++-- SshAgentLib | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/KeeAgent/UI/EntryPanel.cs b/KeeAgent/UI/EntryPanel.cs index cdc2433..305a1d9 100644 --- a/KeeAgent/UI/EntryPanel.cs +++ b/KeeAgent/UI/EntryPanel.cs @@ -60,12 +60,26 @@ protected override void OnLoad(EventArgs e) CurrentSettings = InitialSettings.DeepCopy(); entrySettingsBindingSource.DataSource = CurrentSettings; + if (Type.GetType("Mono.Runtime") != null) { + // prevent crash + destinationConstraintDataGridView.DataSource = null; + } + if (CurrentSettings.DestinationConstraints != null) { foreach (var c in CurrentSettings.DestinationConstraints) { destinationConstraintBindingSource.Add(c); } } + if (Type.GetType("Mono.Runtime") != null) { + destinationConstraintDataGridView.DataSource = destinationConstraintBindingSource; + // binding complete event doesn't fire on mono + destinationConstraintDataGridView.CellEndEdit += (s2, e2) => { + destinationConstraintDataGridView_DataBindingComplete( + s2, new DataGridViewBindingCompleteEventArgs(System.ComponentModel.ListChangedType.ItemChanged)); + }; + } + pwEntryForm.FormClosing += delegate { while (delayedUpdateKeyInfoTimer.Enabled) { Application.DoEvents(); @@ -218,6 +232,12 @@ private void destinationConstraintDataGridView_EnabledChanged(object sender, Eve dgv.ReadOnly = true; dgv.EnableHeadersVisualStyles = false; } + + if (Type.GetType("Mono.Runtime") != null) { + // Mono bug: this doesn't happen automatically when changing the + // properties above like on Windows. + dgv.Refresh(); + } } private void destinationConstraintDataGridView_MouseUp(object sender, MouseEventArgs e) @@ -281,7 +301,7 @@ private void destinationConstraintDataGridView_DataBindingComplete(object sender } var c = row.DataBoundItem as EntrySettings.DestinationConstraint; - list.Add(c); + list.Add(c.DeepCopy()); try { new DestinationConstraint.Constraint( @@ -338,6 +358,10 @@ private void destinationConstraintDataGridView_CellContentClick(object sender, D if (result == DialogResult.OK) { property.SetValue(constraint, dialog.GetKeySpecs()); + + // binding complete event doesn't fire on mono + destinationConstraintDataGridView_DataBindingComplete( + sender, new DataGridViewBindingCompleteEventArgs(System.ComponentModel.ListChangedType.ItemChanged)); } } } diff --git a/KeeAgent/UI/HostKeysDialog.cs b/KeeAgent/UI/HostKeysDialog.cs index 4742726..b2d6764 100644 --- a/KeeAgent/UI/HostKeysDialog.cs +++ b/KeeAgent/UI/HostKeysDialog.cs @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0-only +// SPDX-License-Identifier: GPL-2.0-only // Copyright (c) 2022 David Lechner using System; @@ -14,6 +14,14 @@ public partial class HostKeysDialog : Form public HostKeysDialog() { InitializeComponent(); + + // fix up layout in Mono + if (Type.GetType("Mono.Runtime") != null) { + dataGridView1.Anchor = AnchorStyles.Top | AnchorStyles.Left; + dataGridView1.Height -= 20; + okButton.Top -= 20; + cancelButton.Top -= 20; + } } public void AddKeySpec(EntrySettings.DestinationConstraint.KeySpec spec) @@ -24,7 +32,7 @@ public void AddKeySpec(EntrySettings.DestinationConstraint.KeySpec spec) public EntrySettings.DestinationConstraint.KeySpec[] GetKeySpecs() { return keySpecBindingSource.Cast() - .Where(x => x.HostKey != null).Select(x => x.DeepCopy()).ToArray(); + .Where(x => !string.IsNullOrWhiteSpace(x.HostKey)).Select(x => x.DeepCopy()).ToArray(); } private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) diff --git a/SshAgentLib b/SshAgentLib index 9cca837..b5ca4b4 160000 --- a/SshAgentLib +++ b/SshAgentLib @@ -1 +1 @@ -Subproject commit 9cca837ea517d6e9d715c91550aeb9845799d60d +Subproject commit b5ca4b42343f73bde5e5fa8a790b460162382e36