summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Userland/Applications/Browser/BookmarksBarWidget.cpp56
-rw-r--r--Userland/Applications/Browser/BookmarksBarWidget.h11
-rw-r--r--Userland/Applications/Browser/Tab.cpp6
3 files changed, 54 insertions, 19 deletions
diff --git a/Userland/Applications/Browser/BookmarksBarWidget.cpp b/Userland/Applications/Browser/BookmarksBarWidget.cpp
index 716d88d64e..92ea30d5ec 100644
--- a/Userland/Applications/Browser/BookmarksBarWidget.cpp
+++ b/Userland/Applications/Browser/BookmarksBarWidget.cpp
@@ -30,10 +30,14 @@ class BookmarkEditor final : public GUI::Dialog {
public:
static Vector<JsonValue>
- edit_bookmark(Window* parent_window, StringView title, StringView url)
+ edit_bookmark(Window* parent_window, StringView title, StringView url, BookmarksBarWidget::PerformEditOn perform_edit_on)
{
auto editor = BookmarkEditor::construct(parent_window, title, url);
- editor->set_title("Edit Bookmark");
+ if (perform_edit_on == BookmarksBarWidget::PerformEditOn::NewBookmark) {
+ editor->set_title("Add Bookmark");
+ } else {
+ editor->set_title("Edit Bookmark");
+ }
editor->set_icon(g_icon_bag.bookmark_filled);
if (editor->exec() == ExecResult::OK) {
@@ -306,37 +310,53 @@ bool BookmarksBarWidget::add_bookmark(DeprecatedString const& url, DeprecatedStr
values.append(title);
values.append(url);
- auto& json_model = *static_cast<GUI::JsonArrayModel*>(model());
- if (json_model.add(move(values))) {
- json_model.store();
+ auto was_bookmark_added = update_model(values, [](auto& model, auto&& values) {
+ return model.add(move(values));
+ });
+
+ if (!was_bookmark_added)
+ return false;
+
+ if (on_bookmark_add)
+ on_bookmark_add(url);
+
+ if (edit_bookmark(url, PerformEditOn::NewBookmark))
return true;
- }
+
+ remove_bookmark(url);
return false;
}
-bool BookmarksBarWidget::edit_bookmark(DeprecatedString const& url)
+bool BookmarksBarWidget::edit_bookmark(DeprecatedString const& url, PerformEditOn perform_edit_on)
{
for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
auto item_title = model()->index(item_index, 0).data().to_deprecated_string();
auto item_url = model()->index(item_index, 1).data().to_deprecated_string();
if (item_url == url) {
- auto values = BookmarkEditor::edit_bookmark(window(), item_title, item_url);
- bool item_replaced = false;
+ auto values = BookmarkEditor::edit_bookmark(window(), item_title, item_url, perform_edit_on);
+ return update_model(values, [item_index](auto& model, auto&& values) {
+ return model.set(item_index, move(values));
+ });
+ }
+ }
- if (!values.is_empty()) {
- auto& json_model = *static_cast<GUI::JsonArrayModel*>(model());
- item_replaced = json_model.set(item_index, move(values));
+ return false;
+}
- if (item_replaced)
- json_model.store();
- }
+bool BookmarksBarWidget::update_model(Vector<JsonValue>& values, Function<bool(GUI::JsonArrayModel& model, Vector<JsonValue>&& values)> perform_model_change)
+{
+ bool has_model_changed = false;
- return item_replaced;
- }
+ if (!values.is_empty()) {
+ auto& json_model = *static_cast<GUI::JsonArrayModel*>(model());
+ has_model_changed = perform_model_change(json_model, move(values));
+
+ if (has_model_changed)
+ json_model.store();
}
- return false;
+ return has_model_changed;
}
}
diff --git a/Userland/Applications/Browser/BookmarksBarWidget.h b/Userland/Applications/Browser/BookmarksBarWidget.h
index 99bd845ccc..6737991577 100644
--- a/Userland/Applications/Browser/BookmarksBarWidget.h
+++ b/Userland/Applications/Browser/BookmarksBarWidget.h
@@ -34,11 +34,18 @@ public:
Function<void(DeprecatedString const& url, Open)> on_bookmark_click;
Function<void(DeprecatedString const&, DeprecatedString const&)> on_bookmark_hover;
+ Function<void(DeprecatedString const& url)> on_bookmark_add;
bool contains_bookmark(DeprecatedString const& url);
bool remove_bookmark(DeprecatedString const& url);
bool add_bookmark(DeprecatedString const& url, DeprecatedString const& title);
- bool edit_bookmark(DeprecatedString const& url);
+
+ enum class PerformEditOn {
+ NewBookmark,
+ ExistingBookmark
+ };
+
+ bool edit_bookmark(DeprecatedString const& url, PerformEditOn perform_edit_on = PerformEditOn::ExistingBookmark);
virtual Optional<GUI::UISize> calculated_min_size() const override
{
@@ -57,6 +64,8 @@ private:
void update_content_size();
+ bool update_model(Vector<JsonValue>& values, Function<bool(GUI::JsonArrayModel& model, Vector<JsonValue>&& values)> perform_model_change);
+
RefPtr<GUI::Model> m_model;
RefPtr<GUI::Button> m_additional;
RefPtr<GUI::Widget> m_separator;
diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp
index c18ff748c5..83e724166e 100644
--- a/Userland/Applications/Browser/Tab.cpp
+++ b/Userland/Applications/Browser/Tab.cpp
@@ -610,6 +610,12 @@ void Tab::did_become_active()
m_statusbar->set_text(url);
};
+ BookmarksBarWidget::the().on_bookmark_add = [this](auto& url) {
+ auto current_url = this->url().to_deprecated_string();
+ if (current_url == url)
+ update_bookmark_button(current_url);
+ };
+
BookmarksBarWidget::the().remove_from_parent();
m_toolbar_container->add_child(BookmarksBarWidget::the());