Skip to content

Commit

Permalink
Merge pull request #2131 from cwensley/curtis/wpf-window-closing
Browse files Browse the repository at this point in the history
Wpf: Fix issue closing windows during the LostFocus event
  • Loading branch information
cwensley authored Jan 31, 2022
2 parents bdf1e53 + 0b7e797 commit 5fe2ade
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/Eto.Wpf/Forms/WpfWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public override void AttachEvent(string id)
Control.Activated += (sender, e) => Callback.OnGotFocus(Widget, EventArgs.Empty);
break;
case Eto.Forms.Control.LostFocusEvent:
Control.Deactivated += (sender, e) => Callback.OnLostFocus(Widget, EventArgs.Empty);
Control.LostKeyboardFocus += (sender, e) => Callback.OnLostFocus(Widget, EventArgs.Empty);
break;
case Window.LocationChangedEvent:
Control.LocationChanged += (sender, e) => Callback.OnLocationChanged(Widget, EventArgs.Empty);
Expand Down Expand Up @@ -407,7 +407,11 @@ public void Close()
{
// prevent crash if we call this more than once..
if (!IsClosing)
{
// Clear owner so WPF doesn't change the z-order of the parent when closing
SetOwner(null);
Control.Close();
}
}
else
Visible = false;
Expand Down
51 changes: 42 additions & 9 deletions test/Eto.Test/UnitTests/Forms/WindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@ void DoTest(Window window)
var rightPanel = new StackLayout { Orientation = Orientation.Horizontal };

var autoSize = new CheckBox { Text = "AutoSize", Checked = window.AutoSize };
autoSize.CheckedChanged += (sender, e) => {
autoSize.CheckedChanged += (sender, e) =>
{
window.AutoSize = autoSize.Checked == true;
};

var addBottomButton = new Button { Text = "Add bottom control" };
addBottomButton.Click += (sender, e) => {
addBottomButton.Click += (sender, e) =>
{
bottomPanel.Items.Add(new Panel { Size = new Size(20, 20) });
autoSize.Checked = window.AutoSize;
};

var addRightButton = new Button { Text = "Add right control" };
addRightButton.Click += (sender, e) => {
addRightButton.Click += (sender, e) =>
{
rightPanel.Items.Add(new Panel { Size = new Size(20, 20) });
autoSize.Checked = window.AutoSize;
};
Expand Down Expand Up @@ -120,7 +123,8 @@ public void WindowShouldHaveCorrectInitialSizeWithWrappedLabel(bool useForm, boo
window.Height = 150;
}
label.MouseDown += (sender, e) => {
label.MouseDown += (sender, e) =>
{
label.Text = infoText + Utility.GenerateLoremText(new Random().Next(200));
};
Expand Down Expand Up @@ -252,23 +256,52 @@ Label CreateLabel()
return layout;
});
}

[Test, ManualTest]
public void WindowFromPointShouldReturnWindowUnderPoint()
{
ManualForm("Move your mouse, it should show the title of the window under the mouse pointer",
form => {
form =>
{
var content = new Panel { MinimumSize = new Size(100, 100) };
var timer = new UITimer { Interval = 0.5 };
timer.Elapsed += (sender, e) => {
timer.Elapsed += (sender, e) =>
{
var window = Window.FromPoint(Mouse.Position);
content.Content = $"Window: {window?.Title}";
};
timer.Start();
form.Closed += (sender, e) => {
timer.Stop();
form.Closed += (sender, e) =>
{
timer.Stop();
};
form.Title = "Test Form";
return content;
}
);
}

[Test, ManualTest]
public void WindowShouldCloseOnLostFocusWithoutHidingParent()
{
ManualForm("Click on this window after the child is shown,\nthe form and the main form should not go behind other windows",
form =>
{
var content = new Panel { MinimumSize = new Size(100, 100) };
form.Shown += (sender, e) =>
{
var childForm = new Form
{
Title = "Child Form",
ClientSize = new Size(100, 100),
Owner = form
};
childForm.MouseDown += (s2, e2) => childForm.Close();
childForm.LostFocus += (s2, e2) => childForm.Close();
childForm.Show();
};
form.Title = "Test Form";
form.Owner = Application.Instance.MainForm;
return content;
}
);
Expand Down

0 comments on commit 5fe2ade

Please sign in to comment.