summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibDesktop/Screensaver.cpp
blob: f5f181acdcc70f6534955841f4b02b6b2ba306b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * 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;
static constexpr int mouse_tracking_delay_milliseconds = 750;

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)
{
    auto now = AK::Time::now_monotonic();
    if ((now - m_start_time).to_milliseconds() < mouse_tracking_delay_milliseconds)
        return;

    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();
}

}