From 3918a8492be9b162d94f5b3bf8ba6d4fd2a7fce0 Mon Sep 17 00:00:00 2001 From: Kemal Zebari Date: Fri, 24 Feb 2023 01:10:57 -0800 Subject: [PATCH] Browser: Have the bookmark button use the editor dialog Now when the bookmark button that has not yet bookmarked the current URL is pressed, it will add the bookmark but also prompt the user with the BookmarkEditor dialog in case they wish to make final touches to their bookmark title or URL. If they cancel or escape the dialog, the bookmark will be removed. --- .../Browser/BookmarksBarWidget.cpp | 58 +++++++++++++------ .../Applications/Browser/BookmarksBarWidget.h | 11 +++- Userland/Applications/Browser/Tab.cpp | 6 ++ 3 files changed, 55 insertions(+), 20 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 - 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(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; - - if (!values.is_empty()) { - auto& json_model = *static_cast(model()); - item_replaced = json_model.set(item_index, move(values)); - - if (item_replaced) - json_model.store(); - } - - return item_replaced; + 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)); + }); } } return false; } +bool BookmarksBarWidget::update_model(Vector& values, Function&& values)> perform_model_change) +{ + bool has_model_changed = false; + + if (!values.is_empty()) { + auto& json_model = *static_cast(model()); + has_model_changed = perform_model_change(json_model, move(values)); + + if (has_model_changed) + json_model.store(); + } + + 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 on_bookmark_click; Function on_bookmark_hover; + Function 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 calculated_min_size() const override { @@ -57,6 +64,8 @@ private: void update_content_size(); + bool update_model(Vector& values, Function&& values)> perform_model_change); + RefPtr m_model; RefPtr m_additional; RefPtr 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());