summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/PathBreadcrumbbar.cpp
blob: f44b472065824950bc0aac2ac7bac1064599d3ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
 * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "PathBreadcrumbbar.h"
#include <AK/LexicalPath.h>
#include <LibCore/DeprecatedFile.h>
#include <LibCore/MimeData.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Breadcrumbbar.h>
#include <LibGUI/FileIconProvider.h>
#include <LibGUI/Icon.h>
#include <LibGUI/TextBox.h>

REGISTER_WIDGET(GUI, PathBreadcrumbbar)

namespace GUI {

ErrorOr<NonnullRefPtr<PathBreadcrumbbar>> PathBreadcrumbbar::try_create()
{
    auto location_text_box = TRY(TextBox::try_create());
    auto breadcrumbbar = TRY(Breadcrumbbar::try_create());

    auto path_breadcrumbbar = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) PathBreadcrumbbar(*location_text_box, *breadcrumbbar)));
    (void)TRY(path_breadcrumbbar->try_set_layout<GUI::VerticalBoxLayout>());
    TRY(path_breadcrumbbar->try_add_child(location_text_box));
    TRY(path_breadcrumbbar->try_add_child(breadcrumbbar));

    return path_breadcrumbbar;
}

PathBreadcrumbbar::PathBreadcrumbbar(NonnullRefPtr<GUI::TextBox> location_text_box, NonnullRefPtr<GUI::Breadcrumbbar> breadcrumbbar)
    : Widget()
    , m_location_text_box(move(location_text_box))
    , m_breadcrumbbar(move(breadcrumbbar))
{
    m_location_text_box->set_visible(false);

    m_location_text_box->on_escape_pressed = [&] {
        hide_location_text_box();
    };
    m_location_text_box->on_focusout = [&] {
        hide_location_text_box();
    };

    m_location_text_box->on_return_pressed = [&] {
        if (Core::DeprecatedFile::is_directory(m_location_text_box->text())) {
            set_current_path(m_location_text_box->text());
            hide_location_text_box();
        }
    };

    m_breadcrumbbar->set_visible(true);

    m_breadcrumbbar->on_segment_change = [&](Optional<size_t> segment_index) {
        if (!segment_index.has_value())
            return;

        if (!on_path_change)
            return;

        auto segment_path = m_breadcrumbbar->segment_data(*segment_index);
        on_path_change(segment_path);
    };

    m_breadcrumbbar->on_segment_drag_enter = [&](size_t, GUI::DragEvent& event) {
        if (event.mime_types().contains_slow("text/uri-list"))
            event.accept();
    };

    m_breadcrumbbar->on_segment_drop = [&](size_t segment_index, GUI::DropEvent const& event) {
        if (!event.mime_data().has_urls())
            return;
        if (on_paths_drop)
            on_paths_drop(m_breadcrumbbar->segment_data(segment_index), event);
    };

    m_breadcrumbbar->on_doubleclick = [&](GUI::MouseEvent const&) {
        show_location_text_box();
    };
}

PathBreadcrumbbar::~PathBreadcrumbbar() = default;

void PathBreadcrumbbar::set_current_path(DeprecatedString const& new_path)
{
    if (new_path == m_current_path)
        return;

    LexicalPath lexical_path(new_path);
    m_current_path = new_path;

    auto segment_index_of_new_path_in_breadcrumbbar = m_breadcrumbbar->find_segment_with_data(new_path);

    if (segment_index_of_new_path_in_breadcrumbbar.has_value()) {
        auto new_segment_index = segment_index_of_new_path_in_breadcrumbbar.value();
        m_breadcrumbbar->set_selected_segment(new_segment_index);

        // If the path change was because the directory we were in was deleted,
        // remove the breadcrumbs for it.
        if ((new_segment_index + 1 < m_breadcrumbbar->segment_count())
            && !Core::DeprecatedFile::is_directory(m_breadcrumbbar->segment_data(new_segment_index + 1))) {
            m_breadcrumbbar->remove_end_segments(new_segment_index + 1);
        }
    } else {
        m_breadcrumbbar->clear_segments();

        m_breadcrumbbar->append_segment("/", GUI::FileIconProvider::icon_for_path("/").bitmap_for_size(16), "/", "/");
        StringBuilder builder;

        for (auto& part : lexical_path.parts()) {
            // NOTE: We rebuild the path as we go, so we have something to pass to GUI::FileIconProvider.
            builder.append('/');
            builder.append(part);

            m_breadcrumbbar->append_segment(part, GUI::FileIconProvider::icon_for_path(builder.string_view()).bitmap_for_size(16), builder.string_view(), builder.string_view());
        }

        m_breadcrumbbar->set_selected_segment(m_breadcrumbbar->segment_count() - 1);
    }
}

bool PathBreadcrumbbar::has_parent_segment() const
{
    return m_breadcrumbbar->has_parent_segment();
}

bool PathBreadcrumbbar::has_child_segment() const
{
    return m_breadcrumbbar->has_child_segment();
}

void PathBreadcrumbbar::select_parent_segment()
{
    if (!has_parent_segment())
        return;
    m_breadcrumbbar->set_selected_segment(m_breadcrumbbar->selected_segment().value() - 1);
}

void PathBreadcrumbbar::select_child_segment()
{
    if (!has_child_segment())
        return;
    m_breadcrumbbar->set_selected_segment(m_breadcrumbbar->selected_segment().value() + 1);
}

void PathBreadcrumbbar::show_location_text_box()
{
    if (m_location_text_box->is_visible())
        return;
    m_location_text_box->set_visible(true);
    m_breadcrumbbar->set_visible(false);

    m_location_text_box->set_icon(GUI::FileIconProvider::icon_for_path(m_current_path).bitmap_for_size(16));
    m_location_text_box->set_text(m_current_path);
    m_location_text_box->select_all();
    m_location_text_box->set_focus(true);
}

void PathBreadcrumbbar::hide_location_text_box()
{
    if (!m_location_text_box->is_visible())
        return;
    m_location_text_box->set_visible(false);
    m_breadcrumbbar->set_visible(true);

    m_location_text_box->set_focus(false);

    if (on_hide_location_box)
        on_hide_location_box();
}

}