2021-01-02 23:34:34 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Richard Gráčik <r.gracik@gmail.com>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-04-28 23:23:36 +02:00
|
|
|
*/
|
2021-01-02 23:34:34 +01:00
|
|
|
|
2021-05-09 10:19:20 +02:00
|
|
|
#include "CatDog.h"
|
2021-05-09 12:13:51 +02:00
|
|
|
#include "SpeechBubble.h"
|
|
|
|
#include <LibCore/Timer.h>
|
2021-05-09 10:19:20 +02:00
|
|
|
#include <LibGUI/Action.h>
|
2021-01-21 20:23:52 +01:00
|
|
|
#include <LibGUI/Application.h>
|
2021-01-02 23:34:34 +01:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/Icon.h>
|
|
|
|
#include <LibGUI/Menu.h>
|
2021-04-13 16:18:20 +02:00
|
|
|
#include <LibGUI/Menubar.h>
|
2021-01-02 23:34:34 +01:00
|
|
|
#include <LibGUI/Window.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2021-05-13 23:20:26 +02:00
|
|
|
if (pledge("stdio recvfd sendfd rpath wpath cpath unix", nullptr) < 0) {
|
2021-01-12 06:41:08 +00:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-02 23:34:34 +01:00
|
|
|
auto app = GUI::Application::construct(argc, argv);
|
|
|
|
auto app_icon = GUI::Icon::default_icon("app-catdog");
|
|
|
|
|
2021-01-16 17:42:31 +01:00
|
|
|
if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
|
2021-01-02 23:34:34 +01:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unveil("/res", "r") < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-12 06:41:08 +00:00
|
|
|
if (unveil(nullptr, nullptr) < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-02 23:34:34 +01:00
|
|
|
auto window = GUI::Window::construct();
|
|
|
|
window->set_title("CatDog Demo");
|
|
|
|
window->resize(32, 32);
|
|
|
|
window->set_frameless(true);
|
|
|
|
window->set_resizable(false);
|
|
|
|
window->set_has_alpha_channel(true);
|
2021-02-14 16:42:46 -07:00
|
|
|
window->set_alpha_hit_threshold(1.0f);
|
2021-01-02 23:34:34 +01:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
|
|
|
|
2021-05-09 12:13:20 +02:00
|
|
|
auto& catdog_widget = window->set_main_widget<CatDog>();
|
|
|
|
catdog_widget.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
catdog_widget.layout()->set_spacing(0);
|
2021-01-02 23:34:34 +01:00
|
|
|
|
2021-05-18 19:12:05 +02:00
|
|
|
auto context_menu = GUI::Menu::construct();
|
|
|
|
context_menu->add_action(GUI::CommonActions::make_about_action("CatDog Demo", app_icon, window));
|
|
|
|
context_menu->add_separator();
|
|
|
|
context_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
|
2021-01-02 23:34:34 +01:00
|
|
|
|
|
|
|
window->show();
|
2021-05-09 12:13:20 +02:00
|
|
|
catdog_widget.track_cursor_globally();
|
|
|
|
catdog_widget.start_timer(250, Core::TimerShouldFireWhenNotVisible::Yes);
|
|
|
|
catdog_widget.start_the_timer(); // timer for "mouse sleep detection"
|
2021-01-02 23:34:34 +01:00
|
|
|
|
2021-05-09 12:13:51 +02:00
|
|
|
auto advice_window = GUI::Window::construct();
|
|
|
|
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 = advice_window->set_main_widget<SpeechBubble>();
|
|
|
|
advice_widget.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
advice_widget.layout()->set_spacing(0);
|
|
|
|
|
|
|
|
auto advice_timer = Core::Timer::construct();
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2021-05-19 22:11:59 +02:00
|
|
|
catdog_widget.on_context_menu_request = [&](GUI::ContextMenuEvent& event) {
|
|
|
|
if (catdog_widget.rect().contains(event.position()))
|
|
|
|
context_menu->popup(event.screen_position());
|
2021-05-18 19:12:05 +02:00
|
|
|
};
|
|
|
|
|
2021-01-02 23:34:34 +01:00
|
|
|
return app->exec();
|
|
|
|
}
|