Recent

Author Topic: ShowModal: Setting ModalResult, or calling "close", doesn't close form  (Read 832 times)

Hansaplast

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

I have a project, where one Form uses ShowModal to open another form.
The second form closes when a certain task has been completed.
* Attached a test project where I'm using a timer to close the form after 5 seconds.


This works very well, and as expected, under Windows and Linux, however not under Cocoa.
Under Cocoa the user will have to move the mouse for the form to close (just move, not even click or anything like it). I've tried setting ModalResult (even setting it to mrCancel in the OnCreate event), tried calling "Close", tried all kinds of combinations - none of it works.


I've seen discussions about this, with some saying that the user is expected to interact in some way with he form, since "this is how modalresult works".
This may very well be true, but that means that there still is a bug ... either Cocoa isn't behaving as expected, or Linux and Windows are not behaving as expected.



Or maybe I'm doing something wrong here?

VisualLab

  • Full Member
  • ***
  • Posts: 113
Re: ShowModal: Setting ModalResult, or calling "close", doesn't close form
« Reply #1 on: November 07, 2022, 04:03:32 pm »
I downloaded and checked the test project. Indeed, it compiles and runs as described by the author. I use:

- OS: Windows 10 64-bit (2018),
- IDE: Lazarus 2.2.0 (rev lazarus_2_2_0) FPC 3.2.2 x86_64-win64-win32 / win64).

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All
Re: ShowModal: Setting ModalResult, or calling "close", doesn't close form
« Reply #2 on: November 07, 2022, 05:56:09 pm »
Thank you VisualLab for the confirmation - much appreciated! 👍🏻

zeljko

  • Hero Member
  • *****
  • Posts: 1385
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: ShowModal: Setting ModalResult, or calling "close", doesn't close form
« Reply #3 on: November 07, 2022, 06:27:25 pm »
Why do you use Close; and then ModalResult := mrOK inside Timer1StopTimer ?
Use only ModalResult := mrOK and see if that works.

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All
Re: ShowModal: Setting ModalResult, or calling "close", doesn't close form
« Reply #4 on: November 07, 2022, 08:50:21 pm »
Thank you Zeljko for chiming in!  :)

Apologies, this was a left-over from testing all kinds of combinations.

Unfortunately, just using ModalResult:=mrOK doesn't work either - that's what I started with (see code below).
Calling Close (instead of setting ModalResult) didn't work either, so that's when I started trying all kinds of weird things to see what will work.

p.s. The FormClose event does not seem to get fired fired either, until user moves the mouse.

So the code below results in exact the same issue.
It's almost like the mouse needs to be moved to get the message that the window needs to be closed to be processed.
(I have hardly any experience with the entire messaging in the background - so my apologies if I'm describing this wrong)

Note:
- Everything I tried works under Linux and Windows. Just not under Cocoa.
- I forgot to mention that I've tested this with several Lazarus/FPC versions, Intel and AARCH, and Monterey and Ventura.
  Last version I tried is: Lazarus 2.3.0 (rev main-2_3-2069-g4aa7b5b350) FPC 3.2.2 x86_64-darwin-cocoa
 
Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  8.  
  9. type
  10.  
  11.   { TForm2 }
  12.  
  13.   TForm2 = class(TForm)
  14.     Timer1: TTimer;
  15.     procedure FormShow(Sender: TObject);
  16.     procedure Timer1StartTimer(Sender: TObject);
  17.     procedure Timer1StopTimer(Sender: TObject);
  18.     procedure Timer1Timer(Sender: TObject);
  19.   private
  20.     Counter:integer;
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form2: TForm2;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm2 }
  33.  
  34. procedure TForm2.FormShow(Sender: TObject);
  35. begin
  36.   Timer1.Enabled:=true;
  37. end;
  38.  
  39. procedure TForm2.Timer1StartTimer(Sender: TObject);
  40. begin
  41.   Counter := 0;
  42.   self.caption := 'Timer start';
  43. end;
  44.  
  45. procedure TForm2.Timer1Timer(Sender: TObject);
  46. begin
  47.   inc(Counter);
  48.   self.caption := 'Timer '+IntToStr(Counter);
  49.   if Counter>5 then Timer1.Enabled:=false;
  50. end;
  51.  
  52. procedure TForm2.Timer1StopTimer(Sender: TObject);
  53. begin
  54.   self.caption := 'Timer done';
  55.   ModalResult:=mrOK;
  56. end;
  57.  
  58. end.  
« Last Edit: November 07, 2022, 08:55:45 pm by Hansaplast »

zeljko

  • Hero Member
  • *****
  • Posts: 1385
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: ShowModal: Setting ModalResult, or calling "close", doesn't close form
« Reply #5 on: November 10, 2022, 07:38:05 am »
It works ok here, but with Qt5 under cocoa .... open bug about your issue and attach example project, so somebody will look into.

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All
Re: ShowModal: Setting ModalResult, or calling "close", doesn't close form
« Reply #6 on: November 10, 2022, 11:32:35 am »
Thanks for trying Zeljko!
Bug reported: here.
« Last Edit: November 10, 2022, 11:49:40 am by Hansaplast »

msintle

  • Newbie
  • Posts: 1
Re: ShowModal: Setting ModalResult, or calling "close", doesn't close form
« Reply #7 on: November 11, 2022, 10:34:08 am »
Exactly my experience as well. Seems like something may be stuck in a message loop or something. Trying to work around the issue by using SendMessage doesn't help either. Maybe a ShowModal bug since Show seems to work properly but you have to get creative blocking the call invoker and disabling the caller's UI. All of that code is already in ShowModal so probably needs just some particular fix there. Glad to hear QT works though!

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All
Re: ShowModal: Setting ModalResult, or calling "close", doesn't close form
« Reply #8 on: November 11, 2022, 12:08:01 pm »
Thanks for confirming msintle! Much appreciated! 😊

Hansaplast

  • Hero Member
  • *****
  • Posts: 633
  • Tweaking4All.com
    • Tweaking4All
Re: ShowModal: Setting ModalResult, or calling "close", doesn't close form
« Reply #9 on: November 11, 2022, 12:19:12 pm »
Can anyone verify and confirm if I posted the bug correctly?
Or show me any detailed info on bug reporting.


With Mantis we had the option to be more specific (OS, widgetset, etc), but with GitLab these seemed to be called "labels" but I cannot add them to my bug reports.

 

TinyPortal © 2005-2018