shortcut.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package fyne
  2. import (
  3. "sync"
  4. )
  5. // ShortcutHandler is a default implementation of the shortcut handler
  6. // for the canvasObject
  7. type ShortcutHandler struct {
  8. entry sync.Map // map[string]func(Shortcut)
  9. }
  10. // TypedShortcut handle the registered shortcut
  11. func (sh *ShortcutHandler) TypedShortcut(shortcut Shortcut) {
  12. val, ok := sh.entry.Load(shortcut.ShortcutName())
  13. if !ok {
  14. return
  15. }
  16. f := val.(func(Shortcut))
  17. f(shortcut)
  18. }
  19. // AddShortcut register a handler to be executed when the shortcut action is triggered
  20. func (sh *ShortcutHandler) AddShortcut(shortcut Shortcut, handler func(shortcut Shortcut)) {
  21. sh.entry.Store(shortcut.ShortcutName(), handler)
  22. }
  23. // RemoveShortcut removes a registered shortcut
  24. func (sh *ShortcutHandler) RemoveShortcut(shortcut Shortcut) {
  25. sh.entry.Delete(shortcut.ShortcutName())
  26. }
  27. // Shortcut is the interface used to describe a shortcut action
  28. type Shortcut interface {
  29. ShortcutName() string
  30. }
  31. // KeyboardShortcut describes a shortcut meant to be triggered by a keyboard action.
  32. type KeyboardShortcut interface {
  33. Shortcut
  34. Key() KeyName
  35. Mod() KeyModifier
  36. }
  37. // ShortcutPaste describes a shortcut paste action.
  38. type ShortcutPaste struct {
  39. Clipboard Clipboard
  40. }
  41. var _ KeyboardShortcut = (*ShortcutPaste)(nil)
  42. // Key returns the KeyName for this shortcut.
  43. //
  44. // Implements: KeyboardShortcut
  45. func (se *ShortcutPaste) Key() KeyName {
  46. return KeyV
  47. }
  48. // Mod returns the KeyModifier for this shortcut.
  49. //
  50. // Implements: KeyboardShortcut
  51. func (se *ShortcutPaste) Mod() KeyModifier {
  52. return KeyModifierShortcutDefault
  53. }
  54. // ShortcutName returns the shortcut name
  55. func (se *ShortcutPaste) ShortcutName() string {
  56. return "Paste"
  57. }
  58. // ShortcutCopy describes a shortcut copy action.
  59. type ShortcutCopy struct {
  60. Clipboard Clipboard
  61. }
  62. var _ KeyboardShortcut = (*ShortcutCopy)(nil)
  63. // Key returns the KeyName for this shortcut.
  64. //
  65. // Implements: KeyboardShortcut
  66. func (se *ShortcutCopy) Key() KeyName {
  67. return KeyC
  68. }
  69. // Mod returns the KeyModifier for this shortcut.
  70. //
  71. // Implements: KeyboardShortcut
  72. func (se *ShortcutCopy) Mod() KeyModifier {
  73. return KeyModifierShortcutDefault
  74. }
  75. // ShortcutName returns the shortcut name
  76. func (se *ShortcutCopy) ShortcutName() string {
  77. return "Copy"
  78. }
  79. // ShortcutCut describes a shortcut cut action.
  80. type ShortcutCut struct {
  81. Clipboard Clipboard
  82. }
  83. var _ KeyboardShortcut = (*ShortcutCut)(nil)
  84. // Key returns the KeyName for this shortcut.
  85. //
  86. // Implements: KeyboardShortcut
  87. func (se *ShortcutCut) Key() KeyName {
  88. return KeyX
  89. }
  90. // Mod returns the KeyModifier for this shortcut.
  91. //
  92. // Implements: KeyboardShortcut
  93. func (se *ShortcutCut) Mod() KeyModifier {
  94. return KeyModifierShortcutDefault
  95. }
  96. // ShortcutName returns the shortcut name
  97. func (se *ShortcutCut) ShortcutName() string {
  98. return "Cut"
  99. }
  100. // ShortcutSelectAll describes a shortcut selectAll action.
  101. type ShortcutSelectAll struct{}
  102. var _ KeyboardShortcut = (*ShortcutSelectAll)(nil)
  103. // Key returns the KeyName for this shortcut.
  104. //
  105. // Implements: KeyboardShortcut
  106. func (se *ShortcutSelectAll) Key() KeyName {
  107. return KeyA
  108. }
  109. // Mod returns the KeyModifier for this shortcut.
  110. //
  111. // Implements: KeyboardShortcut
  112. func (se *ShortcutSelectAll) Mod() KeyModifier {
  113. return KeyModifierShortcutDefault
  114. }
  115. // ShortcutName returns the shortcut name
  116. func (se *ShortcutSelectAll) ShortcutName() string {
  117. return "SelectAll"
  118. }