diff --git a/Userland/Applications/ClockSettings/ClockSettingsWidget.cpp b/Userland/Applications/ClockSettings/ClockSettingsWidget.cpp index b0b081cbfb..263a07d1c2 100644 --- a/Userland/Applications/ClockSettings/ClockSettingsWidget.cpp +++ b/Userland/Applications/ClockSettings/ClockSettingsWidget.cpp @@ -8,7 +8,9 @@ #include "ClockSettingsWidget.h" #include #include +#include #include +#include #include #include @@ -25,22 +27,27 @@ ClockSettingsWidget::ClockSettingsWidget() auto& twelve_hour_radio = *find_descendant_of_type_named("12hour_radio"); m_show_seconds_checkbox = *find_descendant_of_type_named("seconds_checkbox"); auto& custom_radio = *find_descendant_of_type_named("custom_radio"); + m_clock_preview = *find_descendant_of_type_named("clock_preview"); - auto saved_format = Config::read_string("Taskbar", "Clock", "TimeFormat"); + m_time_format = Config::read_string("Taskbar", "Clock", "TimeFormat"); m_custom_format_input = *find_descendant_of_type_named("custom_format_input"); - m_custom_format_input->set_text(saved_format); + m_custom_format_input->set_text(m_time_format); m_custom_format_input->set_enabled(false); + m_custom_format_input->on_change = [&] { + m_time_format = m_custom_format_input->get_text(); + update_clock_preview(); + }; - if (saved_format == time_format_12h) { + if (m_time_format == time_format_12h) { twelve_hour_radio.set_checked(true, GUI::AllowCallback::No); m_show_seconds_checkbox->set_checked(false, GUI::AllowCallback::No); - } else if (saved_format == time_format_12h_seconds) { + } else if (m_time_format == time_format_12h_seconds) { twelve_hour_radio.set_checked(true, GUI::AllowCallback::No); m_show_seconds_checkbox->set_checked(true, GUI::AllowCallback::No); - } else if (saved_format == time_format_24h) { + } else if (m_time_format == time_format_24h) { m_24_hour_radio->set_checked(true, GUI::AllowCallback::No); m_show_seconds_checkbox->set_checked(false, GUI::AllowCallback::No); - } else if (saved_format == time_format_24h_seconds) { + } else if (m_time_format == time_format_24h_seconds) { m_24_hour_radio->set_checked(true, GUI::AllowCallback::No); m_show_seconds_checkbox->set_checked(true, GUI::AllowCallback::No); } else { @@ -74,6 +81,12 @@ ClockSettingsWidget::ClockSettingsWidget() m_show_seconds_checkbox->set_enabled(false); m_custom_format_input->set_enabled(true); }; + + m_clock_preview_update_timer = Core::Timer::create_repeating(1000, [&]() { + update_clock_preview(); + }); + m_clock_preview_update_timer->start(); + update_clock_preview(); } void ClockSettingsWidget::apply_settings() @@ -92,7 +105,14 @@ void ClockSettingsWidget::update_time_format_string() { bool show_seconds = m_show_seconds_checkbox->is_checked(); if (m_24_hour_radio->is_checked()) - m_custom_format_input->set_text(show_seconds ? time_format_24h_seconds : time_format_24h); + m_time_format = (show_seconds ? time_format_24h_seconds : time_format_24h); else - m_custom_format_input->set_text(show_seconds ? time_format_12h_seconds : time_format_12h); + m_time_format = (show_seconds ? time_format_12h_seconds : time_format_12h); + m_custom_format_input->set_text(m_time_format); + update_clock_preview(); +} + +void ClockSettingsWidget::update_clock_preview() +{ + m_clock_preview->set_text(Core::DateTime::now().to_string(m_time_format)); } diff --git a/Userland/Applications/ClockSettings/ClockSettingsWidget.gml b/Userland/Applications/ClockSettings/ClockSettingsWidget.gml index 6697bc6f19..0b8ea5a471 100644 --- a/Userland/Applications/ClockSettings/ClockSettingsWidget.gml +++ b/Userland/Applications/ClockSettings/ClockSettingsWidget.gml @@ -8,7 +8,7 @@ @GUI::GroupBox { title: "Time Format" shrink_to_fit: false - fixed_height: 200 + fixed_height: 240 layout: @GUI::VerticalBoxLayout { margins: [16, 8, 8] } @@ -50,5 +50,25 @@ name: "custom_format_input" } } + + @GUI::Widget { + layout: @GUI::HorizontalBoxLayout { + spacing: 4 + } + + @GUI::Label { + text: "Preview:" + text_alignment: "CenterLeft" + } + + @GUI::Frame { + layout: @GUI::VerticalBoxLayout {} + + @GUI::Label { + name: "clock_preview" + text: "12:34:56" + } + } + } } } diff --git a/Userland/Applications/ClockSettings/ClockSettingsWidget.h b/Userland/Applications/ClockSettings/ClockSettingsWidget.h index 3017b489e6..50db28840a 100644 --- a/Userland/Applications/ClockSettings/ClockSettingsWidget.h +++ b/Userland/Applications/ClockSettings/ClockSettingsWidget.h @@ -19,10 +19,14 @@ private: virtual void reset_default_values() override; void update_time_format_string(); + void update_clock_preview(); RefPtr m_24_hour_radio; RefPtr m_show_seconds_checkbox; RefPtr m_custom_format_input; + RefPtr m_clock_preview; - String m_date_format; + RefPtr m_clock_preview_update_timer; + + String m_time_format; };