summaryrefslogtreecommitdiff
path: root/Userland/Applications/ClockSettings/ClockSettingsWidget.cpp
blob: f16c70f6bdc7f37bd891c09b90110b01eb58d5cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (c) 2022, cflip <cflip@cflip.net>
 * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "ClockSettingsWidget.h"
#include <Applications/ClockSettings/ClockSettingsWidgetGML.h>
#include <LibConfig/Client.h>
#include <LibCore/DateTime.h>
#include <LibGUI/CheckBox.h>
#include <LibGUI/Label.h>
#include <LibGUI/RadioButton.h>
#include <LibGUI/TextBox.h>

constexpr auto time_format_12h = "%I:%M %p"sv;
constexpr auto time_format_12h_seconds = "%r"sv;
constexpr auto time_format_24h = "%R"sv;
constexpr auto time_format_24h_seconds = "%T"sv;

ClockSettingsWidget::ClockSettingsWidget()
{
    load_from_gml(clock_settings_widget_gml);

    m_24_hour_radio = *find_descendant_of_type_named<GUI::RadioButton>("24hour_radio");
    auto& twelve_hour_radio = *find_descendant_of_type_named<GUI::RadioButton>("12hour_radio");
    m_show_seconds_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("seconds_checkbox");
    auto& custom_radio = *find_descendant_of_type_named<GUI::RadioButton>("custom_radio");
    m_clock_preview = *find_descendant_of_type_named<GUI::Label>("clock_preview");

    m_time_format = Config::read_string("Taskbar"sv, "Clock"sv, "TimeFormat"sv);
    m_custom_format_input = *find_descendant_of_type_named<GUI::TextBox>("custom_format_input");
    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();
        set_modified(true);
        update_clock_preview();
    };

    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 (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 (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 (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 {
        custom_radio.set_checked(true);
        m_custom_format_input->set_enabled(true);
    }

    m_24_hour_radio->on_checked = [&](bool checked) {
        if (!checked)
            return;
        m_show_seconds_checkbox->set_enabled(true);
        m_custom_format_input->set_enabled(false);
        set_modified(true);
        update_time_format_string();
    };

    twelve_hour_radio.on_checked = [&](bool checked) {
        if (!checked)
            return;
        m_show_seconds_checkbox->set_enabled(true);
        m_custom_format_input->set_enabled(false);
        set_modified(true);
        update_time_format_string();
    };

    m_show_seconds_checkbox->on_checked = [&](bool) {
        set_modified(true);
        update_time_format_string();
    };

    custom_radio.on_checked = [&](bool checked) {
        if (!checked)
            return;
        m_show_seconds_checkbox->set_enabled(false);
        m_custom_format_input->set_enabled(true);
        set_modified(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()
{
    Config::write_string("Taskbar"sv, "Clock"sv, "TimeFormat"sv, m_custom_format_input->text());
}

void ClockSettingsWidget::reset_default_values()
{
    m_24_hour_radio->set_checked(true);
    m_show_seconds_checkbox->set_checked(true);
    Config::write_string("Taskbar"sv, "Clock"sv, "TimeFormat"sv, time_format_24h_seconds);
}

void ClockSettingsWidget::update_time_format_string()
{
    bool show_seconds = m_show_seconds_checkbox->is_checked();
    if (m_24_hour_radio->is_checked())
        m_time_format = (show_seconds ? time_format_24h_seconds : time_format_24h);
    else
        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));
}