MessageBox.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/LexicalPath.h>
  8. #include <AK/NumberFormat.h>
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/Button.h>
  11. #include <LibGUI/ImageWidget.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/MessageBox.h>
  14. namespace GUI {
  15. ErrorOr<NonnullRefPtr<MessageBox>> MessageBox::create(Window* parent_window, StringView text, StringView title, Type type, InputType input_type)
  16. {
  17. auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MessageBox(parent_window, type, input_type)));
  18. TRY(box->build());
  19. box->set_title(TRY(String::from_utf8(title)).to_deprecated_string());
  20. box->set_text(TRY(String::from_utf8(text)));
  21. auto size = box->main_widget()->effective_min_size();
  22. box->resize(TRY(size.width().shrink_value()), TRY(size.height().shrink_value()));
  23. return box;
  24. }
  25. Dialog::ExecResult MessageBox::show(Window* parent_window, StringView text, StringView title, Type type, InputType input_type)
  26. {
  27. return MUST(try_show(parent_window, text, title, type, input_type));
  28. }
  29. ErrorOr<Dialog::ExecResult> MessageBox::try_show(Window* parent_window, StringView text, StringView title, Type type, InputType input_type)
  30. {
  31. auto box = TRY(MessageBox::create(parent_window, text, title, type, input_type));
  32. if (parent_window)
  33. box->set_icon(parent_window->icon());
  34. return box->exec();
  35. }
  36. Dialog::ExecResult MessageBox::show_error(Window* parent_window, StringView text)
  37. {
  38. return MUST(try_show_error(parent_window, text));
  39. }
  40. ErrorOr<Dialog::ExecResult> MessageBox::try_show_error(Window* parent_window, StringView text)
  41. {
  42. return TRY(try_show(parent_window, text, "Error"sv, GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK));
  43. }
  44. Dialog::ExecResult MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<Time> last_unmodified_timestamp)
  45. {
  46. return MUST(try_ask_about_unsaved_changes(parent_window, path, last_unmodified_timestamp));
  47. }
  48. ErrorOr<Dialog::ExecResult> MessageBox::try_ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<Time> last_unmodified_timestamp)
  49. {
  50. StringBuilder builder;
  51. TRY(builder.try_append("Save changes to "sv));
  52. if (path.is_empty())
  53. TRY(builder.try_append("untitled document"sv));
  54. else
  55. TRY(builder.try_appendff("\"{}\"", LexicalPath::basename(path)));
  56. TRY(builder.try_append(" before closing?"sv));
  57. if (!path.is_empty() && last_unmodified_timestamp.has_value()) {
  58. auto age = (Time::now_monotonic() - *last_unmodified_timestamp).to_seconds();
  59. auto readable_time = human_readable_time(age);
  60. TRY(builder.try_appendff("\nLast saved {} ago.", readable_time));
  61. }
  62. auto box = TRY(MessageBox::create(parent_window, builder.string_view(), "Unsaved changes"sv, Type::Warning, InputType::YesNoCancel));
  63. if (parent_window)
  64. box->set_icon(parent_window->icon());
  65. if (path.is_empty())
  66. box->m_yes_button->set_text(TRY("Save As..."_string));
  67. else
  68. box->m_yes_button->set_text("Save"_short_string);
  69. box->m_no_button->set_text("Discard"_short_string);
  70. box->m_cancel_button->set_text("Cancel"_short_string);
  71. return box->exec();
  72. }
  73. void MessageBox::set_text(String text)
  74. {
  75. m_text_label->set_text(move(text).to_deprecated_string());
  76. }
  77. MessageBox::MessageBox(Window* parent_window, Type type, InputType input_type)
  78. : Dialog(parent_window)
  79. , m_type(type)
  80. , m_input_type(input_type)
  81. {
  82. set_resizable(false);
  83. set_auto_shrink(true);
  84. }
  85. ErrorOr<RefPtr<Gfx::Bitmap>> MessageBox::icon() const
  86. {
  87. switch (m_type) {
  88. case Type::Information:
  89. return TRY(Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-information.png"sv));
  90. case Type::Warning:
  91. return TRY(Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-warning.png"sv));
  92. case Type::Error:
  93. return TRY(Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-error.png"sv));
  94. case Type::Question:
  95. return TRY(Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-question.png"sv));
  96. default:
  97. return nullptr;
  98. }
  99. }
  100. bool MessageBox::should_include_ok_button() const
  101. {
  102. return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
  103. }
  104. bool MessageBox::should_include_cancel_button() const
  105. {
  106. return m_input_type == InputType::OKCancel || m_input_type == InputType::YesNoCancel;
  107. }
  108. bool MessageBox::should_include_yes_button() const
  109. {
  110. return m_input_type == InputType::YesNo || m_input_type == InputType::YesNoCancel;
  111. }
  112. bool MessageBox::should_include_no_button() const
  113. {
  114. return should_include_yes_button();
  115. }
  116. ErrorOr<void> MessageBox::build()
  117. {
  118. auto main_widget = TRY(set_main_widget<Widget>());
  119. main_widget->set_fill_with_background_color(true);
  120. TRY(main_widget->try_set_layout<VerticalBoxLayout>(8, 6));
  121. auto message_container = TRY(main_widget->try_add<Widget>());
  122. auto message_margins = Margins { 8, m_type != Type::None ? 8 : 0 };
  123. TRY(message_container->try_set_layout<HorizontalBoxLayout>(message_margins, 8));
  124. if (auto icon = TRY(this->icon()); icon && m_type != Type::None) {
  125. auto image_widget = TRY(message_container->try_add<ImageWidget>());
  126. image_widget->set_bitmap(icon);
  127. }
  128. m_text_label = TRY(message_container->try_add<Label>());
  129. m_text_label->set_text_wrapping(Gfx::TextWrapping::DontWrap);
  130. m_text_label->set_autosize(true);
  131. if (m_type != Type::None)
  132. m_text_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  133. auto button_container = TRY(main_widget->try_add<Widget>());
  134. TRY(button_container->try_set_layout<HorizontalBoxLayout>(Margins {}, 8));
  135. auto add_button = [&](String text, ExecResult result) -> ErrorOr<NonnullRefPtr<Button>> {
  136. auto button = TRY(button_container->try_add<DialogButton>());
  137. button->set_text(move(text));
  138. button->on_click = [this, result](auto) { done(result); };
  139. return button;
  140. };
  141. TRY(button_container->add_spacer());
  142. if (should_include_ok_button())
  143. m_ok_button = TRY(add_button("OK"_short_string, ExecResult::OK));
  144. if (should_include_yes_button())
  145. m_yes_button = TRY(add_button("Yes"_short_string, ExecResult::Yes));
  146. if (should_include_no_button())
  147. m_no_button = TRY(add_button("No"_short_string, ExecResult::No));
  148. if (should_include_cancel_button())
  149. m_cancel_button = TRY(add_button("Cancel"_short_string, ExecResult::Cancel));
  150. TRY(button_container->add_spacer());
  151. return {};
  152. }
  153. }