diff --git a/Userland/Applets/ClipboardHistory/ClipboardHistoryModel.cpp b/Userland/Applets/ClipboardHistory/ClipboardHistoryModel.cpp index 350f7f9a78..93d3606298 100644 --- a/Userland/Applets/ClipboardHistory/ClipboardHistoryModel.cpp +++ b/Userland/Applets/ClipboardHistory/ClipboardHistoryModel.cpp @@ -1,7 +1,7 @@ /* * Copyright (c) 2019-2020, Sergey Bugaev * Copyright (c) 2021, Mustafa Quraish - * 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) { diff --git a/Userland/Applets/ClipboardHistory/ClipboardHistoryModel.h b/Userland/Applets/ClipboardHistory/ClipboardHistoryModel.h index 5711662aff..ca28590eab 100644 --- a/Userland/Applets/ClipboardHistory/ClipboardHistoryModel.h +++ b/Userland/Applets/ClipboardHistory/ClipboardHistoryModel.h @@ -1,7 +1,7 @@ /* * Copyright (c) 2019-2020, Sergey Bugaev * Copyright (c) 2021, Mustafa Quraish - * 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 m_history_items; size_t m_history_limit; diff --git a/Userland/Applets/ClipboardHistory/main.cpp b/Userland/Applets/ClipboardHistory/main.cpp index e8858a67dc..2e73fb26d9 100644 --- a/Userland/Applets/ClipboardHistory/main.cpp +++ b/Userland/Applets/ClipboardHistory/main.cpp @@ -46,7 +46,14 @@ ErrorOr 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&) {