Skip to content

Commit bf62c35

Browse files
Fix the error dialog when dismissing the Ctrl+K palette (#21)
Choosing a result or pressing Esc calls Close(), which moves focus off the palette and raises Deactivated while the close is still running. That handler called Close() a second time, and WPF answers that with "Cannot set Visibility to Visible or call Show, ShowDialog, Close, or WindowInteropHelper.EnsureHandle while a Window is closing." Both ordinary ways of dismissing the palette go through Close(), so the error appeared on almost every search. The Deactivated handler now closes only when a close is not already in progress. Reported by grumpymojo on 6.2.14-B16.
1 parent fd9729f commit bf62c35

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

hmailserver/source/Tools/ControlPanel/Views/NavigationPalette.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public class NavigationPalette : Window
2323
// Display text -> nav page tag, for results that are settings rather than pages.
2424
private readonly Dictionary<string, string> settingResults_ = new Dictionary<string, string>();
2525

26+
// Closing the palette takes the focus off it, which raises Deactivated while
27+
// the close is still running - and a second Close() from that handler throws
28+
// "Cannot set Visibility ... while a Window is closing". Both ordinary ways
29+
// of dismissing the palette (choosing a result, pressing Esc) go through
30+
// Close(), so without this the error dialog appeared almost every time.
31+
private bool closing_;
32+
2633
/// <summary>The chosen page NAME, when the user picked a page.</summary>
2734
public string Selected { get; private set; }
2835

@@ -85,7 +92,11 @@ public NavigationPalette(Window owner, IEnumerable<string> pageNames)
8592
Content = root;
8693

8794
PreviewKeyDown += OnKey;
88-
Deactivated += (s, e) => Close();
95+
Deactivated += (s, e) =>
96+
{
97+
if (!closing_)
98+
Close();
99+
};
89100
Loaded += (s, e) => { Filter(); searchBox_.Focus(); };
90101
}
91102

@@ -170,5 +181,19 @@ private void Accept()
170181

171182
Close();
172183
}
184+
185+
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
186+
{
187+
// Set before the base call, which is where the window starts tearing down
188+
// and the focus change that raises Deactivated happens.
189+
closing_ = true;
190+
191+
base.OnClosing(e);
192+
193+
// Nothing cancels the close today, but if anything ever does the palette
194+
// has to stay dismissable.
195+
if (e.Cancel)
196+
closing_ = false;
197+
}
173198
}
174199
}

0 commit comments

Comments
 (0)