Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wpf: Fix issue closing windows during the LostFocus event #2131

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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