main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CalculatorWidget.h"
  7. #include <LibCore/System.h>
  8. #include <LibCrypto/NumberTheory/ModularFunctions.h>
  9. #include <LibDesktop/Launcher.h>
  10. #include <LibGUI/Action.h>
  11. #include <LibGUI/ActionGroup.h>
  12. #include <LibGUI/Application.h>
  13. #include <LibGUI/Clipboard.h>
  14. #include <LibGUI/Icon.h>
  15. #include <LibGUI/InputBox.h>
  16. #include <LibGUI/Menu.h>
  17. #include <LibGUI/Menubar.h>
  18. #include <LibGUI/Window.h>
  19. #include <LibGfx/Bitmap.h>
  20. #include <LibMain/Main.h>
  21. #include <LibURL/URL.h>
  22. ErrorOr<int> serenity_main(Main::Arguments arguments)
  23. {
  24. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
  25. auto app = TRY(GUI::Application::create(arguments));
  26. auto const man_file = "/usr/share/man/man1/Applications/Calculator.md";
  27. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme(man_file) }));
  28. TRY(Desktop::Launcher::seal_allowlist());
  29. TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
  30. TRY(Core::System::unveil("/res", "r"));
  31. TRY(Core::System::unveil(nullptr, nullptr));
  32. auto app_icon = GUI::Icon::default_icon("app-calculator"sv);
  33. auto window = GUI::Window::construct();
  34. window->set_title("Calculator");
  35. window->set_resizable(false);
  36. window->resize(250, 215);
  37. auto widget = TRY(Calculator::CalculatorWidget::try_create());
  38. window->set_main_widget(widget.ptr());
  39. window->set_icon(app_icon.bitmap_for_size(16));
  40. auto file_menu = window->add_menu("&File"_string);
  41. file_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  42. GUI::Application::the()->quit();
  43. }));
  44. auto edit_menu = window->add_menu("&Edit"_string);
  45. edit_menu->add_action(GUI::CommonActions::make_copy_action([&](auto&) {
  46. GUI::Clipboard::the().set_plain_text(widget->get_entry());
  47. }));
  48. edit_menu->add_action(GUI::CommonActions::make_paste_action([&](auto&) {
  49. auto clipboard = GUI::Clipboard::the().fetch_data_and_type();
  50. if (clipboard.mime_type == "text/plain") {
  51. if (!clipboard.data.is_empty()) {
  52. auto number_or_error = Crypto::BigFraction::from_string(StringView(clipboard.data));
  53. if (!number_or_error.is_error())
  54. widget->set_typed_entry(number_or_error.release_value());
  55. }
  56. }
  57. }));
  58. auto constants_menu = window->add_menu("&Constants"_string);
  59. auto const power = Crypto::NumberTheory::Power("10"_bigint, "10"_bigint);
  60. constants_menu->add_action(GUI::Action::create("&Pi", TRY(Gfx::Bitmap::load_from_file("/res/icons/calculator/pi.png"sv)), [&](auto&) {
  61. widget->set_typed_entry(Crypto::BigFraction { Crypto::SignedBigInteger(31415926535), power });
  62. }));
  63. constants_menu->add_action(GUI::Action::create("&Euler's Number", TRY(Gfx::Bitmap::load_from_file("/res/icons/calculator/eulers_number.png"sv)), [&](auto&) {
  64. widget->set_typed_entry(Crypto::BigFraction { Crypto::SignedBigInteger(27182818284), power });
  65. }));
  66. constants_menu->add_action(GUI::Action::create("&Phi", TRY(Gfx::Bitmap::load_from_file("/res/icons/calculator/phi.png"sv)), [&](auto&) {
  67. widget->set_typed_entry(Crypto::BigFraction { Crypto::SignedBigInteger(16180339887), power });
  68. }));
  69. auto round_menu = window->add_menu("&Round"_string);
  70. GUI::ActionGroup preview_actions;
  71. static constexpr auto rounding_modes = Array { 0, 2, 4 };
  72. Optional<unsigned> last_rounding_mode = 1;
  73. for (unsigned i {}; i < rounding_modes.size(); ++i) {
  74. auto round_action = GUI::Action::create_checkable(ByteString::formatted("To &{} Digits", rounding_modes[i]),
  75. [&widget, rounding_mode = rounding_modes[i], &last_rounding_mode, i](auto&) {
  76. widget->set_rounding_length(rounding_mode);
  77. last_rounding_mode = i;
  78. });
  79. preview_actions.add_action(*round_action);
  80. round_menu->add_action(*round_action);
  81. }
  82. constexpr auto format { "&Custom - {}..."sv };
  83. auto round_custom = GUI::Action::create_checkable(ByteString::formatted(format, 0), [&](auto& action) {
  84. int custom_rounding_length = widget->rounding_length();
  85. auto result = GUI::InputBox::show_numeric(window, custom_rounding_length, 0, 100, "Digits to Round"sv);
  86. if (!result.is_error() && result.value() == GUI::Dialog::ExecResult::OK) {
  87. action.set_text(ByteString::formatted(format, custom_rounding_length));
  88. widget->set_rounding_length(custom_rounding_length);
  89. last_rounding_mode.clear();
  90. } else if (last_rounding_mode.has_value())
  91. round_menu->action_at(last_rounding_mode.value())
  92. ->activate();
  93. });
  94. widget->set_rounding_custom(round_custom, format);
  95. auto shrink_action = GUI::Action::create("&Shrink...", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-cut.png"sv)), [&](auto&) {
  96. int shrink_length = widget->rounding_length();
  97. auto result = GUI::InputBox::show_numeric(window, shrink_length, 0, 100, "Digits to Shrink"sv);
  98. if (!result.is_error() && result.value() == GUI::Dialog::ExecResult::OK) {
  99. round_custom->set_checked(true);
  100. round_custom->set_text(ByteString::formatted(format, shrink_length));
  101. widget->set_rounding_length(shrink_length);
  102. widget->shrink(shrink_length);
  103. }
  104. });
  105. preview_actions.add_action(*round_custom);
  106. preview_actions.set_exclusive(true);
  107. round_menu->add_action(*round_custom);
  108. round_menu->add_action(*shrink_action);
  109. round_menu->action_at(last_rounding_mode.value())->activate();
  110. auto view_menu = window->add_menu("&View"_string);
  111. view_menu->add_action(GUI::CommonActions::make_fullscreen_action([&](auto&) {
  112. window->set_fullscreen(!window->is_fullscreen());
  113. }));
  114. auto help_menu = window->add_menu("&Help"_string);
  115. help_menu->add_action(GUI::CommonActions::make_command_palette_action(window));
  116. help_menu->add_action(GUI::CommonActions::make_help_action([&man_file](auto&) {
  117. Desktop::Launcher::open(URL::create_with_file_scheme(man_file), "/bin/Help");
  118. }));
  119. help_menu->add_action(GUI::CommonActions::make_about_action("Calculator"_string, app_icon, window));
  120. window->show();
  121. return app->exec();
  122. }