summaryrefslogtreecommitdiff
path: root/Userland/Applications/Browser/BookmarksBarWidget.cpp
blob: c47960713368be8d07672983f74aa118ef7c3167 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
 * Copyright (c) 2022, Jakob-Niklas See <git@nwex.de>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <Applications/Browser/BookmarksBarWidget.h>
#include <Applications/Browser/Browser.h>
#include <Applications/Browser/EditBookmarkGML.h>
#include <LibGUI/Action.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/Dialog.h>
#include <LibGUI/Event.h>
#include <LibGUI/JsonArrayModel.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Model.h>
#include <LibGUI/TextBox.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h>
#include <LibGfx/Palette.h>

namespace Browser {

namespace {

class BookmarkEditor final : public GUI::Dialog {
    C_OBJECT(BookmarkEditor)

public:
    static Vector<JsonValue>
    edit_bookmark(Window* parent_window, StringView title, StringView url)
    {
        auto editor = BookmarkEditor::construct(parent_window, title, url);
        editor->set_title("Edit Bookmark");
        editor->set_icon(g_icon_bag.bookmark_filled);

        if (editor->exec() == ExecResult::OK) {
            return Vector<JsonValue> { editor->title(), editor->url() };
        }

        return {};
    }

private:
    BookmarkEditor(Window* parent_window, StringView title, StringView url)
        : Dialog(parent_window)
    {
        auto& widget = set_main_widget<GUI::Widget>();
        if (!widget.load_from_gml(edit_bookmark_gml))
            VERIFY_NOT_REACHED();

        set_resizable(false);
        resize(260, 85);

        m_title_textbox = *widget.find_descendant_of_type_named<GUI::TextBox>("title_textbox");
        m_title_textbox->set_text(title);
        m_title_textbox->set_focus(true);
        m_title_textbox->select_all();

        m_url_textbox = *widget.find_descendant_of_type_named<GUI::TextBox>("url_textbox");
        m_url_textbox->set_text(url);

        auto& ok_button = *widget.find_descendant_of_type_named<GUI::Button>("ok_button");
        ok_button.on_click = [this](auto) {
            done(ExecResult::OK);
        };
        ok_button.set_default(true);

        auto& cancel_button = *widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
        cancel_button.on_click = [this](auto) {
            done(ExecResult::Cancel);
        };
    }

    String title() const
    {
        return m_title_textbox->text();
    }

    String url() const
    {
        return m_url_textbox->text();
    }

    RefPtr<GUI::TextBox> m_title_textbox;
    RefPtr<GUI::TextBox> m_url_textbox;
};

}

static BookmarksBarWidget* s_the;

BookmarksBarWidget& BookmarksBarWidget::the()
{
    return *s_the;
}

BookmarksBarWidget::BookmarksBarWidget(String const& bookmarks_file, bool enabled)
{
    s_the = this;
    set_layout<GUI::HorizontalBoxLayout>();
    layout()->set_spacing(0);
    layout()->set_margins(2);

    set_fixed_height(20);

    if (!enabled)
        set_visible(false);

    m_additional = GUI::Button::construct();
    m_additional->set_tooltip("Show hidden bookmarks");
    m_additional->set_menu(m_additional_menu);
    auto bitmap_or_error = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/overflow-menu.png"sv);
    if (!bitmap_or_error.is_error())
        m_additional->set_icon(bitmap_or_error.release_value());
    m_additional->set_button_style(Gfx::ButtonStyle::Coolbar);
    m_additional->set_fixed_size(22, 20);
    m_additional->set_focus_policy(GUI::FocusPolicy::TabFocus);

    m_separator = GUI::Widget::construct();

    m_context_menu = GUI::Menu::construct();
    auto default_action = GUI::Action::create(
        "&Open", g_icon_bag.go_to, [this](auto&) {
            if (on_bookmark_click)
                on_bookmark_click(m_context_menu_url, OpenInNewTab::No);
        },
        this);
    m_context_menu_default_action = default_action;
    m_context_menu->add_action(default_action);
    m_context_menu->add_action(GUI::Action::create(
        "Open in New &Tab", g_icon_bag.new_tab, [this](auto&) {
            if (on_bookmark_click)
                on_bookmark_click(m_context_menu_url, OpenInNewTab::Yes);
        },
        this));
    m_context_menu->add_separator();
    m_context_menu->add_action(GUI::Action::create(
        "&Edit...", g_icon_bag.rename, [this](auto&) {
            edit_bookmark(m_context_menu_url);
        },
        this));
    m_context_menu->add_action(GUI::CommonActions::make_delete_action(
        [this](auto&) {
            remove_bookmark(m_context_menu_url);
        },
        this));

    Vector<GUI::JsonArrayModel::FieldSpec> fields;
    fields.empend("title", "Title", Gfx::TextAlignment::CenterLeft);
    fields.empend("url", "Url", Gfx::TextAlignment::CenterRight);
    set_model(GUI::JsonArrayModel::create(bookmarks_file, move(fields)));
    model()->invalidate();
}

BookmarksBarWidget::~BookmarksBarWidget()
{
    if (m_model)
        m_model->unregister_client(*this);
}

void BookmarksBarWidget::set_model(RefPtr<GUI::Model> model)
{
    if (model == m_model)
        return;
    if (m_model)
        m_model->unregister_client(*this);
    m_model = move(model);
    m_model->register_client(*this);
}

void BookmarksBarWidget::resize_event(GUI::ResizeEvent& event)
{
    Widget::resize_event(event);
    update_content_size();
}

void BookmarksBarWidget::model_did_update(unsigned)
{
    remove_all_children();

    m_bookmarks.clear();

    int width = 0;
    for (int item_index = 0; item_index < model()->row_count(); ++item_index) {

        auto title = model()->index(item_index, 0).data().to_string();
        auto url = model()->index(item_index, 1).data().to_string();

        Gfx::IntRect rect { width, 0, font().width(title) + 32, height() };

        auto& button = add<GUI::Button>();
        m_bookmarks.append(button);

        button.set_button_style(Gfx::ButtonStyle::Coolbar);
        button.set_text(title);
        button.set_icon(g_icon_bag.filetype_html);
        button.set_fixed_size(font().width(title) + 32, 20);
        button.set_relative_rect(rect);
        button.set_focus_policy(GUI::FocusPolicy::TabFocus);
        button.set_tooltip(url);
        button.set_allowed_mouse_buttons_for_pressing(GUI::MouseButton::Primary | GUI::MouseButton::Middle);

        button.on_click = [title, url, this](auto) {
            if (on_bookmark_click)
                on_bookmark_click(url, OpenInNewTab::No);
        };

        button.on_middle_mouse_click = [title, url, this](auto) {
            if (on_bookmark_click)
                on_bookmark_click(url, OpenInNewTab::Yes);
        };

        button.on_context_menu_request = [this, url](auto& context_menu_event) {
            m_context_menu_url = url;
            m_context_menu->popup(context_menu_event.screen_position(), m_context_menu_default_action);
        };

        width += rect.width();
    }

    add_child(*m_separator);
    add_child(*m_additional);

    update_content_size();
    update();
}

void BookmarksBarWidget::update_content_size()
{
    int x_position = 0;
    m_last_visible_index = -1;

    for (size_t i = 0; i < m_bookmarks.size(); ++i) {
        auto& bookmark = m_bookmarks.at(i);
        if (x_position + bookmark.width() + m_additional->width() > width()) {
            m_last_visible_index = i;
            break;
        }
        bookmark.set_x(x_position);
        bookmark.set_visible(true);
        x_position += bookmark.width();
    }

    if (m_last_visible_index < 0) {
        m_additional->set_visible(false);
    } else {
        // hide all items > m_last_visible_index and create new bookmarks menu for them
        m_additional->set_visible(true);
        m_additional_menu = GUI::Menu::construct("Additional Bookmarks");
        m_additional->set_menu(m_additional_menu);
        for (size_t i = m_last_visible_index; i < m_bookmarks.size(); ++i) {
            auto& bookmark = m_bookmarks.at(i);
            bookmark.set_visible(false);
            m_additional_menu->add_action(GUI::Action::create(bookmark.text(), g_icon_bag.filetype_html, [&](auto&) { bookmark.on_click(0); }));
        }
    }
}

bool BookmarksBarWidget::contains_bookmark(String const& url)
{
    for (int item_index = 0; item_index < model()->row_count(); ++item_index) {

        auto item_title = model()->index(item_index, 0).data().to_string();
        auto item_url = model()->index(item_index, 1).data().to_string();
        if (item_url == url) {
            return true;
        }
    }
    return false;
}

bool BookmarksBarWidget::remove_bookmark(String const& url)
{
    for (int item_index = 0; item_index < model()->row_count(); ++item_index) {

        auto item_title = model()->index(item_index, 0).data().to_string();
        auto item_url = model()->index(item_index, 1).data().to_string();
        if (item_url == url) {
            auto& json_model = *static_cast<GUI::JsonArrayModel*>(model());

            auto const item_removed = json_model.remove(item_index);
            if (item_removed)
                json_model.store();

            return item_removed;
        }
    }

    return false;
}

bool BookmarksBarWidget::add_bookmark(String const& url, String const& title)
{
    Vector<JsonValue> values;
    values.append(title);
    values.append(url);

    auto& json_model = *static_cast<GUI::JsonArrayModel*>(model());
    if (json_model.add(move(values))) {
        json_model.store();
        return true;
    }
    return false;
}

bool BookmarksBarWidget::edit_bookmark(String const& url)
{
    for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
        auto item_title = model()->index(item_index, 0).data().to_string();
        auto item_url = model()->index(item_index, 1).data().to_string();

        if (item_url == url) {
            auto values = BookmarkEditor::edit_bookmark(window(), item_title, item_url);
            bool item_replaced = false;

            if (!values.is_empty()) {
                auto& json_model = *static_cast<GUI::JsonArrayModel*>(model());
                item_replaced = json_model.set(item_index, move(values));

                if (item_replaced)
                    json_model.store();
            }

            return item_replaced;
        }
    }

    return false;
}

}