Add buttons for closing the shortcuts table and reseting all shortcuts
MODIFY
internal/shortcuts/shortcuts.go
internal/shortcuts/shortcuts.go
@@ -24,6 +24,7 @@ var (
Identifier: "focus_channel_container",
scope: globalScope,
Event: tcell.NewEventKey(tcell.KeyRune, 'c', tcell.ModAlt),
+ defaultEvent: tcell.NewEventKey(tcell.KeyRune, 'c', tcell.ModAlt),
}
FocusUserContainer = &Shortcut{
@@ -31,6 +32,7 @@ var (
Identifier: "focus_user_container",
scope: globalScope,
Event: tcell.NewEventKey(tcell.KeyRune, 'u', tcell.ModAlt),
+ defaultEvent: tcell.NewEventKey(tcell.KeyRune, 'u', tcell.ModAlt),
}
ToggleUserContainer = &Shortcut{
@@ -38,6 +40,7 @@ var (
Identifier: "toggle_user_container",
scope: globalScope,
Event: tcell.NewEventKey(tcell.KeyRune, 'U', tcell.ModAlt),
+ defaultEvent: tcell.NewEventKey(tcell.KeyRune, 'U', tcell.ModAlt),
}
FocusGuildContainer = &Shortcut{
@@ -45,6 +48,7 @@ var (
Identifier: "focus_guild_container",
scope: globalScope,
Event: tcell.NewEventKey(tcell.KeyRune, 's', tcell.ModAlt),
+ defaultEvent: tcell.NewEventKey(tcell.KeyRune, 's', tcell.ModAlt),
}
FocusPrivateChatPage = &Shortcut{
@@ -52,6 +56,7 @@ var (
Identifier: "focus_private_chat_page",
scope: globalScope,
Event: tcell.NewEventKey(tcell.KeyRune, 'p', tcell.ModAlt),
+ defaultEvent: tcell.NewEventKey(tcell.KeyRune, 'p', tcell.ModAlt),
}
FocusMessageInput = &Shortcut{
@@ -59,6 +64,7 @@ var (
Identifier: "focus_message_input",
scope: globalScope,
Event: tcell.NewEventKey(tcell.KeyRune, 'm', tcell.ModAlt),
+ defaultEvent: tcell.NewEventKey(tcell.KeyRune, 'm', tcell.ModAlt),
}
FocusMessageContainer = &Shortcut{
@@ -66,6 +72,7 @@ var (
Identifier: "focus_message_container",
scope: globalScope,
Event: tcell.NewEventKey(tcell.KeyRune, 't', tcell.ModAlt),
+ defaultEvent: tcell.NewEventKey(tcell.KeyRune, 't', tcell.ModAlt),
}
FocusCommandInput = &Shortcut{
@@ -73,6 +80,7 @@ var (
Identifier: "focus_command_input",
scope: globalScope,
Event: tcell.NewEventKey(tcell.KeyCtrlI, rune(tcell.KeyCtrlI), tcell.ModNone),
+ defaultEvent: tcell.NewEventKey(tcell.KeyCtrlI, rune(tcell.KeyCtrlI), tcell.ModNone),
}
FocusCommandOutput = &Shortcut{
@@ -80,6 +88,7 @@ var (
Identifier: "focus_command_output",
scope: globalScope,
Event: tcell.NewEventKey(tcell.KeyCtrlO, rune(tcell.KeyCtrlO), tcell.ModCtrl),
+ defaultEvent: tcell.NewEventKey(tcell.KeyCtrlO, rune(tcell.KeyCtrlO), tcell.ModCtrl),
}
ToggleCommandView = &Shortcut{
@@ -87,6 +96,7 @@ var (
Identifier: "toggle_command_view",
scope: globalScope,
Event: tcell.NewEventKey(tcell.KeyRune, '.', tcell.ModAlt),
+ defaultEvent: tcell.NewEventKey(tcell.KeyRune, '.', tcell.ModAlt),
}
multilineTextInput = &Scope{
@@ -100,6 +110,7 @@ var (
Identifier: "add_new_line_character",
scope: multilineTextInput,
Event: tcell.NewEventKey(tcell.KeyEnter, 13, tcell.ModAlt),
+ defaultEvent: tcell.NewEventKey(tcell.KeyEnter, 13, tcell.ModAlt),
}
SendMessage = &Shortcut{
@@ -107,6 +118,7 @@ var (
Identifier: "send_message",
scope: multilineTextInput,
Event: tcell.NewEventKey(tcell.KeyEnter, 13, tcell.ModNone),
+ defaultEvent: tcell.NewEventKey(tcell.KeyEnter, 13, tcell.ModNone),
}
scopes = []*Scope{
@@ -144,6 +156,9 @@ type Shortcut struct {
// Event is the shortcut expressed as it's resulting tcell Event.
Event *tcell.EventKey
+
+ //This shortcuts default, in order to be able to reset it.
+ defaultEvent *tcell.EventKey
}
// Scope is what describes a shortcuts scope within the application. Usually
@@ -207,6 +222,11 @@ func (shortcut *Shortcut) UnmarshalJSON(data []byte) error {
return fmt.Errorf(fmt.Sprintf("error finding scope '%s'", temp.ScopeIdentifier))
}
+// Reset resets this shortcuts event value to the default one.
+func (shortcut *Shortcut) Reset() {
+ shortcut.Event = shortcut.defaultEvent
+}
+
func getShortcutsPath() (string, error) {
configDirecotry, configError := config.GetConfigDirectory()
if configError != nil {

MODIFY
internal/shortcuts/table.go
internal/shortcuts/table.go
@@ -21,6 +21,8 @@ type ShortcutTable struct {
shortcuts []*Shortcut
onClose func()
selection int
+ focusNext func()
+ focusPrevious func()
}
// NewShortcutTable creates a new shortcut table that doesn't contain any data.
@@ -62,6 +64,7 @@ func createHeaderCell(text string) *tview.TableCell {
// SetShortcuts sets the shortcut data and changes the UI accordingly.
func (shortcutTable *ShortcutTable) SetShortcuts(shortcuts []*Shortcut) {
shortcutTable.shortcuts = shortcuts
+ shortcutTable.selection = -1
row, _ := shortcutTable.table.GetFixed()
@@ -96,6 +99,17 @@ func (shortcutTable *ShortcutTable) GetShortcuts() []*Shortcut {
return shortcutTable.shortcuts
}
+// SetFocusNext decides which component will be focused when hitting tab
+func (shortcutTable *ShortcutTable) SetFocusNext(function func()) {
+ shortcutTable.focusNext = function
+}
+
+// SetFocusPrevious decides which component will be focused when hitting
+// shift+tab
+func (shortcutTable *ShortcutTable) SetFocusPrevious(function func()) {
+ shortcutTable.focusPrevious = function
+}
+
func (shortcutTable *ShortcutTable) handleInput(event *tcell.EventKey) *tcell.EventKey {
if shortcutTable.selection == -1 {
if event.Key() == tcell.KeyESC {
@@ -105,6 +119,16 @@ func (shortcutTable *ShortcutTable) handleInput(event *tcell.EventKey) *tcell.Ev
return nil
}
+ if event.Key() == tcell.KeyTab {
+ if shortcutTable.focusNext != nil {
+ shortcutTable.focusNext()
+ }
+ } else if event.Key() == tcell.KeyBacktab {
+ if shortcutTable.focusPrevious != nil {
+ shortcutTable.focusPrevious()
+ }
+ }
+
if event.Key() == tcell.KeyUp || event.Key() == tcell.KeyDown {
return event
}

MODIFY
internal/ui/window.go
internal/ui/window.go
@@ -1026,16 +1026,71 @@ func (window *Window) handleGlobalShortcuts(event *tcell.EventKey) *tcell.EventK
window.userActiveTimer.Reset(userInactiveTime)
if event.Modifiers()&tcell.ModAlt == tcell.ModAlt && event.Rune() == 'S' {
- table := shortcuts.NewShortcutTable()
+ var table *shortcuts.ShortcutTable
+ var exitButton *tview.Button
+ var resetButton *tview.Button
+
+ table = shortcuts.NewShortcutTable()
table.SetShortcuts(shortcuts.Shortcuts)
- table.SetOnClose(func() {
+
+ doClose := func() {
window.app.SetRoot(window.rootContainer, true)
window.currentContainer = window.rootContainer
+ window.app.ForceDraw()
+ }
+ table.SetOnClose(doClose)
+
+ exitButton = tview.NewButton("Go back")
+ exitButton.SetSelectedFunc(doClose)
+ exitButton.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
+ if event.Key() == tcell.KeyTab {
+ window.app.SetFocus(table.GetPrimitive())
+ } else if event.Key() == tcell.KeyBacktab {
+ window.app.SetFocus(resetButton)
+ }
+ return event
})
- window.app.SetRoot(table.GetPrimitive(), true)
+
+ resetButton = tview.NewButton("Restore defaults")
+ resetButton.SetSelectedFunc(func() {
+ for _, shortcut := range shortcuts.Shortcuts {
+ shortcut.Reset()
+ }
+ shortcuts.Persist()
+
+ table.SetShortcuts(shortcuts.Shortcuts)
+ window.app.ForceDraw()
+ })
+ resetButton.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
+ if event.Key() == tcell.KeyTab {
+ window.app.SetFocus(exitButton)
+ } else if event.Key() == tcell.KeyBacktab {
+ window.app.SetFocus(table.GetPrimitive())
+ }
+
+ return event
+ })
+
+ table.SetFocusNext(func() { window.app.SetFocus(resetButton) })
+ table.SetFocusPrevious(func() { window.app.SetFocus(exitButton) })
+
+ buttonBar := tview.NewFlex()
+ buttonBar.SetDirection(tview.FlexColumn)
+
+ buttonBar.AddItem(resetButton, 0, 1, false)
+ buttonBar.AddItem(tview.NewBox(), 1, 0, false)
+ buttonBar.AddItem(exitButton, 0, 1, false)
+
+ shortcutsView := tview.NewFlex()
+ shortcutsView.SetDirection(tview.FlexRow)
+
+ shortcutsView.AddItem(table.GetPrimitive(), 0, 1, false)
+ shortcutsView.AddItem(buttonBar, 1, 0, false)
+
+ window.app.SetRoot(shortcutsView, true)
window.app.SetFocus(table.GetPrimitive())
- window.currentContainer = table.GetPrimitive()
+ window.currentContainer = shortcutsView
} else if shortcuts.EventsEqual(event, shortcuts.ToggleCommandView.Event) {
window.SetCommandModeEnabled(!window.commandMode)

Add buttons for closing the shortcuts table and reseting all shortcuts