MessageBox.cpp 6.5 KB

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