MessageBox.cpp 6.0 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. #include <LibGfx/Font/Font.h>
  15. namespace GUI {
  16. Dialog::ExecResult MessageBox::show(Window* parent_window, StringView text, StringView title, Type type, InputType input_type)
  17. {
  18. auto box = MessageBox::construct(parent_window, text, title, type, input_type);
  19. if (parent_window)
  20. box->set_icon(parent_window->icon());
  21. return box->exec();
  22. }
  23. Dialog::ExecResult MessageBox::show_error(Window* parent_window, StringView text)
  24. {
  25. return show(parent_window, text, "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
  26. }
  27. Dialog::ExecResult MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<Time> last_unmodified_timestamp)
  28. {
  29. StringBuilder builder;
  30. builder.append("Save changes to ");
  31. if (path.is_empty())
  32. builder.append("untitled document");
  33. else
  34. builder.appendff("\"{}\"", LexicalPath::basename(path));
  35. builder.append(" before closing?");
  36. if (!path.is_empty() && last_unmodified_timestamp.has_value()) {
  37. auto age = (Time::now_monotonic() - *last_unmodified_timestamp).to_seconds();
  38. auto readable_time = human_readable_time(age);
  39. builder.appendff("\nLast saved {} ago.", readable_time);
  40. }
  41. auto box = MessageBox::construct(parent_window, builder.string_view(), "Unsaved changes", Type::Warning, InputType::YesNoCancel);
  42. if (parent_window)
  43. box->set_icon(parent_window->icon());
  44. box->m_yes_button->set_text(path.is_empty() ? "Save As..." : "Save");
  45. box->m_no_button->set_text("Discard");
  46. box->m_cancel_button->set_text("Cancel");
  47. return box->exec();
  48. }
  49. void MessageBox::set_text(String text)
  50. {
  51. m_text = move(text);
  52. build();
  53. }
  54. MessageBox::MessageBox(Window* parent_window, StringView text, StringView title, Type type, InputType input_type)
  55. : Dialog(parent_window)
  56. , m_text(text)
  57. , m_type(type)
  58. , m_input_type(input_type)
  59. {
  60. set_title(title);
  61. build();
  62. }
  63. RefPtr<Gfx::Bitmap> MessageBox::icon() const
  64. {
  65. switch (m_type) {
  66. case Type::Information:
  67. return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-information.png").release_value_but_fixme_should_propagate_errors();
  68. case Type::Warning:
  69. return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-warning.png").release_value_but_fixme_should_propagate_errors();
  70. case Type::Error:
  71. return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-error.png").release_value_but_fixme_should_propagate_errors();
  72. case Type::Question:
  73. return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-question.png").release_value_but_fixme_should_propagate_errors();
  74. default:
  75. return nullptr;
  76. }
  77. }
  78. bool MessageBox::should_include_ok_button() const
  79. {
  80. return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
  81. }
  82. bool MessageBox::should_include_cancel_button() const
  83. {
  84. return m_input_type == InputType::OKCancel || m_input_type == InputType::YesNoCancel;
  85. }
  86. bool MessageBox::should_include_yes_button() const
  87. {
  88. return m_input_type == InputType::YesNo || m_input_type == InputType::YesNoCancel;
  89. }
  90. bool MessageBox::should_include_no_button() const
  91. {
  92. return should_include_yes_button();
  93. }
  94. void MessageBox::build()
  95. {
  96. auto& widget = set_main_widget<Widget>();
  97. int text_width = widget.font().width(m_text);
  98. auto number_of_lines = m_text.split('\n').size();
  99. int padded_text_height = widget.font().glyph_height() * 1.6;
  100. int total_text_height = number_of_lines * padded_text_height;
  101. int icon_width = 0;
  102. widget.set_layout<VerticalBoxLayout>();
  103. widget.set_fill_with_background_color(true);
  104. widget.layout()->set_margins(8);
  105. widget.layout()->set_spacing(6);
  106. auto& message_container = widget.add<Widget>();
  107. message_container.set_layout<HorizontalBoxLayout>();
  108. message_container.layout()->set_spacing(8);
  109. if (m_type != Type::None) {
  110. auto& icon_image = message_container.add<ImageWidget>();
  111. icon_image.set_bitmap(icon());
  112. if (icon()) {
  113. icon_width = icon()->width();
  114. if (icon_width > 0)
  115. message_container.layout()->set_margins({ 0, 0, 0, 8 });
  116. }
  117. }
  118. auto& label = message_container.add<Label>(m_text);
  119. label.set_fixed_height(total_text_height);
  120. if (m_type != Type::None)
  121. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  122. auto& button_container = widget.add<Widget>();
  123. button_container.set_layout<HorizontalBoxLayout>();
  124. button_container.set_fixed_height(24);
  125. button_container.layout()->set_spacing(8);
  126. constexpr int button_width = 80;
  127. int button_count = 0;
  128. auto add_button = [&](String label, ExecResult result) -> GUI::Button& {
  129. auto& button = button_container.add<Button>();
  130. button.set_fixed_width(button_width);
  131. button.set_text(label);
  132. button.on_click = [this, label, result](auto) {
  133. done(result);
  134. };
  135. ++button_count;
  136. return button;
  137. };
  138. button_container.layout()->add_spacer();
  139. if (should_include_ok_button())
  140. m_ok_button = add_button("OK", ExecResult::OK);
  141. if (should_include_yes_button())
  142. m_yes_button = add_button("Yes", ExecResult::Yes);
  143. if (should_include_no_button())
  144. m_no_button = add_button("No", ExecResult::No);
  145. if (should_include_cancel_button())
  146. m_cancel_button = add_button("Cancel", ExecResult::Cancel);
  147. button_container.layout()->add_spacer();
  148. int width = (button_count * button_width) + ((button_count - 1) * button_container.layout()->spacing()) + 32;
  149. width = max(width, text_width + icon_width + 56);
  150. set_rect(x(), y(), width, 80 + label.max_height());
  151. set_resizable(false);
  152. }
  153. }