mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:27:34 +00:00
Calculator: Use new GML compiler
This commit is contained in:
parent
182126dfda
commit
15a539a5b0
7 changed files with 84 additions and 63 deletions
|
@ -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)
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <AK/Math.h>
|
||||
#include <LibCrypto/BigFraction/BigFraction.h>
|
||||
|
||||
namespace Calculator {
|
||||
|
||||
Optional<Crypto::BigFraction> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,13 +10,14 @@
|
|||
#include <AK/Optional.h>
|
||||
#include <LibCrypto/BigFraction/BigFraction.h>
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
*/
|
||||
|
||||
#include "CalculatorWidget.h"
|
||||
#include <Applications/Calculator/CalculatorGML.h>
|
||||
#include <LibCrypto/BigFraction/BigFraction.h>
|
||||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/Label.h>
|
||||
|
@ -16,85 +15,91 @@
|
|||
#include <LibGfx/Font/Font.h>
|
||||
#include <LibGfx/Palette.h>
|
||||
|
||||
CalculatorWidget::CalculatorWidget()
|
||||
namespace Calculator {
|
||||
|
||||
ErrorOr<NonnullRefPtr<CalculatorWidget>> 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<GUI::TextBox>("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<GUI::TextBox>("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<GUI::Label>("label");
|
||||
m_label->set_frame_style(Gfx::FrameStyle::SunkenContainer);
|
||||
// FIXME: Use GML for this.
|
||||
widget->m_label = *widget->find_descendant_of_type_named<GUI::Label>("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<GUI::Button>(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<GUI::Button>(TRY(String::formatted("{}_button", i)));
|
||||
widget->add_digit_button(*widget->m_digit_button[i], i);
|
||||
}
|
||||
|
||||
m_mem_add_button = *find_descendant_of_type_named<GUI::Button>("mem_add_button");
|
||||
add_operation_button(*m_mem_add_button, Calculator::Operation::MemAdd);
|
||||
widget->m_mem_add_button = *widget->find_descendant_of_type_named<GUI::Button>("mem_add_button");
|
||||
widget->add_operation_button(*widget->m_mem_add_button, Calculator::Operation::MemAdd);
|
||||
|
||||
m_mem_save_button = *find_descendant_of_type_named<GUI::Button>("mem_save_button");
|
||||
add_operation_button(*m_mem_save_button, Calculator::Operation::MemSave);
|
||||
widget->m_mem_save_button = *widget->find_descendant_of_type_named<GUI::Button>("mem_save_button");
|
||||
widget->add_operation_button(*widget->m_mem_save_button, Calculator::Operation::MemSave);
|
||||
|
||||
m_mem_recall_button = *find_descendant_of_type_named<GUI::Button>("mem_recall_button");
|
||||
add_operation_button(*m_mem_recall_button, Calculator::Operation::MemRecall);
|
||||
widget->m_mem_recall_button = *widget->find_descendant_of_type_named<GUI::Button>("mem_recall_button");
|
||||
widget->add_operation_button(*widget->m_mem_recall_button, Calculator::Operation::MemRecall);
|
||||
|
||||
m_mem_clear_button = *find_descendant_of_type_named<GUI::Button>("mem_clear_button");
|
||||
add_operation_button(*m_mem_clear_button, Calculator::Operation::MemClear);
|
||||
widget->m_mem_clear_button = *widget->find_descendant_of_type_named<GUI::Button>("mem_clear_button");
|
||||
widget->add_operation_button(*widget->m_mem_clear_button, Calculator::Operation::MemClear);
|
||||
|
||||
m_clear_button = *find_descendant_of_type_named<GUI::Button>("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<GUI::Button>("clear_button");
|
||||
widget->m_clear_button->on_click = [self = NonnullRefPtr<CalculatorWidget>(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<GUI::Button>("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<GUI::Button>("clear_error_button");
|
||||
widget->m_clear_error_button->on_click = [self = NonnullRefPtr<CalculatorWidget>(widget)](auto) {
|
||||
self->m_keypad.set_to_0();
|
||||
self->update_display();
|
||||
};
|
||||
|
||||
m_backspace_button = *find_descendant_of_type_named<GUI::Button>("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<GUI::Button>("backspace_button");
|
||||
widget->m_backspace_button->on_click = [self = NonnullRefPtr<CalculatorWidget>(widget)](auto) {
|
||||
self->m_keypad.type_backspace();
|
||||
self->update_display();
|
||||
};
|
||||
|
||||
m_decimal_point_button = *find_descendant_of_type_named<GUI::Button>("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<GUI::Button>("decimal_button");
|
||||
widget->m_decimal_point_button->on_click = [self = NonnullRefPtr<CalculatorWidget>(widget)](auto) {
|
||||
self->m_keypad.type_decimal_point();
|
||||
self->update_display();
|
||||
};
|
||||
|
||||
m_sign_button = *find_descendant_of_type_named<GUI::Button>("sign_button");
|
||||
add_operation_button(*m_sign_button, Calculator::Operation::ToggleSign);
|
||||
widget->m_sign_button = *widget->find_descendant_of_type_named<GUI::Button>("sign_button");
|
||||
widget->add_operation_button(*widget->m_sign_button, Calculator::Operation::ToggleSign);
|
||||
|
||||
m_add_button = *find_descendant_of_type_named<GUI::Button>("add_button");
|
||||
add_operation_button(*m_add_button, Calculator::Operation::Add);
|
||||
widget->m_add_button = *widget->find_descendant_of_type_named<GUI::Button>("add_button");
|
||||
widget->add_operation_button(*widget->m_add_button, Calculator::Operation::Add);
|
||||
|
||||
m_subtract_button = *find_descendant_of_type_named<GUI::Button>("subtract_button");
|
||||
add_operation_button(*m_subtract_button, Calculator::Operation::Subtract);
|
||||
widget->m_subtract_button = *widget->find_descendant_of_type_named<GUI::Button>("subtract_button");
|
||||
widget->add_operation_button(*widget->m_subtract_button, Calculator::Operation::Subtract);
|
||||
|
||||
m_multiply_button = *find_descendant_of_type_named<GUI::Button>("multiply_button");
|
||||
add_operation_button(*m_multiply_button, Calculator::Operation::Multiply);
|
||||
widget->m_multiply_button = *widget->find_descendant_of_type_named<GUI::Button>("multiply_button");
|
||||
widget->add_operation_button(*widget->m_multiply_button, Calculator::Operation::Multiply);
|
||||
|
||||
m_divide_button = *find_descendant_of_type_named<GUI::Button>("divide_button");
|
||||
add_operation_button(*m_divide_button, Calculator::Operation::Divide);
|
||||
widget->m_divide_button = *widget->find_descendant_of_type_named<GUI::Button>("divide_button");
|
||||
widget->add_operation_button(*widget->m_divide_button, Calculator::Operation::Divide);
|
||||
|
||||
m_sqrt_button = *find_descendant_of_type_named<GUI::Button>("sqrt_button");
|
||||
add_operation_button(*m_sqrt_button, Calculator::Operation::Sqrt);
|
||||
widget->m_sqrt_button = *widget->find_descendant_of_type_named<GUI::Button>("sqrt_button");
|
||||
widget->add_operation_button(*widget->m_sqrt_button, Calculator::Operation::Sqrt);
|
||||
|
||||
m_inverse_button = *find_descendant_of_type_named<GUI::Button>("inverse_button");
|
||||
add_operation_button(*m_inverse_button, Calculator::Operation::Inverse);
|
||||
widget->m_inverse_button = *widget->find_descendant_of_type_named<GUI::Button>("inverse_button");
|
||||
widget->add_operation_button(*widget->m_inverse_button, Calculator::Operation::Inverse);
|
||||
|
||||
m_percent_button = *find_descendant_of_type_named<GUI::Button>("mod_button");
|
||||
add_operation_button(*m_percent_button, Calculator::Operation::Percent);
|
||||
widget->m_percent_button = *widget->find_descendant_of_type_named<GUI::Button>("mod_button");
|
||||
widget->add_operation_button(*widget->m_percent_button, Calculator::Operation::Percent);
|
||||
|
||||
m_equals_button = *find_descendant_of_type_named<GUI::Button>("equal_button");
|
||||
add_operation_button(*m_equals_button, Calculator::Operation::Equals);
|
||||
widget->m_equals_button = *widget->find_descendant_of_type_named<GUI::Button>("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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,9 +15,13 @@
|
|||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
|
||||
namespace Calculator {
|
||||
|
||||
class CalculatorWidget final : public GUI::Widget {
|
||||
C_OBJECT(CalculatorWidget)
|
||||
public:
|
||||
static ErrorOr<NonnullRefPtr<CalculatorWidget>> 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<NonnullRefPtr<CalculatorWidget>> 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<GUI::Action> m_rounding_custom;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -42,7 +42,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->set_resizable(false);
|
||||
window->resize(250, 215);
|
||||
|
||||
auto widget = window->set_main_widget<CalculatorWidget>();
|
||||
auto widget = TRY(Calculator::CalculatorWidget::create());
|
||||
window->set_main_widget(widget.ptr());
|
||||
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue