main.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ClipboardHistoryModel.h"
  7. #include <LibGUI/Action.h>
  8. #include <LibGUI/Application.h>
  9. #include <LibGUI/ImageWidget.h>
  10. #include <LibGUI/Menu.h>
  11. #include <LibGUI/TableView.h>
  12. #include <LibGUI/Window.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. int main(int argc, char* argv[])
  16. {
  17. if (pledge("stdio recvfd sendfd rpath unix", nullptr) < 0) {
  18. perror("pledge");
  19. return 1;
  20. }
  21. auto app = GUI::Application::construct(argc, argv);
  22. if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
  23. perror("pledge");
  24. return 1;
  25. }
  26. if (unveil("/res", "r") < 0) {
  27. perror("unveil");
  28. return 1;
  29. }
  30. if (unveil(nullptr, nullptr) < 0) {
  31. perror("unveil");
  32. return 1;
  33. }
  34. auto app_icon = GUI::Icon::default_icon("edit-copy");
  35. auto main_window = GUI::Window::construct();
  36. main_window->set_title("Clipboard history");
  37. main_window->set_rect(670, 65, 325, 500);
  38. main_window->set_icon(app_icon.bitmap_for_size(16));
  39. auto& table_view = main_window->set_main_widget<GUI::TableView>();
  40. auto model = ClipboardHistoryModel::create();
  41. table_view.set_model(model);
  42. table_view.on_activation = [&](const GUI::ModelIndex& index) {
  43. auto& data_and_type = model->item_at(index.row());
  44. GUI::Clipboard::the().set_data(data_and_type.data, data_and_type.mime_type, data_and_type.metadata);
  45. };
  46. auto delete_action = GUI::CommonActions::make_delete_action([&](const GUI::Action&) {
  47. model->remove_item(table_view.selection().first().row());
  48. });
  49. auto entry_context_menu = GUI::Menu::construct();
  50. entry_context_menu->add_action(delete_action);
  51. table_view.on_context_menu_request = [&](const GUI::ModelIndex&, const GUI::ContextMenuEvent& event) {
  52. delete_action->set_enabled(!table_view.selection().is_empty());
  53. entry_context_menu->popup(event.screen_position());
  54. };
  55. auto applet_window = GUI::Window::construct();
  56. applet_window->set_title("ClipboardHistory");
  57. applet_window->set_window_type(GUI::WindowType::Applet);
  58. applet_window->set_has_alpha_channel(true);
  59. auto& icon = applet_window->set_main_widget<GUI::ImageWidget>();
  60. icon.set_tooltip("Clipboard History");
  61. icon.load_from_file("/res/icons/16x16/edit-copy.png");
  62. icon.on_click = [&main_window = *main_window] {
  63. main_window.show();
  64. main_window.move_to_front();
  65. };
  66. applet_window->resize(16, 16);
  67. applet_window->show();
  68. return app->exec();
  69. }