Skip to content

Commit 0833aff

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

File tree

8 files changed

+35
-132
lines changed

8 files changed

+35
-132
lines changed

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: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -193,73 +193,38 @@ 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-
}
216-
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
222-
}
223-
}
224-
225196
func (m *model) notifyModelOpenKey(msg string) tea.Cmd {
226197
switch {
227198
case slices.Contains(common.Hotkeys.CancelTyping, msg) || slices.Contains(common.Hotkeys.Quit, msg):
228-
m.nofigyModel.Close()
229-
if m.nofigyModel.GetConfirmAction() == notify.RenameAction {
199+
m.notifyModel.Close()
200+
switch m.notifyModel.GetConfirmAction() {
201+
case notify.RenameAction:
230202
m.cancelRename()
203+
case notify.QuitAction:
204+
m.modelQuitState = notQuitting
205+
case notify.DeleteAction, notify.NoAction:
206+
// Do nothing
207+
default:
208+
slog.Error("Unknown type of action", "action", m.notifyModel.GetConfirmAction())
231209
}
232210
case slices.Contains(common.Hotkeys.Confirm, msg):
233-
m.nofigyModel.Close()
234-
switch m.nofigyModel.GetConfirmAction() {
211+
m.notifyModel.Close()
212+
switch m.notifyModel.GetConfirmAction() {
235213
case notify.DeleteAction:
236214
return m.getDeleteCmd()
237215
case notify.RenameAction:
238216
m.confirmRename()
239-
case notify.NoAction:
240-
// Do nothing
217+
case notify.NoAction, notify.QuitAction:
218+
m.modelQuitState = quitConfirmationReceived
241219
default:
242-
slog.Error("Unknown type of action", "action", m.nofigyModel.GetConfirmAction())
220+
slog.Error("Unknown type of action", "action", m.notifyModel.GetConfirmAction())
243221
}
244222
default:
245223
slog.Warn("Invalid keypress in notifyModel", "msg", msg)
246224
}
247225
return nil
248226
}
249227

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
258-
default:
259-
return false
260-
}
261-
}
262-
263228
// Handles key inputs inside sort options menu
264229
func (m *model) sortOptionsKey(msg string) {
265230
switch {

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

src/internal/type.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ type filePanelFocusType uint
2222
// Type representing the type of focused panel
2323
type focusPanelType int
2424

25-
type warnType int
26-
2725
type hotkeyType int
2826

2927
type modelQuitStateType int
@@ -34,11 +32,6 @@ const (
3432
selectType
3533
)
3634

37-
const (
38-
confirmDeleteItem warnType = iota
39-
confirmRenameItem
40-
)
41-
4235
// Constants for panel with no focus
4336
const (
4437
nonePanelFocus focusPanelType = iota
@@ -63,7 +56,8 @@ const (
6356
const (
6457
notQuitting modelQuitStateType = iota
6558
quitInitiated
66-
confirmToQuit
59+
quitConfirmationInitiated
60+
quitConfirmationReceived
6761
quitDone
6862
)
6963

@@ -81,10 +75,8 @@ type model struct {
8175
copyItems copyItems
8276

8377
// Modals
84-
nofigyModel notify.Model
85-
notifyModal notifyModal
78+
notifyModel notify.Model
8679
typingModal typingModal
87-
warnModal warnModal
8880
helpMenu helpMenuModal
8981
promptModal prompt.Model
9082

@@ -128,26 +120,13 @@ type helpMenuModalData struct {
128120
subTitle string
129121
}
130122

131-
type warnModal struct {
132-
open bool
133-
warnType warnType
134-
title string
135-
content string
136-
}
137-
138123
type typingModal struct {
139124
location string
140125
open bool
141126
textInput textinput.Model
142127
errorMesssage string
143128
}
144129

145-
type notifyModal struct {
146-
open bool
147-
title string
148-
content string
149-
}
150-
151130
// Copied items
152131
type copyItems struct {
153132
items []string

src/internal/ui/notify/type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ type ConfirmActionType int
55
const (
66
RenameAction ConfirmActionType = iota
77
DeleteAction
8+
QuitAction
89
NoAction
910
)

0 commit comments

Comments
 (0)