Demos: Add sleep option to CatDog
Now you can put your CatDog to sleep while you're at work :)
This commit is contained in:
parent
40a8b009db
commit
6da1d5eb57
Notes:
sideshowbarker
2024-07-17 02:57:43 +09:00
Author: https://github.com/theonlyasdk Commit: https://github.com/SerenityOS/serenity/commit/6da1d5eb57 Pull-request: https://github.com/SerenityOS/serenity/pull/24267 Reviewed-by: https://github.com/ADKaster ✅
5 changed files with 29 additions and 0 deletions
BIN
Base/res/icons/16x16/catdog-sleeping.png
Normal file
BIN
Base/res/icons/16x16/catdog-sleeping.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 136 B |
BIN
Base/res/icons/16x16/catdog-wake-up.png
Normal file
BIN
Base/res/icons/16x16/catdog-wake-up.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 230 B |
|
@ -8,6 +8,7 @@
|
|||
#include "CatDog.h"
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/ProcessStatisticsReader.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibGUI/Window.h>
|
||||
|
||||
|
@ -67,6 +68,12 @@ void CatDog::set_roaming(bool roaming)
|
|||
update();
|
||||
}
|
||||
|
||||
void CatDog::set_sleeping(bool sleeping)
|
||||
{
|
||||
m_state = (sleeping ? State::Sleeping : State::Idle) | special_application_states();
|
||||
update();
|
||||
}
|
||||
|
||||
CatDog::State CatDog::special_application_states() const
|
||||
{
|
||||
auto maybe_proc_info = Core::ProcessStatisticsReader::get_all(*m_proc_all);
|
||||
|
@ -99,6 +106,11 @@ bool CatDog::is_inspector() const
|
|||
return has_flag(special_application_states(), State::Inspector);
|
||||
}
|
||||
|
||||
bool CatDog::is_sleeping() const
|
||||
{
|
||||
return has_flag(m_state, State::Sleeping);
|
||||
}
|
||||
|
||||
void CatDog::timer_event(Core::TimerEvent&)
|
||||
{
|
||||
using namespace AK::TimeLiterals;
|
||||
|
@ -106,6 +118,9 @@ void CatDog::timer_event(Core::TimerEvent&)
|
|||
if (has_flag(m_state, State::Alert))
|
||||
return;
|
||||
|
||||
if (has_flag(m_state, State::Sleeping))
|
||||
return;
|
||||
|
||||
m_state = special_application_states();
|
||||
|
||||
auto const size = window()->size();
|
||||
|
@ -164,6 +179,9 @@ void CatDog::track_mouse_move(Gfx::IntPoint point)
|
|||
if (has_flag(m_state, State::Alert))
|
||||
return;
|
||||
|
||||
if (has_flag(m_state, State::Sleeping))
|
||||
return;
|
||||
|
||||
Gfx::IntPoint relative_offset = point - window()->position();
|
||||
if (m_mouse_offset != relative_offset) {
|
||||
m_mouse_offset = relative_offset;
|
||||
|
|
|
@ -35,9 +35,11 @@ public:
|
|||
Function<void(GUI::ContextMenuEvent&)> on_context_menu_request;
|
||||
|
||||
void set_roaming(bool roaming);
|
||||
void set_sleeping(bool sleeping);
|
||||
|
||||
[[nodiscard]] bool is_artist() const;
|
||||
[[nodiscard]] bool is_inspector() const;
|
||||
[[nodiscard]] bool is_sleeping() const;
|
||||
|
||||
private:
|
||||
enum class State : u16 {
|
||||
|
|
|
@ -24,6 +24,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto app = TRY(GUI::Application::create(arguments));
|
||||
auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-catdog"sv));
|
||||
|
||||
auto catdog_icon_sleep = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/catdog-sleeping.png"sv));
|
||||
auto catdog_icon_wake = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/catdog-wake-up.png"sv));
|
||||
|
||||
TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
|
||||
TRY(Core::System::unveil("/res", "r"));
|
||||
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
||||
|
@ -46,6 +49,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
auto context_menu = GUI::Menu::construct();
|
||||
context_menu->add_action(GUI::CommonActions::make_about_action("CatDog Demo"_string, app_icon, window));
|
||||
context_menu->add_action(GUI::Action::create("Put CatDog to sleep...", catdog_icon_sleep, [&](GUI::Action& action) {
|
||||
catdog_widget->set_sleeping(!catdog_widget->is_sleeping());
|
||||
|
||||
action.set_text(catdog_widget->is_sleeping() ? "Wake CatDog..." : "Put CatDog to sleep...");
|
||||
action.set_icon(catdog_widget->is_sleeping() ? catdog_icon_wake : catdog_icon_sleep);
|
||||
}));
|
||||
context_menu->add_separator();
|
||||
context_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue