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