action.monkey2 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #If Not __WEB_TARGET__
  55. _async=async
  56. #endif
  57. End
  58. #rem monkeydoc Hotkey for the action.
  59. #end
  60. Property HotKey:Key()
  61. Return _hotKey
  62. Setter( hotKey:Key )
  63. If hotKey=_hotKey Return
  64. If _enabled DisableHotKey()
  65. _hotKey=hotKey
  66. If _enabled EnableHotKey()
  67. End
  68. #rem monkeydoc Hotkey modifiers for the action.
  69. #end
  70. Property HotKeyModifiers:Modifier()
  71. Return _hotKeyMods
  72. Setter( hotKeyModifiers:Modifier )
  73. _hotKeyMods=hotKeyModifiers
  74. End
  75. #rem monkeydoc Text representation of the action hotkey.
  76. #end
  77. Property HotKeyText:String()
  78. If Not _hotKey Return ""
  79. Local text:=""
  80. If _hotKeyMods & Modifier.Shift text+="Shift"
  81. If _hotKeyMods & Modifier.Control text+="+Ctrl"
  82. If _hotKeyMods & Modifier.Alt text+="+Alt"
  83. If _hotKeyMods & Modifier.Gui text+="+Cmd"
  84. text+="+"+Keyboard.KeyName( _hotKey )
  85. If text.StartsWith( "+" ) text=text.Slice( 1 )
  86. Return text
  87. End
  88. #rem monkeydoc Triggers the action.
  89. #end
  90. Method Trigger()
  91. #If Not __WEB_TARGET__
  92. If _async
  93. New Fiber( Triggered )
  94. Else
  95. Triggered()
  96. Endif
  97. #Else
  98. Triggered()
  99. #Endif
  100. End
  101. Private
  102. Field _enabled:Bool=True
  103. Field _text:String
  104. Field _icon:Image
  105. Field _hotKey:Key
  106. Field _hotKeyMods:Modifier
  107. #If __DESKTOP_TARGET__
  108. Field _async:Bool=True
  109. #Else
  110. Field _async:Bool=False
  111. #Endif
  112. Global _hotKeys:Map<Key,Stack<Action>>
  113. Method DisableHotKey()
  114. If Not _hotKey Return
  115. _hotKeys[_hotKey].Remove( Self )
  116. End
  117. Method EnableHotKey()
  118. If Not _hotKey Return
  119. If _hotKeyMods & Modifier.Ignore Return
  120. If Not _hotKeys
  121. _hotKeys=New Map<Key,Stack<Action>>
  122. App.KeyEventFilter+=Lambda( event:KeyEvent )
  123. If event.Eaten Return
  124. If App.ModalView Return
  125. If event.Type<>EventType.KeyDown Return
  126. Local actions:=_hotKeys[event.Key]
  127. If Not actions Return
  128. Local mods:=event.Modifiers & (Modifier.Shift|Modifier.Alt|Modifier.Control|Modifier.Gui)
  129. mods|=Cast<Modifier>( (Int(mods) & $541) Shl 1 | (Int(mods) & $a82) Shr 1 )
  130. For Local action:=Eachin actions
  131. If event.Key<>action._hotKey Continue
  132. If mods<>action._hotKeyMods Continue
  133. action.Trigger()
  134. event.Eat()
  135. Return
  136. Next
  137. End
  138. Endif
  139. Local actions:=_hotKeys[_hotKey]
  140. If Not actions
  141. actions=New Stack<Action>
  142. _hotKeys[_hotKey]=actions
  143. Endif
  144. actions.Add( Self )
  145. End
  146. End