main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "PreviewWidget.h"
  8. #include <LibCore/ConfigFile.h>
  9. #include <LibFileSystemAccessClient/Client.h>
  10. #include <LibGUI/Application.h>
  11. #include <LibGUI/BoxLayout.h>
  12. #include <LibGUI/ColorInput.h>
  13. #include <LibGUI/ComboBox.h>
  14. #include <LibGUI/Icon.h>
  15. #include <LibGUI/Menu.h>
  16. #include <LibGUI/Menubar.h>
  17. #include <LibGUI/Model.h>
  18. #include <LibGUI/Window.h>
  19. #include <unistd.h>
  20. class ColorRoleModel final : public GUI::Model {
  21. public:
  22. virtual int row_count(const GUI::ModelIndex&) const { return m_color_roles.size(); }
  23. virtual int column_count(const GUI::ModelIndex&) const { return 1; }
  24. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role = GUI::ModelRole::Display) const
  25. {
  26. if (role == GUI::ModelRole::Display)
  27. return Gfx::to_string(m_color_roles[(size_t)index.row()]);
  28. return {};
  29. }
  30. explicit ColorRoleModel(const Vector<Gfx::ColorRole>& color_roles)
  31. : m_color_roles(color_roles)
  32. {
  33. }
  34. Gfx::ColorRole color_role(const GUI::ModelIndex& index) const
  35. {
  36. return m_color_roles[index.row()];
  37. }
  38. Gfx::ColorRole color_role(size_t index) const
  39. {
  40. return m_color_roles[index];
  41. }
  42. private:
  43. const Vector<Gfx::ColorRole>& m_color_roles;
  44. };
  45. int main(int argc, char** argv)
  46. {
  47. if (pledge("stdio recvfd sendfd thread rpath cpath wpath unix", nullptr) < 0) {
  48. perror("pledge");
  49. return 1;
  50. }
  51. auto app = GUI::Application::construct(argc, argv);
  52. if (pledge("stdio recvfd sendfd thread rpath unix", nullptr) < 0) {
  53. perror("pledge");
  54. return 1;
  55. }
  56. if (unveil("/tmp/portal/filesystemaccess", "rw") < 0) {
  57. perror("unveil");
  58. return 1;
  59. }
  60. if (unveil("/res", "r") < 0) {
  61. perror("unveil");
  62. return 1;
  63. }
  64. if (unveil(nullptr, nullptr) < 0) {
  65. perror("unveil");
  66. return 1;
  67. }
  68. auto app_icon = GUI::Icon::default_icon("app-theme-editor");
  69. Gfx::Palette preview_palette = app->palette();
  70. auto window = GUI::Window::construct();
  71. Vector<Gfx::ColorRole> color_roles;
  72. #define __ENUMERATE_COLOR_ROLE(role) color_roles.append(Gfx::ColorRole::role);
  73. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  74. #undef __ENUMERATE_COLOR_ROLE
  75. auto& file_menu = window->add_menu("&File");
  76. file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
  77. Optional<String> path = {};
  78. auto save_to_result = [&](FileSystemAccessClient::Result result) {
  79. if (result.error != 0)
  80. return;
  81. path = result.chosen_file;
  82. auto theme = Core::ConfigFile::open(*result.chosen_file, *result.fd);
  83. for (auto role : color_roles) {
  84. theme->write_entry("Colors", to_string(role), preview_palette.color(role).to_string());
  85. }
  86. theme->sync();
  87. };
  88. file_menu.add_action(GUI::CommonActions::make_save_action([&](auto&) {
  89. if (path.has_value()) {
  90. save_to_result(FileSystemAccessClient::Client::the().request_file(window->window_id(), *path, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate));
  91. } else {
  92. save_to_result(FileSystemAccessClient::Client::the().save_file(window->window_id(), "Theme", "ini"));
  93. }
  94. }));
  95. file_menu.add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
  96. save_to_result(FileSystemAccessClient::Client::the().save_file(window->window_id(), "Theme", "ini"));
  97. }));
  98. auto& help_menu = window->add_menu("&Help");
  99. help_menu.add_action(GUI::CommonActions::make_about_action("Theme Editor", app_icon, window));
  100. auto& main_widget = window->set_main_widget<GUI::Widget>();
  101. main_widget.set_fill_with_background_color(true);
  102. main_widget.set_layout<GUI::VerticalBoxLayout>();
  103. auto& preview_widget = main_widget.add<ThemeEditor::PreviewWidget>(app->palette());
  104. preview_widget.set_fixed_size(480, 360);
  105. auto& horizontal_container = main_widget.add<GUI::Widget>();
  106. horizontal_container.set_layout<GUI::HorizontalBoxLayout>();
  107. horizontal_container.set_fixed_size(480, 20);
  108. auto& combo_box = horizontal_container.add<GUI::ComboBox>();
  109. auto& color_input = horizontal_container.add<GUI::ColorInput>();
  110. combo_box.set_only_allow_values_from_model(true);
  111. combo_box.set_model(adopt_ref(*new ColorRoleModel(color_roles)));
  112. combo_box.on_change = [&](auto&, auto& index) {
  113. auto role = static_cast<const ColorRoleModel*>(index.model())->color_role(index);
  114. color_input.set_color(preview_palette.color(role));
  115. };
  116. combo_box.set_selected_index((size_t)Gfx::ColorRole::Window - 1);
  117. color_input.on_change = [&] {
  118. auto role = static_cast<const ColorRoleModel*>(combo_box.model())->color_role(combo_box.selected_index());
  119. preview_palette.set_color(role, color_input.color());
  120. preview_widget.set_preview_palette(preview_palette);
  121. };
  122. color_input.set_color(preview_palette.color(Gfx::ColorRole::Window));
  123. window->resize(480, 385);
  124. window->set_resizable(false);
  125. window->show();
  126. window->set_title("Theme Editor");
  127. window->set_icon(app_icon.bitmap_for_size(16));
  128. return app->exec();
  129. }