Gradient.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/System.h>
  7. #include <LibDesktop/Screensaver.h>
  8. #include <LibGUI/Application.h>
  9. #include <LibGUI/Event.h>
  10. #include <LibGUI/Icon.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/Window.h>
  13. #include <LibGfx/Bitmap.h>
  14. #include <LibMain/Main.h>
  15. #include <stdio.h>
  16. #include <time.h>
  17. #include <unistd.h>
  18. class Gradient final : public Desktop::Screensaver {
  19. C_OBJECT(Gradient)
  20. public:
  21. virtual ~Gradient() override = default;
  22. private:
  23. Gradient(int width = 64, int height = 48, int interval = 10000);
  24. RefPtr<Gfx::Bitmap> m_bitmap;
  25. void draw();
  26. virtual void paint_event(GUI::PaintEvent&) override;
  27. virtual void timer_event(Core::TimerEvent&) override;
  28. };
  29. Gradient::Gradient(int width, int height, int interval)
  30. {
  31. on_screensaver_exit = []() { GUI::Application::the()->quit(); };
  32. m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { width, height }).release_value_but_fixme_should_propagate_errors();
  33. srand(time(nullptr));
  34. stop_timer();
  35. start_timer(interval);
  36. draw();
  37. }
  38. void Gradient::paint_event(GUI::PaintEvent& event)
  39. {
  40. GUI::Painter painter(*this);
  41. painter.add_clip_rect(event.rect());
  42. painter.draw_scaled_bitmap(rect(), *m_bitmap, m_bitmap->rect());
  43. }
  44. void Gradient::timer_event(Core::TimerEvent&)
  45. {
  46. draw();
  47. update();
  48. }
  49. void Gradient::draw()
  50. {
  51. Color const colors[] {
  52. Color::Blue,
  53. Color::Cyan,
  54. Color::Green,
  55. Color::Magenta,
  56. Color::Red,
  57. Color::Yellow,
  58. };
  59. Orientation const orientations[] {
  60. Gfx::Orientation::Horizontal,
  61. Gfx::Orientation::Vertical
  62. };
  63. int start_color_index = 0;
  64. int end_color_index = 0;
  65. while (start_color_index == end_color_index) {
  66. start_color_index = rand() % (sizeof(colors) / sizeof(colors[0]));
  67. end_color_index = rand() % (sizeof(colors) / sizeof(colors[0]));
  68. }
  69. GUI::Painter painter(*m_bitmap);
  70. painter.fill_rect_with_gradient(
  71. orientations[rand() % (sizeof(orientations) / sizeof(orientations[0]))],
  72. m_bitmap->rect(),
  73. colors[start_color_index],
  74. colors[end_color_index]);
  75. }
  76. ErrorOr<int> serenity_main(Main::Arguments arguments)
  77. {
  78. TRY(Core::System::pledge("stdio rpath recvfd sendfd unix"));
  79. auto app = TRY(GUI::Application::create(arguments));
  80. TRY(Core::System::pledge("stdio rpath recvfd sendfd"));
  81. TRY(Core::System::unveil("/res", "r"));
  82. TRY(Core::System::unveil(nullptr, nullptr));
  83. auto window = TRY(Desktop::Screensaver::create_window("Gradient"sv, "app-gradient"sv));
  84. auto gradient_widget = window->set_main_widget<Gradient>(64, 48, 10000);
  85. gradient_widget->set_fill_with_background_color(false);
  86. gradient_widget->set_override_cursor(Gfx::StandardCursor::Hidden);
  87. gradient_widget->update();
  88. window->show();
  89. window->move_to_front();
  90. window->set_cursor(Gfx::StandardCursor::Hidden);
  91. window->update();
  92. return app->exec();
  93. }