|
|
@@ -21,14 +21,8 @@ type emojiPicker struct {
|
|
|
browseMode bool
|
|
|
favorites *emojiFavorites
|
|
|
|
|
|
- // items mirrors what was passed to SetItems, so we can look up by cursor.
|
|
|
- items []picker.Item
|
|
|
-
|
|
|
targetMessageID discord.MessageID
|
|
|
targetChannelID discord.ChannelID
|
|
|
-
|
|
|
- // cursor tracks the browse-mode position (0-based into items).
|
|
|
- cursor int
|
|
|
}
|
|
|
|
|
|
var _ help.KeyMap = (*emojiPicker)(nil)
|
|
|
@@ -44,7 +38,7 @@ func newEmojiPicker(cfg *config.Config, chatView *Model) *emojiPicker {
|
|
|
}
|
|
|
|
|
|
// resetBrowse starts the emoji picker in browse (scroll) mode by default.
|
|
|
-func (ep *emojiPicker) resetBrowse() { ep.browseMode = true; ep.cursor = 0 }
|
|
|
+func (ep *emojiPicker) resetBrowse() { ep.browseMode = true }
|
|
|
|
|
|
var commonEmoji = []struct {
|
|
|
emoji string
|
|
|
@@ -156,13 +150,7 @@ func (ep *emojiPicker) update() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- ep.items = items
|
|
|
ep.Model.SetItems(items)
|
|
|
-
|
|
|
- // Clamp cursor.
|
|
|
- if ep.cursor >= len(items) {
|
|
|
- ep.cursor = max(len(items)-1, 0)
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
func emojiItemKey(item picker.Item) string {
|
|
|
@@ -184,12 +172,12 @@ func indexOf(s string, c byte) int {
|
|
|
func (ep *emojiPicker) HandleEvent(event tview.Event) tview.Command {
|
|
|
switch event := event.(type) {
|
|
|
case *tview.KeyEvent:
|
|
|
- if ep.browseMode {
|
|
|
- return ep.handleBrowseKey(event)
|
|
|
- }
|
|
|
- if event.Key() == tcell.KeyEsc {
|
|
|
- ep.browseMode = true
|
|
|
- return nil
|
|
|
+ if cmd, handled := pickerBrowseHandleKey(
|
|
|
+ event, &ep.browseMode, ep.Model,
|
|
|
+ func() { ep.chatView.closeEmojiPicker() },
|
|
|
+ ep.handleFavoriteKey,
|
|
|
+ ); handled {
|
|
|
+ return cmd
|
|
|
}
|
|
|
case *picker.SelectedEvent:
|
|
|
apiEmoji, ok := event.Reference.(discord.APIEmoji)
|
|
|
@@ -214,51 +202,31 @@ func (ep *emojiPicker) HandleEvent(event tview.Event) tview.Command {
|
|
|
return ep.Model.HandleEvent(event)
|
|
|
}
|
|
|
|
|
|
-func (ep *emojiPicker) handleBrowseKey(event *tview.KeyEvent) tview.Command {
|
|
|
- switch {
|
|
|
- case event.Key() == tcell.KeyEsc:
|
|
|
- ep.browseMode = false
|
|
|
- ep.chatView.closeEmojiPicker()
|
|
|
- return nil
|
|
|
- case event.Key() == tcell.KeyRune && event.Str() == "i":
|
|
|
- ep.browseMode = false
|
|
|
- return nil
|
|
|
- case event.Key() == tcell.KeyRune && event.Str() == "j":
|
|
|
- ep.cursor = min(ep.cursor+1, max(len(ep.items)-1, 0))
|
|
|
- return ep.Model.HandleEvent(tcell.NewEventKey(tcell.KeyCtrlN, "", tcell.ModCtrl))
|
|
|
- case event.Key() == tcell.KeyRune && event.Str() == "k":
|
|
|
- ep.cursor = max(ep.cursor-1, 0)
|
|
|
- return ep.Model.HandleEvent(tcell.NewEventKey(tcell.KeyCtrlP, "", tcell.ModCtrl))
|
|
|
- case event.Key() == tcell.KeyRune && event.Str() == "g":
|
|
|
- ep.cursor = 0
|
|
|
- return ep.Model.HandleEvent(tcell.NewEventKey(tcell.KeyHome, "", tcell.ModNone))
|
|
|
- case event.Key() == tcell.KeyRune && event.Str() == "G":
|
|
|
- ep.cursor = max(len(ep.items)-1, 0)
|
|
|
- return ep.Model.HandleEvent(tcell.NewEventKey(tcell.KeyEnd, "", tcell.ModNone))
|
|
|
- case event.Key() == tcell.KeyRune && event.Str() == "f":
|
|
|
- if ep.cursor >= 0 && ep.cursor < len(ep.items) {
|
|
|
- key := emojiItemKey(ep.items[ep.cursor])
|
|
|
- if key != "" {
|
|
|
- ep.favorites.toggle(key)
|
|
|
- ep.update()
|
|
|
- }
|
|
|
+func (ep *emojiPicker) handleFavoriteKey(event *tview.KeyEvent) (tview.Command, bool) {
|
|
|
+ if event.Key() != tcell.KeyRune || event.Str() != "f" {
|
|
|
+ return nil, false
|
|
|
+ }
|
|
|
+
|
|
|
+ // Trigger a select to get the highlighted item's reference without
|
|
|
+ // dispatching the event through the normal loop.
|
|
|
+ cmd := ep.Model.HandleEvent(tcell.NewEventKey(tcell.KeyEnter, "", tcell.ModNone))
|
|
|
+ if cmd == nil {
|
|
|
+ return nil, true
|
|
|
+ }
|
|
|
+ if sel, ok := cmd().(*picker.SelectedEvent); ok {
|
|
|
+ key := emojiItemKey(sel.Item)
|
|
|
+ if key != "" {
|
|
|
+ ep.favorites.toggle(key)
|
|
|
+ ep.update()
|
|
|
}
|
|
|
- return nil
|
|
|
- case event.Key() == tcell.KeyEnter:
|
|
|
- return ep.Model.HandleEvent(event)
|
|
|
}
|
|
|
- return nil
|
|
|
+ return nil, true
|
|
|
}
|
|
|
|
|
|
func (ep *emojiPicker) ShortHelp() []keybind.Keybind {
|
|
|
- cfg := ep.chatView.cfg.Keybinds.Picker
|
|
|
- return []keybind.Keybind{cfg.Up.Keybind, cfg.Down.Keybind, cfg.Select.Keybind, cfg.Cancel.Keybind}
|
|
|
+ return pickerShortHelp(ep.chatView.cfg.Keybinds.Picker)
|
|
|
}
|
|
|
|
|
|
func (ep *emojiPicker) FullHelp() [][]keybind.Keybind {
|
|
|
- cfg := ep.chatView.cfg.Keybinds.Picker
|
|
|
- return [][]keybind.Keybind{
|
|
|
- {cfg.Up.Keybind, cfg.Down.Keybind, cfg.Top.Keybind, cfg.Bottom.Keybind},
|
|
|
- {cfg.Select.Keybind, cfg.Cancel.Keybind},
|
|
|
- }
|
|
|
+ return pickerFullHelp(ep.chatView.cfg.Keybinds.Picker)
|
|
|
}
|