main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <LibCore/System.h>
  8. #include <LibDesktop/AppFile.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Icon.h>
  12. #include <LibGUI/IconView.h>
  13. #include <LibGUI/Menu.h>
  14. #include <LibGUI/Model.h>
  15. #include <LibGUI/Process.h>
  16. #include <LibGUI/Window.h>
  17. #include <LibMain/Main.h>
  18. class ScreensaverAppsModel final : public GUI::Model {
  19. public:
  20. ScreensaverAppsModel()
  21. {
  22. Desktop::AppFile::for_each([&](Desktop::AppFile& app_file) {
  23. if (app_file.category() != "Demos/Screensaver"sv)
  24. return;
  25. m_apps.append(app_file);
  26. });
  27. quick_sort(m_apps, [](auto& a, auto& b) { return a->name() < b->name(); });
  28. }
  29. virtual int row_count(GUI::ModelIndex const&) const override { return static_cast<int>(m_apps.size()); }
  30. virtual int column_count(GUI::ModelIndex const&) const override { return 1; }
  31. virtual GUI::ModelIndex index(int row, int column, GUI::ModelIndex const&) const override
  32. {
  33. if (row < 0 || row >= static_cast<int>(m_apps.size()))
  34. return {};
  35. return create_index(row, column, &m_apps[row]);
  36. }
  37. virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
  38. {
  39. auto const& app = m_apps[index.row()];
  40. if (role == GUI::ModelRole::Icon)
  41. return app->icon();
  42. if (role == GUI::ModelRole::Display) {
  43. if (app->name().ends_with(" Screensaver"sv))
  44. return app->name().substring(0, app->name().length() - " Screensaver"sv.length());
  45. return app->name();
  46. }
  47. if (role == GUI::ModelRole::Custom)
  48. return app->executable();
  49. return {};
  50. }
  51. private:
  52. Vector<NonnullRefPtr<Desktop::AppFile>> m_apps;
  53. };
  54. ErrorOr<int> serenity_main(Main::Arguments arguments)
  55. {
  56. TRY(Core::System::pledge("stdio thread recvfd sendfd rpath cpath wpath unix proc exec"));
  57. auto app = TRY(GUI::Application::try_create(arguments));
  58. TRY(Core::System::pledge("stdio thread recvfd sendfd rpath cpath wpath proc exec"));
  59. auto app_icon = GUI::Icon::default_icon("app-screensaver"sv);
  60. auto window = TRY(GUI::Window::try_create());
  61. window->set_title("Screensaver");
  62. window->resize(360, 240);
  63. auto file_menu = TRY(window->try_add_menu("&File"_short_string));
  64. file_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  65. app->quit();
  66. }));
  67. auto help_menu = TRY(window->try_add_menu("&Help"_short_string));
  68. TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(window)));
  69. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Screensaver", app_icon, window)));
  70. auto main_widget = TRY(window->set_main_widget<GUI::Widget>());
  71. main_widget->set_fill_with_background_color(true);
  72. main_widget->set_layout<GUI::VerticalBoxLayout>();
  73. auto icon_view = TRY(main_widget->try_add<GUI::IconView>());
  74. icon_view->set_should_hide_unnecessary_scrollbars(true);
  75. auto model = adopt_ref(*new ScreensaverAppsModel);
  76. icon_view->set_model(*model);
  77. icon_view->on_activation = [&](GUI::ModelIndex const& index) {
  78. auto executable = model->data(index, GUI::ModelRole::Custom).as_string();
  79. GUI::Process::spawn_or_show_error(window, executable);
  80. };
  81. window->set_icon(app_icon.bitmap_for_size(16));
  82. window->show();
  83. return app->exec();
  84. }