Recent

Author Topic: [SOLVED] Unexpected minimize to taskbar result  (Read 1565 times)

torbente

  • Full Member
  • ***
  • Posts: 229
[SOLVED] Unexpected minimize to taskbar result
« on: August 03, 2018, 07:55:54 pm »
Im pretty suprised whit this:

I added a option to minimize the project to the system tray.

To minimize:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormWindowStateChange(Sender: TObject);
  2. begin
  3. if OptionsData.MinimToTray then
  4.    begin
  5.    if Form1.WindowState = wsMinimized then
  6.       begin
  7.       SysTrayIcon.visible:=true;
  8.       form1.Hide;
  9.       end;
  10.    end;
  11. end;

To restore:
Code: Pascal  [Select][+][-]
  1. Procedure TForm1.DoubleClickSysTray(Sender: TObject);
  2. Begin
  3. SysTrayIcon.visible:=false;
  4. Form1.WindowState:=wsNormal;
  5. Form1.Show;
  6. End;

All works ok visually, but after the restore procedure, it is like if the app was just started, not only restored. (actually, the app stills works correctly until it is restored from the systray)

It is something wrong with my restore procedure? I took it from a wiki example.

Thank you for your help.
« Last Edit: August 04, 2018, 01:11:47 pm by torbente »

Handoko

  • Hero Member
  • *****
  • Posts: 3547
  • My goal: build my own game engine using Lazarus
Re: Unexpected minimize to taskbar result
« Reply #1 on: August 03, 2018, 10:07:27 pm »
... after the restore procedure, it is like if the app was just started, not only restored.

What did you mean with was just started? I use a variable "Test: Integer" to check how many times the OnFormCreate event being called. The result (on the Form.Caption) is 1 and never change. So it does not 'restarted' after the restore procedure.

But I found some weird behavior. I need to double click the tray icon to twice (sometimes more) to restore the form. I don't consider it is a bug. Because what you're trying to do is not a good thing. I meant you should not hide the form when it is in the middle of the process of minimizing.

Luckily, after some tests I found the workaround for it. Tested on Lazarus 1.8.4 Gtk2 Linux not sure if it also works on Windows.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     CheckBox1: TCheckBox;
  16.     Timer1: TTimer;
  17.     TrayIcon1: TTrayIcon;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure FormWindowStateChange(Sender: TObject);
  20.     procedure Timer1Timer(Sender: TObject);
  21.     procedure TrayIcon1DblClick(Sender: TObject);
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.   Test: Integer = 0;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.FormWindowStateChange(Sender: TObject);
  35. begin
  36.   if CheckBox1.Checked then
  37.     if (WindowState = wsMinimized) then
  38.       begin
  39.       AlphaBlend := True;
  40.       AlphaBlendValue := 0;
  41.       WindowState := wsNormal;
  42.       Timer1.Enabled := True;
  43.       end;
  44. end;
  45.  
  46. procedure TForm1.Timer1Timer(Sender: TObject);
  47. begin
  48.   Timer1.Enabled    := False;
  49.   TrayIcon1.Visible := True;
  50.   AlphaBlend := False;
  51.   AlphaBlendValue := 255;
  52.   Hide;
  53. end;
  54.  
  55. procedure TForm1.FormCreate(Sender: TObject);
  56. begin
  57.   Inc(Test);
  58.   Caption := Test.ToString;
  59. end;
  60.  
  61. procedure TForm1.TrayIcon1DblClick(Sender: TObject);
  62. begin
  63.   TrayIcon1.Visible := False;
  64.   Show;
  65. end;
  66.  
  67. end.

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Unexpected minimize to taskbar result
« Reply #2 on: August 04, 2018, 12:43:28 am »
... after the restore procedure, it is like if the app was just started, not only restored.

What did you mean with was just started? I use a variable "Test: Integer" to check how many times the OnFormCreate event being called. The result (on the Form.Caption) is 1 and never change. So it does not 'restarted' after the restore procedure.

But I found some weird behavior. I need to double click the tray icon to twice (sometimes more) to restore the form. I don't consider it is a bug. Because what you're trying to do is not a good thing. I meant you should not hide the form when it is in the middle of the process of minimizing.

Luckily, after some tests I found the workaround for it. Tested on Lazarus 1.8.4 Gtk2 Linux not sure if it also works on Windows.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     CheckBox1: TCheckBox;
  16.     Timer1: TTimer;
  17.     TrayIcon1: TTrayIcon;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure FormWindowStateChange(Sender: TObject);
  20.     procedure Timer1Timer(Sender: TObject);
  21.     procedure TrayIcon1DblClick(Sender: TObject);
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.   Test: Integer = 0;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.FormWindowStateChange(Sender: TObject);
  35. begin
  36.   if CheckBox1.Checked then
  37.     if (WindowState = wsMinimized) then
  38.       begin
  39.       AlphaBlend := True;
  40.       AlphaBlendValue := 0;
  41.       WindowState := wsNormal;
  42.       Timer1.Enabled := True;
  43.       end;
  44. end;
  45.  
  46. procedure TForm1.Timer1Timer(Sender: TObject);
  47. begin
  48.   Timer1.Enabled    := False;
  49.   TrayIcon1.Visible := True;
  50.   AlphaBlend := False;
  51.   AlphaBlendValue := 255;
  52.   Hide;
  53. end;
  54.  
  55. procedure TForm1.FormCreate(Sender: TObject);
  56. begin
  57.   Inc(Test);
  58.   Caption := Test.ToString;
  59. end;
  60.  
  61. procedure TForm1.TrayIcon1DblClick(Sender: TObject);
  62. begin
  63.   TrayIcon1.Visible := False;
  64.   Show;
  65. end;
  66.  
  67. end.
hiding the form has to do with hiding the button in the task bar, changing the forms transparency should not do that.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Handoko

  • Hero Member
  • *****
  • Posts: 3547
  • My goal: build my own game engine using Lazarus
Re: Unexpected minimize to taskbar result
« Reply #3 on: August 04, 2018, 03:52:20 am »
@taazz

I can understand what you meant. But I got weird behavior if I hide the form when the form is minimizing. That issue can be solved using a Timer to delay the process. But then I got another issue, it visually looked ugly:

the form is minimizing > force it to wsNormal > delay > hide the form

The process follows the order above. Because I 'force' it to unminimize when the form is minimizing, I can saw noticable flicker. That's why I used form's transparency (alpha) to solve this problem.

As I mentioned early, we should not hide the form when the form is minimizing. The workaround I suggested may not give the same behavior on different widgetset/OS, although it works correctly on my Linux Gtk + Compiz (window manager).

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Unexpected minimize to taskbar result
« Reply #4 on: August 04, 2018, 03:56:15 am »
@taazz

I can understand what you meant. But I got weird behavior if I hide the form when the form is minimizing. That issue can be solved using a Timer to delay the process. But then I got another issue, it visually looked ugly:

the form is minimizing > force it to wsNormal > delay > hide the form

The process follows the order above. Because I 'force' it to unminimize when the form is minimizing, I can saw noticable flicker. That's why I used form's transparency (alpha) to solve this problem.

As I mentioned early, we should not hide the form when the form is minimizing. The workaround I suggested may not give the same behavior on different widgetset/OS, although it works correctly on my Linux Gtk + Compiz (window manager).
there are two portions for this problem 1) application minimization 2) form minimization. You only try to address the second portion of the problem which is not the one that gives the problem. Its the first portion that has the problem ee application is never restored.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

torbente

  • Full Member
  • ***
  • Posts: 229
Re: Unexpected minimize to taskbar result
« Reply #5 on: August 04, 2018, 05:55:25 am »
Handoko gave me ligth of my fail: i have some routines calls in FormOnShow, so that created the wrong result when the app were restored; i just added a boolean to execute it only the first time the form is shown and the program is now working as expected. (i did not know Formonshow was called with form.visible := true )

Quote
I meant you should not hide the form when it is in the middle of the process of minimizing.

AFAIK, FormWindowStateChange is called once the form changed its state.  :o

Quote
But I found some weird behavior. I need to double click the tray icon to twice (sometimes more) to restore the form.

I did not have that issue here (windows 8.1 32bits + lazarus 1.8.4)

Thank you very much.
« Last Edit: August 04, 2018, 01:13:00 pm by torbente »

 

TinyPortal © 2005-2018