def hookCallback(self, state): print 'state', state if state == u'done': self.setIcon(u'main') if self.idleTimer is not None: self.idleTimer.tearDown() elif state == u'start': self.setIcon(u'waiting') self.idleTimer = Timer(self.IDLE_TIMEOUT, self.idleCallback) elif state == u'key': self.idleTimer.reset() else: raise ValueError(u'Unrecognized hook state: %r' % (state,))
class MozartApplication(Application): IDLE_TIMEOUT = 2 * 1000 def setUp(self): # XXX: don't hardcode this self.hotkey = constants.VK_RMENU self.compositionMap = compositionsFromFile(u'resources/Compose') self.notifyMessageID = NOTIFY_MESSAGE self.installHook() self.icons = { u'main': Icon.fromFile(u'resources/mozart-notify-main.ico'), u'disabled': Icon.fromFile(u'resources/mozart-notify-disabled.ico'), u'waiting': Icon.fromFile(u'resources/mozart-notify-waiting.ico')} tip = u'Mozart' self.notifyIcon = NotifyIcon(self.window, self.notifyMessageID, self.icons[u'main'], tip) self.idleTimer = None def tearDown(self): self.uninstallHook() self.notifyIcon.tearDown() for icon in self.icons.itervalues(): icon.tearDown() def idleCallback(self): self.stopComposing() def hookCallback(self, state): print 'state', state if state == u'done': self.setIcon(u'main') if self.idleTimer is not None: self.idleTimer.tearDown() elif state == u'start': self.setIcon(u'waiting') self.idleTimer = Timer(self.IDLE_TIMEOUT, self.idleCallback) elif state == u'key': self.idleTimer.reset() else: raise ValueError(u'Unrecognized hook state: %r' % (state,)) def setIcon(self, name): self.notifyIcon.setIcon(self.icons[name]) def stopComposing(self): self.keyboardHook.hook.stopComposing() def disable(self): self.stopComposing() self.uninstallHook() self.setIcon(u'disabled') def enable(self): self.installHook() self.setIcon(u'main') def installHook(self): hook = ComposeHook(self.hotkey, self.compositionMap, self.hookCallback) self.keyboardHook = KeyboardHook(hook) def uninstallHook(self): if getattr(self, 'keyboardHook', None) is not None: self.keyboardHook.tearDown() self.keyboardHook = None