TrayIcon picture not updating

Post your Gambas programming questions here.
ForeverNoob
Posts: 6
Joined: Thursday 01st October 2020 10:11am

TrayIcon picture not updating

Post by ForeverNoob » Thursday 28th January 2021 9:52am

Hello,

I'm writing a form with a TrayIcon that should change according periodically using a timer:

Code: Select all

Public Sub Form_Open()
  StatusTimer.Delay = 1
  TrayIcon1.Visible = True
End

Public Sub StatusTimer_Timer()
  UpdateTrayIcon
  StatusTimer.Delay = Globals.StatusTimerDelay
End

Private Sub UpdateTrayIcon()
  Dim CnxStatus As String
  CnxStatus = GetCnxStatus()
  If InStr(CnxStatus, "Connected") > 0 Then
    TrayIcon1.Tooltip = "Connected"
    TrayIcon1.Picture = Picture["icon1.png"]
  Else If InStr(CnxStatus, "Not connected") > 0 Then
    TrayIcon1.Tooltip = "Not connected"
    TrayIcon1.Picture = Picture["icon2.png"]
  Endif
  Print TrayIcon1.Tooltip
End
The first time the timer is invoked the icon is displayed correctly, but after that the icon picture remain the same, even when the returned status changed (the tooltip is updated correctly). Please advise.

TIA

User avatar
stevedee
Posts: 378
Joined: Monday 20th March 2017 6:06pm

Re: TrayIcon picture not updating

Post by stevedee » Thursday 28th January 2021 12:41pm

ForeverNoob wrote:
Thursday 28th January 2021 9:52am
...even when the returned status changed (the tooltip is updated correctly). Please advise.

TIA
Hi TIA and welcome to Gambas One.

I can't immediately see a problem with your code, but I suggest you put a break point at the line starting: "If "

Then run the code and when it stops use F8 to step it on one line at a time while checking variables.

If I get a chance later today I may try to reproduce your problem.

A couple of other tips; paste code on these pages using the gb button, as this preserves Gambas formatting...
[code]
Public Sub Form_Open()
  StatusTimer.Delay = 1
  TrayIcon1.Visible = True
End

Public Sub StatusTimer_Timer()
  UpdateTrayIcon
  StatusTimer.Delay = Globals.StatusTimerDelay
End

Private Sub UpdateTrayIcon()
  Dim CnxStatus As String
  CnxStatus = GetCnxStatus()
  If InStr(CnxStatus, "Connected") > 0 Then
    TrayIcon1.Tooltip = "Connected"
    TrayIcon1.Picture = Picture["icon1.png"]
  Else If InStr(CnxStatus, "Not connected") > 0 Then
    TrayIcon1.Tooltip = "Not connected"
    TrayIcon1.Picture = Picture["icon2.png"]
  Endif
  Print TrayIcon1.Tooltip
End
[/code]

...I also suggest that you enable line numbers in the editor, as they can be very handy!

I hope this helps.

Steve

User avatar
stevedee
Posts: 378
Joined: Monday 20th March 2017 6:06pm

Re: TrayIcon picture not updating

Post by stevedee » Thursday 28th January 2021 2:18pm

OK, I've knocked up a simple program and single-stepped my way through it.
TrayIcon.png
TrayIcon.png (169.7 KiB) Viewed 385 times
The only thing I notice is that while the TrayIcon object is updated as expected, the tray icon only gets updated on the screen when the code exits my timer, which calls the Sub, which changes the TrayIcon icon/picture. Adding Wait statements didn't help (no real reason why they should, its not the form that needs to update its the system panel...but worth a try).

Can you humour me and just temporarily increase your timer delay from 1 second to 10 seconds and retest?

User avatar
cogier
Site Admin
Posts: 646
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: TrayIcon picture not updating

Post by cogier » Thursday 28th January 2021 2:49pm

I have run into one part of this problem which is the 'Comment' not changing. This can be solved by 'Hiding' then 'Showing' the icon which will change the 'Comment'. However, I can't get this to work with the 'Picture'.

Run this code in a Graphical program with 'gb.gui.trayicon' selected.
bSwitch As Boolean
Timer1 As Timer
TrayIcon1 As Trayicon
Label1 As Label

Public Sub Form_Open()

  With Me
    .Height = 300
    .Width = 650
    .Arrangement = Arrange.Vertical
    .Padding = 5
  End With

  With Label1 = New Label(Me) As "Label1"
    .Expand = True
    .Height = 28
    .Font.Size = 60
    .Font.Bold = True
    .Alignment = Align.Center
  End With

  With TrayIcon1 = New TrayIcon As "TrayIcon1"
    TrayIcon1.Tooltip = "Connected"
  End With

  With Timer1 = New Timer As "Timer1"
    .Delay = 3000
    .Trigger
    .Start
  End With

End

Public Sub Timer1_Timer()

  TrayIcon1.Hide                                    'This is an important part

  If bSwitch = False Then
    TrayIcon1.Tooltip = "Connected"
    Label1.Text = "Connected"
  Else
    TrayIcon1.Tooltip = "Not Connected"
    Label1.Text = "Not connected"
  End If

  TrayIcon1.Show                                    'This is an important part
  bSwitch = Not bSwitch

End

Public Sub Form_Close()

  TrayIcon1.Delete                                  'This stops the program from continuing to run

End

User avatar
stevedee
Posts: 378
Joined: Monday 20th March 2017 6:06pm

Re: TrayIcon picture not updating

Post by stevedee » Thursday 28th January 2021 3:29pm

cogier wrote:
Thursday 28th January 2021 2:49pm
I have run into one part of this problem which is the 'Comment' not changing. This can be solved by 'Hiding' then 'Showing' the icon which will change the 'Comment'. However, I can't get this to work with the 'Picture'....
By 'comment' do you mean ToolTip Charlie?

If I remove the breakpoints and just run my code with TimerDelay of 5 seconds its works fine, including the ToolTips.
(Actually, If I run with TimerDelay set to 1ms is still runs fine, but no tool tips.)

I didn't find .Show/.Hide/.Delete of any help with Icon/Picture.

I wonder if this problem is related to the Linux desktop in use, because there is some old chat in TrayIcon Help about DBus and Ubuntu implementations...

TrayIconHelp.png
TrayIconHelp.png (179.49 KiB) Viewed 380 times

...or there could still be a problem with TIA's code. Maybe you could upload the project so we can take a look?

ForeverNoob
Posts: 6
Joined: Thursday 01st October 2020 10:11am

Re: TrayIcon picture not updating

Post by ForeverNoob » Thursday 28th January 2021 3:46pm

stevedee wrote:
Thursday 28th January 2021 2:18pm
OK, I've knocked up a simple program and single-stepped my way through it.

TrayIcon.png

The only thing I notice is that while the TrayIcon object is updated as expected, the tray icon only gets updated on the screen when the code exits my timer, which calls the Sub, which changes the TrayIcon icon/picture. Adding Wait statements didn't help (no real reason why they should, its not the form that needs to update its the system panel...but worth a try).

Can you humour me and just temporarily increase your timer delay from 1 second to 10 seconds and retest?
Hi stevedee,

Thanks for answering. I tried increasing the initial timer delay, but that didn't work. In fact, over the value of 50(ms) or so the TrayIcon didn't updated even on the first iteration and just displayed the default gambas icon.

ForeverNoob
Posts: 6
Joined: Thursday 01st October 2020 10:11am

Re: TrayIcon picture not updating

Post by ForeverNoob » Thursday 28th January 2021 3:55pm

cogier wrote:
Thursday 28th January 2021 2:49pm
I have run into one part of this problem which is the 'Comment' not changing. This can be solved by 'Hiding' then 'Showing' the icon which will change the 'Comment'. However, I can't get this to work with the 'Picture'.

Run this code in a Graphical program with 'gb.gui.trayicon' selected.
bSwitch As Boolean
Timer1 As Timer
TrayIcon1 As Trayicon
Label1 As Label

Public Sub Form_Open()

  With Me
    .Height = 300
    .Width = 650
    .Arrangement = Arrange.Vertical
    .Padding = 5
  End With

  With Label1 = New Label(Me) As "Label1"
    .Expand = True
    .Height = 28
    .Font.Size = 60
    .Font.Bold = True
    .Alignment = Align.Center
  End With

  With TrayIcon1 = New TrayIcon As "TrayIcon1"
    TrayIcon1.Tooltip = "Connected"
  End With

  With Timer1 = New Timer As "Timer1"
    .Delay = 3000
    .Trigger
    .Start
  End With

End

Public Sub Timer1_Timer()

  TrayIcon1.Hide                                    'This is an important part

  If bSwitch = False Then
    TrayIcon1.Tooltip = "Connected"
    Label1.Text = "Connected"
  Else
    TrayIcon1.Tooltip = "Not Connected"
    Label1.Text = "Not connected"
  End If

  TrayIcon1.Show                                    'This is an important part
  bSwitch = Not bSwitch

End

Public Sub Form_Close()

  TrayIcon1.Delete                                  'This stops the program from continuing to run

End
Hi cogier,

Thanks for answering. I've copied the above code to a new form and eidted the relevant part to look like this:
  If bSwitch = False Then
    TrayIcon1.Tooltip = "Connected"
    TrayIcon1.Picture = Picture["icon1.png"]
    Label1.Text = "Connected"
  Else
    TrayIcon1.Tooltip = "Not Connected"
    TrayIcon1.Picture = Picture["icon2.png"]
    Label1.Text = "Not connected"
  End If
The label is periodically updated, but not the icon.

ForeverNoob
Posts: 6
Joined: Thursday 01st October 2020 10:11am

Re: TrayIcon picture not updating

Post by ForeverNoob » Thursday 28th January 2021 3:58pm

OK, this is what I'm going to do: I'll create 2 TrayIcons, each with it's own icon, and just hide or show the relevant one as needed. No use fighting what looks like a bug somewhere.

Thank you all for the time and effort.

User avatar
stevedee
Posts: 378
Joined: Monday 20th March 2017 6:06pm

Re: TrayIcon picture not updating

Post by stevedee » Thursday 28th January 2021 4:05pm

ForeverNoob wrote:
Thursday 28th January 2021 3:55pm
...The label is periodically updated, but not the icon.
Could you post the details of your system?
including:-
- Linux Distro
- Linux kernel
- Gambas version

...and maybe go to menu Project > Properties and post a screen-shot like this;

TrayIconProperties.png
TrayIconProperties.png (137.94 KiB) Viewed 375 times

ForeverNoob
Posts: 6
Joined: Thursday 01st October 2020 10:11am

Re: TrayIcon picture not updating

Post by ForeverNoob » Thursday 28th January 2021 4:16pm

stevedee wrote:
Thursday 28th January 2021 4:05pm
ForeverNoob wrote:
Thursday 28th January 2021 3:55pm
...The label is periodically updated, but not the icon.
Could you post the details of your system?
including:-
- Linux Distro
- Linux kernel
- Gambas version

...and maybe go to menu Project > Properties and post a screen-shot like this;


TrayIconProperties.png
Mint 20.1 cinnamon
Kernel 5.4.0-62-generic
Gambas 3.14.3
Gambas Project Properties.png
Gambas Project Properties.png (91.47 KiB) Viewed 373 times

Post Reply