MessageBox.cpp 5.9 KB

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