Jump to content

how to immediately update tray items when clicking on tray icon?


Recommended Posts

I have a script that gets a list of visible windows and makes a tray item of each in the tray menu. I'd also want it to update every time I want to access that menu again, in case new windows have appeared or some would have been closed. I'm using TrayOnEventMode option and trying to get the update going with TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN...) as in the snippet below. However, the list doesn't update until after the tray menu has been closed again. I think I could work around this by having the script update the list constantly in the background, but that feels like it'd take up unnecessary amount of resources since the script might be used rarely by the user anyway. I tried using TraySetOnEvent($TRAY_EVENT_MOUSEOVER...), and that kind of does it, but it actually still calls my function many times (since the mouse cursor moves quite a bit on the icon) before the tray menu is opened. I could put another menuitem to call the update function, but I'd much rather have just the minimal amount of user actions. Similarly I could have tray menu open with a double click (with TraySetClick()) and have the first click just call the update function, but can anyone tell me if there's a way to call the update once and then have the menu open without these gimmicks?

 

Opt("TrayOnEventMode", 1)

TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN, "UpdateWindowsList")

 

Link to comment
Share on other sites

  • Moderators

jpujut,

Interesting problem - here is my best effort so far:

#include <TrayConstants.au3>

Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown.

; Flag to show if update has occurred
Global $fUpdate = False

$cTray_Change_Item = TrayCreateItem("Item set at " & @SEC)
TrayCreateItem("")
$cTray_Exit_Item = TrayCreateItem("Exit")
TrayItemSetOnEvent($cTray_Exit_Item, "_Exit")

; Update tray when mouse first goes over
TraySetOnEvent($TRAY_EVENT_MOUSEOVER, "_Update_Tray")

While 1
    Sleep(10)
WEnd

Func _Update_Tray()
    ; If not already updated
    If Not $fUpdate Then
        ; Run update code
        ConsoleWrite("Updating at: " & @SEC & @CRLF)
        TrayItemSetText($cTray_Change_Item, "Item set at " & @SEC)
        ; Set flag to show update completed
        $fUpdate = True
        ; Register Adlib to clear flag after 1 sec
        AdlibRegister("_Reset_UpDate", 1000)
        ConsoleWrite("Adlib set" & @CRLF)
    EndIf
EndFunc

Func _Reset_UpDate()

    AdlibUnRegister("_Reset_Update")
    $fUpdate = False
    ConsoleWrite("Reset" & @CRLF)
EndFunc

Func _Exit()
    Exit
EndFunc

It updates the tray when the mouse first goes over the icon, sets a flag to prevent multiple updates, and sets a timer to clear the flag after 1 sec - this timer does not run until after the menu is closed, so the delay could be even shorter.

Please ask if you have any questions.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks for the quick response! Also that seems to have done it! So thanks a lot, I really appreciate it! :) As a newbie I tried to get similar effect with Sleep(), but couldn't get it to work. Didn't know about AdlibRegister until now, so thanks for that, too!

Tho is it possible to have the same effect with just a function of my own (using a flag and a timer like you did) and how exactly does Adlib -functions differ from any other function? Tried googling and function reference, but still didn't quite get it. If you can explain or point me to a good reference, I'd be a bit wiser still! Thanks!

Edited by Melba23
Removed quote
Link to comment
Share on other sites

  • Moderators

jpujut,

When you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - I know what I wrote and it just pads the thread unnecessarily.

I do not really understand the "just a function of my own" statement - that code already uses its own own functions in that code - so can you please explain a little more what it is that want to do.

As to how Adlib works - you tell AutoIt to run the defined function after a specified time and that is what it does. The trick here is to tell AutoIt to stop running the function after it is called the first time. That way you can wait for a while to do something without blocking the whole script.

M23

 

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...