Skip to content

Commit

Permalink
ClockSettings: Port to GML compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
RatcheT2497 committed Jan 11, 2025
1 parent dabb372 commit 0e87322
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
11 changes: 4 additions & 7 deletions Userland/Applications/ClockSettings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ serenity_component(
TARGETS ClockSettings
)

stringify_gml(ClockSettingsWidget.gml ClockSettingsWidgetGML.h clock_settings_widget_gml)
stringify_gml(TimeZoneSettingsWidget.gml TimeZoneSettingsWidgetGML.h time_zone_settings_widget_gml)
compile_gml(ClockSettingsWidget.gml ClockSettingsWidgetGML.cpp)
compile_gml(TimeZoneSettingsWidget.gml TimeZoneSettingsWidgetGML.cpp)

set(SOURCES
main.cpp
ClockSettingsWidget.cpp
TimeZoneSettingsWidget.cpp
)

set(GENERATED_SOURCES
ClockSettingsWidgetGML.h
TimeZoneSettingsWidgetGML.h
ClockSettingsWidgetGML.cpp
TimeZoneSettingsWidgetGML.cpp
)

serenity_app(ClockSettings ICON app-analog-clock) # FIXME: Create a ClockSettings icon.
Expand Down
17 changes: 6 additions & 11 deletions Userland/Applications/ClockSettings/ClockSettingsWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
/*
* Copyright (c) 2022, cflip <[email protected]>
* Copyright (c) 2022, Sam Atkins <[email protected]>
* Copyright (c) 2025, RatcheT2497 <[email protected]>
*
* 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>

namespace ClockSettings {

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;

ErrorOr<NonnullRefPtr<ClockSettingsWidget>> ClockSettingsWidget::try_create()
{
auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ClockSettingsWidget()));
TRY(widget->setup());
return widget;
}

ErrorOr<void> ClockSettingsWidget::setup()
ErrorOr<void> ClockSettingsWidget::initialize()
{
TRY(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");
Expand Down Expand Up @@ -130,3 +123,5 @@ void ClockSettingsWidget::update_clock_preview()
{
m_clock_preview->set_text(Core::DateTime::now().to_string(m_time_format).release_value_but_fixme_should_propagate_errors());
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@GUI::Frame {
@ClockSettings::ClockSettingsWidget {
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
margins: [10]
Expand Down
7 changes: 6 additions & 1 deletion Userland/Applications/ClockSettings/ClockSettingsWidget.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2022, cflip <[email protected]>
* Copyright (c) 2025, RatcheT2497 <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand All @@ -9,15 +10,17 @@
#include <AK/RefPtr.h>
#include <LibGUI/SettingsWindow.h>

namespace ClockSettings {

class ClockSettingsWidget final : public GUI::SettingsWindow::Tab {
C_OBJECT_ABSTRACT(ClockSettingsWidget)

public:
static ErrorOr<NonnullRefPtr<ClockSettingsWidget>> try_create();
ErrorOr<void> initialize();

private:
ClockSettingsWidget() = default;
ErrorOr<void> setup();

virtual void apply_settings() override;
virtual void reset_default_values() override;
Expand All @@ -34,3 +37,5 @@ class ClockSettingsWidget final : public GUI::SettingsWindow::Tab {

ByteString m_time_format;
};

}
29 changes: 13 additions & 16 deletions Userland/Applications/ClockSettings/TimeZoneSettingsWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2022, Tim Flynn <[email protected]>
* Copyright (c) 2025, RatcheT2497 <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include "TimeZoneSettingsWidget.h"
#include <AK/Time.h>
#include <Applications/ClockSettings/TimeZoneSettingsWidgetGML.h>
#include <LibGUI/ComboBox.h>
#include <LibGUI/Event.h>
#include <LibGUI/ImageWidget.h>
Expand All @@ -24,6 +24,8 @@
#include <spawn.h>
#include <unistd.h>

namespace ClockSettings {

static constexpr auto PI_OVER_180 = M_PIf32 / 180.0f;
static constexpr auto PI_OVER_4 = M_PIf32 / 4.0f;
static constexpr auto TAU = M_PIf32 * 2.0f;
Expand All @@ -38,28 +40,17 @@ static constexpr auto TIME_ZONE_TEXT_HEIGHT = 40;
static constexpr auto TIME_ZONE_TEXT_PADDING = 5;
static constexpr auto TIME_ZONE_TEXT_COLOR = Gfx::Color::from_rgb(0xeaf688);

ErrorOr<NonnullRefPtr<TimeZoneSettingsWidget>> TimeZoneSettingsWidget::create()
ErrorOr<void> TimeZoneSettingsWidget::initialize()
{
auto timezonesettings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) TimeZoneSettingsWidget));

auto time_zone_map_bitmap = TRY(Gfx::Bitmap::load_from_file("/res/graphics/map.png"sv));
auto time_zone_rect = time_zone_map_bitmap->rect().shrunken(TIME_ZONE_MAP_NORTHERN_TRIM, 0, TIME_ZONE_MAP_SOUTHERN_TRIM, 0);
time_zone_map_bitmap = TRY(time_zone_map_bitmap->cropped(time_zone_rect));

timezonesettings_widget->m_time_zone_map = *timezonesettings_widget->find_descendant_of_type_named<GUI::ImageWidget>("time_zone_map");
timezonesettings_widget->m_time_zone_map->set_bitmap(time_zone_map_bitmap);
m_time_zone_map = find_descendant_of_type_named<GUI::ImageWidget>("time_zone_map");
m_time_zone_map->set_bitmap(time_zone_map_bitmap);

auto time_zone_marker = TRY(Gfx::Bitmap::load_from_file("/res/icons/32x32/ladyball.png"sv));
timezonesettings_widget->m_time_zone_marker = TRY(time_zone_marker->scaled(0.75f, 0.75f));

timezonesettings_widget->set_time_zone_location();

return timezonesettings_widget;
}

TimeZoneSettingsWidget::TimeZoneSettingsWidget()
{
load_from_gml(time_zone_settings_widget_gml).release_value_but_fixme_should_propagate_errors();
m_time_zone_marker = TRY(time_zone_marker->scaled(0.75f, 0.75f));

static auto time_zones = []() {
Vector<StringView> time_zones;
Expand All @@ -81,6 +72,10 @@ TimeZoneSettingsWidget::TimeZoneSettingsWidget()
m_time_zone_combo_box->on_change = [&](auto, auto) {
set_modified(true);
};

set_time_zone_location();

return {};
}

void TimeZoneSettingsWidget::second_paint_event(GUI::PaintEvent& event)
Expand Down Expand Up @@ -176,3 +171,5 @@ void TimeZoneSettingsWidget::set_time_zone()
{
GUI::Process::spawn_or_show_error(window(), "/bin/timezone"sv, Array { m_time_zone.characters() });
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@GUI::Frame {
@ClockSettings::TimeZoneSettingsWidget {
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
margins: [10]
Expand Down
10 changes: 8 additions & 2 deletions Userland/Applications/ClockSettings/TimeZoneSettingsWidget.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2022, Tim Flynn <[email protected]>
* Copyright (c) 2025, RatcheT2497 <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand All @@ -13,13 +14,16 @@
#include <LibGUI/TextEditor.h>
#include <LibGUI/Window.h>

namespace ClockSettings {

class TimeZoneSettingsWidget final : public GUI::SettingsWindow::Tab {
C_OBJECT_ABSTRACT(TimeZoneSettingsWidget)
public:
static ErrorOr<NonnullRefPtr<TimeZoneSettingsWidget>> create();
static ErrorOr<NonnullRefPtr<TimeZoneSettingsWidget>> try_create();
ErrorOr<void> initialize();

private:
TimeZoneSettingsWidget();
TimeZoneSettingsWidget() = default;

virtual void second_paint_event(GUI::PaintEvent&) override;

Expand All @@ -38,3 +42,5 @@ class TimeZoneSettingsWidget final : public GUI::SettingsWindow::Tab {
Optional<Gfx::FloatPoint> m_time_zone_location;
ByteString m_time_zone_text;
};

}
4 changes: 2 additions & 2 deletions Userland/Applications/ClockSettings/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app_icon = GUI::Icon::default_icon("app-analog-clock"sv); // FIXME: Create a ClockSettings icon.

auto window = TRY(GUI::SettingsWindow::create("Clock Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes));
(void)TRY(window->add_tab<ClockSettingsWidget>("Clock"_string, "clock"sv));
auto timezonesettings_widget = TRY(TimeZoneSettingsWidget::create());
(void)TRY(window->add_tab<ClockSettings::ClockSettingsWidget>("Clock"_string, "clock"sv));
auto timezonesettings_widget = TRY(ClockSettings::TimeZoneSettingsWidget::try_create());
TRY(window->add_tab(timezonesettings_widget, "Time Zone"_string, "time-zone"sv));

window->set_icon(app_icon.bitmap_for_size(16));
Expand Down

0 comments on commit 0e87322

Please sign in to comment.