binding.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. description: string
  12. }
  13. function Binding(triggers: Binding.Trigger[], description = '') {
  14. return Binding.create(triggers, description)
  15. }
  16. namespace Binding {
  17. export function create(triggers: Trigger[], description = ''): Binding {
  18. return { triggers, description }
  19. }
  20. export const Empty: Binding = { triggers: [], description: '' }
  21. export function isEmpty(binding: Binding) {
  22. return binding.triggers.length === 0 ||
  23. binding.triggers.every(t => t.buttons === undefined && t.modifiers === undefined)
  24. }
  25. export function match(binding: Binding, buttons: ButtonsType, modifiers: ModifiersKeys) {
  26. return binding.triggers.some(t => Trigger.match(t, buttons, modifiers))
  27. }
  28. export function format(binding: Binding, name = '') {
  29. const help = binding.description || stringToWords(name)
  30. return interpolate(help, { triggers: binding.triggers.map(t => Trigger.format(t)).join(' or ') })
  31. }
  32. export interface Trigger {
  33. buttons?: ButtonsType,
  34. modifiers?: ModifiersKeys
  35. }
  36. export function Trigger(buttons?: ButtonsType, modifiers?: ModifiersKeys) {
  37. return Trigger.create(buttons, modifiers)
  38. }
  39. export namespace Trigger {
  40. export function create(buttons?: ButtonsType, modifiers?: ModifiersKeys): Trigger {
  41. return { buttons, modifiers }
  42. }
  43. export const Empty: Trigger = {}
  44. export function match(trigger: Trigger, buttons: ButtonsType, modifiers: ModifiersKeys): boolean {
  45. const { buttons: b, modifiers: m } = trigger
  46. return b !== undefined &&
  47. (b === buttons || ButtonsType.has(b, buttons)) &&
  48. (!m || ModifiersKeys.areEqual(m, modifiers))
  49. }
  50. export function format(trigger: Trigger) {
  51. const s: string[] = []
  52. const b = formatButtons(trigger.buttons)
  53. if (b) s.push(b)
  54. const m = formatModifiers(trigger.modifiers)
  55. if (m) s.push(m)
  56. return s.join(' + ')
  57. }
  58. }
  59. }
  60. const B = ButtonsType
  61. function formatButtons(buttons?: ButtonsType) {
  62. const s: string[] = []
  63. if (buttons === undefined) {
  64. s.push('any mouse button')
  65. } else if (buttons === 0) {
  66. s.push('mouse hover')
  67. } else {
  68. if (B.has(buttons, B.Flag.Primary)) s.push('left mouse button')
  69. if (B.has(buttons, B.Flag.Secondary)) s.push('right mouse button')
  70. if (B.has(buttons, B.Flag.Auxilary)) s.push('wheel/middle mouse button')
  71. if (B.has(buttons, B.Flag.Forth)) s.push('three fingers')
  72. }
  73. return s.join(' + ')
  74. }
  75. function formatModifiers(modifiers?: ModifiersKeys, verbose?: boolean) {
  76. const s: string[] = []
  77. if (modifiers) {
  78. if (modifiers.alt) s.push('alt key')
  79. if (modifiers.control) s.push('control key')
  80. if (modifiers.meta) s.push('meta/command key')
  81. if (modifiers.shift) s.push('shift key')
  82. if (verbose && s.length === 0) s.push('no key')
  83. } else {
  84. if (verbose) s.push('any key')
  85. }
  86. return s.join(' + ')
  87. }