From 6f44617d0a378d980de71e6a5e1d634bff95bbcb Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sat, 28 Sep 2024 20:32:18 -0500 Subject: Flesh out dialog. --- src/bookmouse.cpp | 83 +++++++---------------------------------------------- src/bookmouse.hpp | 1 - src/file_dialog.cpp | 51 +++++++++++++++++++++++--------- src/file_dialog.hpp | 9 ++++-- 4 files changed, 54 insertions(+), 90 deletions(-) diff --git a/src/bookmouse.cpp b/src/bookmouse.cpp index 2e13b4e..d367af1 100644 --- a/src/bookmouse.cpp +++ b/src/bookmouse.cpp @@ -107,9 +107,9 @@ void Bookmouse::updateState() // m_imgui.getIO().AddKeyEvent() - if (ImGui::IsKeyPressed(ImGuiKey_O) && m_imgui.getIO().KeyCtrl && !m_openDialog) { - m_openDialog = true; - } + // if (ImGui::IsKeyPressed(ImGuiKey_O) && m_imgui.getIO().KeyCtrl && !m_openDialog) { + // m_openDialog = true; + // } if (ImMenuBar menuBar{}) { IM_ASSERT(menuBar); @@ -176,83 +176,20 @@ void Bookmouse::openDialog() ImGui::CloseCurrentPopup(); } } -} - -void Bookmouse::openDialog2() -{ - if (m_getContents) { - m_getContents = false; - - m_directoryName = fud::String{"./"}; - m_directory = fud::Directory{m_directoryName}; - - auto directoryStatsResult = m_directory.info(); - if (directoryStatsResult.isError()) { - m_openDialog = false; - ImGui::CloseCurrentPopup(); - } - auto directoryStats = directoryStatsResult.getOkay(); - size_t count{0}; - m_directoryContents.reserve(directoryStats.links); + auto result = m_fileDialog->pickFiles(); - while (true && count < SIZE_MAX) { - auto dirEntryResult = m_directory.getNextEntry(); - if (dirEntryResult.isError()) { - break; - } - - auto dirEntryOpt = dirEntryResult.getOkay(); - if (dirEntryOpt == std::nullopt) { - break; - } - - if (dirEntryOpt->inode == directoryStats.inode) { - continue; - } - - m_directoryContents.emplace_back(std::move(*dirEntryOpt)); - auto status = m_directoryContents.back().formatTime(m_timeFormat); - if (status != FudStatus::Success) { - spdlog::error("Could not convert time: {}", fud::FudStatusToString(status)); - } + if (result != std::nullopt) { + auto selectedFiles = *result; + for (const auto& name : selectedFiles) { + spdlog::info("Selected {}", name.c_str()); } - } - - ImText("%s %zu", m_directory.name().c_str(), m_directoryContents.size()); - if (ImTable table{"Entries", 4, ImGuiTableFlags_Borders}) { - auto colFlags = ImGuiSelectableFlags_NoAutoClosePopups; - IM_ASSERT(table); - ImNextColumn(); - ImSelectable("T", &m_typeSelected, colFlags); - ImNextColumn(); - ImSelectable("Name", &m_nameSelectede, colFlags); - ImNextColumn(); - ImSelectable("Size", &m_sizeSelected, colFlags); - ImNextColumn(); - ImSelectable("Date", &m_dateSelected, colFlags); - for (auto& entry : m_directoryContents) { - ImNextRow(); - const char entryLetter = DirEntryTypeToChar(entry.entryType); - ImNextColumn(); - ImText("%c", entryLetter); - ImNextColumn(); - ImSelectable(entry.name.c_str(), &entry.selected, colFlags | ImGuiSelectableFlags_SpanAllColumns); - ImNextColumn(); - ImText("%zu\n", entry.isDirectory() ? entry.links : entry.size); - ImNextColumn(); - ImText("%s", entry.niceTime.c_str()); - } - } - - if (ImButton("Accept")) { m_openDialog = false; ImGui::CloseCurrentPopup(); + return; } - ImSameLine(); - - if (ImButton("Cancel")) { + if (m_fileDialog->canceled()) { m_openDialog = false; ImGui::CloseCurrentPopup(); } diff --git a/src/bookmouse.hpp b/src/bookmouse.hpp index 29a6caf..2086ce8 100644 --- a/src/bookmouse.hpp +++ b/src/bookmouse.hpp @@ -36,7 +36,6 @@ class Bookmouse { void menuing(); void openDialog(); - void openDialog2(); void setArchive(std::unique_ptr&& archive); diff --git a/src/file_dialog.cpp b/src/file_dialog.cpp index faab3e7..80e994d 100644 --- a/src/file_dialog.cpp +++ b/src/file_dialog.cpp @@ -1,8 +1,8 @@ #include "file_dialog.hpp" + #include "imgui_context.hpp" #include - #include namespace bookmouse { @@ -27,8 +27,7 @@ FudStatus DialogEntry::formatTime(TimeFormat& format) } auto result = format.format(this->timeInfo); - if (result.isError()) - { + if (result.isError()) { return result.getError(); } @@ -37,11 +36,8 @@ FudStatus DialogEntry::formatTime(TimeFormat& format) return FudStatus::Success; } -FileDialog::FileDialog( - const fud::String& directoryName, - TimeFormat& timeFormat) : m_directoryName{directoryName}, - m_directory{directoryName}, - m_timeFormat{timeFormat} +FileDialog::FileDialog(const fud::String& directoryName, TimeFormat& timeFormat) : + m_directoryName{directoryName}, m_directory{directoryName}, m_timeFormat{timeFormat} { auto status = getDirectoryContents(); if (status != FudStatus::Success) { @@ -49,13 +45,16 @@ FileDialog::FileDialog( } } -FudStatus FileDialog::getDirectoryContents() { +FudStatus FileDialog::getDirectoryContents() +{ auto directoryStatsResult = m_directory.info(); if (directoryStatsResult.isError()) { return directoryStatsResult.getError(); } auto directoryStats = directoryStatsResult.getOkay(); + m_directoryContents.clear(); + size_t count{0}; m_directoryContents.reserve(directoryStats.links); @@ -87,6 +86,7 @@ FudStatus FileDialog::getDirectoryContents() { FilePickerResult FileDialog::pickFiles() { ImText("%s %zu", m_directory.name().c_str(), m_directoryContents.size()); + size_t selected{0}; if (ImTable table{"Entries", 4, ImGuiTableFlags_Borders}) { auto colFlags = ImGuiSelectableFlags_NoAutoClosePopups; IM_ASSERT(table); @@ -109,21 +109,46 @@ FilePickerResult FileDialog::pickFiles() ImText("%zu\n", entry.isDirectory() ? entry.links : entry.size); ImNextColumn(); ImText("%s", entry.niceTime.c_str()); + if (entry.selected && entry.isDirectory()) { + selected++; + } } } + if (selected == 0) { + ImGui::BeginDisabled(); + } + if (ImButton("Accept")) { - return FilePickerResult::error(FudStatus::NotImplemented); + std::vector files{}; + files.reserve(selected); + + for (const auto& entry: m_directoryContents) { + if (!entry.selected) { + continue; + } + if (entry.isDirectory()) { + spdlog::info("Skipping directory {}", entry.name.c_str()); + continue; + } + files.emplace_back(m_directory.name().catenate(entry.name)); + } + + return files; + } + + if (selected == 0) { + ImGui::EndDisabled(); } ImSameLine(); if (ImButton("Cancel")) { - return FilePickerResult::error(FudStatus::NotImplemented); + m_canceled = true; + return std::nullopt; } - - return FilePickerResult::error(FudStatus::NotImplemented); + return std::nullopt; } } // namespace bookmouse diff --git a/src/file_dialog.hpp b/src/file_dialog.hpp index 9709ec0..6a787b3 100644 --- a/src/file_dialog.hpp +++ b/src/file_dialog.hpp @@ -57,9 +57,7 @@ constexpr char DirEntryTypeToChar(DirEntryType entryType) return entryLetter; } -using FilePickerResult = fud::Result< - std::vector, - fud::FudStatus>; +using FilePickerResult = std::optional>; class FileDialog { @@ -74,6 +72,10 @@ public: return m_valid; } + constexpr bool canceled() const { + return m_canceled; + } + private: fud::FudStatus getDirectoryContents(); @@ -84,6 +86,7 @@ private: std::vector m_directoryContents{}; bool m_valid{false}; + bool m_canceled{false}; bool m_typeSelected{false}; bool m_nameSelectede{false}; -- cgit v1.2.3