diff --git a/MenuApplets/Clock/main.cpp b/MenuApplets/Clock/main.cpp index 66c0b1df6de..a5e77923d76 100644 --- a/MenuApplets/Clock/main.cpp +++ b/MenuApplets/Clock/main.cpp @@ -27,6 +27,11 @@ #include #include #include +#include +#include +#include +#include +#include #include #include #include @@ -52,6 +57,162 @@ public: last_update_time = now; } }); + + m_calendar_window = add(window()); + m_calendar_window->set_frameless(true); + m_calendar_window->set_resizable(false); + m_calendar_window->set_minimizable(false); + m_calendar_window->on_active_input_change = [this](bool is_active_input) { + if (!is_active_input) + close(); + }; + + auto& root_container = m_calendar_window->set_main_widget(); + root_container.set_fill_with_background_color(true); + root_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill); + root_container.set_layout(); + root_container.layout()->set_margins({ 0, 2, 0, 2 }); + root_container.layout()->set_spacing(0); + root_container.set_frame_thickness(2); + root_container.set_frame_shape(Gfx::FrameShape::Container); + root_container.set_frame_shadow(Gfx::FrameShadow::Raised); + + auto& navigation_container = root_container.add(); + navigation_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + navigation_container.set_preferred_size(0, 24); + navigation_container.set_layout(); + navigation_container.layout()->set_margins({ 2, 2, 3, 2 }); + + m_prev_date = navigation_container.add(); + m_prev_date->set_button_style(Gfx::ButtonStyle::CoolBar); + m_prev_date->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed); + m_prev_date->set_preferred_size(24, 24); + m_prev_date->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png")); + m_prev_date->on_click = [&](auto) { + unsigned int target_month = m_calendar->selected_month(); + unsigned int target_year = m_calendar->selected_year(); + + if (m_calendar->mode() == GUI::Calendar::Month) { + target_month--; + if (m_calendar->selected_month() <= 1) { + target_month = 12; + target_year--; + } + } else { + target_year--; + } + + m_calendar->update_tiles(target_year, target_month); + m_selected_calendar_button->set_text(m_calendar->selected_calendar_text(GUI::Calendar::LongNames)); + }; + + m_selected_calendar_button = navigation_container.add(); + m_selected_calendar_button->set_button_style(Gfx::ButtonStyle::CoolBar); + m_selected_calendar_button->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + m_selected_calendar_button->set_preferred_size(0, 24); + m_selected_calendar_button->on_click = [&](auto) { + m_calendar->toggle_mode(); + m_selected_calendar_button->set_text(m_calendar->selected_calendar_text(GUI::Calendar::LongNames)); + }; + + m_next_date = navigation_container.add(); + m_next_date->set_button_style(Gfx::ButtonStyle::CoolBar); + m_next_date->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed); + m_next_date->set_preferred_size(24, 24); + m_next_date->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png")); + m_next_date->on_click = [&](auto) { + unsigned int target_month = m_calendar->selected_month(); + unsigned int target_year = m_calendar->selected_year(); + + if (m_calendar->mode() == GUI::Calendar::Month) { + target_month++; + if (m_calendar->selected_month() >= 12) { + target_month = 1; + target_year++; + } + } else { + target_year++; + } + + m_calendar->update_tiles(target_year, target_month); + m_selected_calendar_button->set_text(m_calendar->selected_calendar_text(GUI::Calendar::LongNames)); + }; + + auto& divider1_container = root_container.add(); + divider1_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + divider1_container.set_preferred_size(0, 2); + divider1_container.set_layout(); + divider1_container.layout()->set_margins({ 2, 0, 3, 0 }); + + auto& divider1 = divider1_container.add(); + divider1.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + divider1.set_preferred_size(0, 2); + divider1.set_frame_shape(Gfx::FrameShape::Panel); + + auto& calendar_frame_container = root_container.add(); + calendar_frame_container.set_layout(); + calendar_frame_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill); + calendar_frame_container.layout()->set_margins({ 4, 4, 5, 4 }); + + auto& calendar_frame = calendar_frame_container.add(); + calendar_frame.set_layout(); + calendar_frame.layout()->set_margins({ 2, 2, 2, 2 }); + + m_calendar = calendar_frame.add(Core::DateTime::now()); + m_selected_calendar_button->set_text(m_calendar->selected_calendar_text(GUI::Calendar::LongNames)); + + m_calendar->on_calendar_tile_click = [&] { + m_selected_calendar_button->set_text(m_calendar->selected_calendar_text(GUI::Calendar::LongNames)); + }; + + m_calendar->on_month_tile_click = [&] { + m_selected_calendar_button->set_text(m_calendar->selected_calendar_text(GUI::Calendar::LongNames)); + }; + + auto& divider2_container = root_container.add(); + divider2_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + divider2_container.set_preferred_size(0, 2); + divider2_container.set_layout(); + divider2_container.layout()->set_margins({ 2, 0, 3, 0 }); + + auto& divider2 = divider2_container.add(); + divider2.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + divider2.set_preferred_size(0, 2); + divider2.set_frame_shape(Gfx::FrameShape::Panel); + + auto& settings_container = root_container.add(); + settings_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + settings_container.set_preferred_size(0, 24); + settings_container.set_layout(); + settings_container.layout()->set_margins({ 2, 2, 3, 2 }); + settings_container.layout()->add_spacer(); + + m_jump_to_button = settings_container.add(); + m_jump_to_button->set_button_style(Gfx::ButtonStyle::CoolBar); + m_jump_to_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed); + m_jump_to_button->set_preferred_size(24, 24); + m_jump_to_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/calendar-date.png")); + m_jump_to_button->set_tooltip("Jump to today"); + m_jump_to_button->on_click = [this](auto) { + jump_to_current_date(); + }; + + m_calendar_launcher = settings_container.add(); + m_calendar_launcher->set_button_style(Gfx::ButtonStyle::CoolBar); + m_calendar_launcher->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed); + m_calendar_launcher->set_preferred_size(24, 24); + m_calendar_launcher->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-calendar.png")); + m_calendar_launcher->set_tooltip("Calendar"); + m_calendar_launcher->on_click = [this](auto) { + pid_t pid; + const char* argv[] = { "Calendar", nullptr }; + if ((errno = posix_spawn(&pid, "/bin/Calendar", nullptr, nullptr, const_cast(argv), environ))) { + perror("posix_spawn"); + } else { + if (disown(pid) < 0) + perror("disown"); + } + }; } virtual ~ClockWidget() override { } @@ -74,16 +235,13 @@ private: virtual void mousedown_event(GUI::MouseEvent& event) override { - if (event.button() != GUI::MouseButton::Left) + if (event.button() != GUI::MouseButton::Left) { return; - - pid_t pid; - const char* argv[] = { "Calendar", nullptr }; - if ((errno = posix_spawn(&pid, "/bin/Calendar", nullptr, nullptr, const_cast(argv), environ))) { - perror("posix_spawn"); } else { - if (disown(pid) < 0) - perror("disown"); + if (!m_calendar_window->is_visible()) + open(); + else + close(); } } @@ -92,6 +250,43 @@ private: update(); } + void open() + { + position_calendar_window(); + jump_to_current_date(); + m_calendar_window->show(); + } + + void close() + { + m_calendar_window->hide(); + } + + void position_calendar_window() + { + m_calendar_window->set_rect( + window()->rect_in_menubar().x() - ((m_calendar_window->rect().width() - window()->rect().width()) / 2), + 19, + 153, + 180); + } + + void jump_to_current_date() + { + if (m_calendar->mode() == GUI::Calendar::Year) + m_calendar->toggle_mode(); + m_calendar->set_selected_date(Core::DateTime::now()); + m_calendar->update_tiles(Core::DateTime::now().year(), Core::DateTime::now().month()); + m_selected_calendar_button->set_text(m_calendar->selected_calendar_text(GUI::Calendar::LongNames)); + } + + RefPtr m_calendar_window; + RefPtr m_calendar; + RefPtr m_next_date; + RefPtr m_prev_date; + RefPtr m_selected_calendar_button; + RefPtr m_jump_to_button; + RefPtr m_calendar_launcher; RefPtr m_timer; int m_time_width; };