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.
This commit is contained in:
Sam Atkins 2021-06-24 15:41:59 +01:00 committed by Ali Mohammad Pur
parent 17615641db
commit d8e0535116
Notes: sideshowbarker 2024-07-18 11:10:21 +09:00
2 changed files with 22 additions and 2 deletions

View file

@ -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()) {

View file

@ -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; }