Skip to content

Commit eda3205

Browse files
refactor(filepanel): replace filePanelFocusType with isFocused boolean (#1040)
The filePanelFocusType typed constant with multiple states (noneFocus, secondFocus, focus) added unnecessary complexity for managing panel focus. It also made the code harder to read and maintain. Solution Replaced filePanelFocusType with a simple isFocused boolean field in filePanel. isFocused = true → panel is focused isFocused = false → panel is not focused This change removes unused constants and simplifies focus handling. Refs #1033 Refs #1030 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - Refactor - Simplified panel focus handling to a single, consistent indicator across navigation, rendering, and key interactions, improving reliability and consistency in focus behavior. - Style - Minor formatting cleanups for readability without changing behavior. - Chores - Consolidated internal variable declarations and adjusted logging to reflect the updated focus indicator. No user-facing features changed; interactions should feel more consistent and stable, especially when switching panels and using the sidebar or process bar. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: lazysegtree <59679977+lazysegtree@users.noreply.github.com>
1 parent fdce299 commit eda3205

File tree

8 files changed

+37
-64
lines changed

8 files changed

+37
-64
lines changed

src/internal/function.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@ func isExternalDiskPath(path string) bool {
5555
strings.HasPrefix(path, "/Volumes")
5656
}
5757

58-
func returnFocusType(focusPanel focusPanelType) filePanelFocusType {
59-
if focusPanel == nonePanelFocus {
60-
return focus
61-
}
62-
return secondFocus
58+
func returnFocusType(focusPanel focusPanelType) bool {
59+
return focusPanel == nonePanelFocus
6360
}
6461

6562
// TODO : Take common.Config.CaseSensitiveSort as a function parameter
@@ -86,7 +83,8 @@ func returnDirElement(location string, displayDotFile bool, sortOptions sortOpti
8683
}
8784

8885
func returnDirElementBySearchString(location string, displayDotFile bool, searchString string,
89-
sortOptions sortOptionsModelData) []element {
86+
sortOptions sortOptionsModelData,
87+
) []element {
9088
items, err := os.ReadDir(location)
9189
if err != nil {
9290
slog.Error("Error while return folder element function", "error", err)
@@ -355,7 +353,8 @@ func processCmdToTeaCmd(cmd processbar.Cmd) tea.Cmd {
355353
}
356354
return func() tea.Msg {
357355
updateMsg := cmd()
358-
return ProcessBarUpdateMsg{pMsg: updateMsg,
356+
return ProcessBarUpdateMsg{
357+
pMsg: updateMsg,
359358
BaseMessage: BaseMessage{
360359
reqID: updateMsg.GetReqID(),
361360
},

src/internal/handle_panel_movement.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (m *model) sidebarSelectDirectory() {
118118
if err != nil {
119119
slog.Error("Error switching to sidebar directory", "error", err)
120120
}
121-
panel.focusType = focus
121+
panel.isFocused = true
122122
}
123123

124124
// Select all item in the file panel (only work on select mode)

src/internal/handle_panel_navigation.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ func (m *model) createNewFilePanel(location string) error {
4141
}
4242

4343
m.fileModel.filePanels = append(m.fileModel.filePanels, filePanel{
44-
4544
location: location,
4645
sortOptions: m.fileModel.filePanels[m.filePanelFocusIndex].sortOptions,
4746
panelMode: browserMode,
48-
focusType: secondFocus,
47+
isFocused: false,
4948
directoryRecords: make(map[string]directoryRecord),
5049
searchBar: common.GenerateSearchBar(),
5150
})
@@ -60,8 +59,8 @@ func (m *model) createNewFilePanel(location string) error {
6059
}
6160
}
6261

63-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = noneFocus
64-
m.fileModel.filePanels[m.filePanelFocusIndex+1].focusType = returnFocusType(m.focusPanel)
62+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = false
63+
m.fileModel.filePanels[m.filePanelFocusIndex+1].isFocused = returnFocusType(m.focusPanel)
6564
m.fileModel.width = (m.fullWidth - common.Config.SidebarWidth - m.fileModel.filePreview.GetWidth() -
6665
(4 + (len(m.fileModel.filePanels)-1)*2)) / len(m.fileModel.filePanels)
6766
m.filePanelFocusIndex++
@@ -99,7 +98,7 @@ func (m *model) closeFilePanel() {
9998

10099
m.fileModel.width = (m.fullWidth - common.Config.SidebarWidth - m.fileModel.filePreview.GetWidth() -
101100
(4 + (len(m.fileModel.filePanels)-1)*2)) / len(m.fileModel.filePanels)
102-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = returnFocusType(m.focusPanel)
101+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = returnFocusType(m.focusPanel)
103102

104103
m.fileModel.maxFilePanel = (m.fullWidth - common.Config.SidebarWidth - m.fileModel.filePreview.GetWidth()) / 20
105104

@@ -134,26 +133,26 @@ func (m *model) toggleFilePreviewPanel() {
134133

135134
// Focus on next file panel
136135
func (m *model) nextFilePanel() {
137-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = noneFocus
136+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = false
138137
if m.filePanelFocusIndex == (len(m.fileModel.filePanels) - 1) {
139138
m.filePanelFocusIndex = 0
140139
} else {
141140
m.filePanelFocusIndex++
142141
}
143142

144-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = returnFocusType(m.focusPanel)
143+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = returnFocusType(m.focusPanel)
145144
}
146145

147146
// Focus on previous file panel
148147
func (m *model) previousFilePanel() {
149-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = noneFocus
148+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = false
150149
if m.filePanelFocusIndex == 0 {
151150
m.filePanelFocusIndex = (len(m.fileModel.filePanels) - 1)
152151
} else {
153152
m.filePanelFocusIndex--
154153
}
155154

156-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = returnFocusType(m.focusPanel)
155+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = returnFocusType(m.focusPanel)
157156
}
158157

159158
// Focus on sidebar
@@ -163,10 +162,10 @@ func (m *model) focusOnSideBar() {
163162
}
164163
if m.focusPanel == sidebarFocus {
165164
m.focusPanel = nonePanelFocus
166-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = focus
165+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = true
167166
} else {
168167
m.focusPanel = sidebarFocus
169-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = secondFocus
168+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = false
170169
}
171170
}
172171

@@ -178,10 +177,10 @@ func (m *model) focusOnProcessBar() {
178177

179178
if m.focusPanel == processBarFocus {
180179
m.focusPanel = nonePanelFocus
181-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = focus
180+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = true
182181
} else {
183182
m.focusPanel = processBarFocus
184-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = secondFocus
183+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = false
185184
}
186185
}
187186

@@ -193,9 +192,9 @@ func (m *model) focusOnMetadata() {
193192

194193
if m.focusPanel == metadataFocus {
195194
m.focusPanel = nonePanelFocus
196-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = focus
195+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = true
197196
} else {
198197
m.focusPanel = metadataFocus
199-
m.fileModel.filePanels[m.filePanelFocusIndex].focusType = secondFocus
198+
m.fileModel.filePanels[m.filePanelFocusIndex].isFocused = false
200199
}
201200
}

src/internal/key_function.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (m *model) mainKey(msg string) tea.Cmd { //nolint: gocyclo,cyclop,funlen //
131131

132132
func (m *model) normalAndBrowserModeKey(msg string) tea.Cmd {
133133
// if not focus on the filepanel return
134-
if m.getFocusedFilePanel().focusType != focus {
134+
if !m.getFocusedFilePanel().isFocused {
135135
if m.focusPanel == sidebarFocus && slices.Contains(common.Hotkeys.Confirm, msg) {
136136
m.sidebarSelectDirectory()
137137
}

src/internal/model.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727
)
2828

2929
// These represent model's state information, its not a global preperty
30-
var LastTimeCursorMove = [2]int{int(time.Now().UnixMicro()), 0} //nolint: gochecknoglobals // TODO: Move to model struct
31-
var et *exiftool.Exiftool //nolint: gochecknoglobals // TODO: Move to model struct
30+
var (
31+
LastTimeCursorMove = [2]int{int(time.Now().UnixMicro()), 0} //nolint: gochecknoglobals // TODO: Move to model struct
32+
et *exiftool.Exiftool //nolint: gochecknoglobals // TODO: Move to model struct
33+
)
3234

3335
// Initialize and return model with default configs
3436
// It returns only tea.Model because when it used in main, the return value
@@ -325,7 +327,7 @@ func (m *model) handleKeyInput(msg tea.KeyMsg) tea.Cmd {
325327
"alt", msg.Alt)
326328
slog.Debug("model.handleKeyInput. model info. ",
327329
"filePanelFocusIndex", m.filePanelFocusIndex,
328-
"filePanel.focusType", m.fileModel.filePanels[m.filePanelFocusIndex].focusType,
330+
"filePanel.isFocused", m.fileModel.filePanels[m.filePanelFocusIndex].isFocused,
329331
"filePanel.panelMode", m.fileModel.filePanels[m.filePanelFocusIndex].panelMode,
330332
"typingModal.open", m.typingModal.open,
331333
"notifyModel.open", m.notifyModel.IsOpen(),
@@ -689,7 +691,7 @@ func (m *model) getFilePanelItems() {
689691
var fileElement []element
690692
nowTime := time.Now()
691693
// Check last time each element was updated, if less then 3 seconds ignore
692-
if filePanel.focusType == noneFocus && nowTime.Sub(filePanel.lastTimeGetElement) < 3*time.Second {
694+
if !filePanel.isFocused && nowTime.Sub(filePanel.lastTimeGetElement) < 3*time.Second {
693695
// TODO : revisit this. This feels like a duct tape solution of an actual
694696
// deep rooted problem. This feels very hacky.
695697
if !m.updatedToggleDotFile {
@@ -709,7 +711,7 @@ func (m *model) getFilePanelItems() {
709711

710712
reRenderTime := int(float64(len(filePanel.element)) / 100)
711713

712-
if filePanel.focusType != noneFocus && !focusPanelReRender &&
714+
if filePanel.isFocused && !focusPanelReRender &&
713715
nowTime.Sub(filePanel.lastTimeGetElement) < time.Duration(reRenderTime)*time.Second {
714716
continue
715717
}
@@ -744,7 +746,7 @@ func (m *model) quitSuperfile(cdOnQuit bool) {
744746
if cdOnQuit {
745747
// escape single quote
746748
currentDir = strings.ReplaceAll(currentDir, "'", "'\\''")
747-
err := os.WriteFile(variable.LastDirFile, []byte("cd '"+currentDir+"'"), 0755)
749+
err := os.WriteFile(variable.LastDirFile, []byte("cd '"+currentDir+"'"), 0o755)
748750
if err != nil {
749751
slog.Error("Error during writing lastdir file", "error", err)
750752
}

src/internal/model_render.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (m *model) filePanelRender() string {
5555
filePanelWidth = m.fileModel.width
5656
}
5757

58-
f[i] = filePanel.Render(m.mainPanelHeight, filePanelWidth, filePanel.focusType != noneFocus)
58+
f[i] = filePanel.Render(m.mainPanelHeight, filePanelWidth, filePanel.isFocused)
5959
}
6060
return lipgloss.JoinHorizontal(lipgloss.Top, f...)
6161
}

src/internal/type.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import (
2020
// Type representing the mode of the panel
2121
type panelMode uint
2222

23-
// Type representing the focus type of the file panel
24-
type filePanelFocusType uint
25-
2623
// Type representing the type of focused panel
2724
type focusPanelType int
2825

@@ -47,13 +44,6 @@ const (
4744
metadataFocus
4845
)
4946

50-
// Constants for file panel with no focus
51-
const (
52-
noneFocus filePanelFocusType = iota
53-
secondFocus
54-
focus
55-
)
56-
5747
// Constants for select mode or browser mode
5848
const (
5949
selectMode panelMode = iota
@@ -165,7 +155,7 @@ type fileModel struct {
165155
type filePanel struct {
166156
cursor int
167157
render int
168-
focusType filePanelFocusType
158+
isFocused bool
169159
location string
170160
sortOptions sortOptionsModel
171161
panelMode panelMode

src/internal/type_utils.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,14 @@ func (m *model) validateLayout() error {
3939
func filePanelSlice(dir []string) []filePanel {
4040
res := make([]filePanel, len(dir))
4141
for i := range dir {
42-
// Making the first panel as the default focus panel
43-
// while others remain secondFocus
44-
focusType := secondFocus
45-
if i == 0 {
46-
focusType = focus
47-
}
48-
res[i] = defaultFilePanel(dir[i], focusType)
42+
// Making the first panel as the focussed
43+
isFocus := i == 0
44+
res[i] = defaultFilePanel(dir[i], isFocus)
4945
}
5046
return res
5147
}
5248

53-
func defaultFilePanel(dir string, currentFocusType filePanelFocusType) filePanel {
49+
func defaultFilePanel(dir string, focused bool) filePanel {
5450
return filePanel{
5551
render: 0,
5652
cursor: 0,
@@ -70,7 +66,7 @@ func defaultFilePanel(dir string, currentFocusType filePanelFocusType) filePanel
7066
},
7167
},
7268
panelMode: browserMode,
73-
focusType: currentFocusType,
69+
isFocused: focused,
7470
directoryRecords: make(map[string]directoryRecord),
7571
searchBar: common.GenerateSearchBar(),
7672
}
@@ -93,19 +89,6 @@ func (f focusPanelType) String() string {
9389
}
9490
}
9591

96-
func (f filePanelFocusType) String() string {
97-
switch f {
98-
case noneFocus:
99-
return "noneFocus"
100-
case secondFocus:
101-
return "secondFocus"
102-
case focus:
103-
return "focus"
104-
default:
105-
return invalidTypeString
106-
}
107-
}
108-
10992
func (p panelMode) String() string {
11093
switch p {
11194
case selectMode:

0 commit comments

Comments
 (0)