diff --git a/Userland/Demos/Screensaver/CMakeLists.txt b/Userland/Demos/Screensaver/CMakeLists.txt index 95708fd1745..4a1061909fc 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 0199d40e260..09fe6528ada 100644 --- a/Userland/Demos/Screensaver/Screensaver.cpp +++ b/Userland/Demos/Screensaver/Screensaver.cpp @@ -5,11 +5,11 @@ */ #include +#include #include #include #include #include -#include #include #include #include @@ -17,7 +17,7 @@ #include #include -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 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 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(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 cf3c9deea0b..ddc5b9941ae 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 59fa4b4adb0..acc0388ce64 100644 --- a/Userland/Demos/Starfield/Starfield.cpp +++ b/Userland/Demos/Starfield/Starfield.cpp @@ -8,11 +8,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include @@ -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 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 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 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(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 e4a4495f0ff..3743536eff3 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 cab53975c55..f710cff2e2a 100644 --- a/Userland/Demos/Tubes/Tubes.cpp +++ b/Userland/Demos/Tubes/Tubes.cpp @@ -16,7 +16,6 @@ #include 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::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 a64e84b608f..4354f41a0b1 100644 --- a/Userland/Demos/Tubes/Tubes.h +++ b/Userland/Demos/Tubes/Tubes.h @@ -8,8 +8,8 @@ #include #include +#include #include -#include #include 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 m_bitmap; FixedArray m_grid; OwnPtr m_gl_context; - Gfx::IntPoint m_mouse_origin; u64 m_ticks { 0 }; Vector m_tubes; }; diff --git a/Userland/Demos/Tubes/main.cpp b/Userland/Demos/Tubes/main.cpp index 2bb834bd70c..66d1fc7393b 100644 --- a/Userland/Demos/Tubes/main.cpp +++ b/Userland/Demos/Tubes/main.cpp @@ -27,16 +27,7 @@ ErrorOr 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(refresh_rate)); diff --git a/Userland/Libraries/LibDesktop/CMakeLists.txt b/Userland/Libraries/LibDesktop/CMakeLists.txt index 54f733fdafb..dd006bdbc04 100644 --- a/Userland/Libraries/LibDesktop/CMakeLists.txt +++ b/Userland/Libraries/LibDesktop/CMakeLists.txt @@ -1,6 +1,7 @@ set(SOURCES AppFile.cpp Launcher.cpp + Screensaver.cpp ) set(GENERATED_SOURCES @@ -9,4 +10,4 @@ set(GENERATED_SOURCES ) serenity_lib(LibDesktop desktop) -target_link_libraries(LibDesktop PRIVATE LibCore LibIPC LibGUI) +target_link_libraries(LibDesktop PRIVATE LibCore LibIPC LibGfx LibGUI) diff --git a/Userland/Libraries/LibDesktop/Screensaver.cpp b/Userland/Libraries/LibDesktop/Screensaver.cpp new file mode 100644 index 00000000000..dec888282bb --- /dev/null +++ b/Userland/Libraries/LibDesktop/Screensaver.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace Desktop { + +static constexpr int mouse_max_distance_move = 10; + +ErrorOr> Screensaver::create_window(StringView title, StringView icon) +{ + auto window = TRY(GUI::Window::try_create()); + window->set_double_buffering_enabled(false); + window->set_frameless(true); + window->set_fullscreen(true); + window->set_minimizable(false); + window->set_resizable(false); + window->set_title(title); + + auto app_icon = TRY(GUI::Icon::try_create_default_icon(icon)); + window->set_icon(app_icon.bitmap_for_size(16)); + + return window; +} + +void Screensaver::keydown_event(GUI::KeyEvent&) +{ + trigger_exit(); +} + +void Screensaver::mousedown_event(GUI::MouseEvent&) +{ + trigger_exit(); +} + +void Screensaver::mousemove_event(GUI::MouseEvent& event) +{ + if (!m_mouse_origin.has_value()) + m_mouse_origin = event.position(); + else if (event.position().distance_from(m_mouse_origin.value()) > mouse_max_distance_move) + trigger_exit(); +} + +void Screensaver::trigger_exit() +{ + if (on_screensaver_exit) + on_screensaver_exit(); +} + +} diff --git a/Userland/Libraries/LibDesktop/Screensaver.h b/Userland/Libraries/LibDesktop/Screensaver.h new file mode 100644 index 00000000000..c3d715d1a65 --- /dev/null +++ b/Userland/Libraries/LibDesktop/Screensaver.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace Desktop { + +class Screensaver : public GUI::Widget { + C_OBJECT_ABSTRACT(Screensaver) +public: + Function on_screensaver_exit; + + static ErrorOr> create_window(StringView title, StringView icon); + + virtual void keydown_event(GUI::KeyEvent&) override; + virtual void mousedown_event(GUI::MouseEvent& event) override; + virtual void mousemove_event(GUI::MouseEvent& event) override; + +private: + void trigger_exit(); + + Optional m_mouse_origin; +}; + +}