MessageBox.cpp 7.2 KB

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