ThemeWidget.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2021-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ThemeWidget.h"
  7. #include <AK/LexicalPath.h>
  8. #include <Applications/MouseSettings/ThemeWidgetGML.h>
  9. #include <LibCore/DirIterator.h>
  10. #include <LibGUI/Button.h>
  11. #include <LibGUI/ComboBox.h>
  12. #include <LibGUI/ConnectionToWindowServer.h>
  13. #include <LibGUI/SortingProxyModel.h>
  14. #include <LibGUI/TableView.h>
  15. String MouseCursorModel::column_name(int column_index) const
  16. {
  17. switch (column_index) {
  18. case Column::Bitmap:
  19. return {};
  20. case Column::Name:
  21. return "Name";
  22. }
  23. VERIFY_NOT_REACHED();
  24. }
  25. GUI::Variant MouseCursorModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
  26. {
  27. auto& cursor = m_cursors[index.row()];
  28. if (role == GUI::ModelRole::Display) {
  29. switch (index.column()) {
  30. case Column::Bitmap:
  31. if (!cursor.bitmap)
  32. return {};
  33. return *cursor.bitmap;
  34. case Column::Name:
  35. return cursor.name;
  36. }
  37. VERIFY_NOT_REACHED();
  38. }
  39. return {};
  40. }
  41. void MouseCursorModel::invalidate()
  42. {
  43. if (m_theme_name.is_empty())
  44. return;
  45. m_cursors.clear();
  46. Core::DirIterator iterator(String::formatted("/res/cursor-themes/{}", m_theme_name), Core::DirIterator::Flags::SkipDots);
  47. while (iterator.has_next()) {
  48. auto path = iterator.next_full_path();
  49. if (path.ends_with(".ini"))
  50. continue;
  51. if (path.contains("2x"))
  52. continue;
  53. Cursor cursor;
  54. cursor.path = move(path);
  55. cursor.name = LexicalPath::basename(cursor.path);
  56. // FIXME: Animated cursor bitmaps
  57. auto cursor_bitmap = Gfx::Bitmap::try_load_from_file(cursor.path).release_value_but_fixme_should_propagate_errors();
  58. auto cursor_bitmap_rect = cursor_bitmap->rect();
  59. cursor.params = Gfx::CursorParams::parse_from_filename(cursor.name, cursor_bitmap_rect.center()).constrained(*cursor_bitmap);
  60. cursor.bitmap = cursor_bitmap->cropped(Gfx::IntRect(Gfx::FloatRect(cursor_bitmap_rect).scaled(1.0 / cursor.params.frames(), 1.0))).release_value_but_fixme_should_propagate_errors();
  61. m_cursors.append(move(cursor));
  62. }
  63. Model::invalidate();
  64. }
  65. GUI::Variant ThemeModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
  66. {
  67. if (role == GUI::ModelRole::Display) {
  68. return m_themes[index.row()];
  69. }
  70. return {};
  71. }
  72. void ThemeModel::invalidate()
  73. {
  74. m_themes.clear();
  75. Core::DirIterator iterator("/res/cursor-themes", Core::DirIterator::Flags::SkipDots);
  76. while (iterator.has_next()) {
  77. auto path = iterator.next_path();
  78. if (access(String::formatted("/res/cursor-themes/{}/Config.ini", path).characters(), R_OK) == 0)
  79. m_themes.append(path);
  80. }
  81. Model::invalidate();
  82. }
  83. ThemeWidget::ThemeWidget()
  84. {
  85. load_from_gml(theme_widget_gml);
  86. m_cursors_tableview = find_descendant_of_type_named<GUI::TableView>("cursors_tableview");
  87. m_cursors_tableview->set_highlight_selected_rows(true);
  88. m_cursors_tableview->set_alternating_row_colors(false);
  89. m_cursors_tableview->set_vertical_padding(16);
  90. m_cursors_tableview->set_column_headers_visible(false);
  91. m_cursors_tableview->set_highlight_key_column(false);
  92. auto mouse_cursor_model = MouseCursorModel::create();
  93. auto sorting_proxy_model = MUST(GUI::SortingProxyModel::create(mouse_cursor_model));
  94. sorting_proxy_model->set_sort_role(GUI::ModelRole::Display);
  95. m_cursors_tableview->set_model(sorting_proxy_model);
  96. m_cursors_tableview->set_key_column_and_sort_order(MouseCursorModel::Column::Name, GUI::SortOrder::Ascending);
  97. m_cursors_tableview->set_column_width(0, 25);
  98. m_cursors_tableview->model()->invalidate();
  99. m_theme_name = GUI::ConnectionToWindowServer::the().get_cursor_theme();
  100. mouse_cursor_model->change_theme(m_theme_name);
  101. m_theme_name_box = find_descendant_of_type_named<GUI::ComboBox>("theme_name_box");
  102. m_theme_name_box->on_change = [this, mouse_cursor_model](String const& value, GUI::ModelIndex const&) mutable {
  103. m_theme_name = value;
  104. mouse_cursor_model->change_theme(m_theme_name);
  105. };
  106. m_theme_name_box->set_model(ThemeModel::create());
  107. m_theme_name_box->model()->invalidate();
  108. m_theme_name_box->set_text(m_theme_name);
  109. }
  110. void ThemeWidget::apply_settings()
  111. {
  112. GUI::ConnectionToWindowServer::the().async_apply_cursor_theme(m_theme_name_box->text());
  113. }
  114. void ThemeWidget::reset_default_values()
  115. {
  116. m_theme_name_box->set_text("Default");
  117. }