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!ia0y~yU=RRdCT0c(hNQXTpBNYzI0Jk_Tp1V`{{R2az`#&^ zthtbZfq}6k$S;_|;n|He1_lOQPZ!4!j_b(@4Ge(-2X8Q_+&B;;(9P{(z}B!sNI{8l ni4v2_7N*#a#zfXDG7S4HSOdx6VPl3ju*UGcG@5U|?V_3Gxg6&%kK>^1xCC28KdU7sn8e>&XcM zDIx+Q0x1a$6MK4^e0UV3Iz%K{W=gWMvdXYD_As!qg|Vx$F%v>l4dMb-LSNAsl*K-2?w6;ZCkeVh$$qyu?v(K8F3V?Wolw|(3Uvh ZAi$8MEwPegqc+GF44$rjF6*2UngCkCMqdB` 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(); }));