| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package chat
- import (
- "github.com/ayn2op/discordo/internal/config"
- "github.com/ayn2op/discordo/internal/ui"
- "github.com/ayn2op/discordo/pkg/picker"
- "github.com/ayn2op/tview"
- "github.com/ayn2op/tview/help"
- "github.com/ayn2op/tview/keybind"
- "github.com/gdamore/tcell/v3"
- )
- type attachmentItem struct {
- label string
- open func()
- }
- type attachmentsPicker struct {
- *picker.Picker
- cfg *config.Config
- chatView *Model
- items []attachmentItem
- }
- var _ help.KeyMap = (*attachmentsPicker)(nil)
- func newAttachmentsPicker(cfg *config.Config, chatView *Model) *attachmentsPicker {
- ap := &attachmentsPicker{
- Picker: picker.New(),
- cfg: cfg,
- chatView: chatView,
- }
- ap.Box = ui.ConfigureBox(tview.NewBox(), &cfg.Theme)
- ap.
- SetBlurFunc(nil).
- SetFocusFunc(nil).
- SetBorderSet(cfg.Theme.Border.ActiveSet.BorderSet).
- SetBorderStyle(cfg.Theme.Border.ActiveStyle.Style).
- SetTitleStyle(cfg.Theme.Title.ActiveStyle.Style).
- SetFooterStyle(cfg.Theme.Footer.ActiveStyle.Style)
- ap.SetTitle("Attachments")
- ap.SetKeyMap(&picker.KeyMap{
- Cancel: cfg.Keybinds.Picker.Cancel.Keybind,
- Up: cfg.Keybinds.Picker.Up.Keybind,
- Down: cfg.Keybinds.Picker.Down.Keybind,
- Top: cfg.Keybinds.Picker.Top.Keybind,
- Bottom: cfg.Keybinds.Picker.Bottom.Keybind,
- Select: cfg.Keybinds.Picker.Select.Keybind,
- })
- ap.SetScrollBarVisibility(cfg.Theme.ScrollBar.Visibility.ScrollBarVisibility)
- ap.SetScrollBar(tview.NewScrollBar().
- SetTrackStyle(cfg.Theme.ScrollBar.TrackStyle.Style).
- SetThumbStyle(cfg.Theme.ScrollBar.ThumbStyle.Style).
- SetGlyphSet(cfg.Theme.ScrollBar.GlyphSet.GlyphSet))
- return ap
- }
- func (ap *attachmentsPicker) SetItems(items []attachmentItem) {
- ap.items = items
- ap.ClearItems()
- for i, item := range items {
- ap.AddItem(picker.Item{
- Text: item.label,
- FilterText: item.label,
- Reference: i,
- })
- }
- ap.Update()
- }
- func (ap *attachmentsPicker) close() {
- ap.chatView.RemoveLayer(attachmentsListLayerName)
- ap.chatView.app.SetFocus(ap.chatView.messagesList)
- }
- func (ap *attachmentsPicker) HandleEvent(event tcell.Event) tview.Command {
- switch event := event.(type) {
- case *picker.SelectedEvent:
- index, ok := event.Reference.(int)
- if !ok {
- return nil
- }
- if index < 0 || index >= len(ap.items) {
- return nil
- }
- ap.items[index].open()
- ap.close()
- return nil
- case *picker.CancelEvent:
- ap.close()
- return nil
- }
- return ap.Picker.HandleEvent(event)
- }
- func (ap *attachmentsPicker) ShortHelp() []keybind.Keybind {
- cfg := ap.cfg.Keybinds.Picker
- return []keybind.Keybind{cfg.Up.Keybind, cfg.Down.Keybind, cfg.Select.Keybind, cfg.Cancel.Keybind}
- }
- func (ap *attachmentsPicker) FullHelp() [][]keybind.Keybind {
- cfg := ap.cfg.Keybinds.Picker
- return [][]keybind.Keybind{
- {cfg.Up.Keybind, cfg.Down.Keybind, cfg.Top.Keybind, cfg.Bottom.Keybind},
- {cfg.Select.Keybind, cfg.Cancel.Keybind},
- }
- }
|