summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@gmail.com>2021-06-24 15:41:59 +0100
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-07-01 17:15:26 +0430
commitd8e0535116c11da509f2fa1a4514c54799b73ca7 (patch)
treecabde0539bae9c4084e862cbe20ca5b306e5cef5
parent17615641dbc1a857b381fdda3be8b13cb95ecc6e (diff)
downloadserenity-d8e0535116c11da509f2fa1a4514c54799b73ca7.zip
LibGUI: Add search/removal methods to Breadcrumbbar
Both are used by FileManager in the next commit. find_segment_with_data() was previously a single-use lambda in FileManager, but making it a method of Breadcrumbbar means we can re-use it more easily.
-rw-r--r--Userland/Libraries/LibGUI/Breadcrumbbar.cpp20
-rw-r--r--Userland/Libraries/LibGUI/Breadcrumbbar.h4
2 files changed, 22 insertions, 2 deletions
diff --git a/Userland/Libraries/LibGUI/Breadcrumbbar.cpp b/Userland/Libraries/LibGUI/Breadcrumbbar.cpp
index 6ae045dffc..7d10216624 100644
--- a/Userland/Libraries/LibGUI/Breadcrumbbar.cpp
+++ b/Userland/Libraries/LibGUI/Breadcrumbbar.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -72,7 +73,7 @@ void Breadcrumbbar::clear_segments()
remove_all_children();
}
-void Breadcrumbbar::append_segment(String text, const Gfx::Bitmap* icon, String data, String tooltip)
+void Breadcrumbbar::append_segment(String text, Gfx::Bitmap const* icon, String data, String tooltip)
{
auto& button = add<BreadcrumbButton>();
button.set_button_style(Gfx::ButtonStyle::Coolbar);
@@ -105,6 +106,23 @@ void Breadcrumbbar::append_segment(String text, const Gfx::Bitmap* icon, String
m_segments.append(move(segment));
}
+void Breadcrumbbar::remove_end_segments(size_t start_segment_index)
+{
+ while (segment_count() > start_segment_index) {
+ auto segment = m_segments.take_last();
+ remove_child(*segment.button);
+ }
+}
+
+Optional<size_t> Breadcrumbbar::find_segment_with_data(String const& data)
+{
+ for (size_t i = 0; i < segment_count(); ++i) {
+ if (segment_data(i) == data)
+ return i;
+ }
+ return {};
+}
+
void Breadcrumbbar::set_selected_segment(Optional<size_t> index)
{
if (!index.has_value()) {
diff --git a/Userland/Libraries/LibGUI/Breadcrumbbar.h b/Userland/Libraries/LibGUI/Breadcrumbbar.h
index 6cb5e79169..0695fc2f9b 100644
--- a/Userland/Libraries/LibGUI/Breadcrumbbar.h
+++ b/Userland/Libraries/LibGUI/Breadcrumbbar.h
@@ -17,10 +17,12 @@ public:
virtual ~Breadcrumbbar() override;
void clear_segments();
- void append_segment(String text, const Gfx::Bitmap* icon = nullptr, String data = {}, String tooltip = {});
+ void append_segment(String text, Gfx::Bitmap const* icon = nullptr, String data = {}, String tooltip = {});
+ void remove_end_segments(size_t segment_index);
size_t segment_count() const { return m_segments.size(); }
String segment_data(size_t index) const { return m_segments[index].data; }
+ Optional<size_t> find_segment_with_data(String const& data);
void set_selected_segment(Optional<size_t> index);
Optional<size_t> selected_segment() const { return m_selected_segment; }