mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
Calculator: Add a Rounding menu
This menu has three actions. Each one of them enable a different level of rounding: to 0, 2 or 4 digits.
This commit is contained in:
parent
164094e161
commit
de568f87fd
Notes:
sideshowbarker
2024-07-17 06:23:27 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/de568f87fd Pull-request: https://github.com/SerenityOS/serenity/pull/15322 Issue: https://github.com/SerenityOS/serenity/issues/14062 Reviewed-by: https://github.com/trflynn89
5 changed files with 35 additions and 5 deletions
|
@ -218,3 +218,9 @@ void CalculatorWidget::keydown_event(GUI::KeyEvent& event)
|
|||
|
||||
update_display();
|
||||
}
|
||||
|
||||
void CalculatorWidget::set_rounding_length(unsigned rounding_threshold)
|
||||
{
|
||||
m_keypad.set_rounding_length(rounding_threshold);
|
||||
update_display();
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@ public:
|
|||
String get_entry();
|
||||
void set_entry(Crypto::BigFraction);
|
||||
|
||||
void set_rounding_length(unsigned);
|
||||
|
||||
private:
|
||||
CalculatorWidget();
|
||||
void add_operation_button(GUI::Button&, Calculator::Operation);
|
||||
|
|
|
@ -113,10 +113,8 @@ void Keypad::set_to_0()
|
|||
|
||||
String Keypad::to_string() const
|
||||
{
|
||||
// TODO: Implement custom rounding length in the calculator.
|
||||
constexpr auto maximum_precision = 6;
|
||||
if (m_state == State::External)
|
||||
return m_internal_value.to_string(maximum_precision);
|
||||
return m_internal_value.to_string(m_displayed_fraction_length);
|
||||
|
||||
StringBuilder builder;
|
||||
|
||||
|
@ -136,3 +134,8 @@ String Keypad::to_string() const
|
|||
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
void Keypad::set_rounding_length(unsigned rounding_threshold)
|
||||
{
|
||||
m_displayed_fraction_length = rounding_threshold;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,8 @@ public:
|
|||
void set_value(Crypto::BigFraction);
|
||||
void set_to_0();
|
||||
|
||||
void set_rounding_length(unsigned);
|
||||
|
||||
String to_string() const;
|
||||
|
||||
private:
|
||||
|
@ -46,6 +48,8 @@ private:
|
|||
|
||||
mutable Crypto::BigFraction m_internal_value {};
|
||||
|
||||
unsigned m_displayed_fraction_length { 0 };
|
||||
|
||||
enum class State {
|
||||
External,
|
||||
TypingInteger,
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <LibCore/System.h>
|
||||
#include <LibCrypto/NumberTheory/ModularFunctions.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/ActionGroup.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Clipboard.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
|
@ -16,8 +17,6 @@
|
|||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
|
@ -70,6 +69,22 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
widget->set_entry(Crypto::BigFraction { Crypto::SignedBigInteger(16180339887), power });
|
||||
}));
|
||||
|
||||
auto& round_menu = window->add_menu("&Round");
|
||||
GUI::ActionGroup preview_actions;
|
||||
|
||||
static constexpr auto rounding_modes = Array { 0, 2, 4 };
|
||||
|
||||
for (auto const rounding_mode : rounding_modes) {
|
||||
auto round_action = GUI::Action::create_checkable(String::formatted("To &{} digits", rounding_mode), [&widget, rounding_mode](auto&) {
|
||||
widget->set_rounding_length(rounding_mode);
|
||||
});
|
||||
|
||||
preview_actions.add_action(*round_action);
|
||||
round_menu.add_action(*round_action);
|
||||
}
|
||||
|
||||
preview_actions.set_exclusive(true);
|
||||
|
||||
auto& help_menu = window->add_menu("&Help");
|
||||
help_menu.add_action(GUI::CommonActions::make_about_action("Calculator", app_icon, window));
|
||||
|
||||
|
|
Loading…
Reference in a new issue