/muntjac/demo/sampler/features/shortcuts/ShortcutScopeExample.py

https://github.com/rwl/muntjac
Python | 92 lines | 50 code | 26 blank | 16 comment | 2 complexity | 3c0fc2843480651d06e49cb9335c260c MD5 | raw file
  1. from muntjac.api import \
  2. VerticalLayout, HorizontalLayout, Panel, TextField, Button
  3. from muntjac.event.shortcut_listener import ShortcutListener
  4. from muntjac.ui.component import IFocusable
  5. from muntjac.ui.abstract_field import FocusShortcut
  6. from muntjac.event.shortcut_action import KeyCode, ModifierKey
  7. from muntjac.ui.button import ClickShortcut, IClickListener
  8. class ShortcutScopeExample(VerticalLayout):
  9. def __init__(self):
  10. super(ShortcutScopeExample, self).__init__()
  11. self.setSpacing(True)
  12. # We want the panels side-by-side
  13. hz = HorizontalLayout()
  14. hz.setSpacing(True)
  15. self.addComponent(hz)
  16. # Add two identical panels
  17. hz.addComponent(self.createPanel(1))
  18. hz.addComponent(self.createPanel(2))
  19. def createPanel(self, number):
  20. p = Panel('Panel %d' % number)
  21. p.getContent().setSpacing(True)
  22. # Let's create a customized shortcut that jumps to the next field
  23. p.addAction(NextFieldListener("Next field", KeyCode.ARROW_DOWN, None))
  24. # Firstname input with an input prompt for demo clarity
  25. firstname = TextField('Firstname')
  26. firstname.setInputPrompt('ALT-SHIFT-F to focus')
  27. p.addComponent(firstname)
  28. # Using firstname.addShortcutListener() would add globally,
  29. # but we want the shortcut only in this panel:
  30. p.addAction(FocusShortcut(firstname, KeyCode.F, ModifierKey.ALT,
  31. ModifierKey.SHIFT))
  32. # additinally we'll add a global shortcut for this field using the
  33. # shorthand notation (^1 == CTRL-1,NextFieldListener etc)
  34. firstname.addShortcutListener(FocusShortcut(firstname,
  35. 'Focus panel &_' + str(number)))
  36. p.setDescription('CTRL-' + str(number) + ' to focus')
  37. # Lastname input with an input prompt for demo clarity
  38. lastname = TextField('Lastname')
  39. lastname.setInputPrompt('ALT-SHIFT-L to focus')
  40. p.addComponent(lastname)
  41. # Using firstname.addShortcutListener() would add globally,
  42. # but we want the shortcut only in this panel:
  43. p.addAction(FocusShortcut(lastname, KeyCode.L, ModifierKey.ALT,
  44. ModifierKey.SHIFT))
  45. # Button with a simple click-listener
  46. save = Button('Save', SaveListener(self, p))
  47. p.addComponent(save)
  48. # setClickShortcut() would add global shortcut, instead we
  49. # 'scope' the shortcut to the panel:
  50. p.addAction(ClickShortcut(save, KeyCode.S, ModifierKey.ALT,
  51. ModifierKey.SHIFT))
  52. return p
  53. class NextFieldListener(ShortcutListener):
  54. def handleAction(self, sender, target):
  55. # The panel is the sender, loop trough content
  56. for nxt in sender.getComponentIterator():
  57. # target is the field we're currently in, focus the next
  58. if isinstance(nxt, IFocusable):
  59. nxt.focus()
  60. class SaveListener(IClickListener):
  61. def __init__(self, c, panel):
  62. self._c = c
  63. self._panel = panel
  64. def buttonClick(self, event):
  65. self._c.getWindow().showNotification(self._panel.getCaption()
  66. + ' save clicked')