main.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "PreviewWidget.h"
  7. #include <LibGUI/Application.h>
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/ColorInput.h>
  10. #include <LibGUI/ComboBox.h>
  11. #include <LibGUI/Icon.h>
  12. #include <LibGUI/Model.h>
  13. #include <LibGUI/Window.h>
  14. #include <unistd.h>
  15. class ColorRoleModel final : public GUI::Model {
  16. public:
  17. virtual int row_count(const GUI::ModelIndex&) const { return m_color_roles.size(); }
  18. virtual int column_count(const GUI::ModelIndex&) const { return 1; }
  19. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role = GUI::ModelRole::Display) const
  20. {
  21. if (role == GUI::ModelRole::Display)
  22. return Gfx::to_string(m_color_roles[(size_t)index.row()]);
  23. return {};
  24. }
  25. virtual void update() { did_update(); }
  26. explicit ColorRoleModel(const Vector<Gfx::ColorRole>& color_roles)
  27. : m_color_roles(color_roles)
  28. {
  29. }
  30. Gfx::ColorRole color_role(const GUI::ModelIndex& index) const
  31. {
  32. return m_color_roles[index.row()];
  33. }
  34. Gfx::ColorRole color_role(size_t index) const
  35. {
  36. return m_color_roles[index];
  37. }
  38. private:
  39. const Vector<Gfx::ColorRole>& m_color_roles;
  40. };
  41. int main(int argc, char** argv)
  42. {
  43. if (pledge("stdio recvfd sendfd thread rpath accept cpath wpath unix fattr", nullptr) < 0) {
  44. perror("pledge");
  45. return 1;
  46. }
  47. auto app = GUI::Application::construct(argc, argv);
  48. if (pledge("stdio recvfd sendfd thread rpath accept", nullptr) < 0) {
  49. perror("pledge");
  50. return 1;
  51. }
  52. if (unveil("/res", "r") < 0) {
  53. perror("unveil");
  54. return 1;
  55. }
  56. if (unveil(nullptr, nullptr) < 0) {
  57. perror("unveil");
  58. return 1;
  59. }
  60. auto app_icon = GUI::Icon::default_icon("app-theme-editor");
  61. Gfx::Palette preview_palette = app->palette();
  62. auto window = GUI::Window::construct();
  63. auto& main_widget = window->set_main_widget<GUI::Widget>();
  64. main_widget.set_fill_with_background_color(true);
  65. main_widget.set_layout<GUI::VerticalBoxLayout>();
  66. auto& preview_widget = main_widget.add<ThemeEditor::PreviewWidget>(app->palette());
  67. preview_widget.set_fixed_size(480, 360);
  68. auto& horizontal_container = main_widget.add<GUI::Widget>();
  69. horizontal_container.set_layout<GUI::HorizontalBoxLayout>();
  70. horizontal_container.set_fixed_size(480, 20);
  71. auto& combo_box = horizontal_container.add<GUI::ComboBox>();
  72. auto& color_input = horizontal_container.add<GUI::ColorInput>();
  73. Vector<Gfx::ColorRole> color_roles;
  74. #define __ENUMERATE_COLOR_ROLE(role) color_roles.append(Gfx::ColorRole::role);
  75. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  76. #undef __ENUMERATE_COLOR_ROLE
  77. combo_box.set_only_allow_values_from_model(true);
  78. combo_box.set_model(adopt_ref(*new ColorRoleModel(color_roles)));
  79. combo_box.on_change = [&](auto&, auto& index) {
  80. auto role = static_cast<const ColorRoleModel*>(index.model())->color_role(index);
  81. color_input.set_color(preview_palette.color(role));
  82. };
  83. combo_box.set_selected_index((size_t)Gfx::ColorRole::Window - 1);
  84. color_input.on_change = [&] {
  85. auto role = static_cast<const ColorRoleModel*>(combo_box.model())->color_role(combo_box.selected_index());
  86. preview_palette.set_color(role, color_input.color());
  87. preview_widget.set_preview_palette(preview_palette);
  88. };
  89. window->resize(480, 500);
  90. window->show();
  91. window->set_title("Theme Editor");
  92. window->set_icon(app_icon.bitmap_for_size(16));
  93. return app->exec();
  94. }