Explorar o código

feat: add confirmation modal for delete action (#575) (#605)

Co-authored-by: Ayyan <ayn2op@gmail.com>
Dmytro Maistruk hai 7 meses
pai
achega
a2f16a4d72
Modificáronse 4 ficheiros con 44 adicións e 5 borrados
  1. 22 0
      cmd/application.go
  2. 16 1
      cmd/messages_list.go
  3. 2 1
      internal/config/config.toml
  4. 4 3
      internal/config/keys.go

+ 22 - 0
cmd/application.go

@@ -6,6 +6,7 @@ import (
 	"github.com/ayn2op/discordo/internal/config"
 	"github.com/ayn2op/discordo/internal/consts"
 	"github.com/ayn2op/discordo/internal/login"
+	"github.com/ayn2op/discordo/internal/ui"
 	"github.com/ayn2op/tview"
 	"github.com/gdamore/tcell/v2"
 	"github.com/zalando/go-keyring"
@@ -15,6 +16,7 @@ const (
 	flexPageName            = "flex"
 	mentionsListPageName    = "mentionsList"
 	attachmentsListPageName = "attachmentsList"
+	confirmModalPageName    = "confirmModal"
 )
 
 type application struct {
@@ -151,3 +153,23 @@ func (a *application) onFlexInputCapture(event *tcell.EventKey) *tcell.EventKey
 
 	return event
 }
+
+func (a *application) showConfirmModal(prompt string, buttons []string, onDone func(label string)) {
+	previousFocus := a.GetFocus()
+
+	modal := tview.NewModal().
+		SetText(prompt).
+		AddButtons(buttons).
+		SetDoneFunc(func(_ int, buttonLabel string) {
+			a.pages.RemovePage(confirmModalPageName).SwitchToPage(flexPageName)
+			a.SetFocus(previousFocus)
+
+			if onDone != nil {
+				onDone(buttonLabel)
+			}
+		})
+
+	a.pages.
+		AddAndSwitchToPage(confirmModalPageName, ui.Centered(modal, 40, 10), true).
+		ShowPage(flexPageName)
+}

+ 16 - 1
cmd/messages_list.go

@@ -282,8 +282,9 @@ func (ml *messagesList) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
 		ml.reply(true)
 	case ml.cfg.Keys.MessagesList.Delete:
 		ml.delete()
+	case ml.cfg.Keys.MessagesList.DeleteConfirm:
+		ml.confirmDelete()
 	}
-
 	return nil
 }
 
@@ -557,6 +558,20 @@ func (ml *messagesList) reply(mention bool) {
 	app.SetFocus(app.messageInput)
 }
 
+func (ml *messagesList) confirmDelete() {
+	onChoice := func(choice string) {
+		if choice == "Yes" {
+			ml.delete()
+		}
+	}
+
+	app.showConfirmModal(
+		"Are you sure you want to delete this message?",
+		[]string{"Yes", "No"},
+		onChoice,
+	)
+}
+
 func (ml *messagesList) delete() {
 	msg, err := ml.selectedMsg()
 	if err != nil {

+ 2 - 1
internal/config/config.toml

@@ -69,7 +69,8 @@ reply = "Rune[r]"
 # Reply (with mention) to the selected message.
 reply_mention = "Rune[R]"
 cancel = "Esc"
-delete = "Rune[d]"
+delete = "Rune[D]"
+delete_confirm = "Rune[d]"
 # Open the selected message's attachments or hyperlinks in the message
 # using the default browser application.
 open = "Rune[o]"

+ 4 - 3
internal/config/keys.go

@@ -40,9 +40,10 @@ type (
 		Reply        string `toml:"reply"`
 		ReplyMention string `toml:"reply_mention"`
 
-		Cancel string `toml:"cancel"`
-		Delete string `toml:"delete"`
-		Open   string `toml:"open"`
+		Cancel        string `toml:"cancel"`
+		Delete        string `toml:"delete"`
+		DeleteConfirm string `toml:"delete_confirm"`
+		Open          string `toml:"open"`
 
 		YankContent string `toml:"yank_content"`
 		YankURL     string `toml:"yank_url"`