Serenity Operating System
1/*
2 * Copyright (c) 2022, cflip <cflip@cflip.net>
3 * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include "ClockSettingsWidget.h"
9#include <Applications/ClockSettings/ClockSettingsWidgetGML.h>
10#include <LibConfig/Client.h>
11#include <LibCore/DateTime.h>
12#include <LibGUI/CheckBox.h>
13#include <LibGUI/Label.h>
14#include <LibGUI/RadioButton.h>
15#include <LibGUI/TextBox.h>
16
17constexpr auto time_format_12h = "%I:%M %p"sv;
18constexpr auto time_format_12h_seconds = "%r"sv;
19constexpr auto time_format_24h = "%R"sv;
20constexpr auto time_format_24h_seconds = "%T"sv;
21
22ClockSettingsWidget::ClockSettingsWidget()
23{
24 load_from_gml(clock_settings_widget_gml).release_value_but_fixme_should_propagate_errors();
25
26 m_24_hour_radio = *find_descendant_of_type_named<GUI::RadioButton>("24hour_radio");
27 auto& twelve_hour_radio = *find_descendant_of_type_named<GUI::RadioButton>("12hour_radio");
28 m_show_seconds_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("seconds_checkbox");
29 auto& custom_radio = *find_descendant_of_type_named<GUI::RadioButton>("custom_radio");
30 m_clock_preview = *find_descendant_of_type_named<GUI::Label>("clock_preview");
31
32 m_time_format = Config::read_string("Taskbar"sv, "Clock"sv, "TimeFormat"sv);
33 m_custom_format_input = *find_descendant_of_type_named<GUI::TextBox>("custom_format_input");
34 m_custom_format_input->set_text(m_time_format);
35 m_custom_format_input->set_enabled(false);
36 m_custom_format_input->on_change = [&] {
37 m_time_format = m_custom_format_input->get_text();
38 set_modified(true);
39 update_clock_preview();
40 };
41
42 if (m_time_format == time_format_12h) {
43 twelve_hour_radio.set_checked(true, GUI::AllowCallback::No);
44 m_show_seconds_checkbox->set_checked(false, GUI::AllowCallback::No);
45 } else if (m_time_format == time_format_12h_seconds) {
46 twelve_hour_radio.set_checked(true, GUI::AllowCallback::No);
47 m_show_seconds_checkbox->set_checked(true, GUI::AllowCallback::No);
48 } else if (m_time_format == time_format_24h) {
49 m_24_hour_radio->set_checked(true, GUI::AllowCallback::No);
50 m_show_seconds_checkbox->set_checked(false, GUI::AllowCallback::No);
51 } else if (m_time_format == time_format_24h_seconds) {
52 m_24_hour_radio->set_checked(true, GUI::AllowCallback::No);
53 m_show_seconds_checkbox->set_checked(true, GUI::AllowCallback::No);
54 } else {
55 custom_radio.set_checked(true);
56 m_custom_format_input->set_enabled(true);
57 }
58
59 m_24_hour_radio->on_checked = [&](bool checked) {
60 if (!checked)
61 return;
62 m_show_seconds_checkbox->set_enabled(true);
63 m_custom_format_input->set_enabled(false);
64 set_modified(true);
65 update_time_format_string();
66 };
67
68 twelve_hour_radio.on_checked = [&](bool checked) {
69 if (!checked)
70 return;
71 m_show_seconds_checkbox->set_enabled(true);
72 m_custom_format_input->set_enabled(false);
73 set_modified(true);
74 update_time_format_string();
75 };
76
77 m_show_seconds_checkbox->on_checked = [&](bool) {
78 set_modified(true);
79 update_time_format_string();
80 };
81
82 custom_radio.on_checked = [&](bool checked) {
83 if (!checked)
84 return;
85 m_show_seconds_checkbox->set_enabled(false);
86 m_custom_format_input->set_enabled(true);
87 set_modified(true);
88 };
89
90 m_clock_preview_update_timer = Core::Timer::create_repeating(1000, [&]() {
91 update_clock_preview();
92 }).release_value_but_fixme_should_propagate_errors();
93 m_clock_preview_update_timer->start();
94 update_clock_preview();
95}
96
97void ClockSettingsWidget::apply_settings()
98{
99 Config::write_string("Taskbar"sv, "Clock"sv, "TimeFormat"sv, m_custom_format_input->text());
100}
101
102void ClockSettingsWidget::reset_default_values()
103{
104 m_24_hour_radio->set_checked(true);
105 m_show_seconds_checkbox->set_checked(true);
106 Config::write_string("Taskbar"sv, "Clock"sv, "TimeFormat"sv, time_format_24h_seconds);
107}
108
109void ClockSettingsWidget::update_time_format_string()
110{
111 bool show_seconds = m_show_seconds_checkbox->is_checked();
112 if (m_24_hour_radio->is_checked())
113 m_time_format = (show_seconds ? time_format_24h_seconds : time_format_24h);
114 else
115 m_time_format = (show_seconds ? time_format_12h_seconds : time_format_12h);
116 m_custom_format_input->set_text(m_time_format);
117 update_clock_preview();
118}
119
120void ClockSettingsWidget::update_clock_preview()
121{
122 m_clock_preview->set_text(Core::DateTime::now().to_deprecated_string(m_time_format));
123}