mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-06 17:06:31 +00:00
ClipboardHistory: Add Time column
Add a Time column to ClipboardHistory that shows the time when each item in the list was copied.
This commit is contained in:
parent
9d94c85b6a
commit
eebdd86fbd
|
@ -30,6 +30,8 @@ String ClipboardHistoryModel::column_name(int column) const
|
|||
return "Type";
|
||||
case Column::Size:
|
||||
return "Size";
|
||||
case Column::Time:
|
||||
return "Time";
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
@ -62,7 +64,9 @@ GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::Mode
|
|||
{
|
||||
if (role != GUI::ModelRole::Display)
|
||||
return {};
|
||||
auto& data_and_type = m_history_items[index.row()];
|
||||
auto& item = m_history_items[index.row()];
|
||||
auto& data_and_type = item.data_and_type;
|
||||
auto& time = item.time;
|
||||
switch (index.column()) {
|
||||
case Column::Data:
|
||||
if (data_and_type.mime_type.starts_with("text/"))
|
||||
|
@ -91,6 +95,8 @@ GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::Mode
|
|||
return data_and_type.mime_type;
|
||||
case Column::Size:
|
||||
return AK::human_readable_size(data_and_type.data.size());
|
||||
case Column::Time:
|
||||
return time.to_string();
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
@ -98,14 +104,14 @@ GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::Mode
|
|||
|
||||
void ClipboardHistoryModel::add_item(const GUI::Clipboard::DataAndType& item)
|
||||
{
|
||||
m_history_items.remove_first_matching([&](GUI::Clipboard::DataAndType& existing) {
|
||||
return existing.data == item.data && existing.mime_type == item.mime_type;
|
||||
m_history_items.remove_first_matching([&](ClipboardItem& existing) {
|
||||
return existing.data_and_type.data == item.data && existing.data_and_type.mime_type == item.mime_type;
|
||||
});
|
||||
|
||||
if (m_history_items.size() == m_history_limit)
|
||||
m_history_items.take_last();
|
||||
|
||||
m_history_items.prepend(item);
|
||||
m_history_items.prepend({ item, Core::DateTime::now() });
|
||||
invalidate();
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
#include <AK/Vector.h>
|
||||
#include <LibConfig/Listener.h>
|
||||
#include <LibCore/DateTime.h>
|
||||
#include <LibGUI/Clipboard.h>
|
||||
#include <LibGUI/Model.h>
|
||||
|
||||
|
@ -23,12 +24,18 @@ public:
|
|||
Data,
|
||||
Type,
|
||||
Size,
|
||||
Time,
|
||||
__Count
|
||||
};
|
||||
|
||||
struct ClipboardItem {
|
||||
GUI::Clipboard::DataAndType data_and_type;
|
||||
Core::DateTime time;
|
||||
};
|
||||
|
||||
virtual ~ClipboardHistoryModel() override = default;
|
||||
|
||||
const GUI::Clipboard::DataAndType& item_at(int index) const { return m_history_items[index]; }
|
||||
const ClipboardItem& item_at(int index) const { return m_history_items[index]; }
|
||||
void remove_item(int index);
|
||||
|
||||
// ^GUI::Model
|
||||
|
@ -49,6 +56,6 @@ private:
|
|||
// ^GUI::Clipboard::ClipboardClient
|
||||
virtual void clipboard_content_did_change(const String&) override { add_item(GUI::Clipboard::the().fetch_data_and_type()); }
|
||||
|
||||
Vector<GUI::Clipboard::DataAndType> m_history_items;
|
||||
Vector<ClipboardItem> m_history_items;
|
||||
size_t m_history_limit;
|
||||
};
|
||||
|
|
|
@ -38,7 +38,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
table_view->set_model(model);
|
||||
|
||||
table_view->on_activation = [&](GUI::ModelIndex const& index) {
|
||||
auto& data_and_type = model->item_at(index.row());
|
||||
auto& 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);
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue