Browse Source

Browser: Don't show error message box when canceling editor dialog

Currently, an error message box appears when a user tries to cancel
the editor dialog while editing or adding a bookmark.

This snapshot resolves this by having `add_bookmark()` and
`BookmarksBarWidget::edit_bookmark()` perform an if check on the
result of `BookmarkEditor::edit_bookmark()` to see if the dialog
was canceled.
Kemal Zebari 2 năm trước cách đây
mục cha
commit
8f5cc613d2

+ 25 - 5
Userland/Applications/Browser/BookmarksBarWidget.cpp

@@ -321,22 +321,42 @@ ErrorOr<void> BookmarksBarWidget::add_bookmark(StringView url, StringView title)
     if (on_bookmark_change)
         on_bookmark_change();
 
-    if (auto result = edit_bookmark(url, PerformEditOn::NewBookmark); result.is_error()) {
-        (void)remove_bookmark(url);
-        return Error::copy(result.release_error());
+    values = BookmarkEditor::edit_bookmark(window(), title, url, PerformEditOn::NewBookmark);
+    if (values.is_empty())
+        return remove_bookmark(url);
+
+    auto model_has_updated = false;
+    for (int item_index = 0; item_index < model()->row_count(); item_index++) {
+        auto item_url = model()->index(item_index, 1).data().to_deprecated_string();
+
+        if (item_url == url) {
+            TRY(update_model(values, [item_index](auto& model, auto&& values) {
+                return model.set(item_index, move(values));
+            }));
+            model_has_updated = true;
+            break;
+        }
     }
 
+    if (!model_has_updated)
+        return Error::from_string_view("Bookmark not found"sv);
+
+    if (on_bookmark_change)
+        on_bookmark_change();
+
     return {};
 }
 
-ErrorOr<void> BookmarksBarWidget::edit_bookmark(StringView url, PerformEditOn perform_edit_on)
+ErrorOr<void> BookmarksBarWidget::edit_bookmark(StringView url)
 {
     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, perform_edit_on);
+            auto values = BookmarkEditor::edit_bookmark(window(), item_title, item_url, PerformEditOn::ExistingBookmark);
+            if (values.is_empty())
+                return {};
 
             TRY(update_model(values, [item_index](auto& model, auto&& values) {
                 return model.set(item_index, move(values));

+ 1 - 1
Userland/Applications/Browser/BookmarksBarWidget.h

@@ -44,7 +44,7 @@ public:
     bool contains_bookmark(StringView url);
     ErrorOr<void> remove_bookmark(StringView url);
     ErrorOr<void> add_bookmark(StringView url, StringView title);
-    ErrorOr<void> edit_bookmark(StringView url, PerformEditOn perform_edit_on = PerformEditOn::ExistingBookmark);
+    ErrorOr<void> edit_bookmark(StringView url);
 
     virtual Optional<GUI::UISize> calculated_min_size() const override
     {