diff --git a/Userland/Applications/Calculator/CMakeLists.txt b/Userland/Applications/Calculator/CMakeLists.txt index 941bf75a39..676e838931 100644 --- a/Userland/Applications/Calculator/CMakeLists.txt +++ b/Userland/Applications/Calculator/CMakeLists.txt @@ -4,17 +4,15 @@ serenity_component( TARGETS Calculator ) -stringify_gml(CalculatorWindow.gml CalculatorGML.h calculator_gml) +compile_gml(CalculatorWindow.gml CalculatorGML.cpp) + set(SOURCES main.cpp Calculator.cpp + CalculatorGML.cpp CalculatorWidget.cpp Keypad.cpp ) -set(GENERATED_SOURCES - CalculatorGML.h -) - serenity_app(Calculator ICON app-calculator) target_link_libraries(Calculator PRIVATE LibCore LibCrypto LibDesktop LibGfx LibGUI LibMain) diff --git a/Userland/Applications/Calculator/Calculator.cpp b/Userland/Applications/Calculator/Calculator.cpp index 7893790450..3f1c73609d 100644 --- a/Userland/Applications/Calculator/Calculator.cpp +++ b/Userland/Applications/Calculator/Calculator.cpp @@ -10,6 +10,8 @@ #include #include +namespace Calculator { + Optional Calculator::operation_with_literal_argument(Operation operation, Crypto::BigFraction argument) { // Support binary operations with percentages, for example "2+3%" == 2.06 @@ -169,3 +171,5 @@ void Calculator::clear_operation() m_binary_operation_saved_left_side.set_to_0(); clear_error(); } + +} diff --git a/Userland/Applications/Calculator/Calculator.h b/Userland/Applications/Calculator/Calculator.h index 7d249a4539..e96cc8ec7b 100644 --- a/Userland/Applications/Calculator/Calculator.h +++ b/Userland/Applications/Calculator/Calculator.h @@ -10,13 +10,14 @@ #include #include +namespace Calculator { + // This type implements the regular calculator // behavior, such as performing arithmetic // operations and providing a memory cell. // It does not deal with number input; you // have to pass in already parsed double // values. - class Calculator final { public: Calculator() = default; @@ -64,3 +65,5 @@ private: Crypto::BigFraction finish_binary_operation(Crypto::BigFraction const& left_side, Operation operation, Crypto::BigFraction const& right_side); }; + +} diff --git a/Userland/Applications/Calculator/CalculatorWidget.cpp b/Userland/Applications/Calculator/CalculatorWidget.cpp index a9b320c7da..cf9dc79373 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.cpp +++ b/Userland/Applications/Calculator/CalculatorWidget.cpp @@ -8,7 +8,6 @@ */ #include "CalculatorWidget.h" -#include #include #include #include @@ -16,85 +15,91 @@ #include #include -CalculatorWidget::CalculatorWidget() +namespace Calculator { + +ErrorOr> CalculatorWidget::create() { - load_from_gml(calculator_gml).release_value_but_fixme_should_propagate_errors(); + auto widget = TRY(CalculatorWidget::try_create()); - m_entry = *find_descendant_of_type_named("entry_textbox"); - m_entry->set_relative_rect(5, 5, 244, 26); - m_entry->set_text_alignment(Gfx::TextAlignment::CenterRight); + widget->m_entry = *widget->find_descendant_of_type_named("entry_textbox"); + // FIXME: Use GML for this. + widget->m_entry->set_relative_rect(5, 5, 244, 26); + widget->m_entry->set_text_alignment(Gfx::TextAlignment::CenterRight); - m_label = *find_descendant_of_type_named("label"); - m_label->set_frame_style(Gfx::FrameStyle::SunkenContainer); + // FIXME: Use GML for this. + widget->m_label = *widget->find_descendant_of_type_named("label"); + widget->m_label->set_frame_style(Gfx::FrameStyle::SunkenContainer); for (int i = 0; i < 10; i++) { - m_digit_button[i] = *find_descendant_of_type_named(String::formatted("{}_button", i).release_value_but_fixme_should_propagate_errors()); - add_digit_button(*m_digit_button[i], i); + widget->m_digit_button[i] = *widget->find_descendant_of_type_named(TRY(String::formatted("{}_button", i))); + widget->add_digit_button(*widget->m_digit_button[i], i); } - m_mem_add_button = *find_descendant_of_type_named("mem_add_button"); - add_operation_button(*m_mem_add_button, Calculator::Operation::MemAdd); + widget->m_mem_add_button = *widget->find_descendant_of_type_named("mem_add_button"); + widget->add_operation_button(*widget->m_mem_add_button, Calculator::Operation::MemAdd); - m_mem_save_button = *find_descendant_of_type_named("mem_save_button"); - add_operation_button(*m_mem_save_button, Calculator::Operation::MemSave); + widget->m_mem_save_button = *widget->find_descendant_of_type_named("mem_save_button"); + widget->add_operation_button(*widget->m_mem_save_button, Calculator::Operation::MemSave); - m_mem_recall_button = *find_descendant_of_type_named("mem_recall_button"); - add_operation_button(*m_mem_recall_button, Calculator::Operation::MemRecall); + widget->m_mem_recall_button = *widget->find_descendant_of_type_named("mem_recall_button"); + widget->add_operation_button(*widget->m_mem_recall_button, Calculator::Operation::MemRecall); - m_mem_clear_button = *find_descendant_of_type_named("mem_clear_button"); - add_operation_button(*m_mem_clear_button, Calculator::Operation::MemClear); + widget->m_mem_clear_button = *widget->find_descendant_of_type_named("mem_clear_button"); + widget->add_operation_button(*widget->m_mem_clear_button, Calculator::Operation::MemClear); - m_clear_button = *find_descendant_of_type_named("clear_button"); - m_clear_button->on_click = [this](auto) { - m_keypad.set_to_0(); - m_calculator.clear_operation(); - update_display(); + widget->m_clear_button = *widget->find_descendant_of_type_named("clear_button"); + widget->m_clear_button->on_click = [self = NonnullRefPtr(widget)](auto) { + self->m_keypad.set_to_0(); + self->m_calculator.clear_operation(); + self->update_display(); }; - m_clear_error_button = *find_descendant_of_type_named("clear_error_button"); - m_clear_error_button->on_click = [this](auto) { - m_keypad.set_to_0(); - update_display(); + widget->m_clear_error_button = *widget->find_descendant_of_type_named("clear_error_button"); + widget->m_clear_error_button->on_click = [self = NonnullRefPtr(widget)](auto) { + self->m_keypad.set_to_0(); + self->update_display(); }; - m_backspace_button = *find_descendant_of_type_named("backspace_button"); - m_backspace_button->on_click = [this](auto) { - m_keypad.type_backspace(); - update_display(); + widget->m_backspace_button = *widget->find_descendant_of_type_named("backspace_button"); + widget->m_backspace_button->on_click = [self = NonnullRefPtr(widget)](auto) { + self->m_keypad.type_backspace(); + self->update_display(); }; - m_decimal_point_button = *find_descendant_of_type_named("decimal_button"); - m_decimal_point_button->on_click = [this](auto) { - m_keypad.type_decimal_point(); - update_display(); + widget->m_decimal_point_button = *widget->find_descendant_of_type_named("decimal_button"); + widget->m_decimal_point_button->on_click = [self = NonnullRefPtr(widget)](auto) { + self->m_keypad.type_decimal_point(); + self->update_display(); }; - m_sign_button = *find_descendant_of_type_named("sign_button"); - add_operation_button(*m_sign_button, Calculator::Operation::ToggleSign); + widget->m_sign_button = *widget->find_descendant_of_type_named("sign_button"); + widget->add_operation_button(*widget->m_sign_button, Calculator::Operation::ToggleSign); - m_add_button = *find_descendant_of_type_named("add_button"); - add_operation_button(*m_add_button, Calculator::Operation::Add); + widget->m_add_button = *widget->find_descendant_of_type_named("add_button"); + widget->add_operation_button(*widget->m_add_button, Calculator::Operation::Add); - m_subtract_button = *find_descendant_of_type_named("subtract_button"); - add_operation_button(*m_subtract_button, Calculator::Operation::Subtract); + widget->m_subtract_button = *widget->find_descendant_of_type_named("subtract_button"); + widget->add_operation_button(*widget->m_subtract_button, Calculator::Operation::Subtract); - m_multiply_button = *find_descendant_of_type_named("multiply_button"); - add_operation_button(*m_multiply_button, Calculator::Operation::Multiply); + widget->m_multiply_button = *widget->find_descendant_of_type_named("multiply_button"); + widget->add_operation_button(*widget->m_multiply_button, Calculator::Operation::Multiply); - m_divide_button = *find_descendant_of_type_named("divide_button"); - add_operation_button(*m_divide_button, Calculator::Operation::Divide); + widget->m_divide_button = *widget->find_descendant_of_type_named("divide_button"); + widget->add_operation_button(*widget->m_divide_button, Calculator::Operation::Divide); - m_sqrt_button = *find_descendant_of_type_named("sqrt_button"); - add_operation_button(*m_sqrt_button, Calculator::Operation::Sqrt); + widget->m_sqrt_button = *widget->find_descendant_of_type_named("sqrt_button"); + widget->add_operation_button(*widget->m_sqrt_button, Calculator::Operation::Sqrt); - m_inverse_button = *find_descendant_of_type_named("inverse_button"); - add_operation_button(*m_inverse_button, Calculator::Operation::Inverse); + widget->m_inverse_button = *widget->find_descendant_of_type_named("inverse_button"); + widget->add_operation_button(*widget->m_inverse_button, Calculator::Operation::Inverse); - m_percent_button = *find_descendant_of_type_named("mod_button"); - add_operation_button(*m_percent_button, Calculator::Operation::Percent); + widget->m_percent_button = *widget->find_descendant_of_type_named("mod_button"); + widget->add_operation_button(*widget->m_percent_button, Calculator::Operation::Percent); - m_equals_button = *find_descendant_of_type_named("equal_button"); - add_operation_button(*m_equals_button, Calculator::Operation::Equals); + widget->m_equals_button = *widget->find_descendant_of_type_named("equal_button"); + widget->add_operation_button(*widget->m_equals_button, Calculator::Operation::Equals); + + return widget; } void CalculatorWidget::perform_operation(Calculator::Operation operation) @@ -212,3 +217,5 @@ void CalculatorWidget::set_rounding_custom(GUI::Action& action, StringView forma m_format = format; m_rounding_custom = action; } + +} diff --git a/Userland/Applications/Calculator/CalculatorWidget.h b/Userland/Applications/Calculator/CalculatorWidget.h index 603b70d0de..fc7d2152fc 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.h +++ b/Userland/Applications/Calculator/CalculatorWidget.h @@ -15,9 +15,13 @@ #include #include +namespace Calculator { + class CalculatorWidget final : public GUI::Widget { C_OBJECT(CalculatorWidget) public: + static ErrorOr> create(); + virtual ~CalculatorWidget() override = default; String get_entry(); void set_entry(Crypto::BigFraction); @@ -30,7 +34,9 @@ public: void set_rounding_custom(GUI::Action& action, StringView); private: - CalculatorWidget(); + static ErrorOr> try_create(); + CalculatorWidget() = default; + void add_operation_button(GUI::Button&, Calculator::Operation); void add_digit_button(GUI::Button&, int digit); @@ -67,3 +73,5 @@ private: StringView m_format; RefPtr m_rounding_custom; }; + +} diff --git a/Userland/Applications/Calculator/CalculatorWindow.gml b/Userland/Applications/Calculator/CalculatorWindow.gml index 58c43af364..f137cf3cc4 100644 --- a/Userland/Applications/Calculator/CalculatorWindow.gml +++ b/Userland/Applications/Calculator/CalculatorWindow.gml @@ -1,4 +1,4 @@ -@GUI::Widget { +@Calculator::CalculatorWidget { fixed_width: 250 fixed_height: 215 fill_with_background_color: true @@ -18,7 +18,7 @@ @GUI::TextBox { name: "entry_textbox" - font_type: "FixedWidth" + font_fixed_width: true mode: "DisplayOnly" focus_policy: "NoFocus" } diff --git a/Userland/Applications/Calculator/main.cpp b/Userland/Applications/Calculator/main.cpp index efe42fcb14..508d95c959 100644 --- a/Userland/Applications/Calculator/main.cpp +++ b/Userland/Applications/Calculator/main.cpp @@ -42,7 +42,8 @@ ErrorOr serenity_main(Main::Arguments arguments) window->set_resizable(false); window->resize(250, 215); - auto widget = window->set_main_widget(); + auto widget = TRY(Calculator::CalculatorWidget::create()); + window->set_main_widget(widget.ptr()); window->set_icon(app_icon.bitmap_for_size(16));