forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
acc107a
commit f7ae66c
Showing
9 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
add_subdirectory(Audio) | ||
add_subdirectory(ClipboardHistory) | ||
add_subdirectory(Clock) | ||
add_subdirectory(ResourceGraph) | ||
add_subdirectory(UserName) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
set(SOURCES | ||
ClipboardHistoryModel.cpp | ||
IconWidget.cpp | ||
main.cpp | ||
) | ||
|
||
serenity_bin(ClipboardHistory.MenuApplet) | ||
target_link_libraries(ClipboardHistory.MenuApplet LibGUI LibCore LibGfx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2019-2020, Sergey Bugaev <[email protected]> | ||
* 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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2019-2020, Sergey Bugaev <[email protected]> | ||
* 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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2019-2020, Sergey Bugaev <[email protected]> | ||
* 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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2019-2020, Sergey Bugaev <[email protected]> | ||
* 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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) 2019-2020, Sergey Bugaev <[email protected]> | ||
* 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(); | ||
} |