Recent

Author Topic: [Solved] Unfocussed application form does not close on closing application  (Read 675 times)

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All

This has been around for quite a while already and I started to wonder if this is just me or a bug.


When an application is open, and its form is visible and focussed (!), right clicking the icon in the dock and selecting "Quit" closes the application and the form as expected.
However, when the application does not have focus, right clicking the icon in the dock and selecting "Quit" will do nothing. The application remains open, the form remains visible ... until you give the application form focus again.


Is this expected behavior? Am I missing something here? Or is this a bug?


Steps to reproduce:

  • Create a new blank application in Lazarus.
  • Run it (I ran it without debugger).
  • Now bring another application to the front (focus).
  • In the dock, right click the newly created Lazarus application icon and select "Quit".
  • Nothing happens.
  • Move the mouse over the form, nothing happens.
  • Click the form: the form and application now terminate.
Tested with macOS Monterey, Lazarus 2.3.0 (rev main-2_3-2069-g4aa7b5b350) FPC 3.2.2, tested on Intel and AARCH64.
This is not a new phenomena, I've seen it with several other macOS and Lazarus versions as well.
It does however seem Cocoa specific (cannot test Carbon anymore, and I do recall seeing this to happen under Windows or Linux).
« Last Edit: October 27, 2022, 10:08:34 am by Hansaplast »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 971
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Unfocussed application form does not close on closing application
« Reply #1 on: October 26, 2022, 02:27:52 pm »
On Windows I do not know such matters.
On Windows I would try investigate with its Messages (WM_CLOSE for example) to see if application receive it.
I am sorry that I can not tell how to do on non-Windows, maybe try with a memo as log, within "OnClose" event log it to memo to see that application tries to.
(if you can log but app not close, maybe try "Application.Restore" in "OnClose"-Event ?)
Maybe this has to do with memory garbage collection and auto clean... ?
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All
Re: Unfocussed application form does not close on closing application
« Reply #2 on: October 26, 2022, 02:49:02 pm »
Thanks for chiming in KodeZwerg!


The OnClose event does actually get fired (tested based on your suggestion).
When I change the form caption in the OnClose caption, it actually does change the form caption as expected - yet the form doesn't close.


Some random things I tried in the OnClose event, and none of them do anything until the form receives focus again:
  • Application.Terminate doesn't do anything,
  • set "CloseAction" to "caFree" doesn't seem to so anything either,
  • self.SetFocus doesn't do anything,
  • self.SetFocusedControl(self) same result
Note:
With each of these tests, having self.caption:='123'; in the OnClose event worked, and the caption would actually change. The listed tests were done individually with changing the caption before and after the listed things.


Other observations:
- The icon remains visible in the Dock until the form closes.
- Right clicking the icon in the dock, allows me to select Quit, over and over again, until the form is closed.
- Left clicking the icon in the dock, after having done a "Quit", actually does close the form and application.
- Multiple calls changing the caption works,
- Calling "ShowMessage('123')" works, but the dialog remains behind the other active application.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 971
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Unfocussed application form does not close on closing application
« Reply #3 on: October 26, 2022, 03:12:14 pm »
Give this a try:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
  2. begin
  3.   Application.Restore;
  4.   if (not Self.Visible) then
  5.     Self.Show;
  6.   Self.BringToFront;
  7.   Action := caFree;
  8. end;
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All
Re: Unfocussed application form does not close on closing application
« Reply #4 on: October 26, 2022, 03:30:00 pm »


Oh wow ... that worked!  8)
By process of elimination, self.show seemed to do the trick.


Now, since Windows (for example) doesn't need this; should this be considered a bug or not?
Also: is setting the CloseAction needed or just good practice? (just asking since I never really use it)


Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.   // Application.Restore;
  4.   // if (not Self.Visible) then
  5.     Self.Show;
  6.   // Self.BringToFront;
  7.   // CloseAction := caFree;
  8. end;          


KodeZwerg

  • Hero Member
  • *****
  • Posts: 971
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Unfocussed application form does not close on closing application
« Reply #5 on: October 26, 2022, 03:46:07 pm »
Oh wow ... that worked!  8)
I am happy that it works for you!
should this be considered a bug or not?
I would say that it's a OS bug/feature when you say that other applications (non-FreePascal) do the same.
You would need to read OS documentation about that topic to know how developers was thinking ...
is setting the CloseAction needed or just good practice?
When you use a "OnClose" I would always tell the Action what it should be since "caNone" for example would simple ignore your Close request :-P

To be honest, I would let everything what I showed in.
Application.Restore = if app is minimized, pop it up
Self.Show = if app is hidden, show it
Self.BringToFront = try to put your form in front (on Windows "Self.SetFocus" does not work by OS restriction)
caFree = tell app to try call "Free/Close" for all objects and end in Form.Destructor (what you also could modify to your needs)
« Last Edit: October 26, 2022, 03:48:50 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All
Re: Unfocussed application form does not close on closing application
« Reply #6 on: October 26, 2022, 04:08:31 pm »
Other applications do not behave like this under macOS.
This most certainly is a Lazarus specific thing.
I reported this as an issue. (link)



Thanks for the OnClose suggestion - learning something new every day!
And ... thank you again for the help! 😊

VTwin

  • Hero Member
  • *****
  • Posts: 1172
  • Former Turbo Pascal 3 user
Re: Unfocussed application form does not close on closing application
« Reply #7 on: October 26, 2022, 05:06:28 pm »
Thanks for reporting this. I had not noticed it, but can confirm it.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.4 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.4 (64 bit on VBox)

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All
Re: Unfocussed application form does not close on closing application
« Reply #8 on: October 26, 2022, 06:50:48 pm »
Thanks for confirming 😊

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2004
  • Former Delphi 1-7, 10.2 user
Re: Unfocussed application form does not close on closing application
« Reply #9 on: November 20, 2022, 02:15:25 pm »
Other applications do not behave like this under macOS.
This most certainly is a Lazarus specific thing.

No, it's a macOS thing - see https://stackoverflow.com/questions/48041279/stopping-the-nsapplication-main-event-loop/48064752#48064752
Lazarus 2.3, FPC 3.3.1 macOS 12.6.1 x86_64 Xcode 14.1
Lazarus 2.3, FPC 3.3.1 macOS 12.6.1 aarch64 Xcode 14.1

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All
Re: [Solved] Unfocussed application form does not close on closing application
« Reply #10 on: November 20, 2022, 04:27:15 pm »
It seems caused by a macOS "thing", however it is just not how any other (non-Lazarus) macOS application closes.
I did see this discussion as well in the Bugtracker - definitely over my head  8-)

VTwin

  • Hero Member
  • *****
  • Posts: 1172
  • Former Turbo Pascal 3 user
Re: [Solved] Unfocussed application form does not close on closing application
« Reply #11 on: November 20, 2022, 07:01:31 pm »
It may have to do with the difference in gui philosophy/convention/history. Historically, on Macs the window is a document, while on Windows the window is an application. Note the difference in menu placement.

Here is an interesting discussion:

https://unix.stackexchange.com/questions/4618/why-mac-applications-never-get-closed/5160#5160



“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.4 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.4 (64 bit on VBox)

 

TinyPortal © 2005-2018