From d8e0535116c11da509f2fa1a4514c54799b73ca7 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 24 Jun 2021 15:41:59 +0100 Subject: 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. --- Userland/Libraries/LibGUI/Breadcrumbbar.cpp | 20 +++++++++++++++++++- Userland/Libraries/LibGUI/Breadcrumbbar.h | 4 +++- 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 + * Copyright (c) 2021, Sam Atkins * * 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(); 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 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 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 find_segment_with_data(String const& data); void set_selected_segment(Optional index); Optional selected_segment() const { return m_selected_segment; } -- cgit v1.2.3