Skip to content

Cannot handle ApplicationShouldTerminate in application without window #11

@PaulBol

Description

@PaulBol

Some observations related to #2

When the application currently does not display any form ApplicationShouldTerminate in MonoApplicationDelegate.cs obviously cannot notify any window. It will return NSApplicationTerminateReply.Cancel to allow termination but the application has no chance to clean up before terminating.

I came up with this added code as a workaround

Message m = Message.Create(IntPtr.Zero, (int)Msg.WM_QUIT, IntPtr.Zero, IntPtr.Zero);
if (Application.FilterMessage(ref m))
	return NSApplicationTerminateReply.Cancel;

This way an application without a main form may install a message filter and handle ApplicationShouldTerminate.

The next observation is that with a dynamically top-level window ApplicationShouldTerminate is not received. I figured out that the behavior is caused by the SetModal call for the top-level window. Though I'm not sure why. The following change resolves the issue. But since I haven't really understood modal sessions I'm wondering if it might break anything else.

internal override void SetModal (IntPtr handle, bool Modal)
{
	NSView vuWrap = handle.ToNSView();
	NSWindow winWrap = vuWrap.Window;
	IntPtr session;
	if (Modal)
	{
		if (winWrap.ParentWindow == null)
		{
			winWrap.Center();
			ModalSessions.Push(IntPtr.Zero);
		}
		else
		{
			session = NSApplication.SharedApplication.BeginModalSession(winWrap);
			ModalSessions.Push(session);
		}
	}
	else if ((session = ModalSessions.Pop()) != IntPtr.Zero)
		NSApplication.SharedApplication.EndModalSession(session);
}

Are the suggested changes good enough for a pull request or do they not make sense?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions