diff --git a/changelog.md b/changelog.md index 963d32ce..e55c1d85 100644 --- a/changelog.md +++ b/changelog.md @@ -2,7 +2,16 @@ All notable changes to this project will be documented in this file. Dates are displayed in UTC(YYYY/MM/DD). -# [**v1.1.0**]() +# [**v1.1.1**]() + +> xxxx/xx/xx + +#### Update + +#### Bug fix + + +# [**v1.1.0**](https://github.com/MHNightCat/superfile/releases/tag/v1.1.0) > 2024/04/20 diff --git a/src/components/key_function.go b/src/components/key_function.go new file mode 100644 index 00000000..2562a9a7 --- /dev/null +++ b/src/components/key_function.go @@ -0,0 +1,196 @@ +package components + +func mainKey(msg string, m model) model { + switch msg { + /* LIST CONTROLLER START */ + // up list + case hotkeys.ListUp[0], hotkeys.ListUp[1]: + if m.focusPanel == sidebarFocus { + m = controllerSideBarListUp(m) + } else if m.focusPanel == processBarFocus { + m = contollerProcessBarListUp(m) + } else if m.focusPanel == metadataFocus { + m = controllerMetaDataListUp(m) + } else if m.focusPanel == nonePanelFocus { + m = controllerFilePanelListUp(m) + m.fileMetaData.renderIndex = 0 + go func() { + m = returnMetaData(m) + }() + } + // down list + case hotkeys.ListDown[0], hotkeys.ListDown[1]: + if m.focusPanel == sidebarFocus { + m = controllerSideBarListDown(m) + } else if m.focusPanel == processBarFocus { + m = contollerProcessBarListDown(m) + } else if m.focusPanel == metadataFocus { + m = controllerMetaDataListDown(m) + } else if m.focusPanel == nonePanelFocus { + m = controllerFilePanelListDown(m) + m.fileMetaData.renderIndex = 0 + go func() { + m = returnMetaData(m) + }() + } + /* LIST CONTROLLER END */ + case hotkeys.ChangePanelMode[0], hotkeys.ChangePanelMode[1]: + m = selectedMode(m) + /* NAVIGATION CONTROLLER START */ + // change file panel + case hotkeys.NextFilePanel[0], hotkeys.NextFilePanel[1]: + m = nextFilePanel(m) + // change file panel + case hotkeys.PreviousFilePanel[0], hotkeys.PreviousFilePanel[1]: + m = previousFilePanel(m) + // close file panel + case hotkeys.CloseFilePanel[0], hotkeys.CloseFilePanel[1]: + m = closeFilePanel(m) + // create new file panel + case hotkeys.CreateNewFilePanel[0], hotkeys.CreateNewFilePanel[1]: + m = createNewFilePanel(m) + // focus to sidebar or file panel + case hotkeys.FocusOnSideBar[0], hotkeys.FocusOnSideBar[1]: + m = focusOnSideBar(m) + /* NAVIGATION CONTROLLER END */ + case hotkeys.FocusOnProcessBar[0], hotkeys.FocusOnProcessBar[1]: + m = focusOnProcessBar(m) + case hotkeys.FocusOnMetaData[0], hotkeys.FocusOnMetaData[1]: + m = focusOnMetaData(m) + go func() { + m = returnMetaData(m) + }() + case hotkeys.PasteItem[0], hotkeys.PasteItem[1]: + go func() { + m = pasteItem(m) + }() + case hotkeys.FilePanelFileCreate[0], hotkeys.FilePanelFileCreate[1]: + m = panelCreateNewFile(m) + case hotkeys.FilePanelDirectoryCreate[0], hotkeys.FilePanelDirectoryCreate[1]: + m = panelCreateNewFolder(m) + case hotkeys.PinnedDirectory[0], hotkeys.PinnedDirectory[1]: + m = pinnedFolder(m) + case hotkeys.ToggleDotFile[0], hotkeys.ToggleDotFile[1]: + m = toggleDotFileController(m) + case hotkeys.ExtractFile[0], hotkeys.ExtractFile[1]: + go func() { + m = extractFile(m) + }() + case hotkeys.CompressFile[0], hotkeys.CompressFile[1]: + + go func() { + m = compressFile(m) + }() + default: + m = normalAndBrowserModeKey(msg, m) + } + return m +} + +func normalAndBrowserModeKey(msg string, m model) model { + // if not focus on the filepanel return + if m.fileModel.filePanels[m.filePanelFocusIndex].focusType != focus { + if m.focusPanel == sidebarFocus && (msg == hotkeys.SelectItem[0] || msg == hotkeys.SelectItem[1]) { + m = sidebarSelectFolder(m) + } + return m + } + // Check if in the select mode and focusOn filepanel + if m.fileModel.filePanels[m.filePanelFocusIndex].panelMode == selectMode { + switch msg { + case hotkeys.FilePanelSelectModeItemSingleSelect[0], hotkeys.FilePanelSelectModeItemSingleSelect[1]: + m = singleItemSelect(m) + case hotkeys.FilePanelSelectModeItemSelectUp[0], hotkeys.FilePanelSelectModeItemSelectUp[1]: + m = itemSelectUp(m) + case hotkeys.FilePanelSelectModeItemSelectDown[0], hotkeys.FilePanelSelectModeItemSelectDown[1]: + m = itemSelectDown(m) + case hotkeys.FilePanelSelectModeItemDelete[0], hotkeys.FilePanelSelectModeItemDelete[1]: + go func() { + m = deleteMultipleItem(m) + if !isExternalDiskPath(m.fileModel.filePanels[m.filePanelFocusIndex].location) { + m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0] + } + }() + case hotkeys.FilePanelSelectModeItemCopy[0], hotkeys.FilePanelSelectModeItemCopy[1]: + m = copyMultipleItem(m) + case hotkeys.FilePanelSelectModeItemCut[0], hotkeys.FilePanelSelectModeItemCut[1]: + m = cutMultipleItem(m) + case hotkeys.FilePanelSelectAllItem[0], hotkeys.FilePanelSelectAllItem[1]: + m = selectAllItem(m) + } + // else + } else { + switch msg { + case hotkeys.SelectItem[0], hotkeys.SelectItem[1]: + forceReloadElement = true + m = enterPanel(m) + case hotkeys.ParentDirectory[0], hotkeys.ParentDirectory[1]: + forceReloadElement = true + m = parentFolder(m) + case hotkeys.DeleteItem[0], hotkeys.DeleteItem[1]: + go func() { + m = deleteSingleItem(m) + }() + case hotkeys.CopySingleItem[0], hotkeys.CopySingleItem[1]: + m = copySingleItem(m) + case hotkeys.CutSingleItem[0], hotkeys.CutSingleItem[1]: + m = cutSingleItem(m) + case hotkeys.FilePanelItemRename[0], hotkeys.FilePanelItemRename[1]: + m = panelItemRename(m) + case hotkeys.SearchBar[0], hotkeys.SearchBar[1]: + m = searchBarFocus(m) + } + + } + return m +} + +func typingModalOpenKey(msg string, m model) model { + switch msg { + case hotkeys.Cancel[0], hotkeys.Cancel[1]: + m = cancelTypingModal(m) + case hotkeys.Confirm[0], hotkeys.Confirm[1]: + m = createItem(m) + } + return m +} + +func warnModalOpenKey(msg string, m model) model { + switch msg { + case hotkeys.Cancel[0], hotkeys.Cancel[1]: + m = cancelWarnModal(m) + case hotkeys.Confirm[0], hotkeys.Confirm[1]: + m.warnModal.open = false + if m.fileModel.filePanels[m.filePanelFocusIndex].panelMode == selectMode { + go func() { + m = completelyDeleteMultipleFile(m) + m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0] + }() + } else { + go func() { + m = completelyDeleteSingleFile(m) + }() + } + } + return m +} + +func renamingKey(msg string, m model) model { + switch msg { + case hotkeys.Cancel[0], hotkeys.Cancel[1]: + m = cancelReanem(m) + case hotkeys.Confirm[0], hotkeys.Confirm[1]: + m = confirmRename(m) + } + return m +} + +func focusOnSearchbarKey(msg string, m model) model { + switch msg { + case hotkeys.Cancel[0], hotkeys.Cancel[1]: + m = cancelSearch(m) + case hotkeys.Confirm[0], hotkeys.Confirm[1], hotkeys.SearchBar[0], hotkeys.SearchBar[1]: + m = confirmSearch(m) + } + return m +} diff --git a/src/components/model.go b/src/components/model.go index 250cc170..52e3d35f 100644 --- a/src/components/model.go +++ b/src/components/model.go @@ -27,6 +27,8 @@ const ( var LastTimeCursorMove = [2]int{int(time.Now().UnixMicro()), 0} var ListeningMessage = true +var forceReloadElement = false + var HomeDir = basedir.Home var SuperFileMainDir = basedir.ConfigHome + "/superfile" var SuperFileCacheDir = basedir.CacheHome + "/superfile" @@ -41,8 +43,6 @@ var et *exiftool.Exiftool var channel = make(chan channelMessage, 1000) -var foreceReloadElement = false - func InitialModel(dir string) model { toggleDotFileBool, firstFilePanelDir := loadConfigFile(dir) @@ -95,7 +95,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd panel := m.fileModel.filePanels[m.filePanelFocusIndex] switch msg := msg.(type) { - // check is the message by thread case channelMessage: if msg.returnWarnModal { m.warnModal = msg.warnModal @@ -107,7 +106,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } m.processBarModel.process[msg.messageId] = msg.processNewState } - // if the message by windows size change + forceReloadElement = true case tea.WindowSizeMsg: if msg.Height < 30 { footerHeight = 10 @@ -123,190 +122,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.fileModel.maxFilePanel = 10 } return m, nil - // if just user press key case tea.KeyMsg: - // if in the create item modal if m.typingModal.open { - switch msg.String() { - case hotkeys.Cancel[0], hotkeys.Cancel[1]: - m = cancelTypingModal(m) - case hotkeys.Confirm[0], hotkeys.Confirm[1]: - m = createItem(m) - } - // if in the renaming mode + m = typingModalOpenKey(msg.String(), m) } else if m.warnModal.open { - switch msg.String() { - case hotkeys.Cancel[0], hotkeys.Cancel[1]: - m = cancelWarnModal(m) - case hotkeys.Confirm[0], hotkeys.Confirm[1]: - m.warnModal.open = false - if m.fileModel.filePanels[m.filePanelFocusIndex].panelMode == selectMode { - go func() { - m = completelyDeleteMultipleFile(m) - m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0] - }() - } else { - go func() { - m = completelyDeleteSingleFile(m) - }() - } - } - // if in the renaming mode + m = warnModalOpenKey(msg.String(), m) } else if m.fileModel.renaming { - switch msg.String() { - case hotkeys.Cancel[0], hotkeys.Cancel[1]: - m = cancelReanem(m) - case hotkeys.Confirm[0], hotkeys.Confirm[1]: - m = confirmRename(m) - } - // if search bar focus + m = renamingKey(msg.String(), m) } else if panel.searchBar.Focused() { - switch msg.String() { - case hotkeys.Cancel[0], hotkeys.Cancel[1]: - m = cancelSearch(m) - case hotkeys.Confirm[0], hotkeys.Confirm[1], hotkeys.SearchBar[0], hotkeys.SearchBar[1]: - m = confirmSearch(m) - } + m = focusOnSearchbarKey(msg.String(), m) } else { - switch msg.String() { // return superfile - case hotkeys.Quit[0], hotkeys.Quit[1]: + if msg.String() == hotkeys.Quit[0] || msg.String() == hotkeys.Quit[1] { return m, tea.Quit - /* LIST CONTROLLER START */ - // up list - case hotkeys.ListUp[0], hotkeys.ListUp[1]: - if m.focusPanel == sidebarFocus { - m = controllerSideBarListUp(m) - } else if m.focusPanel == processBarFocus { - m = contollerProcessBarListUp(m) - } else if m.focusPanel == metadataFocus { - m = controllerMetaDataListUp(m) - } else if m.focusPanel == nonePanelFocus { - m = controllerFilePanelListUp(m) - m.fileMetaData.renderIndex = 0 - go func() { - m = returnMetaData(m) - }() - } - // down list - case hotkeys.ListDown[0], hotkeys.ListDown[1]: - if m.focusPanel == sidebarFocus { - m = controllerSideBarListDown(m) - } else if m.focusPanel == processBarFocus { - m = contollerProcessBarListDown(m) - } else if m.focusPanel == metadataFocus { - m = controllerMetaDataListDown(m) - } else if m.focusPanel == nonePanelFocus { - m = controllerFilePanelListDown(m) - m.fileMetaData.renderIndex = 0 - go func() { - m = returnMetaData(m) - }() - } - /* LIST CONTROLLER END */ - case hotkeys.ChangePanelMode[0], hotkeys.ChangePanelMode[1]: - m = selectedMode(m) - /* NAVIGATION CONTROLLER START */ - // change file panel - case hotkeys.NextFilePanel[0], hotkeys.NextFilePanel[1]: - m = nextFilePanel(m) - // change file panel - case hotkeys.PreviousFilePanel[0], hotkeys.PreviousFilePanel[1]: - m = previousFilePanel(m) - // close file panel - case hotkeys.CloseFilePanel[0], hotkeys.CloseFilePanel[1]: - m = closeFilePanel(m) - // create new file panel - case hotkeys.CreateNewFilePanel[0], hotkeys.CreateNewFilePanel[1]: - m = createNewFilePanel(m) - // focus to sidebar or file panel - case hotkeys.FocusOnSideBar[0], hotkeys.FocusOnSideBar[1]: - m = focusOnSideBar(m) - /* NAVIGATION CONTROLLER END */ - case hotkeys.FocusOnProcessBar[0], hotkeys.FocusOnProcessBar[1]: - m = focusOnProcessBar(m) - case hotkeys.FocusOnMetaData[0], hotkeys.FocusOnMetaData[1]: - m = focusOnMetaData(m) - go func() { - m = returnMetaData(m) - }() - case hotkeys.PasteItem[0], hotkeys.PasteItem[1]: - go func() { - m = pasteItem(m) - }() - case hotkeys.FilePanelFileCreate[0], hotkeys.FilePanelFileCreate[1]: - m = panelCreateNewFile(m) - case hotkeys.FilePanelDirectoryCreate[0], hotkeys.FilePanelDirectoryCreate[1]: - m = panelCreateNewFolder(m) - case hotkeys.PinnedDirectory[0], hotkeys.PinnedDirectory[1]: - m = pinnedFolder(m) - case hotkeys.ToggleDotFile[0], hotkeys.ToggleDotFile[1]: - m = toggleDotFileController(m) - case hotkeys.ExtractFile[0], hotkeys.ExtractFile[1]: - go func() { - m = extractFile(m) - }() - case hotkeys.CompressFile[0], hotkeys.CompressFile[1]: - go func() { - m = compressFile(m) - }() - default: - // check if it's the select mode - if m.fileModel.filePanels[m.filePanelFocusIndex].focusType == focus && m.fileModel.filePanels[m.filePanelFocusIndex].panelMode == selectMode { - switch msg.String() { - case hotkeys.FilePanelSelectModeItemSingleSelect[0], hotkeys.FilePanelSelectModeItemSingleSelect[1]: - m = singleItemSelect(m) - case hotkeys.FilePanelSelectModeItemSelectUp[0], hotkeys.FilePanelSelectModeItemSelectUp[1]: - m = itemSelectUp(m) - case hotkeys.FilePanelSelectModeItemSelectDown[0], hotkeys.FilePanelSelectModeItemSelectDown[1]: - m = itemSelectDown(m) - case hotkeys.FilePanelSelectModeItemDelete[0], hotkeys.FilePanelSelectModeItemDelete[1]: - go func() { - m = deleteMultipleItem(m) - if !isExternalDiskPath(m.fileModel.filePanels[m.filePanelFocusIndex].location) { - m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0] - } - }() - case hotkeys.FilePanelSelectModeItemCopy[0], hotkeys.FilePanelSelectModeItemCopy[1]: - m = copyMultipleItem(m) - case hotkeys.FilePanelSelectModeItemCut[0], hotkeys.FilePanelSelectModeItemCut[1]: - m = cutMultipleItem(m) - case hotkeys.FilePanelSelectAllItem[0], hotkeys.FilePanelSelectAllItem[1]: - m = selectAllItem(m) - } - // else - } else { - switch msg.String() { - case hotkeys.SelectItem[0], hotkeys.SelectItem[1]: - if m.focusPanel == sidebarFocus { - m = sidebarSelectFolder(m) - } else if m.focusPanel == processBarFocus { - - } else if m.focusPanel == nonePanelFocus { - foreceReloadElement = true - m = enterPanel(m) - } - case hotkeys.ParentDirectory[0], hotkeys.ParentDirectory[1]: - foreceReloadElement = true - m = parentFolder(m) - case hotkeys.DeleteItem[0], hotkeys.DeleteItem[1]: - go func() { - m = deleteSingleItem(m) - }() - case hotkeys.CopySingleItem[0], hotkeys.CopySingleItem[1]: - m = copySingleItem(m) - case hotkeys.CutSingleItem[0], hotkeys.CutSingleItem[1]: - m = cutSingleItem(m) - case hotkeys.FilePanelItemRename[0], hotkeys.FilePanelItemRename[1]: - m = panelItemRename(m) - case hotkeys.SearchBar[0], hotkeys.SearchBar[1]: - m = searchBarFocus(m) - } - - } } + m = mainKey(msg.String(), m) } } + if m.firstTextInput { m.firstTextInput = false } else if m.fileModel.renaming { @@ -330,19 +163,22 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { for i, filePanel := range m.fileModel.filePanels { var fileElenent []element nowTime := time.Now() - if filePanel.focusType != noneFocus || nowTime.Sub(filePanel.lastTimeGetElement) > 3*time.Second || foreceReloadElement { - if len(filePanel.element) < 500 || (len(filePanel.element) > 500 && (nowTime.Sub(filePanel.lastTimeGetElement) > 3*time.Second)) || foreceReloadElement { - if filePanel.searchBar.Value() != "" { - fileElenent = returnFolderElementBySearchString(filePanel.location, m.toggleDotFile, filePanel.searchBar.Value()) - } else { - fileElenent = returnFolderElement(filePanel.location, m.toggleDotFile) - } - filePanel.element = fileElenent - m.fileModel.filePanels[i].element = fileElenent - m.fileModel.filePanels[i].lastTimeGetElement = nowTime - } - foreceReloadElement = false + if filePanel.focusType == noneFocus && nowTime.Sub(filePanel.lastTimeGetElement) < 3*time.Second && !forceReloadElement { + continue + } + if len(filePanel.element) > 500 && (len(filePanel.element) > 500 && (nowTime.Sub(filePanel.lastTimeGetElement) > 3*time.Second)) && !forceReloadElement { + continue + } + + if filePanel.searchBar.Value() != "" { + fileElenent = returnFolderElementBySearchString(filePanel.location, m.toggleDotFile, filePanel.searchBar.Value()) + } else { + fileElenent = returnFolderElement(filePanel.location, m.toggleDotFile) } + filePanel.element = fileElenent + m.fileModel.filePanels[i].element = fileElenent + m.fileModel.filePanels[i].lastTimeGetElement = nowTime + forceReloadElement = false } return m, cmd }