action.monkey2 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. Namespace mojox
  2. #rem monkeydoc The Action class.
  3. #end
  4. Class Action
  5. #rem monkeydoc Invoked when the action is triggered.
  6. #end
  7. Field Triggered:Void()
  8. #rem monkeydoc Invoked when the [[Text]], [[Icon]] or [[Enabled]] property is changed.
  9. #end
  10. Field Modified:Void()
  11. #rem monkeydoc Creates a new action.
  12. #end
  13. Method New( text:String,icon:Image=Null )
  14. _text=text
  15. _icon=icon
  16. End
  17. #rem monkeydoc Text representintg the action.
  18. This is used for button or menu text.
  19. #end
  20. Property Text:String()
  21. Return _text
  22. Setter( text:String )
  23. If text=_text Return
  24. _text=text
  25. Modified()
  26. End
  27. #rem monkeydoc Icon representing the action.
  28. This is used for button or menu icon.
  29. #end
  30. Property Icon:Image()
  31. Return _icon
  32. Setter( icon:Image )
  33. If icon=_icon Return
  34. _icon=icon
  35. Modified()
  36. End
  37. #rem monkeydoc Action enabled state.
  38. #end
  39. Property Enabled:Bool()
  40. Return _enabled
  41. Setter( enabled:Bool )
  42. If enabled=_enabled Return
  43. _enabled=enabled
  44. If _enabled EnableHotKey() Else DisableHotKey()
  45. Modified()
  46. End
  47. #rem monkeydoc Action async flag.
  48. If true (the default), then when the action is triggered the [[Triggered]] signal is run on a new fiber.
  49. Note: Fiber are not supported in emscripten, so this property has no effect.
  50. #end
  51. Property Async:Bool()
  52. Return _async
  53. Setter( async:Bool )
  54. _async=async
  55. End
  56. #rem monkeydoc Hotkey for the action.
  57. #end
  58. Property HotKey:Key()
  59. Return _hotKey
  60. Setter( hotKey:Key )
  61. If hotKey=_hotKey Return
  62. If _enabled DisableHotKey()
  63. _hotKey=hotKey
  64. If _enabled EnableHotKey()
  65. End
  66. #rem monkeydoc Hotkey modifiers for the action.
  67. #end
  68. Property HotKeyModifiers:Modifier()
  69. Return _hotKeyMods
  70. Setter( hotKeyModifiers:Modifier )
  71. _hotKeyMods=hotKeyModifiers
  72. End
  73. #rem monkeydoc Text representation of the action hotkey.
  74. #end
  75. Property HotKeyText:String()
  76. If Not _hotKey Return ""
  77. Local text:=""
  78. If _hotKeyMods & Modifier.Shift text+="Shift"
  79. If _hotKeyMods & Modifier.Control text+="+Ctrl"
  80. If _hotKeyMods & Modifier.Alt text+="+Alt"
  81. If _hotKeyMods & Modifier.Gui text+="+Cmd"
  82. text+="+"+Keyboard.KeyName( _hotKey )
  83. If text.StartsWith( "+" ) text=text.Slice( 1 )
  84. Return text
  85. End
  86. #rem monkeydoc Triggers the action.
  87. #end
  88. Method Trigger()
  89. #if __TARGET__<>"emscripten"
  90. If _async
  91. New Fiber( Triggered )
  92. Else
  93. Triggered()
  94. Endif
  95. #else
  96. Triggered()
  97. #endif
  98. End
  99. Private
  100. Field _enabled:Bool=True
  101. Field _text:String
  102. Field _icon:Image
  103. Field _hotKey:Key
  104. Field _hotKeyMods:Modifier
  105. Field _async:Bool=True
  106. Global _hotKeys:Map<Key,Stack<Action>>
  107. Method DisableHotKey()
  108. If Not _hotKey Return
  109. _hotKeys[_hotKey].Remove( Self )
  110. End
  111. Method EnableHotKey()
  112. If Not _hotKey Return
  113. If _hotKeyMods & Modifier.Ignore Return
  114. If Not _hotKeys
  115. _hotKeys=New Map<Key,Stack<Action>>
  116. App.KeyEventFilter+=Lambda( event:KeyEvent )
  117. If event.Eaten Return
  118. If App.ModalView Return
  119. If event.Type<>EventType.KeyDown Return
  120. Local actions:=_hotKeys[event.Key]
  121. If Not actions Return
  122. Local mods:=event.Modifiers & (Modifier.Shift|Modifier.Alt|Modifier.Control|Modifier.Gui)
  123. mods|=Cast<Modifier>( (Int(mods) & $541) Shl 1 | (Int(mods) & $a82) Shr 1 )
  124. For Local action:=Eachin actions
  125. If event.Key<>action._hotKey Continue
  126. If mods<>action._hotKeyMods Continue
  127. action.Trigger()
  128. event.Eat()
  129. Return
  130. Next
  131. End
  132. Endif
  133. Local actions:=_hotKeys[_hotKey]
  134. If Not actions
  135. actions=New Stack<Action>
  136. _hotKeys[_hotKey]=actions
  137. Endif
  138. actions.Add( Self )
  139. End
  140. End