Skip to content

Commit f6a0fc7

Browse files
committed
feat: Remove existing notify and warn modals
1 parent ab3fdfa commit f6a0fc7

File tree

10 files changed

+67
-146
lines changed

10 files changed

+67
-146
lines changed

src/internal/function.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,10 @@ func processCmdToTeaCmd(cmd processbar.Cmd) tea.Cmd {
361361
}
362362
}
363363
}
364+
365+
func getCopyOrCutOperationName(cut bool) string {
366+
if cut {
367+
return "cut"
368+
}
369+
return "copy"
370+
}

src/internal/handle_file_operations.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (m *model) warnModalForRenaming() tea.Cmd {
6565
"There is already a file or directory with that name",
6666
"This operation will override the existing file",
6767
notify.RenameAction)
68-
return NewNofifyModalMsg(notifyModel, reqID)
68+
return NewNotifyModalMsg(notifyModel, reqID)
6969
}
7070
}
7171

@@ -170,7 +170,7 @@ func (m *model) getDeleteTriggerCmd() tea.Cmd {
170170
title = "Are you sure you want to completely delete"
171171
content = "This operation cannot be undone and your data will be completely lost."
172172
}
173-
return NewNofifyModalMsg(notify.New(true, title, content, notify.DeleteAction), reqID)
173+
return NewNotifyModalMsg(notify.New(true, title, content, notify.DeleteAction), reqID)
174174
}
175175
}
176176

@@ -216,7 +216,7 @@ func (m *model) getPasteItemCmd() tea.Cmd {
216216
return func() tea.Msg {
217217
err := validatePasteOperation(panelLocation, copyItems, cut)
218218
if err != nil {
219-
return NewNofifyModalMsg(notify.New(true, "Invalid paste location", err.Error(), notify.NoAction),
219+
return NewNotifyModalMsg(notify.New(true, "Invalid paste location", err.Error(), notify.NoAction),
220220
reqID)
221221
}
222222
state := executePasteOperation(&m.processBarModel, panelLocation, copyItems, cut)
@@ -238,11 +238,8 @@ func validatePasteOperation(panelLocation string, copyItems []string, cut bool)
238238
}
239239

240240
if isAncestor(srcPath, panelLocation) {
241-
operation := "copy"
242-
if cut {
243-
operation = "cut"
244-
}
245-
return fmt.Errorf("cannot %s and paste a directory into itself or its subdirectory", operation)
241+
return fmt.Errorf("cannot %s and paste a directory into itself or its subdirectory",
242+
getCopyOrCutOperationName(cut))
246243
}
247244
}
248245

src/internal/handle_modal.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ func (m *model) cancelTypingModal() {
1313
m.typingModal.open = false
1414
}
1515

16-
// Close warn modal
17-
func (m *model) cancelWarnModal() {
18-
m.warnModal.open = false
19-
}
20-
2116
// Confirm to create file or directory
2217
func (m *model) createItem() {
2318
if err := checkFileNameValidity(m.typingModal.textInput.Value()); err != nil {

src/internal/key_function.go

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -193,71 +193,50 @@ func (m *model) typingModalOpenKey(msg string) {
193193
}
194194
}
195195

196-
// TODO : There is a lot of duplication for these models, each one of them has to handle
197-
// ConfirmTyping and CancelTyping in a similar way. There is a scope of some good refactoring here.
198-
func (m *model) warnModalOpenKey(msg string) tea.Cmd {
199-
switch {
200-
case slices.Contains(common.Hotkeys.CancelTyping, msg) || slices.Contains(common.Hotkeys.Quit, msg):
201-
m.cancelWarnModal()
202-
if m.warnModal.warnType == confirmRenameItem {
203-
m.cancelRename()
204-
}
205-
case slices.Contains(common.Hotkeys.Confirm, msg):
206-
m.warnModal.open = false
207-
switch m.warnModal.warnType {
208-
case confirmDeleteItem:
209-
return m.getDeleteCmd()
210-
case confirmRenameItem:
211-
m.confirmRename()
212-
}
213-
}
214-
return nil
215-
}
196+
func (m *model) notifyModelOpenKey(msg string) tea.Cmd {
197+
isCancel := slices.Contains(common.Hotkeys.CancelTyping, msg) || slices.Contains(common.Hotkeys.Quit, msg)
198+
isConfirm := slices.Contains(common.Hotkeys.Confirm, msg)
216199

217-
func (m *model) notifyModalOpenKey(msg string) {
218-
//nolint:gocritic // We use switch here because other key logic is also using switch, so it's more consistent.
219-
switch {
220-
case slices.Contains(common.Hotkeys.Confirm, msg):
221-
m.notifyModal.open = false
200+
if !isCancel && !isConfirm {
201+
slog.Warn("Invalid keypress in notifyModel", "msg", msg)
202+
return nil
203+
}
204+
m.notifyModel.Close()
205+
action := m.notifyModel.GetConfirmAction()
206+
if isCancel {
207+
return m.handleNotifyModelCancel(action)
222208
}
209+
return m.handleNotifyModelConfirm(action)
223210
}
224211

225-
func (m *model) notifyModelOpenKey(msg string) tea.Cmd {
226-
switch {
227-
case slices.Contains(common.Hotkeys.CancelTyping, msg) || slices.Contains(common.Hotkeys.Quit, msg):
228-
m.nofigyModel.Close()
229-
if m.nofigyModel.GetConfirmAction() == notify.RenameAction {
230-
m.cancelRename()
231-
}
232-
case slices.Contains(common.Hotkeys.Confirm, msg):
233-
m.nofigyModel.Close()
234-
switch m.nofigyModel.GetConfirmAction() {
235-
case notify.DeleteAction:
236-
return m.getDeleteCmd()
237-
case notify.RenameAction:
238-
m.confirmRename()
239-
case notify.NoAction:
240-
// Do nothing
241-
default:
242-
slog.Error("Unknown type of action", "action", m.nofigyModel.GetConfirmAction())
243-
}
212+
func (m *model) handleNotifyModelCancel(action notify.ConfirmActionType) tea.Cmd {
213+
switch action {
214+
case notify.RenameAction:
215+
m.cancelRename()
216+
case notify.QuitAction:
217+
m.modelQuitState = notQuitting
218+
case notify.DeleteAction, notify.NoAction:
219+
// Do nothing
244220
default:
245-
slog.Warn("Invalid keypress in notifyModel", "msg", msg)
221+
slog.Error("Unknown type of action", "action", action)
246222
}
247223
return nil
248224
}
249225

250-
// Handle key input to confirm or cancel and close quiting warn in SPF
251-
func (m *model) confirmToQuitSuperfile(msg string) bool {
252-
switch {
253-
case slices.Contains(common.Hotkeys.CancelTyping, msg) || slices.Contains(common.Hotkeys.Quit, msg):
254-
m.modelQuitState = notQuitting
255-
return false
256-
case slices.Contains(common.Hotkeys.Confirm, msg):
257-
return true
226+
func (m *model) handleNotifyModelConfirm(action notify.ConfirmActionType) tea.Cmd {
227+
switch action {
228+
case notify.DeleteAction:
229+
return m.getDeleteCmd()
230+
case notify.RenameAction:
231+
m.confirmRename()
232+
case notify.QuitAction:
233+
m.modelQuitState = quitConfirmationReceived
234+
case notify.NoAction:
235+
// Ignore
258236
default:
259-
return false
237+
slog.Error("Unknown type of action", "action", action)
260238
}
239+
return nil
261240
}
262241

263242
// Handles key inputs inside sort options menu

src/internal/model.go

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/yorukot/superfile/src/config/icon"
1515
"github.com/yorukot/superfile/src/internal/common"
1616
"github.com/yorukot/superfile/src/internal/ui/metadata"
17+
"github.com/yorukot/superfile/src/internal/ui/notify"
1718
"github.com/yorukot/superfile/src/internal/utils"
1819

1920
"github.com/barasher/go-exiftool"
@@ -278,7 +279,7 @@ func (m *model) handleKeyInput(msg tea.KeyMsg) tea.Cmd {
278279
"filePanel.focusType", m.fileModel.filePanels[m.filePanelFocusIndex].focusType,
279280
"filePanel.panelMode", m.fileModel.filePanels[m.filePanelFocusIndex].panelMode,
280281
"typingModal.open", m.typingModal.open,
281-
"warnModal.open", m.warnModal.open,
282+
"notifyModel.open", m.notifyModel.IsOpen(),
282283
"promptModal.open", m.promptModal.IsOpen(),
283284
"fileModel.renaming", m.fileModel.renaming,
284285
"searchBar.focussed", m.fileModel.filePanels[m.filePanelFocusIndex].searchBar.Focused(),
@@ -291,21 +292,18 @@ func (m *model) handleKeyInput(msg tea.KeyMsg) tea.Cmd {
291292
return nil
292293
}
293294
var cmd tea.Cmd
294-
quitSuperfile := false
295295
switch {
296296
case m.typingModal.open:
297297
m.typingModalOpenKey(msg.String())
298298
case m.promptModal.IsOpen():
299299
// Ignore keypress. It will be handled in Update call via
300300
// updateFilePanelState
301+
// TODO: Convert that to async via tea.Cmd
301302

302-
case m.nofigyModel.IsOpen():
303-
cmd = m.notifyModelOpenKey(msg.String())
304303
// Handles all warn models except the warn model for confirming to quit
305-
case m.warnModal.open:
306-
cmd = m.warnModalOpenKey(msg.String())
307-
case m.notifyModal.open:
308-
m.notifyModalOpenKey(msg.String())
304+
case m.notifyModel.IsOpen():
305+
cmd = m.notifyModelOpenKey(msg.String())
306+
309307
// If renaming a object
310308
case m.fileModel.renaming:
311309
cmd = m.renamingKey(msg.String())
@@ -322,9 +320,6 @@ func (m *model) handleKeyInput(msg tea.KeyMsg) tea.Cmd {
322320
// If help menu is open
323321
case m.helpMenu.open:
324322
m.helpMenuKey(msg.String())
325-
// If asking to confirm quiting
326-
case m.modelQuitState == confirmToQuit:
327-
quitSuperfile = m.confirmToQuitSuperfile(msg.String())
328323

329324
case slices.Contains(common.Hotkeys.Quit, msg.String()):
330325
m.modelQuitState = quitInitiated
@@ -338,12 +333,13 @@ func (m *model) handleKeyInput(msg tea.KeyMsg) tea.Cmd {
338333
if m.modelQuitState == quitInitiated {
339334
if m.processBarModel.HasRunningProcesses() {
340335
// Dont quit now, get a confirmation first.
336+
m.modelQuitState = quitConfirmationInitiated
341337
m.warnModalForQuit()
342338
return cmd
343339
}
344-
quitSuperfile = true
340+
m.modelQuitState = quitConfirmationReceived
345341
}
346-
if quitSuperfile {
342+
if m.modelQuitState == quitConfirmationReceived {
347343
m.quitSuperfile()
348344
return tea.Quit
349345
}
@@ -447,9 +443,9 @@ func (m *model) updateCurrentFilePanelDir(path string) error {
447443

448444
// Triggers a warn for confirm quiting
449445
func (m *model) warnModalForQuit() {
450-
m.modelQuitState = confirmToQuit
451-
m.warnModal.title = "Confirm to quit superfile"
452-
m.warnModal.content = "You still have files being processed. Are you sure you want to exit?"
446+
m.notifyModel = notify.New(true, "Confirm to quit superfile",
447+
"You still have files being processed. Are you sure you want to exit?",
448+
notify.QuitAction)
453449
}
454450

455451
// Implement View function for bubble tea model to handle visualization.
@@ -542,29 +538,13 @@ func (m *model) View() string {
542538
return stringfunction.PlaceOverlay(overlayX, overlayY, typingModal, finalRender)
543539
}
544540

545-
if m.warnModal.open {
546-
warnModal := m.warnModalRender()
547-
overlayX := m.fullWidth/2 - common.ModalWidth/2
548-
overlayY := m.fullHeight/2 - common.ModalHeight/2
549-
return stringfunction.PlaceOverlay(overlayX, overlayY, warnModal, finalRender)
550-
}
551-
552-
if m.notifyModal.open {
553-
notifyModal := m.notifyModalRender()
541+
if m.notifyModel.IsOpen() {
542+
notifyModal := m.notifyModel.Render()
554543
overlayX := m.fullWidth/2 - common.ModalWidth/2
555544
overlayY := m.fullHeight/2 - common.ModalHeight/2
556545
return stringfunction.PlaceOverlay(overlayX, overlayY, notifyModal, finalRender)
557546
}
558547

559-
// This is also a render for warnmodal, but its being driven via a different flag
560-
// we should also drive it via warnModal.open
561-
if m.modelQuitState == confirmToQuit {
562-
warnModal := m.warnModalRender()
563-
overlayX := m.fullWidth/2 - common.ModalWidth/2
564-
overlayY := m.fullHeight/2 - common.ModalHeight/2
565-
return stringfunction.PlaceOverlay(overlayX, overlayY, warnModal, finalRender)
566-
}
567-
568548
return finalRender
569549
}
570550

src/internal/model_msg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ type NotifyModalUpdateMsg struct {
146146
m notify.Model
147147
}
148148

149-
func NewNofifyModalMsg(m notify.Model, reqID int) NotifyModalUpdateMsg {
149+
func NewNotifyModalMsg(m notify.Model, reqID int) NotifyModalUpdateMsg {
150150
return NotifyModalUpdateMsg{
151151
m: m,
152152
BaseMessage: BaseMessage{
@@ -156,6 +156,6 @@ func NewNofifyModalMsg(m notify.Model, reqID int) NotifyModalUpdateMsg {
156156
}
157157

158158
func (msg NotifyModalUpdateMsg) ApplyToModel(m *model) tea.Cmd {
159-
m.nofigyModel = msg.m
159+
m.notifyModel = msg.m
160160
return nil
161161
}

src/internal/model_render.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -329,23 +329,6 @@ func (m *model) introduceModalRender() string {
329329
subTwo + "\n\n" + subThree + "\n\n" + subFour + "\n\n")
330330
}
331331

332-
func (m *model) warnModalRender() string {
333-
title := m.warnModal.title
334-
content := m.warnModal.content
335-
confirm := common.ModalConfirm.Render(" (" + common.Hotkeys.Confirm[0] + ") Confirm ")
336-
cancel := common.ModalCancel.Render(" (" + common.Hotkeys.Quit[0] + ") Cancel ")
337-
tip := confirm + lipgloss.NewStyle().Background(common.ModalBGColor).Render(" ") + cancel
338-
return common.ModalBorderStyle(common.ModalHeight, common.ModalWidth).Render(title + "\n\n" + content + "\n\n" + tip)
339-
}
340-
341-
func (m *model) notifyModalRender() string {
342-
title := m.notifyModal.title
343-
content := m.notifyModal.content
344-
okay := common.ModalConfirm.Render(" (" + common.Hotkeys.Confirm[0] + ") Okay ")
345-
okay = common.MainStyle.AlignHorizontal(lipgloss.Center).AlignVertical(lipgloss.Center).Render(okay)
346-
return common.ModalBorderStyle(common.ModalHeight, common.ModalWidth).Render(title + "\n\n" + content + "\n\n" + okay)
347-
}
348-
349332
func (m *model) promptModalRender() string {
350333
return m.promptModal.Render()
351334
}

src/internal/model_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func TestQuit(t *testing.T) {
119119

120120
assert.Equal(t, notQuitting, m.modelQuitState)
121121
cmd := TeaUpdateWithErrCheck(m, utils.TeaRuneKeyMsg(common.Hotkeys.Quit[0]))
122-
assert.Equal(t, confirmToQuit, m.modelQuitState)
122+
assert.Equal(t, quitConfirmationInitiated, m.modelQuitState)
123123
assert.False(t, IsTeaQuit(cmd))
124124

125125
// Now we would be asked for confirmation.
@@ -130,7 +130,7 @@ func TestQuit(t *testing.T) {
130130

131131
// Again trigger quit
132132
cmd = TeaUpdateWithErrCheck(m, utils.TeaRuneKeyMsg(common.Hotkeys.Quit[0]))
133-
assert.Equal(t, confirmToQuit, m.modelQuitState)
133+
assert.Equal(t, quitConfirmationInitiated, m.modelQuitState)
134134
assert.False(t, IsTeaQuit(cmd))
135135

136136
// Confirm this time

0 commit comments

Comments
 (0)