ClockSettingsWidget.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "ClockSettingsWidget.h"
  8. #include <Applications/ClockSettings/ClockSettingsWidgetGML.h>
  9. #include <LibConfig/Client.h>
  10. #include <LibCore/DateTime.h>
  11. #include <LibGUI/CheckBox.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/RadioButton.h>
  14. #include <LibGUI/TextBox.h>
  15. constexpr auto time_format_12h = "%I:%M %p"sv;
  16. constexpr auto time_format_12h_seconds = "%r"sv;
  17. constexpr auto time_format_24h = "%R"sv;
  18. constexpr auto time_format_24h_seconds = "%T"sv;
  19. ClockSettingsWidget::ClockSettingsWidget()
  20. {
  21. load_from_gml(clock_settings_widget_gml).release_value_but_fixme_should_propagate_errors();
  22. m_24_hour_radio = *find_descendant_of_type_named<GUI::RadioButton>("24hour_radio");
  23. auto& twelve_hour_radio = *find_descendant_of_type_named<GUI::RadioButton>("12hour_radio");
  24. m_show_seconds_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("seconds_checkbox");
  25. auto& custom_radio = *find_descendant_of_type_named<GUI::RadioButton>("custom_radio");
  26. m_clock_preview = *find_descendant_of_type_named<GUI::Label>("clock_preview");
  27. m_time_format = Config::read_string("Taskbar"sv, "Clock"sv, "TimeFormat"sv);
  28. m_custom_format_input = *find_descendant_of_type_named<GUI::TextBox>("custom_format_input");
  29. m_custom_format_input->set_text(m_time_format);
  30. m_custom_format_input->set_enabled(false);
  31. m_custom_format_input->on_change = [&] {
  32. m_time_format = m_custom_format_input->get_text();
  33. set_modified(true);
  34. update_clock_preview();
  35. };
  36. if (m_time_format == time_format_12h) {
  37. twelve_hour_radio.set_checked(true, GUI::AllowCallback::No);
  38. m_show_seconds_checkbox->set_checked(false, GUI::AllowCallback::No);
  39. } else if (m_time_format == time_format_12h_seconds) {
  40. twelve_hour_radio.set_checked(true, GUI::AllowCallback::No);
  41. m_show_seconds_checkbox->set_checked(true, GUI::AllowCallback::No);
  42. } else if (m_time_format == time_format_24h) {
  43. m_24_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_24h_seconds) {
  46. m_24_hour_radio->set_checked(true, GUI::AllowCallback::No);
  47. m_show_seconds_checkbox->set_checked(true, GUI::AllowCallback::No);
  48. } else {
  49. custom_radio.set_checked(true);
  50. m_custom_format_input->set_enabled(true);
  51. }
  52. m_24_hour_radio->on_checked = [&](bool checked) {
  53. if (!checked)
  54. return;
  55. m_show_seconds_checkbox->set_enabled(true);
  56. m_custom_format_input->set_enabled(false);
  57. set_modified(true);
  58. update_time_format_string();
  59. };
  60. twelve_hour_radio.on_checked = [&](bool checked) {
  61. if (!checked)
  62. return;
  63. m_show_seconds_checkbox->set_enabled(true);
  64. m_custom_format_input->set_enabled(false);
  65. set_modified(true);
  66. update_time_format_string();
  67. };
  68. m_show_seconds_checkbox->on_checked = [&](bool) {
  69. set_modified(true);
  70. update_time_format_string();
  71. };
  72. custom_radio.on_checked = [&](bool checked) {
  73. if (!checked)
  74. return;
  75. m_show_seconds_checkbox->set_enabled(false);
  76. m_custom_format_input->set_enabled(true);
  77. set_modified(true);
  78. };
  79. m_clock_preview_update_timer = Core::Timer::create_repeating(1000, [&]() {
  80. update_clock_preview();
  81. }).release_value_but_fixme_should_propagate_errors();
  82. m_clock_preview_update_timer->start();
  83. update_clock_preview();
  84. }
  85. void ClockSettingsWidget::apply_settings()
  86. {
  87. Config::write_string("Taskbar"sv, "Clock"sv, "TimeFormat"sv, m_custom_format_input->text());
  88. }
  89. void ClockSettingsWidget::reset_default_values()
  90. {
  91. m_24_hour_radio->set_checked(true);
  92. m_show_seconds_checkbox->set_checked(true);
  93. Config::write_string("Taskbar"sv, "Clock"sv, "TimeFormat"sv, time_format_24h_seconds);
  94. }
  95. void ClockSettingsWidget::update_time_format_string()
  96. {
  97. bool show_seconds = m_show_seconds_checkbox->is_checked();
  98. if (m_24_hour_radio->is_checked())
  99. m_time_format = (show_seconds ? time_format_24h_seconds : time_format_24h);
  100. else
  101. m_time_format = (show_seconds ? time_format_12h_seconds : time_format_12h);
  102. m_custom_format_input->set_text(m_time_format);
  103. update_clock_preview();
  104. }
  105. void ClockSettingsWidget::update_clock_preview()
  106. {
  107. m_clock_preview->set_text(Core::DateTime::now().to_string(m_time_format).release_value_but_fixme_should_propagate_errors());
  108. }