TUI: add quit confirmation
This commit is contained in:
@@ -87,8 +87,9 @@ func (d *Model) handleKeyMsg(msg tea.KeyMsg) tea.Cmd {
|
||||
d.helpBar.ToggleHelp()
|
||||
d.logAction("tui toggled help")
|
||||
case key.Matches(msg, d.keyMap.Quit):
|
||||
d.startConfirmQuit()
|
||||
d.logAction("tui quit requested")
|
||||
return tea.Quit
|
||||
return nil
|
||||
case key.Matches(msg, d.keyMap.SwitchWindow):
|
||||
d.ToggleFocusedWindow()
|
||||
case !d.IsInModal() && key.Matches(msg, d.keyMap.SwitchToKanbanBoard):
|
||||
|
||||
@@ -45,6 +45,7 @@ type Model struct {
|
||||
confirmingDelete bool // true while confirming a delete
|
||||
deleteConfirmID string
|
||||
deleteConfirmIndex int
|
||||
confirmingQuit bool // true while confirming quit
|
||||
|
||||
choosingStatus bool
|
||||
statusIssueID string
|
||||
@@ -170,6 +171,37 @@ func (m *Model) startConfirmDelete(issueID string, index int) {
|
||||
m.deleteConfirmIndex = index
|
||||
}
|
||||
|
||||
func (m *Model) startConfirmQuit() {
|
||||
m.confirmingQuit = true
|
||||
}
|
||||
|
||||
// cancelAllModals closes any open modal/input so quit can be triggered from anywhere.
|
||||
func (m *Model) cancelAllModals() {
|
||||
m.creatingIssue = false
|
||||
m.createTitleInput.Blur()
|
||||
m.createTitleInput.Reset()
|
||||
m.editingTitle = false
|
||||
m.titleInput.Blur()
|
||||
m.editingDescription = false
|
||||
m.descriptionInput.Blur()
|
||||
m.addingComment = false
|
||||
m.commentInput.Blur()
|
||||
m.commentInput.Reset()
|
||||
m.editingAssignee = false
|
||||
m.assigneeInput.Blur()
|
||||
m.choosingStatus = false
|
||||
m.statusIssueID = ""
|
||||
m.choosingPriority = false
|
||||
m.priorityIssueID = ""
|
||||
m.choosingType = false
|
||||
m.typeIssueID = ""
|
||||
m.confirmingDelete = false
|
||||
m.deleteConfirmID = ""
|
||||
m.choosingCloseReason = false
|
||||
m.closingOtherReason = false
|
||||
m.closeReasonInput.Blur()
|
||||
}
|
||||
|
||||
func (m *Model) startChooseStatus(selected ListIssue) {
|
||||
m.choosingStatus = true
|
||||
m.statusIssueID = selected.ID
|
||||
@@ -200,7 +232,7 @@ func (m *Model) Init() tea.Cmd {
|
||||
func (m *Model) IsInModal() bool {
|
||||
return m.editingTitle || m.creatingIssue || m.editingDescription ||
|
||||
m.choosingStatus || m.choosingPriority || m.confirmingDelete ||
|
||||
m.choosingType || m.editingAssignee ||
|
||||
m.confirmingQuit || m.choosingType || m.editingAssignee ||
|
||||
m.choosingCloseReason || m.closingOtherReason
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/LazyBachelor/LazyPM/pkg/tui/components"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/tui/issues"
|
||||
"github.com/charmbracelet/bubbles/list"
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
@@ -323,6 +324,29 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
})
|
||||
|
||||
case tea.KeyMsg:
|
||||
// Always allow quit with q/ctrl+c, even when in inputs or other modals
|
||||
if !m.confirmingQuit && (key.Matches(msg, m.keyMap.Quit) || msg.String() == "q" || msg.String() == "ctrl+c") {
|
||||
m.cancelAllModals()
|
||||
m.startConfirmQuit()
|
||||
m.logAction("tui quit requested")
|
||||
return m, nil
|
||||
}
|
||||
|
||||
if m.confirmingQuit {
|
||||
switch msg.String() {
|
||||
case "y", "Y", "ctrl+c":
|
||||
m.logAction("tui confirmed quit")
|
||||
m.confirmingQuit = false
|
||||
return m, tea.Quit
|
||||
case "n", "N", "esc":
|
||||
m.logAction("tui canceled quit")
|
||||
m.confirmingQuit = false
|
||||
return m, nil
|
||||
default:
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
if m.confirmingDelete {
|
||||
switch msg.String() {
|
||||
case "y", "Y":
|
||||
|
||||
@@ -61,6 +61,19 @@ func (m *Model) View() string {
|
||||
|
||||
mainView := lipgloss.JoinVertical(lipgloss.Left, header, content, footer)
|
||||
|
||||
if m.confirmingQuit {
|
||||
confirmContent := lipgloss.JoinVertical(lipgloss.Left,
|
||||
styles.LabelStyle.Render("Sure you want to quit?"),
|
||||
lipgloss.NewStyle().Foreground(styles.FaintText).Render("y = quit n/Esc = cancel"),
|
||||
)
|
||||
confirmBoxWidth := min(40, m.width-4)
|
||||
confirmBox := styles.ContainerStyle.
|
||||
Width(confirmBoxWidth).
|
||||
BorderForeground(styles.PrimaryBorder).
|
||||
Render(confirmContent)
|
||||
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, confirmBox)
|
||||
}
|
||||
|
||||
if m.editingAssignee {
|
||||
editBoxWidth := min(60, m.width-4)
|
||||
m.assigneeInput.Width = editBoxWidth - 2
|
||||
|
||||
Reference in New Issue
Block a user