From 6d64f06c6ac32c2eaffa1405f563788889f5bb1e Mon Sep 17 00:00:00 2001 From: Nikola Ducak Date: Fri, 15 Nov 2024 10:18:34 +0100 Subject: [PATCH] feat: preview window title --- source/app.cpp | 40 ++++++++++++++++++++++++++++---- source/app.hpp | 4 ++-- source/view/annual_view.hpp | 4 +++- source/view/annual_view_base.hpp | 2 +- source/view/preview.cpp | 9 ++++--- source/view/preview.hpp | 3 ++- test/mocks.hpp | 14 +++++++---- 7 files changed, 56 insertions(+), 20 deletions(-) diff --git a/source/app.cpp b/source/app.cpp index ebe4eb6..f3aa8fe 100644 --- a/source/app.cpp +++ b/source/app.cpp @@ -13,6 +13,31 @@ using namespace log; using namespace view; using namespace utils; +namespace { + +std::string makePreviewTitle(std::chrono::year_month_day date, const CalendarEvents &events) { + auto eventForDate = [&]() -> std::optional> { + for (const auto &[groupName, events] : events) { + for (const auto &event : events) { + if (event.date == date::monthDay(date)) { + return std::make_tuple(groupName, event); + } + } + } + return std::nullopt; + }(); + + const auto eventStr = + eventForDate.has_value() + ? fmt::format("({} - {})", std::get<0>(*eventForDate), std::get<1>(*eventForDate).name) + : ""; + const auto title = + fmt::format("log preview for {} {}", utils::date::formatToString(date), eventStr); + return title; +} + +} // namespace + ViewDataUpdater::ViewDataUpdater(std::shared_ptr view, const AnnualLogData &data) : m_view{std::move(view)}, m_data{data} {} @@ -60,7 +85,8 @@ void ViewDataUpdater::updateTagMenuItemsPerSection() { } } -void ViewDataUpdater::updateViewAfterDataChange(const std::string &previewString) { +void ViewDataUpdater::updateViewAfterDataChange(const std::string &previewTitle, + const std::string &previewString) { // update sections menu items { const auto oldSections = m_view->sectionMenuItems().getKeys(); @@ -117,7 +143,7 @@ void ViewDataUpdater::updateViewAfterDataChange(const std::string &previewString } // update preview string - m_view->setPreviewString(previewString); + m_view->setPreviewString(previewTitle, previewString); } MenuItems ViewDataUpdater::makeTagMenuItems(const std::string §ion) { @@ -168,7 +194,9 @@ void App::updateDataAndViewAfterLogChange(const std::chrono::year_month_day &dat previewString = log->getContent(); } } - m_viewDataUpdater.updateViewAfterDataChange(previewString); + + m_viewDataUpdater.updateViewAfterDataChange(makePreviewTitle(dateOfChangedLog, m_config.events), + previewString); } App::App(std::shared_ptr view, std::shared_ptr repo, @@ -230,10 +258,12 @@ bool App::handleRootEvent(const std::string &input) { } void App::handleFocusedDateChange() { + + const auto title = makePreviewTitle(m_view->getFocusedDate(), m_config.events); if (auto log = m_repo->read(m_view->getFocusedDate())) { - m_view->setPreviewString(log->getContent()); + m_view->setPreviewString(title, log->getContent()); } else { - m_view->setPreviewString(""); + m_view->setPreviewString(title, ""); } } diff --git a/source/app.hpp b/source/app.hpp index 5fa8ddc..4f0b318 100644 --- a/source/app.hpp +++ b/source/app.hpp @@ -11,7 +11,6 @@ #include #include #include -#include namespace caps_log { @@ -39,7 +38,8 @@ class ViewDataUpdater final { void handleFocusedTagChange(); void handleFocusedSectionChange(); - void updateViewAfterDataChange(const std::string &previewString); + void updateViewAfterDataChange(const std::string &previewTitle, + const std::string &previewString); private: void updateTagMenuItemsPerSection(); diff --git a/source/view/annual_view.hpp b/source/view/annual_view.hpp index 3d617d6..3966831 100644 --- a/source/view/annual_view.hpp +++ b/source/view/annual_view.hpp @@ -59,7 +59,9 @@ class AnnualView : public AnnualViewBase { void setHighlightedDates(const utils::date::Dates *map) override { m_highlightedDates = map; } void setEventDates(const CalendarEvents *events) override { m_eventDates = events; } - void setPreviewString(const std::string &string) override { m_preview->setContent(string); } + void setPreviewString(const std::string &title, const std::string &string) override { + m_preview->setContent(title, string); + } void withRestoredIO(std::function func) override { m_screen.WithRestoredIO(func)(); } diff --git a/source/view/annual_view_base.hpp b/source/view/annual_view_base.hpp index 095a3c1..981550e 100644 --- a/source/view/annual_view_base.hpp +++ b/source/view/annual_view_base.hpp @@ -78,7 +78,7 @@ class AnnualViewBase { // NOLINT virtual MenuItems &tagMenuItems() = 0; virtual MenuItems §ionMenuItems() = 0; - virtual void setPreviewString(const std::string &string) = 0; + virtual void setPreviewString(const std::string &title, const std::string &string) = 0; virtual void withRestoredIO(std::function func) = 0; diff --git a/source/view/preview.cpp b/source/view/preview.cpp index aa73994..440965f 100644 --- a/source/view/preview.cpp +++ b/source/view/preview.cpp @@ -11,10 +11,8 @@ Element Preview::Render() { } constexpr auto kHeight = 14; - if (Focused()) { - return vbox(visibleLines) | borderRounded | flex_shrink | size(HEIGHT, EQUAL, kHeight); - } - return vbox(visibleLines) | borderRounded | flex_shrink | size(HEIGHT, EQUAL, kHeight) | dim; + auto element = window(m_title, vbox(visibleLines)) | flex_shrink | size(HEIGHT, EQUAL, kHeight); + return Focused() ? element : element | dim; } bool Preview::Focusable() const { return true; } @@ -42,7 +40,8 @@ bool Preview::OnEvent(Event event) { void Preview::resetScroll() { m_topLineIndex = 0; } -void Preview::setContent(const std::string &str) { +void Preview::setContent(const std::string &title, const std::string &str) { + m_title = text(title) | underlined | center; Elements lines; std::istringstream input{str}; for (std::string line; std::getline(input, line);) { diff --git a/source/view/preview.hpp b/source/view/preview.hpp index 67c5d63..8796aec 100644 --- a/source/view/preview.hpp +++ b/source/view/preview.hpp @@ -7,6 +7,7 @@ namespace caps_log::view { class Preview : public ftxui::ComponentBase { int m_topLineIndex = 0; ftxui::Elements m_lines; + ftxui::Element m_title = ftxui::text("log preview"); public: ftxui::Element Render() override; @@ -14,7 +15,7 @@ class Preview : public ftxui::ComponentBase { bool OnEvent(ftxui::Event event) override; void resetScroll(); - void setContent(const std::string &str); + void setContent(const std::string &title, const std::string &str); }; } // namespace caps_log::view diff --git a/test/mocks.hpp b/test/mocks.hpp index 806e2b2..aeab9fd 100644 --- a/test/mocks.hpp +++ b/test/mocks.hpp @@ -17,7 +17,7 @@ class DummyYearView : public caps_log::view::AnnualViewBase { std::chrono::year m_displayedYear{}; std::chrono::year_month_day m_focusedDate{std::chrono::year{2005}, std::chrono::month{1}, std::chrono::day{1}}; - std::string m_previewString; + std::string m_previewString, m_previewTitle; const caps_log::utils::date::Dates *m_datesWithLogs{}, *m_highlightedDates{}; caps_log::view::MenuItems m_tagMenuItems, m_sectionMenuItems; std::string m_selectedTag, m_selectedSection; @@ -48,7 +48,10 @@ class DummyYearView : public caps_log::view::AnnualViewBase { m_highlightedDates = map; } - void setPreviewString(const std::string &string) override { m_previewString = string; } + void setPreviewString(const std::string &title, const std::string &string) override { + m_previewString = string; + m_previewTitle = title; + } void withRestoredIO(std::function func) override { func(); } caps_log::view::MenuItems &tagMenuItems() override { return m_tagMenuItems; } @@ -81,8 +84,8 @@ class DMockYearView : public caps_log::view::AnnualViewBase { ON_CALL(*this, prompt).WillByDefault([&](auto msg, auto callback) { m_view.prompt(std::move(msg), std::move(callback)); }); - ON_CALL(*this, setPreviewString).WillByDefault([&](const auto &str) { - m_view.setPreviewString(str); + ON_CALL(*this, setPreviewString).WillByDefault([&](const auto &title, const auto &str) { + m_view.setPreviewString(title, str); }); ON_CALL(*this, tagMenuItems).WillByDefault([&]() -> auto & { @@ -124,7 +127,8 @@ class DMockYearView : public caps_log::view::AnnualViewBase { MOCK_METHOD(void, setDatesWithLogs, (const caps_log::utils::date::Dates *map), (override)); MOCK_METHOD(void, setHighlightedDates, (const caps_log::utils::date::Dates *map), (override)); - MOCK_METHOD(void, setPreviewString, (const std::string &string), (override)); + MOCK_METHOD(void, setPreviewString, (const std::string &titile, const std::string &string), + (override)); MOCK_METHOD(void, withRestoredIO, (std::function func), (override)); MOCK_METHOD(caps_log::view::MenuItems &, tagMenuItems, (), (override));