Skip to content

Commit

Permalink
Fix some mono UI bugs.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dlech committed May 27, 2022
1 parent 4c02764 commit 4d29054
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
26 changes: 25 additions & 1 deletion KeeAgent/UI/EntryPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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));
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions KeeAgent/UI/HostKeysDialog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: GPL-2.0-only
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2022 David Lechner <[email protected]>

using System;
Expand All @@ -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)
Expand All @@ -24,7 +32,7 @@ public void AddKeySpec(EntrySettings.DestinationConstraint.KeySpec spec)
public EntrySettings.DestinationConstraint.KeySpec[] GetKeySpecs()
{
return keySpecBindingSource.Cast<EntrySettings.DestinationConstraint.KeySpec>()
.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)
Expand Down
2 changes: 1 addition & 1 deletion SshAgentLib

0 comments on commit 4d29054

Please sign in to comment.