diff options
author | Jelle Raaijmakers <jelle@gmta.nl> | 2022-12-27 00:40:39 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-12-27 12:38:08 +0100 |
commit | 18b6bdb563dcdb7f69eda9bec8dd3fc4e3845ada (patch) | |
tree | eb645dd0d91ef8aec8cc309b1c6e807cef123edc /Userland/Demos | |
parent | 379e4a2432e3db557c4ffb47a57a08df40f5662f (diff) | |
download | serenity-18b6bdb563dcdb7f69eda9bec8dd3fc4e3845ada.zip |
Demos+LibDesktop: Centralize screensaver logic
We have 3 demos with pretty similar window logic and quitting behavior
on user activity, so centralize that into `Desktop::Screensaver`.
Diffstat (limited to 'Userland/Demos')
-rw-r--r-- | Userland/Demos/Screensaver/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Userland/Demos/Screensaver/Screensaver.cpp | 39 | ||||
-rw-r--r-- | Userland/Demos/Starfield/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Userland/Demos/Starfield/Starfield.cpp | 29 | ||||
-rw-r--r-- | Userland/Demos/Tubes/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Userland/Demos/Tubes/Tubes.cpp | 21 | ||||
-rw-r--r-- | Userland/Demos/Tubes/Tubes.h | 8 | ||||
-rw-r--r-- | Userland/Demos/Tubes/main.cpp | 11 |
8 files changed, 16 insertions, 98 deletions
diff --git a/Userland/Demos/Screensaver/CMakeLists.txt b/Userland/Demos/Screensaver/CMakeLists.txt index 95708fd174..4a1061909f 100644 --- a/Userland/Demos/Screensaver/CMakeLists.txt +++ b/Userland/Demos/Screensaver/CMakeLists.txt @@ -8,4 +8,4 @@ set(SOURCES ) serenity_app(Screensaver ICON app-screensaver) -target_link_libraries(Screensaver PRIVATE LibGUI LibCore LibGfx LibMain) +target_link_libraries(Screensaver PRIVATE LibDesktop LibGUI LibCore LibGfx LibMain) diff --git a/Userland/Demos/Screensaver/Screensaver.cpp b/Userland/Demos/Screensaver/Screensaver.cpp index 0199d40e26..09fe6528ad 100644 --- a/Userland/Demos/Screensaver/Screensaver.cpp +++ b/Userland/Demos/Screensaver/Screensaver.cpp @@ -5,11 +5,11 @@ */ #include <LibCore/System.h> +#include <LibDesktop/Screensaver.h> #include <LibGUI/Application.h> #include <LibGUI/Event.h> #include <LibGUI/Icon.h> #include <LibGUI/Painter.h> -#include <LibGUI/Widget.h> #include <LibGUI/Window.h> #include <LibGfx/Bitmap.h> #include <LibMain/Main.h> @@ -17,7 +17,7 @@ #include <time.h> #include <unistd.h> -class Screensaver final : public GUI::Widget { +class Screensaver final : public Desktop::Screensaver { C_OBJECT(Screensaver) public: virtual ~Screensaver() override = default; @@ -25,18 +25,15 @@ public: private: Screensaver(int width = 64, int height = 48, int interval = 10000); RefPtr<Gfx::Bitmap> m_bitmap; - Gfx::IntPoint m_mouse_origin; void draw(); virtual void paint_event(GUI::PaintEvent&) override; virtual void timer_event(Core::TimerEvent&) override; - virtual void keydown_event(GUI::KeyEvent&) override; - virtual void mousedown_event(GUI::MouseEvent& event) override; - virtual void mousemove_event(GUI::MouseEvent& event) override; }; Screensaver::Screensaver(int width, int height, int interval) { + on_screensaver_exit = []() { GUI::Application::the()->quit(); }; m_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { width, height }).release_value_but_fixme_should_propagate_errors(); srand(time(nullptr)); stop_timer(); @@ -44,26 +41,6 @@ Screensaver::Screensaver(int width, int height, int interval) draw(); } -void Screensaver::mousemove_event(GUI::MouseEvent& event) -{ - constexpr float max_distance_move = 10; - if (m_mouse_origin.is_null()) { - m_mouse_origin = event.position(); - } else if (event.position().distance_from(m_mouse_origin) > max_distance_move) { - GUI::Application::the()->quit(); - } -} - -void Screensaver::mousedown_event(GUI::MouseEvent&) -{ - GUI::Application::the()->quit(); -} - -void Screensaver::keydown_event(GUI::KeyEvent&) -{ - GUI::Application::the()->quit(); -} - void Screensaver::paint_event(GUI::PaintEvent& event) { GUI::Painter painter(*this); @@ -118,15 +95,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) TRY(Core::System::unveil("/res", "r")); TRY(Core::System::unveil(nullptr, nullptr)); - auto app_icon = GUI::Icon::default_icon("app-screensaver"sv); - auto window = TRY(GUI::Window::try_create()); - window->set_double_buffering_enabled(false); - window->set_title("Screensaver"); - window->set_resizable(false); - window->set_frameless(true); - window->set_fullscreen(true); - window->set_minimizable(false); - window->set_icon(app_icon.bitmap_for_size(16)); + auto window = TRY(Desktop::Screensaver::create_window("Screensaver"sv, "app-screensaver"sv)); auto screensaver_window = TRY(window->try_set_main_widget<Screensaver>(64, 48, 10000)); screensaver_window->set_fill_with_background_color(false); diff --git a/Userland/Demos/Starfield/CMakeLists.txt b/Userland/Demos/Starfield/CMakeLists.txt index cf3c9deea0..ddc5b9941a 100644 --- a/Userland/Demos/Starfield/CMakeLists.txt +++ b/Userland/Demos/Starfield/CMakeLists.txt @@ -8,4 +8,4 @@ set(SOURCES ) serenity_app(Starfield ICON app-starfield) -target_link_libraries(Starfield PRIVATE LibGUI LibCore LibGfx LibMain) +target_link_libraries(Starfield PRIVATE LibDesktop LibGUI LibCore LibGfx LibMain) diff --git a/Userland/Demos/Starfield/Starfield.cpp b/Userland/Demos/Starfield/Starfield.cpp index 59fa4b4adb..acc0388ce6 100644 --- a/Userland/Demos/Starfield/Starfield.cpp +++ b/Userland/Demos/Starfield/Starfield.cpp @@ -8,11 +8,11 @@ #include <AK/Vector.h> #include <LibCore/ArgsParser.h> #include <LibCore/System.h> +#include <LibDesktop/Screensaver.h> #include <LibGUI/Application.h> #include <LibGUI/Event.h> #include <LibGUI/Icon.h> #include <LibGUI/Painter.h> -#include <LibGUI/Widget.h> #include <LibGUI/Window.h> #include <LibGfx/Bitmap.h> #include <LibMain/Main.h> @@ -30,7 +30,7 @@ struct Coordinate { } }; -class Starfield final : public GUI::Widget { +class Starfield final : public Desktop::Screensaver { C_OBJECT(Starfield) public: virtual ~Starfield() override = default; @@ -46,8 +46,6 @@ private: virtual void paint_event(GUI::PaintEvent&) override; virtual void timer_event(Core::TimerEvent&) override; virtual void keydown_event(GUI::KeyEvent&) override; - virtual void mousedown_event(GUI::MouseEvent& event) override; - virtual void mousemove_event(GUI::MouseEvent& event) override; Vector<Coordinate> m_stars; int m_sweep_plane = 2000; @@ -56,6 +54,7 @@ private: Starfield::Starfield(int interval) { + on_screensaver_exit = []() { GUI::Application::the()->quit(); }; srand(time(nullptr)); stop_timer(); start_timer(interval); @@ -75,15 +74,6 @@ ErrorOr<void> Starfield::create_stars(int width, int height, int stars) return {}; } -void Starfield::mousemove_event(GUI::MouseEvent&) -{ -} - -void Starfield::mousedown_event(GUI::MouseEvent&) -{ - GUI::Application::the()->quit(); -} - void Starfield::keydown_event(GUI::KeyEvent& event) { switch (event.key()) { @@ -95,7 +85,7 @@ void Starfield::keydown_event(GUI::KeyEvent& event) m_speed = 1; break; default: - GUI::Application::the()->quit(); + Desktop::Screensaver::keydown_event(event); } } @@ -164,16 +154,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) TRY(Core::System::pledge("stdio recvfd sendfd rpath")); - auto app_icon = GUI::Icon::default_icon("app-starfield"sv); - auto window = TRY(GUI::Window::try_create()); - - window->set_double_buffering_enabled(true); - window->set_title("Starfield"); - window->set_resizable(false); - window->set_frameless(true); - window->set_fullscreen(true); - window->set_minimizable(false); - window->set_icon(app_icon.bitmap_for_size(16)); + auto window = TRY(Desktop::Screensaver::create_window("Starfield"sv, "app-starfield"sv)); auto starfield_window = TRY(window->try_set_main_widget<Starfield>(refresh_rate)); starfield_window->set_fill_with_background_color(false); diff --git a/Userland/Demos/Tubes/CMakeLists.txt b/Userland/Demos/Tubes/CMakeLists.txt index e4a4495f0f..3743536eff 100644 --- a/Userland/Demos/Tubes/CMakeLists.txt +++ b/Userland/Demos/Tubes/CMakeLists.txt @@ -10,4 +10,4 @@ set(SOURCES ) serenity_app(Tubes ICON app-tubes) -target_link_libraries(Tubes PRIVATE LibGUI LibCore LibGfx LibGL LibMain) +target_link_libraries(Tubes PRIVATE LibCore LibDesktop LibGfx LibGL LibGUI LibMain) diff --git a/Userland/Demos/Tubes/Tubes.cpp b/Userland/Demos/Tubes/Tubes.cpp index cab53975c5..f710cff2e2 100644 --- a/Userland/Demos/Tubes/Tubes.cpp +++ b/Userland/Demos/Tubes/Tubes.cpp @@ -16,7 +16,6 @@ #include <LibGfx/Bitmap.h> constexpr size_t grid_resolution = 15; -constexpr int mouse_max_distance_move = 10; constexpr int reset_every_ticks = 900; constexpr double rotation_range = 35.; constexpr u8 tube_maximum_count = 12; @@ -78,6 +77,7 @@ static IntVector3 vector_for_direction(Direction direction) Tubes::Tubes(int interval) : m_grid(MUST(FixedArray<u8>::try_create(grid_resolution * grid_resolution * grid_resolution))) { + on_screensaver_exit = []() { GUI::Application::the()->quit(); }; start_timer(interval); } @@ -149,25 +149,6 @@ void Tubes::set_grid(IntVector3 position, u8 value) m_grid[position.z() * grid_resolution * grid_resolution + position.y() * grid_resolution + position.x()] = value; } -void Tubes::mousemove_event(GUI::MouseEvent& event) -{ - if (m_mouse_origin.is_null()) { - m_mouse_origin = event.position(); - } else if (event.position().distance_from(m_mouse_origin) > mouse_max_distance_move) { - GUI::Application::the()->quit(); - } -} - -void Tubes::mousedown_event(GUI::MouseEvent&) -{ - GUI::Application::the()->quit(); -} - -void Tubes::keydown_event(GUI::KeyEvent&) -{ - GUI::Application::the()->quit(); -} - void Tubes::paint_event(GUI::PaintEvent& event) { GUI::Painter painter(*this); diff --git a/Userland/Demos/Tubes/Tubes.h b/Userland/Demos/Tubes/Tubes.h index a64e84b608..4354f41a0b 100644 --- a/Userland/Demos/Tubes/Tubes.h +++ b/Userland/Demos/Tubes/Tubes.h @@ -8,8 +8,8 @@ #include <AK/FixedArray.h> #include <AK/Vector.h> +#include <LibDesktop/Screensaver.h> #include <LibGL/GLContext.h> -#include <LibGUI/Widget.h> #include <LibGfx/Vector3.h> enum class Direction : u8 { @@ -31,7 +31,7 @@ struct Tube { double progress_to_target { 0 }; }; -class Tubes final : public GUI::Widget { +class Tubes final : public Desktop::Screensaver { C_OBJECT(Tubes) public: virtual ~Tubes() override = default; @@ -51,14 +51,10 @@ private: virtual void paint_event(GUI::PaintEvent&) override; virtual void timer_event(Core::TimerEvent&) override; - virtual void keydown_event(GUI::KeyEvent&) override; - virtual void mousedown_event(GUI::MouseEvent& event) override; - virtual void mousemove_event(GUI::MouseEvent& event) override; RefPtr<Gfx::Bitmap> m_bitmap; FixedArray<u8> m_grid; OwnPtr<GL::GLContext> m_gl_context; - Gfx::IntPoint m_mouse_origin; u64 m_ticks { 0 }; Vector<Tube> m_tubes; }; diff --git a/Userland/Demos/Tubes/main.cpp b/Userland/Demos/Tubes/main.cpp index 2bb834bd70..66d1fc7393 100644 --- a/Userland/Demos/Tubes/main.cpp +++ b/Userland/Demos/Tubes/main.cpp @@ -27,16 +27,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) TRY(Core::System::pledge("stdio recvfd sendfd rpath prot_exec")); - auto app_icon = GUI::Icon::default_icon("app-tubes"sv); - auto window = TRY(GUI::Window::try_create()); - - window->set_double_buffering_enabled(true); - window->set_title("Tubes"); - window->set_resizable(false); - window->set_frameless(true); - window->set_fullscreen(true); - window->set_minimizable(false); - window->set_icon(app_icon.bitmap_for_size(16)); + auto window = TRY(Desktop::Screensaver::create_window("Tubes"sv, "app-tubes"sv)); window->update(); auto tubes_widget = TRY(window->try_set_main_widget<Tubes>(refresh_rate)); |