From 51a75e9cc1935456b6372e055f3b3fa79cfe936a Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Fri, 29 Jul 2022 03:03:31 +0300 Subject: [PATCH] add breadcrumb bar in the topbar --- src/AppWidget.cpp | 11 +++++++++++ src/DirectoryView.cpp | 2 +- src/DirectoryView.hpp | 3 +++ src/TopBar.cpp | 32 ++++++++++++++++++++++++++------ src/TopBar.hpp | 19 +++++++++++++------ 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/AppWidget.cpp b/src/AppWidget.cpp index 3c3a640..51b1a99 100644 --- a/src/AppWidget.cpp +++ b/src/AppWidget.cpp @@ -3,6 +3,7 @@ #include "BoxLayout.hpp" #include "DirectoryView.hpp" #include "TopBar.hpp" +#include void AppWidget::on_init() { auto layout = set_layout(Raven::Direction::Vertical); @@ -12,6 +13,14 @@ void AppWidget::on_init() { auto top_bar = add(); auto directory_view = add(); + directory_view->on_path_update = [top_bar](std::filesystem::path where) { + top_bar->breadcrumb_set(where); + }; + + top_bar->on_navigate = [directory_view](std::filesystem::path where) { + directory_view->navigate(where); + }; + top_bar->on_action = [directory_view](TopBar::Action action) { if (action == TopBar::Action::Back) { directory_view->go_back(); @@ -24,5 +33,7 @@ void AppWidget::on_init() { } }; + directory_view->navigate_home(); + set_did_init(true); } diff --git a/src/DirectoryView.cpp b/src/DirectoryView.cpp index 4cf3129..cfd9e1c 100644 --- a/src/DirectoryView.cpp +++ b/src/DirectoryView.cpp @@ -6,6 +6,7 @@ DirectoryView::~DirectoryView() {} void DirectoryView::set_current_path(std::filesystem::path path) { m_current_path = path; + on_path_update(m_current_path); // microtasks defer execution of the callback until all external events are processed. // if they're not used here and set_current_path() is called in a click or hover handler, @@ -34,7 +35,6 @@ void DirectoryView::on_init() { m_target = make_target(); m_target->set_layout(16.0); - navigate_home(); set_did_init(true); } diff --git a/src/DirectoryView.hpp b/src/DirectoryView.hpp index 30909ce..be62683 100644 --- a/src/DirectoryView.hpp +++ b/src/DirectoryView.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -22,6 +23,8 @@ public: void go_forward(); void navigate_home(); void navigate_parent_directory(); + + std::function on_path_update { [](std::filesystem::path _){} }; protected: void set_current_path(std::filesystem::path path); diff --git a/src/TopBar.cpp b/src/TopBar.cpp index 5d507bc..6c7c9d0 100644 --- a/src/TopBar.cpp +++ b/src/TopBar.cpp @@ -1,9 +1,10 @@ #include "TopBar.hpp" -#include "raven/Styles.hpp" -#include "raven/Box.hpp" -#include "raven/BoxLayout.hpp" -#include "raven/SvgWidget.hpp" -#include "raven/RowLayout.hpp" +#include "Styles.hpp" +#include "Box.hpp" +#include "BoxLayout.hpp" +#include "SvgWidget.hpp" +#include "RowLayout.hpp" +#include "Button.hpp" void TopBar::on_init() { set_style(&Raven::accent_widget_style); @@ -22,8 +23,12 @@ void TopBar::on_init() { auto home_button = add("/usr/share/icons/Papirus/24x24/actions/go-home.svg"); home_button->set_style(&Raven::default_button_style); + m_breadcrumbs = add(); + m_breadcrumbs->set_style(&Raven::accent_widget_style); + auto breadcrumbs_layout = m_breadcrumbs->set_layout(Raven::Direction::Horizontal); + breadcrumbs_layout->set_spacing(8); + back_button->on_click = [this, back_button]() { - std::cout << back_button->rect().width() << ", " << back_button->rect().height() << std::endl; on_action(Action::Back); }; forward_button->on_click = [this]() { @@ -38,3 +43,18 @@ void TopBar::on_init() { set_did_init(true); } + +void TopBar::breadcrumb_set(std::filesystem::path path) { + window()->queue_microtask([this, path]() { + m_breadcrumbs->clear_children(); + + std::filesystem::path current_path; + for (std::filesystem::path::iterator i = path.begin(); i != path.end(); i++) { + auto button = m_breadcrumbs->add(*i == "/" ? "/" : i->filename()); + current_path /= *i; + button->on_click = [this, current_path]() { + on_navigate(current_path); + }; + } + }); +} diff --git a/src/TopBar.hpp b/src/TopBar.hpp index e1f42e2..cd1c974 100644 --- a/src/TopBar.hpp +++ b/src/TopBar.hpp @@ -1,22 +1,29 @@ #pragma once #include "Widget.hpp" +#include #include +#include class TopBar : public Raven::Widget { public: -enum class Action { - Back = 0, - Forward, - Home, - ParentDirectory -}; + enum class Action { + Back = 0, + Forward, + Home, + ParentDirectory + }; public: TopBar() : Raven::Widget() {} + void breadcrumb_set(std::filesystem::path path); + std::function on_action { [](Action _){} }; + std::function on_navigate { [](std::filesystem::path _){} }; protected: void on_init(); private: + std::shared_ptr m_breadcrumbs; + std::filesystem::path m_breadcrumb_path { "/" }; };