From 6da1d5eb57ebec2e8d4423d8a5741f412df06d2c Mon Sep 17 00:00:00 2001 From: theonlyasdk <168300808+theonlyasdk@users.noreply.github.com> Date: Thu, 9 May 2024 00:24:06 +0530 Subject: [PATCH] Demos: Add sleep option to CatDog Now you can put your CatDog to sleep while you're at work :) --- Base/res/icons/16x16/catdog-sleeping.png | Bin 0 -> 136 bytes Base/res/icons/16x16/catdog-wake-up.png | Bin 0 -> 230 bytes Userland/Demos/CatDog/CatDog.cpp | 18 ++++++++++++++++++ Userland/Demos/CatDog/CatDog.h | 2 ++ Userland/Demos/CatDog/main.cpp | 9 +++++++++ 5 files changed, 29 insertions(+) create mode 100644 Base/res/icons/16x16/catdog-sleeping.png create mode 100644 Base/res/icons/16x16/catdog-wake-up.png diff --git a/Base/res/icons/16x16/catdog-sleeping.png b/Base/res/icons/16x16/catdog-sleeping.png new file mode 100644 index 0000000000000000000000000000000000000000..60f845ea90849b724f9ea386082d1f0ef866ecfa GIT binary patch literal 136 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!93?!50ihlx9oB=)|u0Z<#|NlU;_*ioxkYX$e z@(X5gcy=QV$kFw5aSY+Oo}AFY5GZi)27}6t12F>K+#UvO4LgJslo*#NF{x}}itT7j cWW6H8u+M@upgh9f7pRfJ)78&qol`;+0B{f^X#fBK literal 0 HcmV?d00001 diff --git a/Base/res/icons/16x16/catdog-wake-up.png b/Base/res/icons/16x16/catdog-wake-up.png new file mode 100644 index 0000000000000000000000000000000000000000..3530fe6214ff24cbfda84b1c3d47e121b8f7cf1b GIT binary patch literal 230 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPFv4DbnY1=2w9LO|f@iIq#rJ^!ER z`EqLBx05qp?4NW?Mg}N;XU64+Ko!g-L4Lvi85oUU9#{(G7J9lkhHzX@P7p{D5fBkb zNnn`R)6?X`qaf8GBFQpSl9iQJhNZEGfsHMUU6qaTa)1IyVv2x)w}*GKw`1ZIW=GBm ziSv#fn821aW2x$frHxA^ZU{*@@N{q6vZY5%A=!;xpv1_Cqi8Kt6RU%^!~q8Zh9qr? Ul^h$jLB3${boFyt=akR{09wIDUjP6A literal 0 HcmV?d00001 diff --git a/Userland/Demos/CatDog/CatDog.cpp b/Userland/Demos/CatDog/CatDog.cpp index fd34b224dd2..d23cce501f8 100644 --- a/Userland/Demos/CatDog/CatDog.cpp +++ b/Userland/Demos/CatDog/CatDog.cpp @@ -8,6 +8,7 @@ #include "CatDog.h" #include #include +#include #include #include @@ -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; diff --git a/Userland/Demos/CatDog/CatDog.h b/Userland/Demos/CatDog/CatDog.h index b7e878aca01..9b45317a270 100644 --- a/Userland/Demos/CatDog/CatDog.h +++ b/Userland/Demos/CatDog/CatDog.h @@ -35,9 +35,11 @@ public: Function 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 { diff --git a/Userland/Demos/CatDog/main.cpp b/Userland/Demos/CatDog/main.cpp index 408df755d14..8f32622d213 100644 --- a/Userland/Demos/CatDog/main.cpp +++ b/Userland/Demos/CatDog/main.cpp @@ -24,6 +24,9 @@ ErrorOr 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 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(); }));