summaryrefslogtreecommitdiff
path: root/internal/tui/keys.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/tui/keys.go')
-rw-r--r--internal/tui/keys.go190
1 files changed, 92 insertions, 98 deletions
diff --git a/internal/tui/keys.go b/internal/tui/keys.go
index 46b664c..e1ac587 100644
--- a/internal/tui/keys.go
+++ b/internal/tui/keys.go
@@ -14,22 +14,21 @@ const (
ScopeProjectsBox
ScopeHistoryBoxSummaries
ScopeHistoryBoxDetails
+ ScopeModal
)
// KeyBinding represents the available key bindings for a view
type KeyBinding struct {
Key string
Description func(AppModel) string
- Scope KeyBindingScope
- Result func(AppModel) tea.Cmd
+ Result func(*AppModel) tea.Cmd
Hide bool
}
type (
- createClientMsg struct{}
- createProjectMsg struct{}
- deleteHistoryEntry struct{}
- editHistoryEntry struct{}
+ createClientMsg struct{}
+ createProjectMsg struct{}
+ editHistoryEntry struct{}
)
func msgAsCmd(msg tea.Msg) tea.Cmd {
@@ -41,14 +40,12 @@ var Bindings map[KeyBindingScope]map[string]KeyBinding = map[KeyBindingScope]map
"ctrl+n": KeyBinding{
Key: "Ctrl+n",
Description: func(AppModel) string { return "Next Pane" },
- Scope: ScopeGlobal,
- Result: func(AppModel) tea.Cmd { return navigate(true) },
+ Result: func(*AppModel) tea.Cmd { return navigate(true) },
},
"ctrl+p": KeyBinding{
Key: "Ctrl+p",
Description: func(AppModel) string { return "Prev Pane" },
- Scope: ScopeGlobal,
- Result: func(AppModel) tea.Cmd { return navigate(false) },
+ Result: func(*AppModel) tea.Cmd { return navigate(false) },
},
"p": KeyBinding{
Key: "p",
@@ -58,44 +55,33 @@ var Bindings map[KeyBindingScope]map[string]KeyBinding = map[KeyBindingScope]map
}
return "Punch In"
},
- Scope: ScopeGlobal,
- Result: func(am AppModel) tea.Cmd {
+ Result: func(am *AppModel) tea.Cmd {
if am.timerBox.timerInfo.IsActive {
- return punchOut(am)
+ return punchOut(*am)
}
- return punchIn(am)
+ return punchIn(*am)
},
},
- "/": KeyBinding{
- Key: "/",
- Description: func(am AppModel) string { return "Search" },
- Scope: ScopeGlobal,
- Result: func(AppModel) tea.Cmd { return activateSearch() },
- },
"r": KeyBinding{
Key: "r",
Description: func(am AppModel) string { return "Refresh" },
- Scope: ScopeGlobal,
- Result: func(am AppModel) tea.Cmd { return am.refreshCmd },
+ Result: func(am *AppModel) tea.Cmd { return am.refreshCmd },
},
"q": KeyBinding{
Key: "q",
Description: func(am AppModel) string { return "Quit" },
- Scope: ScopeGlobal,
- Result: func(AppModel) tea.Cmd { return tea.Quit },
+ Result: func(*AppModel) tea.Cmd { return tea.Quit },
},
"ctrl+c": KeyBinding{
Key: "Ctrl+c",
Description: func(am AppModel) string { return "Quit" },
- Scope: ScopeGlobal,
- Result: func(AppModel) tea.Cmd { return tea.Quit },
+ Result: func(*AppModel) tea.Cmd { return tea.Quit },
Hide: true,
},
"ctrl+d": KeyBinding{
Key: "Ctrl+d",
Description: func(am AppModel) string { return "Quit" },
- Scope: ScopeGlobal,
- Result: func(AppModel) tea.Cmd { return tea.Quit },
+ Result: func(*AppModel) tea.Cmd { return tea.Quit },
Hide: true,
},
},
@@ -108,12 +94,11 @@ var Bindings map[KeyBindingScope]map[string]KeyBinding = map[KeyBindingScope]map
}
return "Punch In"
},
- Scope: ScopeTimerBox,
- Result: func(am AppModel) tea.Cmd {
+ Result: func(am *AppModel) tea.Cmd {
if am.timerBox.timerInfo.IsActive {
- return punchOut(am)
+ return punchOut(*am)
}
- return punchIn(am)
+ return punchIn(*am)
},
},
},
@@ -121,147 +106,150 @@ var Bindings map[KeyBindingScope]map[string]KeyBinding = map[KeyBindingScope]map
"j": KeyBinding{
Key: "j",
Description: func(AppModel) string { return "Down" },
- Scope: ScopeProjectsBox,
- Result: func(AppModel) tea.Cmd { return changeSelection(true) },
+ Result: func(*AppModel) tea.Cmd { return changeSelection(true) },
},
"k": KeyBinding{
Key: "k",
Description: func(AppModel) string { return "Up" },
- Scope: ScopeProjectsBox,
- Result: func(AppModel) tea.Cmd { return changeSelection(false) },
+ Result: func(*AppModel) tea.Cmd { return changeSelection(false) },
},
"down": KeyBinding{
Key: "down",
Description: func(AppModel) string { return "Down" },
- Scope: ScopeProjectsBox,
- Result: func(AppModel) tea.Cmd { return changeSelection(true) },
+ Result: func(*AppModel) tea.Cmd { return changeSelection(true) },
Hide: true,
},
"up": KeyBinding{
Key: "up",
Description: func(AppModel) string { return "Up" },
- Scope: ScopeProjectsBox,
- Result: func(AppModel) tea.Cmd { return changeSelection(false) },
+ Result: func(*AppModel) tea.Cmd { return changeSelection(false) },
Hide: true,
},
"enter": KeyBinding{
Key: "Enter",
Description: func(AppModel) string { return "Punch In on Selection" },
- Scope: ScopeProjectsBox,
- Result: func(am AppModel) tea.Cmd { return punchInOnSelection(am) },
+ Result: func(am *AppModel) tea.Cmd { return punchInOnSelection(*am) },
},
"n": KeyBinding{
Key: "n",
Description: func(AppModel) string { return "New Project" },
- Scope: ScopeProjectsBox,
- Result: func(AppModel) tea.Cmd { return msgAsCmd(createProjectMsg{}) },
+ Result: func(*AppModel) tea.Cmd { return msgAsCmd(createProjectMsg{}) },
},
"N": KeyBinding{
Key: "N",
Description: func(AppModel) string { return "New Client" },
- Scope: ScopeProjectsBox,
- Result: func(AppModel) tea.Cmd { return msgAsCmd(createClientMsg{}) },
+ Result: func(*AppModel) tea.Cmd { return msgAsCmd(createClientMsg{}) },
},
},
ScopeHistoryBoxSummaries: {
"j": KeyBinding{
Key: "j",
Description: func(AppModel) string { return "Down" },
- Scope: ScopeHistoryBoxSummaries,
- Result: func(AppModel) tea.Cmd { return changeSelection(true) },
+ Result: func(*AppModel) tea.Cmd { return changeSelection(true) },
},
"k": KeyBinding{
Key: "k",
Description: func(AppModel) string { return "Up" },
- Scope: ScopeHistoryBoxSummaries,
- Result: func(AppModel) tea.Cmd { return changeSelection(false) },
+ Result: func(*AppModel) tea.Cmd { return changeSelection(false) },
},
"down": KeyBinding{
Key: "down",
Description: func(AppModel) string { return "Down" },
- Scope: ScopeHistoryBoxSummaries,
- Result: func(AppModel) tea.Cmd { return changeSelection(true) },
+ Result: func(*AppModel) tea.Cmd { return changeSelection(true) },
Hide: true,
},
"up": KeyBinding{
Key: "up",
Description: func(AppModel) string { return "Up" },
- Scope: ScopeHistoryBoxSummaries,
- Result: func(AppModel) tea.Cmd { return changeSelection(false) },
+ Result: func(*AppModel) tea.Cmd { return changeSelection(false) },
Hide: true,
},
"enter": KeyBinding{
Key: "Enter",
Description: func(AppModel) string { return "Select" },
- Scope: ScopeHistoryBoxSummaries,
- Result: func(AppModel) tea.Cmd { return selectHistorySummary() },
+ Result: func(*AppModel) tea.Cmd { return selectHistorySummary() },
},
},
ScopeHistoryBoxDetails: {
"j": KeyBinding{
Key: "j",
Description: func(AppModel) string { return "Down" },
- Scope: ScopeHistoryBoxDetails,
- Result: func(AppModel) tea.Cmd { return changeSelection(true) },
+ Result: func(*AppModel) tea.Cmd { return changeSelection(true) },
},
"k": KeyBinding{
Key: "k",
Description: func(AppModel) string { return "Up" },
- Scope: ScopeHistoryBoxDetails,
- Result: func(AppModel) tea.Cmd { return changeSelection(false) },
+ Result: func(*AppModel) tea.Cmd { return changeSelection(false) },
},
"down": KeyBinding{
Key: "Down",
Description: func(AppModel) string { return "Down" },
- Scope: ScopeHistoryBoxDetails,
- Result: func(AppModel) tea.Cmd { return changeSelection(true) },
- Hide: true,
+ Result: func(*AppModel) tea.Cmd { return changeSelection(true) },
+ Hide: true,
},
"up": KeyBinding{
Key: "Up",
Description: func(AppModel) string { return "Up" },
- Scope: ScopeHistoryBoxDetails,
- Result: func(AppModel) tea.Cmd { return changeSelection(false) },
- Hide: true,
+ Result: func(*AppModel) tea.Cmd { return changeSelection(false) },
+ Hide: true,
},
"e": KeyBinding{
Key: "e",
Description: func(AppModel) string { return "Edit" },
- Scope: ScopeHistoryBoxDetails,
- Result: func(AppModel) tea.Cmd { return msgAsCmd(editHistoryEntry{}) },
+ Result: func(am *AppModel) tea.Cmd { return editCurrentEntry() },
},
"d": KeyBinding{
Key: "d",
Description: func(AppModel) string { return "Delete" },
- Scope: ScopeHistoryBoxDetails,
- Result: func(AppModel) tea.Cmd { return msgAsCmd(deleteHistoryEntry{}) },
+ Result: func(*AppModel) tea.Cmd { return confirmDeleteEntry() },
},
"enter": KeyBinding{
Key: "Enter",
Description: func(AppModel) string { return "Resume" },
- Scope: ScopeHistoryBoxDetails,
- Result: func(am AppModel) tea.Cmd { return punchInOnSelection(am) },
+ Result: func(am *AppModel) tea.Cmd { return punchInOnSelection(*am) },
},
"b": KeyBinding{
Key: "b",
Description: func(AppModel) string { return "Back" },
- Scope: ScopeHistoryBoxDetails,
- Result: func(AppModel) tea.Cmd { return backToHistorySummary() },
+ Result: func(*AppModel) tea.Cmd { return backToHistorySummary() },
},
"esc": KeyBinding{
Key: "Esc",
Description: func(AppModel) string { return "Back" },
- Scope: ScopeHistoryBoxDetails,
- Result: func(AppModel) tea.Cmd { return backToHistorySummary() },
+ Result: func(*AppModel) tea.Cmd { return backToHistorySummary() },
Hide: true,
},
},
+ ScopeModal: {
+ "enter": KeyBinding{
+ Key: "Enter",
+ Description: func(AppModel) string { return "Submit" },
+ Result: func(am *AppModel) tea.Cmd {
+ return tea.Sequence(
+ closeModal(),
+ am.modalBox.SubmitForm(*am),
+ )
+ },
+ },
+ "esc": KeyBinding{
+ Key: "Esc",
+ Description: func(AppModel) string { return "Close" },
+ Result: func(*AppModel) tea.Cmd { return closeModal() },
+ },
+ },
}
-// KeyHandler processes key messages and returns the appropriate action
-func HandleKeyPress(msg tea.KeyMsg, data AppModel) tea.Cmd {
+// HandleKeyPress processes key messages and returns the appropriate action
+func HandleKeyPress(msg tea.KeyMsg, data *AppModel) tea.Cmd {
key := msg.String()
+ if data.modalBox.Active {
+ if binding, ok := Bindings[ScopeModal][key]; ok {
+ return binding.Result(data)
+ }
+ return data.modalBox.HandleKeyPress(msg)
+ }
+
if binding, ok := Bindings[ScopeGlobal][key]; ok {
return binding.Result(data)
}
@@ -280,36 +268,42 @@ func HandleKeyPress(msg tea.KeyMsg, data AppModel) tea.Cmd {
local = Bindings[ScopeHistoryBoxDetails]
}
}
-
if binding, ok := local[key]; ok {
return binding.Result(data)
}
return nil
}
-func activeBindings(box BoxType, level HistoryViewLevel) []KeyBinding {
+func activeBindings(box BoxType, level HistoryViewLevel, modal ModalBoxModel) []KeyBinding {
out := make([]KeyBinding, 0, len(Bindings[ScopeGlobal]))
- for _, binding := range Bindings[ScopeGlobal] {
- out = append(out, binding)
- }
- var scope KeyBindingScope
- switch box {
- case TimerBox:
- scope = ScopeTimerBox
- case ProjectsBox:
- scope = ScopeProjectsBox
- case HistoryBox:
- switch level {
- case HistoryLevelSummary:
- scope = ScopeHistoryBoxSummaries
- case HistoryLevelDetails:
- scope = ScopeHistoryBoxDetails
+ if modal.Active {
+ for _, binding := range Bindings[ScopeModal] {
+ out = append(out, binding)
+ }
+ } else {
+ for _, binding := range Bindings[ScopeGlobal] {
+ out = append(out, binding)
}
- }
- for _, binding := range Bindings[scope] {
- out = append(out, binding)
+ var scope KeyBindingScope
+ switch box {
+ case TimerBox:
+ scope = ScopeTimerBox
+ case ProjectsBox:
+ scope = ScopeProjectsBox
+ case HistoryBox:
+ switch level {
+ case HistoryLevelSummary:
+ scope = ScopeHistoryBoxSummaries
+ case HistoryLevelDetails:
+ scope = ScopeHistoryBoxDetails
+ }
+ }
+
+ for _, binding := range Bindings[scope] {
+ out = append(out, binding)
+ }
}
slices.SortFunc(out, func(a, b KeyBinding) int {