Friday 18 October 2013

System tray

Do you ever dreamed to put an icon of you program in the system tray? Well, today we'll discover how to do this task with few lines of Rebol code.
System tray is that small zone in the monitor area, near the clock, where there are small icons:



to put the icon just use the ToSystemTray special function. This function was created by Izkata and we'll its code at the end of this post. ToSystemTray needs a block with a string for the tip bubble description, then string and action for every line you want to show with right mouse button; the first action is the default action with left mouse button. You can add any bar you need with the keyword bar. Let's see an example:


ToSystemTray [
{I am the little help-bubble that appears while the mouse hovers over the icon}
[
{Item 1 (Also done when icon left-clicked on)} [ alert {Example} ]
bar
{Item 2 - Remove from System Tray} [RemoveSystemTray]
bar
{Quit} [quit]
]
]


Here is the result:



Here is the source code of ToSystemTray:

REBOL [
  Author: {Izkata}
  Email: Izkata@Comcast.net
  File: %simple-system-tray.r
  Date: 5-Jul-2005
  Title: {Simple System Tray}
  Purpose: {
      After learning Rebol 1.3 could place itself in the System Tray on Windows,
      and after finding Graham's example, I decided to make a slightly simpler
      version of the System Tray dialect....
      Since it's based on Graham's example, it may well be missing stuff...
      Feel free to email me anything about it, I'll likely edit and repost it!   (^.-)
  }
]
MySysTray: make object! [
  Defines: none
  Tray: none
  Parse-Systray: func [Systray][
      Defines: copy []
      Tray: compose/deep [add main [help: (first Systray) menu: []]]
      parse second Systray [
        some [
            'bar (append Tray/3/4 'bar) |
            set Str string! set Blk block! (
              append Tray/3/4 compose [(to-word rejoin [{Defines} 1 + length? Defines {:}]) (Str)]
              append Defines compose/only [(to-word join copy {Defines} 1 + length? Defines) (Blk)]
            )
        ]
      ]
      return Tray
  ]
  set 'ToSystemTray func [
      {Send REBOL to the System Tray}
      Systray [block!]
      /return {Return immediately}
  ][
      Tray: Parse-Systray Systray
      system/ports/system: open [scheme: 'system]
      append system/ports/wait-list system/ports/system
      system/ports/system/awake: func [port /local Which][
        if all [
            r: pick port 1
            r/1 = 'tray
        ][
            if r/3 = 'menu [do select Defines r/4]
            if r/3 = 'activate [do second Defines]
        ]
        return false
      ]
      set-modes system/ports/system compose/only [tray: (load mold Tray)]
      if not return [do-events]
  ]
  set 'RemoveSystemTray func [{Remove REBOL from the System Tray}][
      remove find system/ports/wait-list system/ports/system
      close system/ports/system
      system/ports/system: open [scheme: 'system]
  ]
]

1 comment:

  1. Jeez, I'd forgotten all about this - found out about this post on Facebook.

    It was pointed out on there that 'r was leaked outside that function, and since I no longer have that email address either, I went ahead and updated it on rebol.org

    ReplyDelete