summaryrefslogtreecommitdiff
path: root/Userland/Demos/CatDog/main.cpp
blob: 1dd739e17cf193589a844065d3c98fa488e8c450 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (c) 2021, Richard Gráčik <r.gracik@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "CatDog.h"
#include "SpeechBubble.h"
#include <LibCore/System.h>
#include <LibCore/Timer.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/Window.h>
#include <LibMain/Main.h>

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    TRY(Core::System::pledge("stdio recvfd sendfd rpath wpath cpath unix"));

    auto app = TRY(GUI::Application::try_create(arguments));
    auto app_icon = GUI::Icon::default_icon("app-catdog");

    TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
    TRY(Core::System::unveil("/res", "r"));
    TRY(Core::System::unveil(nullptr, nullptr));

    auto window = TRY(GUI::Window::try_create());
    window->set_title("CatDog Demo");
    window->resize(32, 32);
    window->set_frameless(true);
    window->set_resizable(false);
    window->set_has_alpha_channel(true);
    window->set_alpha_hit_threshold(1.0f);
    window->set_icon(app_icon.bitmap_for_size(16));

    auto catdog_widget = TRY(window->try_set_main_widget<CatDog>());
    TRY(catdog_widget->try_set_layout<GUI::VerticalBoxLayout>());
    catdog_widget->layout()->set_spacing(0);

    auto context_menu = TRY(GUI::Menu::try_create());
    TRY(context_menu->try_add_action(GUI::CommonActions::make_about_action("CatDog Demo", app_icon, window)));
    TRY(context_menu->try_add_separator());
    TRY(context_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); })));

    window->show();
    catdog_widget->start_timer(250, Core::TimerShouldFireWhenNotVisible::Yes);
    catdog_widget->start_the_timer(); // timer for "mouse sleep detection"

    auto advice_window = TRY(GUI::Window::try_create());
    advice_window->set_title("CatDog Advice");
    advice_window->resize(225, 50);
    advice_window->set_frameless(true);
    advice_window->set_resizable(false);
    advice_window->set_has_alpha_channel(true);
    advice_window->set_alpha_hit_threshold(1.0f);

    auto advice_widget = TRY(advice_window->try_set_main_widget<SpeechBubble>());
    TRY(advice_widget->try_set_layout<GUI::VerticalBoxLayout>());
    advice_widget->layout()->set_spacing(0);

    auto advice_timer = TRY(Core::Timer::try_create());
    advice_timer->set_interval(15000);
    advice_timer->set_single_shot(true);
    advice_timer->on_timeout = [&] {
        window->move_to_front();
        advice_window->move_to_front();
        catdog_widget->set_roaming(false);
        advice_window->move_to(window->x() - advice_window->width() / 2, window->y() - advice_window->height());
        advice_window->show();
    };
    advice_timer->start();

    advice_widget->on_dismiss = [&] {
        catdog_widget->set_roaming(true);
        advice_window->hide();
        advice_timer->start();
    };

    // Let users toggle the advice functionality by clicking on catdog.
    catdog_widget->on_click = [&] {
        if (advice_timer->is_active())
            advice_timer->stop();
        else
            advice_timer->start();
    };

    catdog_widget->on_context_menu_request = [&](GUI::ContextMenuEvent& event) {
        if (catdog_widget->rect().contains(event.position()))
            context_menu->popup(event.screen_position());
    };

    return app->exec();
}