Skip to content

Commit d8e0535

Browse files
AtkinsSJalimpfard
authored andcommitted
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.
1 parent 1761564 commit d8e0535

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Userland/Libraries/LibGUI/Breadcrumbbar.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3+
* Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
34
*
45
* SPDX-License-Identifier: BSD-2-Clause
56
*/
@@ -72,7 +73,7 @@ void Breadcrumbbar::clear_segments()
7273
remove_all_children();
7374
}
7475

75-
void Breadcrumbbar::append_segment(String text, const Gfx::Bitmap* icon, String data, String tooltip)
76+
void Breadcrumbbar::append_segment(String text, Gfx::Bitmap const* icon, String data, String tooltip)
7677
{
7778
auto& button = add<BreadcrumbButton>();
7879
button.set_button_style(Gfx::ButtonStyle::Coolbar);
@@ -105,6 +106,23 @@ void Breadcrumbbar::append_segment(String text, const Gfx::Bitmap* icon, String
105106
m_segments.append(move(segment));
106107
}
107108

109+
void Breadcrumbbar::remove_end_segments(size_t start_segment_index)
110+
{
111+
while (segment_count() > start_segment_index) {
112+
auto segment = m_segments.take_last();
113+
remove_child(*segment.button);
114+
}
115+
}
116+
117+
Optional<size_t> Breadcrumbbar::find_segment_with_data(String const& data)
118+
{
119+
for (size_t i = 0; i < segment_count(); ++i) {
120+
if (segment_data(i) == data)
121+
return i;
122+
}
123+
return {};
124+
}
125+
108126
void Breadcrumbbar::set_selected_segment(Optional<size_t> index)
109127
{
110128
if (!index.has_value()) {

Userland/Libraries/LibGUI/Breadcrumbbar.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ class Breadcrumbbar : public GUI::Widget {
1717
virtual ~Breadcrumbbar() override;
1818

1919
void clear_segments();
20-
void append_segment(String text, const Gfx::Bitmap* icon = nullptr, String data = {}, String tooltip = {});
20+
void append_segment(String text, Gfx::Bitmap const* icon = nullptr, String data = {}, String tooltip = {});
21+
void remove_end_segments(size_t segment_index);
2122

2223
size_t segment_count() const { return m_segments.size(); }
2324
String segment_data(size_t index) const { return m_segments[index].data; }
25+
Optional<size_t> find_segment_with_data(String const& data);
2426

2527
void set_selected_segment(Optional<size_t> index);
2628
Optional<size_t> selected_segment() const { return m_selected_segment; }

0 commit comments

Comments
 (0)