Recent

Author Topic: [Solved] Cocoa ShowModal  (Read 469 times)

VTwin

  • Hero Member
  • *****
  • Posts: 737
  • Former Turbo Pascal 3 user
[Solved] Cocoa ShowModal
« on: April 25, 2019, 12:45:53 am »
Cocoa TForm.ShowModal does not work properly. I've been resorting to code like this as a workaround:

Code: Pascal  [Select]
  1. function RunSelectCRNSeriesDlg(series: TStringArray): string;
  2. begin
  3.   if SelectCRNSeriesDlg = nil then
  4.     SelectCRNSeriesDlg := TSelectCRNSeriesDlg.Create(Application);
  5.   SelectCRNSeriesDlg.SetPrefs(series);
  6.   {$IFDEF LCLCOCOA}
  7.   SelectCRNSeriesDlg.Show;
  8.   while SelectCRNSeriesDlg.Visible do
  9.     Application.ProcessMessages;
  10.   {$ELSE}
  11.   SelectCRNSeriesDlg.ShowModal;
  12.   {$ENDIF}
  13.   result := SelectCRNSeriesDlg.GetPrefs;
  14. end;

I'm afraid it has introduced numerous bugs into my releases where I have not included such code. This is a big problem for me.

Has anyone else run into such problems? If so, have you found a better workaround?
« Last Edit: April 25, 2019, 09:16:06 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.11.6: Lazarus 2.1.0 svn 61673M (64 bit Cocoa trunk)
Ubuntu 18.04.2: Lazarus 2.0.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.4 (64 bit on VBox)

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2234
    • havefunsoft.com
Re: Cocoa ShowModal
« Reply #1 on: April 25, 2019, 02:17:02 am »
Cocoa TForm.ShowModal does not work properly.
what exactly doesn't work properly?
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

VTwin

  • Hero Member
  • *****
  • Posts: 737
  • Former Turbo Pascal 3 user
Re: Cocoa ShowModal
« Reply #2 on: April 25, 2019, 02:32:21 am »
Cocoa TForm.ShowModal does not work properly.
what exactly doesn't work properly?

Thanks. It does not not close properly, closing hides it but does not return control to the calling form. I'll try to come up with a full example demonstrating the problem. Perhaps (?) it is related to the issue I reported in the IDE, where a modal dialog will not close while debugging.
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.11.6: Lazarus 2.1.0 svn 61673M (64 bit Cocoa trunk)
Ubuntu 18.04.2: Lazarus 2.0.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.4 (64 bit on VBox)

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2234
    • havefunsoft.com
Re: Cocoa ShowModal
« Reply #3 on: April 25, 2019, 04:48:24 am »
Thanks. It does not not close properly, closing hides it but does not return control to the calling form. I'll try to come up with a full example demonstrating the problem.
I've created a project with 2 forms.
Added "Show modal" on form1, where there method calls Form2.ShowModal;
Added a "special close" button on Form2 that is calling Close() method

Launching the app.
- click show Modal - form2 shows up as expected
- click "special close" - the focus returns to form1
- click show Modal again - form 2 shows up as expected
- clock on X icon (close) the focus returns to form2 as expected

Sidenote: I'm using trunk
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

VTwin

  • Hero Member
  • *****
  • Posts: 737
  • Former Turbo Pascal 3 user
Re: Cocoa ShowModal
« Reply #4 on: April 25, 2019, 07:31:31 pm »
Many thanks. Perhaps it was a mistake on my part?  :-[

I was calling Hide instead of Close. Calling Close works as expected.

However, calling Hide causes odd behavior, which may be a bug. Form2 disappears, but it seems to not lose focus, and it is not possible to gain focus for Form1.

I am on trunk, however I normally use the 2.0 Fixes branch, which I assume is more likely to be stable. Is it possible to merge Cocoa fixes to that branch?
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.11.6: Lazarus 2.1.0 svn 61673M (64 bit Cocoa trunk)
Ubuntu 18.04.2: Lazarus 2.0.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.4 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 737
  • Former Turbo Pascal 3 user
Re: Cocoa ShowModal
« Reply #5 on: April 25, 2019, 07:46:58 pm »
It also works as expected if I assign modal result mrOk instead of explicitly closing Form2.
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.11.6: Lazarus 2.1.0 svn 61673M (64 bit Cocoa trunk)
Ubuntu 18.04.2: Lazarus 2.0.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.4 (64 bit on VBox)

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2234
    • havefunsoft.com
Re: Cocoa ShowModal
« Reply #6 on: April 25, 2019, 08:18:50 pm »
I was calling Hide instead of Close. Calling Close works as expected.

However, calling Hide causes odd behavior, which may be a bug. Form2 disappears, but it seems to not lose focus, and it is not possible to gain focus for Form1.
Win32 works exactly the same way.
It makes sense, because the modality has not been removed. Thus Form1 should not be even available.
The modality is removed only after closing the modal form.
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

VTwin

  • Hero Member
  • *****
  • Posts: 737
  • Former Turbo Pascal 3 user
Re: Cocoa ShowModal
« Reply #7 on: April 25, 2019, 09:00:03 pm »
Thanks, that is perfectly sensible. I appreciate your response.

I am checking through my code to make sure I did not make the same mistake elsewhere. I never noticed that behavior before, so perhaps this is the first time I did.
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.11.6: Lazarus 2.1.0 svn 61673M (64 bit Cocoa trunk)
Ubuntu 18.04.2: Lazarus 2.0.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.4 (64 bit on VBox)