binding.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ButtonsType, ModifiersKeys } from './input/input-observer';
  7. import { interpolate, stringToWords } from './string';
  8. export { Binding }
  9. interface Binding {
  10. triggers: Binding.Trigger[]
  11. action: string
  12. description: string
  13. }
  14. function Binding(triggers: Binding.Trigger[], action = '', description = '') {
  15. return Binding.create(triggers, action, description)
  16. }
  17. namespace Binding {
  18. export function create(triggers: Trigger[], action = '', description = ''): Binding {
  19. return { triggers, action, description }
  20. }
  21. export const Empty: Binding = { triggers: [], action: '', description: '' }
  22. export function isEmpty(binding: Binding) {
  23. return binding.triggers.length === 0 ||
  24. binding.triggers.every(t => t.buttons === undefined && t.modifiers === undefined)
  25. }
  26. export function match(binding: Binding, buttons: ButtonsType, modifiers: ModifiersKeys) {
  27. return binding.triggers.some(t => Trigger.match(t, buttons, modifiers))
  28. }
  29. export function formatTriggers(binding: Binding) {
  30. return binding.triggers.map(Trigger.format).join(' or ');
  31. }
  32. export function format(binding: Binding, name = '') {
  33. const help = binding.description || stringToWords(name)
  34. return interpolate(help, { triggers: '<i>' + formatTriggers(binding) + '</i>' })
  35. }
  36. export interface Trigger {
  37. buttons?: ButtonsType,
  38. modifiers?: ModifiersKeys
  39. }
  40. export function Trigger(buttons?: ButtonsType, modifiers?: ModifiersKeys) {
  41. return Trigger.create(buttons, modifiers)
  42. }
  43. export namespace Trigger {
  44. export function create(buttons?: ButtonsType, modifiers?: ModifiersKeys): Trigger {
  45. return { buttons, modifiers }
  46. }
  47. export const Empty: Trigger = {}
  48. export function match(trigger: Trigger, buttons: ButtonsType, modifiers: ModifiersKeys): boolean {
  49. const { buttons: b, modifiers: m } = trigger
  50. return b !== undefined &&
  51. (b === buttons || ButtonsType.has(b, buttons)) &&
  52. (!m || ModifiersKeys.areEqual(m, modifiers))
  53. }
  54. export function format(trigger: Trigger) {
  55. const s: string[] = []
  56. const b = formatButtons(trigger.buttons)
  57. if (b) s.push(b)
  58. const m = formatModifiers(trigger.modifiers)
  59. if (m) s.push(m)
  60. return s.join(' + ')
  61. }
  62. }
  63. }
  64. const B = ButtonsType
  65. function formatButtons(buttons?: ButtonsType) {
  66. const s: string[] = []
  67. if (buttons === undefined) {
  68. s.push('any mouse button')
  69. } else if (buttons === 0) {
  70. s.push('mouse hover')
  71. } else {
  72. if (B.has(buttons, B.Flag.Primary)) s.push('left mouse button')
  73. if (B.has(buttons, B.Flag.Secondary)) s.push('right mouse button')
  74. if (B.has(buttons, B.Flag.Auxilary)) s.push('wheel/middle mouse button')
  75. if (B.has(buttons, B.Flag.Forth)) s.push('three fingers')
  76. }
  77. return s.join(' + ')
  78. }
  79. function formatModifiers(modifiers?: ModifiersKeys, verbose?: boolean) {
  80. const s: string[] = []
  81. if (modifiers) {
  82. if (modifiers.alt) s.push('alt key')
  83. if (modifiers.control) s.push('control key')
  84. if (modifiers.meta) s.push('meta/command key')
  85. if (modifiers.shift) s.push('shift key')
  86. if (verbose && s.length === 0) s.push('no key')
  87. } else {
  88. if (verbose) s.push('any key')
  89. }
  90. return s.join(' + ')
  91. }