mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 06:24:58 +00:00
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`.
This commit is contained in:
parent
379e4a2432
commit
18b6bdb563
11 changed files with 108 additions and 99 deletions
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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)
|
||||
|
|
54
Userland/Libraries/LibDesktop/Screensaver.cpp
Normal file
54
Userland/Libraries/LibDesktop/Screensaver.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibDesktop/Screensaver.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
|
||||
namespace Desktop {
|
||||
|
||||
static constexpr int mouse_max_distance_move = 10;
|
||||
|
||||
ErrorOr<NonnullRefPtr<GUI::Window>> 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();
|
||||
}
|
||||
|
||||
}
|
36
Userland/Libraries/LibDesktop/Screensaver.h
Normal file
36
Userland/Libraries/LibDesktop/Screensaver.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Point.h>
|
||||
|
||||
namespace Desktop {
|
||||
|
||||
class Screensaver : public GUI::Widget {
|
||||
C_OBJECT_ABSTRACT(Screensaver)
|
||||
public:
|
||||
Function<void()> on_screensaver_exit;
|
||||
|
||||
static ErrorOr<NonnullRefPtr<GUI::Window>> 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<Gfx::IntPoint> m_mouse_origin;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue