diff options
Diffstat (limited to 'src/bookmouse.cpp')
-rw-r--r-- | src/bookmouse.cpp | 125 |
1 files changed, 111 insertions, 14 deletions
diff --git a/src/bookmouse.cpp b/src/bookmouse.cpp index 1f6fa00..2e13b4e 100644 --- a/src/bookmouse.cpp +++ b/src/bookmouse.cpp @@ -1,18 +1,25 @@ #include "bookmouse.hpp" +#include "bookmouse_time.hpp" +#include "file_dialog.hpp" + #include <SDL_opengl.h> +#include <atomic> #include <fud_array.hpp> +#include <fud_directory.hpp> #include <spdlog/spdlog.h> - -#include <atomic> +#include <vector> namespace bookmouse { +using fud::FudStatus; + constexpr const char* OpenDialogHandle = "Open File"; Bookmouse::Bookmouse() : m_sdlContext{}, m_mainWindow{m_sdlContext}, m_glContext{m_mainWindow}, - m_imgui{m_glContext, m_sdlContext, m_mainWindow} + m_imgui{m_glContext, m_sdlContext, m_mainWindow}, + m_directory{m_directoryName} { /* * Load Fonts @@ -92,23 +99,30 @@ void Bookmouse::updateState() { ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f)); ImGui::SetNextWindowSize(m_imgui.getIO().DisplaySize); - auto stateResult = ImGui::Begin( + auto stateResult = ImBegin( "My First Tool", &m_running, // Is this right? ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_MenuBar); IM_ASSERT(stateResult); - if (ImguiMenuBar menuBar{}) { + // m_imgui.getIO().AddKeyEvent() + + if (ImGui::IsKeyPressed(ImGuiKey_O) && m_imgui.getIO().KeyCtrl && !m_openDialog) { + m_openDialog = true; + } + + if (ImMenuBar menuBar{}) { IM_ASSERT(menuBar); menuing(); } if (m_openDialog) { m_openDialog = false; + m_getContents = true; ImGui::OpenPopup(OpenDialogHandle); } - if (ImguiPopupModal popup{OpenDialogHandle}) { + if (ImPopupModal popup{OpenDialogHandle}) { IM_ASSERT(popup); openDialog(); } @@ -121,20 +135,20 @@ void Bookmouse::updateState() float samples[100]; for (int idx = 0; idx < 100; idx++) { auto num = static_cast<float>(idx); - auto time = static_cast<float>(ImGui::GetTime()); + auto time = static_cast<float>(ImGetTime()); samples[idx] = sinf(num * 0.2f + time * 1.5f); } ImGui::PlotLines("Samples", samples, 100); // Display contents in a scrolling region ImGui::TextColored(ImVec4(1, 1, 0, 1), "Important Stuff"); - ImGui::Text("%04d: Some text", 42); - ImGui::End(); + ImText("%04d: Some text", 42); + ImEnd(); } void Bookmouse::menuing() { - if (ImguiMenu menu{"File"}) { + if (ImMenu menu{"File"}) { IM_ASSERT(menu); if (ImGui::MenuItem("Open Archive", "Ctrl+O")) { /* Do stuff */ m_openDialog = true; @@ -144,7 +158,7 @@ void Bookmouse::menuing() } } - if (ImguiMenu menu{"Help"}) { + if (ImMenu menu{"Help"}) { IM_ASSERT(menu); if (ImGui::MenuItem("About")) { spdlog::debug("About"); @@ -153,9 +167,92 @@ void Bookmouse::menuing() } } -void Bookmouse::openDialog() { - ImGui::Text("Hello from another window!"); - if (ImGui::Button("Close Me")) { +void Bookmouse::openDialog() +{ + if (m_fileDialog == nullptr) { + m_fileDialog = std::make_unique<FileDialog>(m_directoryName, m_timeFormat); + if (!m_fileDialog->valid()) { + m_openDialog = false; + 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); + + 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)); + } + } + } + + 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(); + } + + ImSameLine(); + + if (ImButton("Cancel")) { m_openDialog = false; ImGui::CloseCurrentPopup(); } |