1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 05:27:45 +00:00

LibGfx: Use "try_" prefix for static factory functions

Also mark them as [[nodiscard]].
This commit is contained in:
Andreas Kling 2021-07-21 18:02:15 +02:00
parent f0409081f5
commit c7d891765c
131 changed files with 422 additions and 421 deletions

View file

@ -39,8 +39,8 @@ RefPtr<Gfx::Bitmap> ClipboardServerConnection::get_bitmap()
return nullptr;
auto data = ByteBuffer::copy(clipping.data().data<void>(), clipping.data().size());
auto clipping_bitmap = Gfx::Bitmap::create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), data.data());
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { (int)width.value(), (int)height.value() }, scale.value());
auto clipping_bitmap = Gfx::Bitmap::try_create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), data.data());
auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { (int)width.value(), (int)height.value() }, scale.value());
for (int y = 0; y < clipping_bitmap->physical_height(); ++y) {
for (int x = 0; x < clipping_bitmap->physical_width(); ++x) {

View file

@ -62,7 +62,7 @@ ClockWidget::ClockWidget()
m_prev_date = navigation_container.add<GUI::Button>();
m_prev_date->set_button_style(Gfx::ButtonStyle::Coolbar);
m_prev_date->set_fixed_size(24, 24);
m_prev_date->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"));
m_prev_date->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-back.png"));
m_prev_date->on_click = [&](auto) {
unsigned view_month = m_calendar->view_month();
unsigned view_year = m_calendar->view_year();
@ -96,7 +96,7 @@ ClockWidget::ClockWidget()
m_next_date = navigation_container.add<GUI::Button>();
m_next_date->set_button_style(Gfx::ButtonStyle::Coolbar);
m_next_date->set_fixed_size(24, 24);
m_next_date->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"));
m_next_date->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png"));
m_next_date->on_click = [&](auto) {
unsigned view_month = m_calendar->view_month();
unsigned view_year = m_calendar->view_year();
@ -146,7 +146,7 @@ ClockWidget::ClockWidget()
m_jump_to_button = settings_container.add<GUI::Button>();
m_jump_to_button->set_button_style(Gfx::ButtonStyle::Coolbar);
m_jump_to_button->set_fixed_size(24, 24);
m_jump_to_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/calendar-date.png"));
m_jump_to_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/calendar-date.png"));
m_jump_to_button->set_tooltip("Jump to today");
m_jump_to_button->on_click = [this](auto) {
jump_to_current_date();
@ -155,7 +155,7 @@ ClockWidget::ClockWidget()
m_calendar_launcher = settings_container.add<GUI::Button>();
m_calendar_launcher->set_button_style(Gfx::ButtonStyle::Coolbar);
m_calendar_launcher->set_fixed_size(24, 24);
m_calendar_launcher->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-calendar.png"));
m_calendar_launcher->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-calendar.png"));
m_calendar_launcher->set_tooltip("Calendar");
m_calendar_launcher->on_click = [](auto) {
pid_t pid;

View file

@ -63,7 +63,7 @@ ShutdownDialog::ShutdownDialog()
icon_wrapper.set_layout<GUI::VerticalBoxLayout>();
auto& icon_image = icon_wrapper.add<GUI::ImageWidget>();
icon_image.set_bitmap(Gfx::Bitmap::load_from_file("/res/icons/32x32/shutdown.png"));
icon_image.set_bitmap(Gfx::Bitmap::try_load_from_file("/res/icons/32x32/shutdown.png"));
auto& right_container = content_container.add<GUI::Widget>();
right_container.set_layout<GUI::VerticalBoxLayout>();
@ -112,7 +112,7 @@ ShutdownDialog::ShutdownDialog()
center_on_screen();
set_resizable(false);
set_title("Exit SerenityOS");
set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png"));
set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/power.png"));
// Request WindowServer to re-update us on the current theme as we might've not been alive for the last notification.
refresh_system_theme();

View file

@ -78,7 +78,7 @@ TaskbarWindow::TaskbarWindow(NonnullRefPtr<GUI::Menu> start_menu)
m_task_button_container->set_layout<GUI::HorizontalBoxLayout>();
m_task_button_container->layout()->set_spacing(3);
m_default_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png");
m_default_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/window.png");
m_applet_area_container = main_widget.add<GUI::Frame>();
m_applet_area_container->set_frame_thickness(1);

View file

@ -110,7 +110,7 @@ NonnullRefPtr<GUI::Menu> build_system_menu()
const Vector<String> sorted_app_categories = discover_apps_and_categories();
auto system_menu = GUI::Menu::construct("\xE2\x9A\xA1"); // HIGH VOLTAGE SIGN
system_menu->add_action(GUI::Action::create("About SerenityOS", Gfx::Bitmap::load_from_file("/res/icons/16x16/ladyball.png"), [](auto&) {
system_menu->add_action(GUI::Action::create("About SerenityOS", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/ladyball.png"), [](auto&) {
pid_t child_pid;
const char* argv[] = { "/bin/About", nullptr };
if ((errno = posix_spawn(&child_pid, "/bin/About", nullptr, nullptr, const_cast<char**>(argv), environ))) {
@ -152,7 +152,7 @@ NonnullRefPtr<GUI::Menu> build_system_menu()
auto& category_menu = parent_menu->add_submenu(child_category);
auto category_icon_path = category_icons->read_entry("16x16", category);
if (!category_icon_path.is_empty()) {
auto icon = Gfx::Bitmap::load_from_file(category_icon_path);
auto icon = Gfx::Bitmap::try_load_from_file(category_icon_path);
category_menu.set_icon(icon);
}
app_category_menus.set(category, category_menu);
@ -200,7 +200,7 @@ NonnullRefPtr<GUI::Menu> build_system_menu()
g_themes_group.set_unchecking_allowed(false);
g_themes_menu = &system_menu->add_submenu("Themes");
g_themes_menu->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/themes.png"));
g_themes_menu->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/themes.png"));
{
Core::DirIterator dt("/res/themes", Core::DirIterator::SkipDots);
@ -232,7 +232,7 @@ NonnullRefPtr<GUI::Menu> build_system_menu()
}
system_menu->add_separator();
system_menu->add_action(GUI::Action::create("Help", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-help.png"), [](auto&) {
system_menu->add_action(GUI::Action::create("Help", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-help.png"), [](auto&) {
pid_t child_pid;
const char* argv[] = { "/bin/Help", nullptr };
if ((errno = posix_spawn(&child_pid, "/bin/Help", nullptr, nullptr, const_cast<char**>(argv), environ))) {
@ -242,7 +242,7 @@ NonnullRefPtr<GUI::Menu> build_system_menu()
perror("disown");
}
}));
system_menu->add_action(GUI::Action::create("Run...", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-run.png"), [](auto&) {
system_menu->add_action(GUI::Action::create("Run...", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-run.png"), [](auto&) {
pid_t child_pid;
const char* argv[] = { "/bin/Run", nullptr };
if ((errno = posix_spawn(&child_pid, "/bin/Run", nullptr, nullptr, const_cast<char**>(argv), environ))) {
@ -253,7 +253,7 @@ NonnullRefPtr<GUI::Menu> build_system_menu()
}
}));
system_menu->add_separator();
system_menu->add_action(GUI::Action::create("Exit...", Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png"), [](auto&) {
system_menu->add_action(GUI::Action::create("Exit...", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/power.png"), [](auto&) {
auto command = ShutdownDialog::show();
if (command.size() == 0)

View file

@ -680,7 +680,7 @@ void ClientConnection::set_window_backing_store(i32 window_id, [[maybe_unused]]
did_misbehave("SetWindowBackingStore: Failed to create anonymous buffer for window backing store");
return;
}
auto backing_store = Gfx::Bitmap::create_with_anonymous_buffer(
auto backing_store = Gfx::Bitmap::try_create_with_anonymous_buffer(
has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888,
move(buffer),
size,
@ -1001,7 +1001,7 @@ Messages::WindowServer::GetScreenBitmapResponse ClientConnection::get_screen_bit
return bitmap.to_shareable_bitmap();
}
// TODO: Mixed scale setups at what scale? Lowest? Highest? Configurable?
if (auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Screen::bounding_rect().size(), 1)) {
if (auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, Screen::bounding_rect().size(), 1)) {
Gfx::Painter painter(*bitmap);
Screen::for_each([&](auto& screen) {
auto screen_rect = screen.rect();
@ -1043,7 +1043,7 @@ Messages::WindowServer::GetScreenBitmapAroundCursorResponse ClientConnection::ge
return bitmap->to_shareable_bitmap();
}
if (auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, rect.size(), 1)) {
if (auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, rect.size(), 1)) {
auto bounding_screen_src_rect = Screen::bounding_rect().intersected(rect);
Gfx::Painter painter(*bitmap);
auto& screen_with_cursor = ScreenInput::the().cursor_location_screen();

View file

@ -99,20 +99,20 @@ void CompositorScreenData::init_bitmaps(Compositor& compositor, Screen& screen)
auto size = screen.size();
m_front_bitmap = nullptr;
m_front_bitmap = Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor(), screen.pitch(), screen.scanline(0, 0));
m_front_bitmap = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor(), screen.pitch(), screen.scanline(0, 0));
m_front_painter = make<Gfx::Painter>(*m_front_bitmap);
m_front_painter->translate(-screen.rect().location());
m_back_bitmap = nullptr;
if (m_screen_can_set_buffer)
m_back_bitmap = Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor(), screen.pitch(), screen.scanline(1, 0));
m_back_bitmap = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor(), screen.pitch(), screen.scanline(1, 0));
else
m_back_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor());
m_back_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor());
m_back_painter = make<Gfx::Painter>(*m_back_bitmap);
m_back_painter->translate(-screen.rect().location());
m_temp_bitmap = nullptr;
m_temp_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor());
m_temp_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor());
m_temp_painter = make<Gfx::Painter>(*m_temp_bitmap);
m_temp_painter->translate(-screen.rect().location());
}
@ -786,7 +786,7 @@ bool Compositor::set_wallpaper(const String& path, Function<void(bool)>&& callba
{
Threading::BackgroundAction<RefPtr<Gfx::Bitmap>>::create(
[path](auto&) {
return Gfx::Bitmap::load_from_file(path);
return Gfx::Bitmap::try_load_from_file(path);
},
[this, path, callback = move(callback)](RefPtr<Gfx::Bitmap> bitmap) {
@ -915,7 +915,7 @@ void CompositorScreenData::draw_cursor(Screen& screen, const Gfx::IntRect& curso
auto& wm = WindowManager::the();
if (!m_cursor_back_bitmap || m_cursor_back_bitmap->size() != cursor_rect.size() || m_cursor_back_bitmap->scale() != screen.scale_factor()) {
m_cursor_back_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, cursor_rect.size(), screen.scale_factor());
m_cursor_back_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, cursor_rect.size(), screen.scale_factor());
m_cursor_back_painter = make<Gfx::Painter>(*m_cursor_back_bitmap);
}

View file

@ -132,7 +132,7 @@ bool Cursor::load(const StringView& filename, const StringView& default_filename
bool did_load_any = false;
auto load_bitmap = [&](const StringView& path, int scale_factor) {
auto bitmap = Gfx::Bitmap::load_from_file(path, scale_factor);
auto bitmap = Gfx::Bitmap::try_load_from_file(path, scale_factor);
if (bitmap) {
did_load_any = true;
m_bitmaps.set(scale_factor, bitmap.release_nonnull());

View file

@ -52,7 +52,7 @@ bool MultiScaleBitmaps::load(StringView const& filename, StringView const& defau
m_bitmaps.clear(); // If we're reloading the bitmaps get rid of the old ones
auto add_bitmap = [&](StringView const& path, int scale_factor) {
auto bitmap = Gfx::Bitmap::load_from_file(path, scale_factor);
auto bitmap = Gfx::Bitmap::try_load_from_file(path, scale_factor);
if (bitmap) {
auto bitmap_format = bitmap->format();
if (m_format == Gfx::BitmapFormat::Invalid || m_format == bitmap_format) {

View file

@ -106,7 +106,7 @@ void RectangularOverlay::render(Gfx::Painter& painter, Screen const& screen)
auto scale_factor = screen.scale_factor();
auto* bitmap = m_rendered_bitmaps->find_bitmap(scale_factor);
if (!bitmap) {
auto new_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect().size(), scale_factor);
auto new_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, rect().size(), scale_factor);
if (!new_bitmap)
return;
bitmap = new_bitmap.ptr();
@ -291,7 +291,7 @@ void DndOverlay::update_rect()
RefPtr<Gfx::Bitmap> DndOverlay::create_bitmap(int scale_factor)
{
auto new_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect().size(), scale_factor);
auto new_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, rect().size(), scale_factor);
if (!new_bitmap)
return {};

View file

@ -31,7 +31,7 @@ static Gfx::Bitmap& default_window_icon()
{
static Gfx::Bitmap* s_icon;
if (!s_icon)
s_icon = Gfx::Bitmap::load_from_file(default_window_icon_path()).leak_ref();
s_icon = Gfx::Bitmap::try_load_from_file(default_window_icon_path()).leak_ref();
return *s_icon;
}
@ -39,7 +39,7 @@ static Gfx::Bitmap& minimize_icon()
{
static Gfx::Bitmap* s_icon;
if (!s_icon)
s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png").leak_ref();
s_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/downward-triangle.png").leak_ref();
return *s_icon;
}
@ -47,7 +47,7 @@ static Gfx::Bitmap& maximize_icon()
{
static Gfx::Bitmap* s_icon;
if (!s_icon)
s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/upward-triangle.png").leak_ref();
s_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/upward-triangle.png").leak_ref();
return *s_icon;
}
@ -55,7 +55,7 @@ static Gfx::Bitmap& restore_icon()
{
static Gfx::Bitmap* s_icon;
if (!s_icon)
s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-restore.png").leak_ref();
s_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/window-restore.png").leak_ref();
return *s_icon;
}
@ -63,7 +63,7 @@ static Gfx::Bitmap& close_icon()
{
static Gfx::Bitmap* s_icon;
if (!s_icon)
s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-close.png").leak_ref();
s_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/window-close.png").leak_ref();
return *s_icon;
}
@ -71,7 +71,7 @@ static Gfx::Bitmap& pin_icon()
{
static Gfx::Bitmap* s_icon;
if (!s_icon)
s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-pin.png").leak_ref();
s_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/window-pin.png").leak_ref();
return *s_icon;
}
Window::Window(Core::Object& parent, WindowType type)
@ -144,7 +144,7 @@ void Window::set_rect(const Gfx::IntRect& rect)
if (rect.is_empty()) {
m_backing_store = nullptr;
} else if (!m_client && (!m_backing_store || old_rect.size() != rect.size())) {
m_backing_store = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, m_rect.size());
m_backing_store = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, m_rect.size());
}
invalidate(true, old_rect.size() != rect.size());

View file

@ -410,7 +410,7 @@ void WindowFrame::PerScaleRenderedCache::render(WindowFrame& frame, Screen& scre
if (tmp_it != s_tmp_bitmap_cache.end())
tmp_it->value = nullptr;
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, frame_rect_including_shadow.size(), scale);
auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, frame_rect_including_shadow.size(), scale);
if (!bitmap) {
s_tmp_bitmap_cache.remove(scale);
dbgln("Could not create bitmap of size {}", frame_rect_including_shadow.size());
@ -433,14 +433,14 @@ void WindowFrame::PerScaleRenderedCache::render(WindowFrame& frame, Screen& scre
if (!m_top_bottom || m_top_bottom->width() != frame_rect_including_shadow.width() || m_top_bottom->height() != top_bottom_height || m_top_bottom->scale() != scale) {
if (top_bottom_height > 0)
m_top_bottom = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { frame_rect_including_shadow.width(), top_bottom_height }, scale);
m_top_bottom = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { frame_rect_including_shadow.width(), top_bottom_height }, scale);
else
m_top_bottom = nullptr;
m_shadow_dirty = true;
}
if (!m_left_right || m_left_right->height() != frame_rect_including_shadow.height() || m_left_right->width() != left_right_width || m_left_right->scale() != scale) {
if (left_right_width > 0)
m_left_right = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { left_right_width, frame_rect_including_shadow.height() }, scale);
m_left_right = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { left_right_width, frame_rect_including_shadow.height() }, scale);
else
m_left_right = nullptr;
m_shadow_dirty = true;