ClipboardHistory: Add a clipboard history applet :^)

It will listen for clipboard content changes in the backgroud. Once you click
on its icon, it will pop up a window listing all recorded clipboard contents.
You can then double-click on an item to copy it again.
This commit is contained in:
Sergey Bugaev 2020-05-15 22:37:31 +03:00 committed by Andreas Kling
parent acc107a44f
commit f7ae66cee3
9 changed files with 309 additions and 0 deletions

View file

@ -81,6 +81,11 @@ Priority=low
KeepAlive=1
User=anon
[ClipboardHistory.MenuApplet]
Priority=low
KeepAlive=1
User=anon
[AudioServer]
Socket=/tmp/portal/audio
# TODO: we may want to start it lazily, but right now WindowServer connects to it immediately on startup

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View file

@ -1,4 +1,5 @@
add_subdirectory(Audio)
add_subdirectory(ClipboardHistory)
add_subdirectory(Clock)
add_subdirectory(ResourceGraph)
add_subdirectory(UserName)

View file

@ -0,0 +1,8 @@
set(SOURCES
ClipboardHistoryModel.cpp
IconWidget.cpp
main.cpp
)
serenity_bin(ClipboardHistory.MenuApplet)
target_link_libraries(ClipboardHistory.MenuApplet LibGUI LibCore LibGfx)

View file

@ -0,0 +1,86 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ClipboardHistoryModel.h"
NonnullRefPtr<ClipboardHistoryModel> ClipboardHistoryModel::create()
{
return adopt(*new ClipboardHistoryModel());
}
ClipboardHistoryModel::~ClipboardHistoryModel()
{
}
String ClipboardHistoryModel::column_name(int column) const
{
switch (column) {
case Column::Data:
return "Data";
case Column::Type:
return "Type";
default:
ASSERT_NOT_REACHED();
}
}
GUI::Model::ColumnMetadata ClipboardHistoryModel::column_metadata(int column) const
{
switch (column) {
case Column::Data:
return { 200 };
case Column::Type:
return { 100 };
default:
ASSERT_NOT_REACHED();
}
}
GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, Role) const
{
auto& data_and_type = m_history_items[index.row()];
switch (index.column()) {
case Column::Data:
if (data_and_type.type.starts_with("text/"))
return data_and_type.data;
return "<...>";
case Column::Type:
return data_and_type.type;
default:
ASSERT_NOT_REACHED();
}
}
void ClipboardHistoryModel::update()
{
did_update();
}
void ClipboardHistoryModel::add_item(const GUI::Clipboard::DataAndType& item)
{
m_history_items.prepend(item);
update();
}

View file

@ -0,0 +1,57 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Vector.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/Model.h>
class ClipboardHistoryModel final : public GUI::Model {
public:
static NonnullRefPtr<ClipboardHistoryModel> create();
enum Column {
Data,
Type,
__Count
};
virtual ~ClipboardHistoryModel() override;
const GUI::Clipboard::DataAndType& item_at(int index) const { return m_history_items[index]; }
void add_item(const GUI::Clipboard::DataAndType& item);
private:
virtual int row_count(const GUI::ModelIndex&) const override { return m_history_items.size(); }
virtual String column_name(int) const override;
virtual GUI::Model::ColumnMetadata column_metadata(int column) const override;
virtual int column_count(const GUI::ModelIndex&) const override { return Column::__Count; }
virtual GUI::Variant data(const GUI::ModelIndex&, Role = Role::Display) const override;
virtual void update() override;
Vector<GUI::Clipboard::DataAndType> m_history_items;
};

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "IconWidget.h"
#include <LibGfx/Bitmap.h>
IconWidget::IconWidget()
{
set_icon(Gfx::Bitmap::load_from_file("/res/icons/clipboard.png"));
set_fill_with_background_color(true);
}
IconWidget::~IconWidget()
{
}
void IconWidget::mousedown_event(GUI::MouseEvent&)
{
if (on_click)
on_click();
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Function.h>
#include <LibGUI/Label.h>
class IconWidget : public GUI::Label {
C_OBJECT(IconWidget)
public:
IconWidget();
virtual ~IconWidget() override;
Function<void()> on_click;
private:
virtual void mousedown_event(GUI::MouseEvent&) override;
};

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ClipboardHistoryModel.h"
#include "IconWidget.h"
#include <LibGUI/Application.h>
#include <LibGUI/TableView.h>
#include <LibGUI/Window.h>
int main(int argc, char* argv[])
{
GUI::Application app(argc, argv);
auto main_window = GUI::Window::construct();
main_window->set_title("Clipboard history");
main_window->set_rect(670, 65, 325, 500);
auto& table_view = main_window->set_main_widget<GUI::TableView>();
auto model = ClipboardHistoryModel::create();
table_view.set_model(model);
GUI::Clipboard::the().on_change = [&](const String&) {
auto item = GUI::Clipboard::the().data_and_type();
model->add_item(item);
};
table_view.on_activation = [&](const GUI::ModelIndex& index) {
auto& data_and_type = model->item_at(index.row());
GUI::Clipboard::the().set_data(data_and_type.data, data_and_type.type);
};
auto applet_window = GUI::Window::construct();
applet_window->set_title("Clipboard history");
applet_window->set_window_type(GUI::WindowType::MenuApplet);
auto& icon = applet_window->set_main_widget<IconWidget>();
icon.on_click = [&main_window = *main_window] {
main_window.show();
};
applet_window->resize(16, 16);
applet_window->show();
return app.exec();
}