Lazarus

Programming => Operating Systems => macOS / Mac OS X => Topic started by: ling on March 12, 2021, 09:21:03 am

Title: [SOLVED] Second form does not show until clicked on the dock icon
Post by: ling on March 12, 2021, 09:21:03 am
I have a project there are multiple forms, I encounter a problem on form show.

When the application starts, the main form shows, I click a menu item to open a second form, main form is deactivate but the second form does not show. The only way to bring form to show is clicking on the application dock icon. The second form is not specific, every form in my project has the same situation.

I tried to reproduce the situation by a simple project, but the simple project is working fine. Any idea for debugging this problem?

--
Lazarus version: 2.0.6
FPC: 3.0.4
macOS Catalina 10.15.7
Title: Re: Second form does not show until clicked on the dock icon
Post by: GetMem on March 12, 2021, 09:45:41 am
@ling

How do you create the second form? Dynamically? Is there any code on the second form's OnCreate, OnShow, OnActivate event?
Best is to attach a simplified version of your project, where the issue is clearly reproducible.
Title: Re: Second form does not show until clicked on the dock icon
Post by: ling on March 12, 2021, 10:29:02 am
@GetMem Thanks.

I just create a simple form dynamically:
Code: Pascal  [Select][+][-]
  1. TForm1.MenuItem1Click(Sender: TObject);
  2. var
  3.   aform: TForm;
  4. begin
  5.   aform:=TForm.Create(Self);
  6.   aform.Show;
  7. end;
  8.  

I'm still trying to strip the project, but it is complicated, the codes are bundled. The project is developing by several people and originally on Windows. I will attach then simplified version when it is done.
Title: Re: Second form does not show until clicked on the dock icon
Post by: dseligo on March 12, 2021, 11:13:29 am
You have to declare variable aform global, not in procedure.

Or, you can try this:
Code: Pascal  [Select][+][-]
  1. TForm1.MenuItem1Click(Sender: TObject);
  2. var
  3.   aform: TForm;
  4. begin
  5.   aform:=TForm.Create(nil);
  6.   try
  7.     aform.ShowModal;
  8.   finally
  9.     aform.Free;
  10.   end;
  11. end;
Title: Re: Second form does not show until clicked on the dock icon
Post by: ling on March 13, 2021, 05:57:13 am
@dseligo Thanks.

It's a sample code. I have tried global variable too, the same situation.
ShowModal works fine, but that will block other operations, not what I want.
Title: Re: Second form does not show until clicked on the dock icon
Post by: winni on March 13, 2021, 11:54:05 am
Hi!

Replace

 aform.ShowModal;

with

 aform.Show;

That is the whole secret.

Winni
Title: Re: Second form does not show until clicked on the dock icon
Post by: lucamar on March 13, 2021, 02:53:03 pm
Replace

 aform.ShowModal;

with

 aform.Show;

That is the whole secret.

AFAIU, it seems to be also the whole problem: if he uses Show the new form will appear behind the caller and he has to bring it to front by clicking on the dock; isn't that it?
Title: Re: Second form does not show until clicked on the dock icon
Post by: GetMem on March 13, 2021, 03:24:50 pm
Then perhaps setting the second form's PopupMode property to pmExplicit and PopupParent to main form solves the issue. Also in worse case scenario FormStyle property can be changed to fsStayOnTop.

PS: Under normal circumstances none of the above should be necessary.
Title: Re: Second form does not show until clicked on the dock icon
Post by: winni on March 13, 2021, 06:44:18 pm


AFAIU, it seems to be also the whole problem: if he uses Show the new form will appear behind the caller and he has to bring it to front by clicking on the dock; isn't that it?

Hi!

aForm.show brings the Form to the front.

If it is inbetween hidden by other Forms another aForm.show brings this Form again to the front.

Winni
Title: Re: Second form does not show until clicked on the dock icon
Post by: MarkMLl on March 13, 2021, 07:05:59 pm
Cleverness of this sort troubles me, I've tried it too many times myself.

I think OP should be looking very carefully at how forms are normally created in the .lpr during program startup: it's not a straight Create.

MarkMLl
Title: Re: Second form does not show until clicked on the dock icon
Post by: Fred vS on March 13, 2021, 07:07:10 pm
Hello.

And what about this one ?:

Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   aform: TForm;
  4.  
  5. implementation
  6.  
  7. {$R *.lfm}
  8.  
  9. { TForm1 }
  10.  
  11. procedure TForm1.MenuItem1Click(Sender: TObject);
  12. begin
  13.   if not assigned(aform) then aform := TForm.Create(nil);
  14.   aform.visible := true;
  15.   aform.BringToFront;
  16. end;
  17.  
  18. procedure TForm1.FormDestroy(Sender: TObject);
  19. begin
  20.   if assigned(aform) then aform.free;
  21. end;  
Title: Re: Second form does not show until clicked on the dock icon
Post by: lucamar on March 13, 2021, 09:26:06 pm
I think OP should be looking very carefully at how forms are normally created in the .lpr during program startup: it's not a straight Create.

No, it isn't. It's a call to Application.CreateForm(). But IIRC that's basically so that the application can keep the list of forms and close itself when the main one is exited (yeah, I know it's not just that but that's its main function, IIRC).

The question really is that however a form is created (and let's remember that e.g. most dialogs---the "simple" ones, I mean: InputBox, etc.---are created with a simple call to Create), a call to Show should, as Winni said, also bring it to the front, but according to the OP it doesn't (at least in macOS) in his program.
Title: Re: Second form does not show until clicked on the dock icon
Post by: trev on March 13, 2021, 11:28:41 pm
If none of the above help solve your issue, you are going to have to provide the code which causes this behaviour.
Title: Re: Second form does not show until clicked on the dock icon
Post by: cdbc on March 14, 2021, 01:10:38 pm
Hi
My 2 cents:
Lazarus does it very well  ::)
Why not have a look at the source-code?
Regards Benny
Title: Re: Second form does not show until clicked on the dock icon
Post by: skalogryz on March 14, 2021, 03:55:01 pm
I have a project there are multiple forms, I encounter a problem on form show.
--
Lazarus version: 2.0.6
I'd think Lazarus version you're using has the problem.
You might want to try to compile the app on the later version of Lazarus.
Title: Re: Second form does not show until clicked on the dock icon
Post by: ling on March 15, 2021, 08:51:46 am
@skalogryz Thanks!

I updated lazarus version to 2.0.12, fpc to 3.2.0, it works fine now.
TinyPortal © 2005-2018