MainWidget.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, networkException <networkexception@serenityos.org>
  4. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2021, Antonio Di Stefano <tonio9681@gmail.com>
  6. * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include "MainWidget.h"
  11. #include <Applications/ThemeEditor/AlignmentPropertyGML.h>
  12. #include <Applications/ThemeEditor/ColorPropertyGML.h>
  13. #include <Applications/ThemeEditor/FlagPropertyGML.h>
  14. #include <Applications/ThemeEditor/MetricPropertyGML.h>
  15. #include <Applications/ThemeEditor/PathPropertyGML.h>
  16. #include <Applications/ThemeEditor/ThemeEditorGML.h>
  17. #include <LibFileSystemAccessClient/Client.h>
  18. #include <LibGUI/ActionGroup.h>
  19. #include <LibGUI/BoxLayout.h>
  20. #include <LibGUI/Button.h>
  21. #include <LibGUI/ConnectionToWindowServer.h>
  22. #include <LibGUI/FilePicker.h>
  23. #include <LibGUI/Frame.h>
  24. #include <LibGUI/GroupBox.h>
  25. #include <LibGUI/Icon.h>
  26. #include <LibGUI/ItemListModel.h>
  27. #include <LibGUI/Label.h>
  28. #include <LibGUI/Menu.h>
  29. #include <LibGUI/Menubar.h>
  30. #include <LibGUI/MessageBox.h>
  31. #include <LibGUI/ScrollableContainerWidget.h>
  32. #include <LibGfx/Filters/ColorBlindnessFilter.h>
  33. namespace ThemeEditor {
  34. static const PropertyTab window_tab {
  35. "Windows",
  36. {
  37. { "General",
  38. { { Gfx::FlagRole::IsDark },
  39. { Gfx::AlignmentRole::TitleAlignment },
  40. { Gfx::MetricRole::TitleHeight },
  41. { Gfx::MetricRole::TitleButtonWidth },
  42. { Gfx::MetricRole::TitleButtonHeight },
  43. { Gfx::PathRole::TitleButtonIcons },
  44. { Gfx::FlagRole::TitleButtonsIconOnly } } },
  45. { "Border",
  46. { { Gfx::MetricRole::BorderThickness },
  47. { Gfx::MetricRole::BorderRadius } } },
  48. { "Active Window",
  49. { { Gfx::ColorRole::ActiveWindowBorder1 },
  50. { Gfx::ColorRole::ActiveWindowBorder2 },
  51. { Gfx::ColorRole::ActiveWindowTitle },
  52. { Gfx::ColorRole::ActiveWindowTitleShadow },
  53. { Gfx::ColorRole::ActiveWindowTitleStripes },
  54. { Gfx::PathRole::ActiveWindowShadow } } },
  55. { "Inactive Window",
  56. { { Gfx::ColorRole::InactiveWindowBorder1 },
  57. { Gfx::ColorRole::InactiveWindowBorder2 },
  58. { Gfx::ColorRole::InactiveWindowTitle },
  59. { Gfx::ColorRole::InactiveWindowTitleShadow },
  60. { Gfx::ColorRole::InactiveWindowTitleStripes },
  61. { Gfx::PathRole::InactiveWindowShadow } } },
  62. { "Highlighted Window",
  63. { { Gfx::ColorRole::HighlightWindowBorder1 },
  64. { Gfx::ColorRole::HighlightWindowBorder2 },
  65. { Gfx::ColorRole::HighlightWindowTitle },
  66. { Gfx::ColorRole::HighlightWindowTitleShadow },
  67. { Gfx::ColorRole::HighlightWindowTitleStripes } } },
  68. { "Moving Window",
  69. { { Gfx::ColorRole::MovingWindowBorder1 },
  70. { Gfx::ColorRole::MovingWindowBorder2 },
  71. { Gfx::ColorRole::MovingWindowTitle },
  72. { Gfx::ColorRole::MovingWindowTitleShadow },
  73. { Gfx::ColorRole::MovingWindowTitleStripes } } },
  74. { "Contents",
  75. { { Gfx::ColorRole::Window },
  76. { Gfx::ColorRole::WindowText } } },
  77. { "Desktop",
  78. { { Gfx::ColorRole::DesktopBackground },
  79. { Gfx::PathRole::TaskbarShadow } } },
  80. }
  81. };
  82. static const PropertyTab widgets_tab {
  83. "Widgets",
  84. {
  85. { "General",
  86. { { Gfx::ColorRole::Accent },
  87. { Gfx::ColorRole::Base },
  88. { Gfx::ColorRole::ThreedHighlight },
  89. { Gfx::ColorRole::ThreedShadow1 },
  90. { Gfx::ColorRole::ThreedShadow2 },
  91. { Gfx::ColorRole::HoverHighlight } } },
  92. { "Text",
  93. { { Gfx::ColorRole::BaseText },
  94. { Gfx::ColorRole::DisabledTextFront },
  95. { Gfx::ColorRole::DisabledTextBack },
  96. { Gfx::ColorRole::PlaceholderText } } },
  97. { "Links",
  98. { { Gfx::ColorRole::Link },
  99. { Gfx::ColorRole::ActiveLink },
  100. { Gfx::ColorRole::VisitedLink } } },
  101. { "Buttons",
  102. { { Gfx::ColorRole::Button },
  103. { Gfx::ColorRole::ButtonText } } },
  104. { "Tooltips",
  105. { { Gfx::ColorRole::Tooltip },
  106. { Gfx::ColorRole::TooltipText },
  107. { Gfx::PathRole::TooltipShadow } } },
  108. { "Trays",
  109. { { Gfx::ColorRole::Tray },
  110. { Gfx::ColorRole::TrayText } } },
  111. { "Ruler",
  112. { { Gfx::ColorRole::Ruler },
  113. { Gfx::ColorRole::RulerBorder },
  114. { Gfx::ColorRole::RulerActiveText },
  115. { Gfx::ColorRole::RulerInactiveText } } },
  116. { "Gutter",
  117. { { Gfx::ColorRole::Gutter },
  118. { Gfx::ColorRole::GutterBorder } } },
  119. { "Rubber Band",
  120. { { Gfx::ColorRole::RubberBandBorder },
  121. { Gfx::ColorRole::RubberBandFill } } },
  122. { "Menus",
  123. { { Gfx::ColorRole::MenuBase },
  124. { Gfx::ColorRole::MenuBaseText },
  125. { Gfx::ColorRole::MenuSelection },
  126. { Gfx::ColorRole::MenuSelectionText },
  127. { Gfx::ColorRole::MenuStripe },
  128. { Gfx::PathRole::MenuShadow } } },
  129. { "Selection",
  130. { { Gfx::ColorRole::FocusOutline },
  131. { Gfx::ColorRole::TextCursor },
  132. { Gfx::ColorRole::Selection },
  133. { Gfx::ColorRole::SelectionText },
  134. { Gfx::ColorRole::InactiveSelection },
  135. { Gfx::ColorRole::InactiveSelectionText },
  136. { Gfx::ColorRole::HighlightSearching },
  137. { Gfx::ColorRole::HighlightSearchingText } } },
  138. }
  139. };
  140. static const PropertyTab syntax_highlighting_tab {
  141. "Syntax Highlighting",
  142. {
  143. { "General",
  144. { { Gfx::ColorRole::SyntaxComment },
  145. { Gfx::ColorRole::SyntaxControlKeyword },
  146. { Gfx::ColorRole::SyntaxIdentifier },
  147. { Gfx::ColorRole::SyntaxKeyword },
  148. { Gfx::ColorRole::SyntaxNumber },
  149. { Gfx::ColorRole::SyntaxOperator },
  150. { Gfx::ColorRole::SyntaxPreprocessorStatement },
  151. { Gfx::ColorRole::SyntaxPreprocessorValue },
  152. { Gfx::ColorRole::SyntaxPunctuation },
  153. { Gfx::ColorRole::SyntaxString },
  154. { Gfx::ColorRole::SyntaxType },
  155. { Gfx::ColorRole::SyntaxFunction },
  156. { Gfx::ColorRole::SyntaxVariable },
  157. { Gfx::ColorRole::SyntaxCustomType },
  158. { Gfx::ColorRole::SyntaxNamespace },
  159. { Gfx::ColorRole::SyntaxMember },
  160. { Gfx::ColorRole::SyntaxParameter } } },
  161. }
  162. };
  163. static const PropertyTab color_scheme_tab {
  164. "Color Scheme",
  165. {
  166. { "General",
  167. { { Gfx::ColorRole::Black },
  168. { Gfx::ColorRole::Red },
  169. { Gfx::ColorRole::Green },
  170. { Gfx::ColorRole::Yellow },
  171. { Gfx::ColorRole::Blue },
  172. { Gfx::ColorRole::Magenta },
  173. { Gfx::ColorRole::Cyan },
  174. { Gfx::ColorRole::White },
  175. { Gfx::ColorRole::BrightBlack },
  176. { Gfx::ColorRole::BrightRed },
  177. { Gfx::ColorRole::BrightGreen },
  178. { Gfx::ColorRole::BrightYellow },
  179. { Gfx::ColorRole::BrightBlue },
  180. { Gfx::ColorRole::BrightMagenta },
  181. { Gfx::ColorRole::BrightCyan },
  182. { Gfx::ColorRole::BrightWhite } } },
  183. }
  184. };
  185. ErrorOr<NonnullRefPtr<MainWidget>> MainWidget::try_create()
  186. {
  187. auto alignment_model = TRY(AlignmentModel::try_create());
  188. auto main_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MainWidget(move(alignment_model))));
  189. TRY(main_widget->load_from_gml(theme_editor_gml));
  190. main_widget->m_preview_widget = main_widget->find_descendant_of_type_named<ThemeEditor::PreviewWidget>("preview_widget");
  191. main_widget->m_property_tabs = main_widget->find_descendant_of_type_named<GUI::TabWidget>("property_tabs");
  192. TRY(main_widget->add_property_tab(window_tab));
  193. TRY(main_widget->add_property_tab(widgets_tab));
  194. TRY(main_widget->add_property_tab(syntax_highlighting_tab));
  195. TRY(main_widget->add_property_tab(color_scheme_tab));
  196. main_widget->build_override_controls();
  197. return main_widget;
  198. }
  199. MainWidget::MainWidget(NonnullRefPtr<AlignmentModel> alignment_model)
  200. : m_current_palette(GUI::Application::the()->palette())
  201. , m_alignment_model(move(alignment_model))
  202. {
  203. }
  204. ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
  205. {
  206. auto file_menu = TRY(window.try_add_menu("&File"));
  207. TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) {
  208. if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
  209. return;
  210. auto response = FileSystemAccessClient::Client::the().open_file(&window, "Select theme file", "/res/themes"sv);
  211. if (response.is_error())
  212. return;
  213. auto load_from_file_result = load_from_file(response.value().filename(), response.value().release_stream());
  214. if (load_from_file_result.is_error()) {
  215. GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Can't open file named {}: {}", response.value().filename(), load_from_file_result.error()));
  216. return;
  217. }
  218. })));
  219. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  220. if (m_path.has_value()) {
  221. auto result = FileSystemAccessClient::Client::the().request_file(&window, *m_path, Core::Stream::OpenMode::ReadWrite | Core::Stream::OpenMode::Truncate);
  222. if (result.is_error())
  223. return;
  224. save_to_file(result.value().filename(), result.value().release_stream());
  225. } else {
  226. auto result = FileSystemAccessClient::Client::the().save_file(&window, "Theme", "ini", Core::Stream::OpenMode::ReadWrite | Core::Stream::OpenMode::Truncate);
  227. if (result.is_error())
  228. return;
  229. save_to_file(result.value().filename(), result.value().release_stream());
  230. }
  231. });
  232. TRY(file_menu->try_add_action(*m_save_action));
  233. TRY(file_menu->try_add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
  234. auto result = FileSystemAccessClient::Client::the().save_file(&window, "Theme", "ini", Core::Stream::OpenMode::ReadWrite | Core::Stream::OpenMode::Truncate);
  235. if (result.is_error())
  236. return;
  237. save_to_file(result.value().filename(), result.value().release_stream());
  238. })));
  239. TRY(file_menu->try_add_separator());
  240. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  241. if (request_close() == GUI::Window::CloseRequestDecision::Close)
  242. GUI::Application::the()->quit();
  243. })));
  244. TRY(window.try_add_menu(TRY(GUI::CommonMenus::make_accessibility_menu(*m_preview_widget))));
  245. auto help_menu = TRY(window.try_add_menu("&Help"));
  246. TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(&window)));
  247. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Theme Editor", GUI::Icon::default_icon("app-theme-editor"sv), &window)));
  248. return {};
  249. }
  250. void MainWidget::update_title()
  251. {
  252. window()->set_title(DeprecatedString::formatted("{}[*] - Theme Editor", m_path.value_or("Untitled")));
  253. }
  254. GUI::Window::CloseRequestDecision MainWidget::request_close()
  255. {
  256. if (!window()->is_modified())
  257. return GUI::Window::CloseRequestDecision::Close;
  258. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path.value_or(""), m_last_modified_time);
  259. if (result == GUI::MessageBox::ExecResult::Yes) {
  260. m_save_action->activate();
  261. if (window()->is_modified())
  262. return GUI::Window::CloseRequestDecision::StayOpen;
  263. return GUI::Window::CloseRequestDecision::Close;
  264. }
  265. if (result == GUI::MessageBox::ExecResult::No)
  266. return GUI::Window::CloseRequestDecision::Close;
  267. return GUI::Window::CloseRequestDecision::StayOpen;
  268. }
  269. void MainWidget::set_path(DeprecatedString path)
  270. {
  271. m_path = path;
  272. update_title();
  273. }
  274. void MainWidget::save_to_file(String const& filename, NonnullOwnPtr<Core::Stream::File> file)
  275. {
  276. auto theme = Core::ConfigFile::open(filename.to_deprecated_string(), move(file)).release_value_but_fixme_should_propagate_errors();
  277. #define __ENUMERATE_ALIGNMENT_ROLE(role) theme->write_entry("Alignments", to_string(Gfx::AlignmentRole::role), to_string(m_current_palette.alignment(Gfx::AlignmentRole::role)));
  278. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  279. #undef __ENUMERATE_ALIGNMENT_ROLE
  280. #define __ENUMERATE_COLOR_ROLE(role) theme->write_entry("Colors", to_string(Gfx::ColorRole::role), m_current_palette.color(Gfx::ColorRole::role).to_deprecated_string());
  281. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  282. #undef __ENUMERATE_COLOR_ROLE
  283. #define __ENUMERATE_FLAG_ROLE(role) theme->write_bool_entry("Flags", to_string(Gfx::FlagRole::role), m_current_palette.flag(Gfx::FlagRole::role));
  284. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  285. #undef __ENUMERATE_FLAG_ROLE
  286. #define __ENUMERATE_METRIC_ROLE(role) theme->write_num_entry("Metrics", to_string(Gfx::MetricRole::role), m_current_palette.metric(Gfx::MetricRole::role));
  287. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  288. #undef __ENUMERATE_METRIC_ROLE
  289. #define __ENUMERATE_PATH_ROLE(role) theme->write_entry("Paths", to_string(Gfx::PathRole::role), m_current_palette.path(Gfx::PathRole::role));
  290. ENUMERATE_PATH_ROLES(__ENUMERATE_PATH_ROLE)
  291. #undef __ENUMERATE_PATH_ROLE
  292. auto sync_result = theme->sync();
  293. if (sync_result.is_error()) {
  294. GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Failed to save theme file: {}", sync_result.error()));
  295. } else {
  296. m_last_modified_time = Time::now_monotonic();
  297. set_path(filename.to_deprecated_string());
  298. window()->set_modified(false);
  299. }
  300. }
  301. ErrorOr<Core::AnonymousBuffer> MainWidget::encode()
  302. {
  303. auto buffer = TRY(Core::AnonymousBuffer::create_with_size(sizeof(Gfx::SystemTheme)));
  304. auto* data = buffer.data<Gfx::SystemTheme>();
  305. #define __ENUMERATE_ALIGNMENT_ROLE(role) \
  306. data->alignment[(int)Gfx::AlignmentRole::role] = m_current_palette.alignment(Gfx::AlignmentRole::role);
  307. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  308. #undef __ENUMERATE_ALIGNMENT_ROLE
  309. #define __ENUMERATE_COLOR_ROLE(role) \
  310. data->color[(int)Gfx::ColorRole::role] = m_current_palette.color(Gfx::ColorRole::role).value();
  311. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  312. #undef __ENUMERATE_COLOR_ROLE
  313. #define __ENUMERATE_FLAG_ROLE(role) \
  314. data->flag[(int)Gfx::FlagRole::role] = m_current_palette.flag(Gfx::FlagRole::role);
  315. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  316. #undef __ENUMERATE_FLAG_ROLE
  317. #define __ENUMERATE_METRIC_ROLE(role) \
  318. data->metric[(int)Gfx::MetricRole::role] = m_current_palette.metric(Gfx::MetricRole::role);
  319. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  320. #undef __ENUMERATE_METRIC_ROLE
  321. #define ENCODE_PATH(role, allow_empty) \
  322. do { \
  323. auto path = m_current_palette.path(Gfx::PathRole::role); \
  324. char const* characters; \
  325. if (path.is_empty()) { \
  326. switch (Gfx::PathRole::role) { \
  327. case Gfx::PathRole::TitleButtonIcons: \
  328. characters = "/res/icons/16x16/"; \
  329. break; \
  330. default: \
  331. characters = allow_empty ? "" : "/res/"; \
  332. } \
  333. } \
  334. characters = path.characters(); \
  335. memcpy(data->path[(int)Gfx::PathRole::role], characters, min(strlen(characters) + 1, sizeof(data->path[(int)Gfx::PathRole::role]))); \
  336. data->path[(int)Gfx::PathRole::role][sizeof(data->path[(int)Gfx::PathRole::role]) - 1] = '\0'; \
  337. } while (0)
  338. ENCODE_PATH(TitleButtonIcons, false);
  339. ENCODE_PATH(ActiveWindowShadow, true);
  340. ENCODE_PATH(InactiveWindowShadow, true);
  341. ENCODE_PATH(TaskbarShadow, true);
  342. ENCODE_PATH(MenuShadow, true);
  343. ENCODE_PATH(TooltipShadow, true);
  344. return buffer;
  345. }
  346. void MainWidget::build_override_controls()
  347. {
  348. auto* theme_override_controls = find_descendant_of_type_named<GUI::Widget>("theme_override_controls");
  349. m_theme_override_apply = theme_override_controls->find_child_of_type_named<GUI::DialogButton>("apply_button");
  350. m_theme_override_reset = theme_override_controls->find_child_of_type_named<GUI::DialogButton>("reset_button");
  351. m_theme_override_apply->on_click = [&](auto) {
  352. auto encoded = encode();
  353. if (encoded.is_error())
  354. return;
  355. GUI::ConnectionToWindowServer::the().async_set_system_theme_override(encoded.value());
  356. };
  357. m_theme_override_reset->on_click = [&](auto) {
  358. GUI::ConnectionToWindowServer::the().async_clear_system_theme_override();
  359. };
  360. GUI::Application::the()->on_theme_change = [&]() {
  361. auto override_active = GUI::ConnectionToWindowServer::the().is_system_theme_overridden();
  362. m_theme_override_apply->set_enabled(!override_active && window()->is_modified());
  363. m_theme_override_reset->set_enabled(override_active);
  364. };
  365. }
  366. ErrorOr<void> MainWidget::add_property_tab(PropertyTab const& property_tab)
  367. {
  368. auto scrollable_container = TRY(m_property_tabs->try_add_tab<GUI::ScrollableContainerWidget>(property_tab.title));
  369. scrollable_container->set_should_hide_unnecessary_scrollbars(true);
  370. auto properties_list = TRY(GUI::Widget::try_create());
  371. scrollable_container->set_widget(properties_list);
  372. (void)TRY(properties_list->try_set_layout<GUI::VerticalBoxLayout>());
  373. properties_list->layout()->set_spacing(12);
  374. properties_list->layout()->set_margins({ 8 });
  375. for (auto const& group : property_tab.property_groups) {
  376. NonnullRefPtr<GUI::GroupBox> group_box = TRY(properties_list->try_add<GUI::GroupBox>(group.title));
  377. (void)TRY(group_box->try_set_layout<GUI::VerticalBoxLayout>());
  378. group_box->layout()->set_spacing(12);
  379. // 1px less on the left makes the text line up with the group title.
  380. group_box->layout()->set_margins({ 8, 8, 8, 7 });
  381. group_box->set_preferred_height(GUI::SpecialDimension::Fit);
  382. for (auto const& property : group.properties) {
  383. NonnullRefPtr<GUI::Widget> row_widget = TRY(group_box->try_add<GUI::Widget>());
  384. row_widget->set_fixed_height(22);
  385. TRY(property.role.visit(
  386. [&](Gfx::AlignmentRole role) -> ErrorOr<void> {
  387. TRY(row_widget->load_from_gml(alignment_property_gml));
  388. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  389. name_label.set_text(to_string(role));
  390. auto& alignment_picker = *row_widget->find_descendant_of_type_named<GUI::ComboBox>("combo_box");
  391. alignment_picker.set_model(*m_alignment_model);
  392. alignment_picker.on_change = [&, role](auto&, auto& index) {
  393. set_alignment(role, index.data(GUI::ModelRole::Custom).to_text_alignment(Gfx::TextAlignment::CenterLeft));
  394. };
  395. alignment_picker.set_selected_index(m_alignment_model->index_of(m_current_palette.alignment(role)), GUI::AllowCallback::No);
  396. VERIFY(m_alignment_inputs[to_underlying(role)].is_null());
  397. m_alignment_inputs[to_underlying(role)] = alignment_picker;
  398. return {};
  399. },
  400. [&](Gfx::ColorRole role) -> ErrorOr<void> {
  401. TRY(row_widget->load_from_gml(color_property_gml));
  402. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  403. name_label.set_text(to_string(role));
  404. auto& color_input = *row_widget->find_descendant_of_type_named<GUI::ColorInput>("color_input");
  405. color_input.on_change = [&, role] {
  406. set_color(role, color_input.color());
  407. };
  408. color_input.set_color(m_current_palette.color(role), GUI::AllowCallback::No);
  409. VERIFY(m_color_inputs[to_underlying(role)].is_null());
  410. m_color_inputs[to_underlying(role)] = color_input;
  411. return {};
  412. },
  413. [&](Gfx::FlagRole role) -> ErrorOr<void> {
  414. TRY(row_widget->load_from_gml(flag_property_gml));
  415. auto& checkbox = *row_widget->find_descendant_of_type_named<GUI::CheckBox>("checkbox");
  416. checkbox.set_text(String::from_deprecated_string(DeprecatedString(to_string(role))).release_value_but_fixme_should_propagate_errors());
  417. checkbox.on_checked = [&, role](bool checked) {
  418. set_flag(role, checked);
  419. };
  420. checkbox.set_checked(m_current_palette.flag(role), GUI::AllowCallback::No);
  421. VERIFY(m_flag_inputs[to_underlying(role)].is_null());
  422. m_flag_inputs[to_underlying(role)] = checkbox;
  423. return {};
  424. },
  425. [&](Gfx::MetricRole role) -> ErrorOr<void> {
  426. TRY(row_widget->load_from_gml(metric_property_gml));
  427. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  428. name_label.set_text(to_string(role));
  429. auto& spin_box = *row_widget->find_descendant_of_type_named<GUI::SpinBox>("spin_box");
  430. spin_box.on_change = [&, role](int value) {
  431. set_metric(role, value);
  432. };
  433. spin_box.set_value(m_current_palette.metric(role), GUI::AllowCallback::No);
  434. VERIFY(m_metric_inputs[to_underlying(role)].is_null());
  435. m_metric_inputs[to_underlying(role)] = spin_box;
  436. return {};
  437. },
  438. [&](Gfx::PathRole role) -> ErrorOr<void> {
  439. TRY(row_widget->load_from_gml(path_property_gml));
  440. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  441. name_label.set_text(to_string(role));
  442. auto& path_input = *row_widget->find_descendant_of_type_named<GUI::TextBox>("path_input");
  443. path_input.on_change = [&, role] {
  444. set_path(role, path_input.text());
  445. };
  446. path_input.set_text(m_current_palette.path(role), GUI::AllowCallback::No);
  447. auto& path_picker_button = *row_widget->find_descendant_of_type_named<GUI::Button>("path_picker_button");
  448. auto picker_target = (role == Gfx::PathRole::TitleButtonIcons) ? PathPickerTarget::Folder : PathPickerTarget::File;
  449. path_picker_button.on_click = [&, role, picker_target](auto) {
  450. show_path_picker_dialog(to_string(role), path_input, picker_target);
  451. };
  452. VERIFY(m_path_inputs[to_underlying(role)].is_null());
  453. m_path_inputs[to_underlying(role)] = path_input;
  454. return {};
  455. }));
  456. }
  457. }
  458. return {};
  459. }
  460. void MainWidget::set_alignment(Gfx::AlignmentRole role, Gfx::TextAlignment value)
  461. {
  462. auto preview_palette = m_current_palette;
  463. preview_palette.set_alignment(role, value);
  464. set_palette(preview_palette);
  465. }
  466. void MainWidget::set_color(Gfx::ColorRole role, Gfx::Color value)
  467. {
  468. auto preview_palette = m_current_palette;
  469. preview_palette.set_color(role, value);
  470. set_palette(preview_palette);
  471. }
  472. void MainWidget::set_flag(Gfx::FlagRole role, bool value)
  473. {
  474. auto preview_palette = m_current_palette;
  475. preview_palette.set_flag(role, value);
  476. set_palette(preview_palette);
  477. }
  478. void MainWidget::set_metric(Gfx::MetricRole role, int value)
  479. {
  480. auto preview_palette = m_current_palette;
  481. preview_palette.set_metric(role, value);
  482. set_palette(preview_palette);
  483. }
  484. void MainWidget::set_path(Gfx::PathRole role, DeprecatedString value)
  485. {
  486. auto preview_palette = m_current_palette;
  487. preview_palette.set_path(role, value);
  488. set_palette(preview_palette);
  489. }
  490. void MainWidget::set_palette(Gfx::Palette palette)
  491. {
  492. m_current_palette = move(palette);
  493. m_preview_widget->set_preview_palette(m_current_palette);
  494. m_theme_override_apply->set_enabled(true);
  495. window()->set_modified(true);
  496. }
  497. void MainWidget::show_path_picker_dialog(StringView property_display_name, GUI::TextBox& path_input, PathPickerTarget path_picker_target)
  498. {
  499. bool open_folder = path_picker_target == PathPickerTarget::Folder;
  500. auto window_title = DeprecatedString::formatted(open_folder ? "Select {} folder"sv : "Select {} file"sv, property_display_name);
  501. auto target_path = path_input.text();
  502. if (Core::File::exists(target_path)) {
  503. if (!Core::File::is_directory(target_path))
  504. target_path = LexicalPath::dirname(target_path);
  505. } else {
  506. target_path = "/res/icons";
  507. }
  508. auto result = GUI::FilePicker::get_open_filepath(window(), window_title, target_path, open_folder);
  509. if (!result.has_value())
  510. return;
  511. path_input.set_text(*result);
  512. }
  513. ErrorOr<void> MainWidget::load_from_file(String const& filename, NonnullOwnPtr<Core::Stream::File> file)
  514. {
  515. auto config_file = TRY(Core::ConfigFile::open(filename.to_deprecated_string(), move(file)));
  516. auto theme = TRY(Gfx::load_system_theme(config_file));
  517. VERIFY(theme.is_valid());
  518. auto new_palette = Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(theme));
  519. set_palette(move(new_palette));
  520. set_path(filename.to_deprecated_string());
  521. #define __ENUMERATE_ALIGNMENT_ROLE(role) \
  522. if (auto alignment_input = m_alignment_inputs[to_underlying(Gfx::AlignmentRole::role)]) \
  523. alignment_input->set_selected_index(m_alignment_model->index_of(m_current_palette.alignment(Gfx::AlignmentRole::role)), GUI::AllowCallback::No);
  524. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  525. #undef __ENUMERATE_ALIGNMENT_ROLE
  526. #define __ENUMERATE_COLOR_ROLE(role) \
  527. if (auto color_input = m_color_inputs[to_underlying(Gfx::ColorRole::role)]) \
  528. color_input->set_color(m_current_palette.color(Gfx::ColorRole::role), GUI::AllowCallback::No);
  529. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  530. #undef __ENUMERATE_COLOR_ROLE
  531. #define __ENUMERATE_FLAG_ROLE(role) \
  532. if (auto flag_input = m_flag_inputs[to_underlying(Gfx::FlagRole::role)]) \
  533. flag_input->set_checked(m_current_palette.flag(Gfx::FlagRole::role), GUI::AllowCallback::No);
  534. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  535. #undef __ENUMERATE_FLAG_ROLE
  536. #define __ENUMERATE_METRIC_ROLE(role) \
  537. if (auto metric_input = m_metric_inputs[to_underlying(Gfx::MetricRole::role)]) \
  538. metric_input->set_value(m_current_palette.metric(Gfx::MetricRole::role), GUI::AllowCallback::No);
  539. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  540. #undef __ENUMERATE_METRIC_ROLE
  541. #define __ENUMERATE_PATH_ROLE(role) \
  542. if (auto path_input = m_path_inputs[to_underlying(Gfx::PathRole::role)]) \
  543. path_input->set_text(m_current_palette.path(Gfx::PathRole::role), GUI::AllowCallback::No);
  544. ENUMERATE_PATH_ROLES(__ENUMERATE_PATH_ROLE)
  545. #undef __ENUMERATE_PATH_ROLE
  546. m_last_modified_time = Time::now_monotonic();
  547. window()->set_modified(false);
  548. return {};
  549. }
  550. void MainWidget::drag_enter_event(GUI::DragEvent& event)
  551. {
  552. auto const& mime_types = event.mime_types();
  553. if (mime_types.contains_slow("text/uri-list"))
  554. event.accept();
  555. }
  556. void MainWidget::drop_event(GUI::DropEvent& event)
  557. {
  558. event.accept();
  559. window()->move_to_front();
  560. if (event.mime_data().has_urls()) {
  561. auto urls = event.mime_data().urls();
  562. if (urls.is_empty())
  563. return;
  564. if (urls.size() > 1) {
  565. GUI::MessageBox::show(window(), "ThemeEditor can only open one file at a time!"sv, "One at a time please!"sv, GUI::MessageBox::Type::Error);
  566. return;
  567. }
  568. if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
  569. return;
  570. auto response = FileSystemAccessClient::Client::the().request_file(window(), urls.first().path(), Core::Stream::OpenMode::Read);
  571. if (response.is_error())
  572. return;
  573. auto load_from_file_result = load_from_file(response.value().filename(), response.value().release_stream());
  574. if (load_from_file_result.is_error())
  575. GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Can't open file named {}: {}", response.value().filename(), load_from_file_result.error()));
  576. }
  577. }
  578. }