mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 09:27:45 +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
|
@ -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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue