开发者

Auto updating (time) tooltip in Griffon SystemTray

Hey, I'm thinking about using griffon in an very simple DeskTop app I've been asked to code. 90% of it's features are a few mouse clicks away from the system tray, it's like "start/choose/stop". One thing I'd need is an tooltip, that would update it's content each second (or minute, nevermind ;-)) counting the time someone has been working on some particular task.

My questions are:

  • How can I make the "Youre working for: ${HourMin.since model.startedWorkingAt}" be开发者_开发问答 called on an regular basis, to make the tootip always up to date? Using this code, it only gets called once, at tooltip construction time.
  • How can I dynamically add "actions" to the popupMenu during the application lifetime?

Thanks for any advice - book advices where such problems are solved would be also quite helpful.

actions {
  action(id: 'cutAction',
          name: 'Pokaż okno',
          closure: {
            trayIcon.displayMessage("Cut", "Cut some content", NONE)
          },
          mnemonic: 'T',
          accelerator: shortcut('X'),
          smallIcon: imageIcon(resource: "icons/cut.png", class: Console),
          shortDescription: 'Cut'
  )
//... more actions
}

systemTray {
  trayIcon(id: "trayIcon",
          resource: "/groovy/ui/ConsoleIcon.png",
          class: groovy.ui.Console,
          toolTip: {
            "Youre working for: ${HourMin.since model.startedWorkingAt}"
          },
          actionPerformed: {
//            todo do toggle
          }) {
    popupMenu {
      menuItem(cutAction)
      menuItem(copyAction)
      menuItem(pasteAction)
      separator()
      menuItem(exitAction)
    }
  }
}


Have you tried binding the GString?

toolTip: bind {
        "Youre working for: ${HourMin.since model.startedWorkingAt}"
      },


I figured out an way to do it, here's how:

// in the controller
def updateSystemTimeTask = new TimerTask() {
  @Override
  void run() {
    onSystemTimeUpdate System.currentTimeMillis()
  }
}

def onSystemTimeUpdate(Long systemTime) {
  if (model.working) {
    view.systemTray.trayIcons[0].toolTip = "Pracujesz już ${HourMin.since model.startedWorkingAt}".toString()
  } else {
    view.systemTray.trayIcons[0].toolTip = = "Obecnie nie pracujesz."
  }
}

// ant this in the initialization method
def myTimer = new Timer().schedule updateSystemTimeTask, initialDelay, minute

I'm not sure if it's the cleanest way possible, but it's quite ok I guess, since all the action is in the views controller anyways.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜