瀏覽代碼

ClipboardHistory: Update clipboard when the topmost item is deleted

Tim Ledbetter 2 年之前
父節點
當前提交
174135f909

+ 8 - 1
Userland/Applets/ClipboardHistory/ClipboardHistoryModel.cpp

@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
- * Copyright (c) 2022, the SerenityOS developers.
+ * Copyright (c) 2022-2023, the SerenityOS developers.
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -110,6 +110,13 @@ GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::Mode
     }
 }
 
+void ClipboardHistoryModel::clipboard_content_did_change(DeprecatedString const&)
+{
+    auto data_and_type = GUI::Clipboard::the().fetch_data_and_type();
+    if (!(data_and_type.data.is_empty() && data_and_type.mime_type.is_empty() && data_and_type.metadata.is_empty()))
+        add_item(data_and_type);
+}
+
 void ClipboardHistoryModel::add_item(const GUI::Clipboard::DataAndType& item)
 {
     m_history_items.remove_first_matching([&](ClipboardItem& existing) {

+ 3 - 2
Userland/Applets/ClipboardHistory/ClipboardHistoryModel.h

@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
- * Copyright (c) 2022, the SerenityOS developers.
+ * Copyright (c) 2022-2023, the SerenityOS developers.
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -37,6 +37,7 @@ public:
 
     ClipboardItem const& item_at(int index) const { return m_history_items[index]; }
     void remove_item(int index);
+    bool is_empty() { return m_history_items.is_empty(); }
 
     // ^GUI::Model
     virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
@@ -54,7 +55,7 @@ private:
     virtual int column_count(const GUI::ModelIndex&) const override { return Column::__Count; }
 
     // ^GUI::Clipboard::ClipboardClient
-    virtual void clipboard_content_did_change(DeprecatedString const&) override { add_item(GUI::Clipboard::the().fetch_data_and_type()); }
+    virtual void clipboard_content_did_change(DeprecatedString const&) override;
 
     Vector<ClipboardItem> m_history_items;
     size_t m_history_limit;

+ 8 - 1
Userland/Applets/ClipboardHistory/main.cpp

@@ -46,7 +46,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
         if (table_view->selection().is_empty())
             return;
 
-        model->remove_item(table_view->selection().first().row());
+        auto index = table_view->selection().first();
+        model->remove_item(index.row());
+        if (model->is_empty()) {
+            GUI::Clipboard::the().clear();
+        } else if (index.row() == 0) {
+            auto const& data_and_type = model->item_at(index.row()).data_and_type;
+            GUI::Clipboard::the().set_data(data_and_type.data, data_and_type.mime_type, data_and_type.metadata);
+        }
     });
 
     auto debug_dump_action = GUI::Action::create("Dump to debug console", [&](const GUI::Action&) {