mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 08:37:35 +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
|
@ -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