UI/AppKit: Extract gfx_bitmap_to_ns_image() code

This commit is contained in:
Sam Atkins 2025-02-20 14:24:43 +00:00 committed by Andreas Kling
parent bfd7ac1204
commit e0b5e742de
Notes: github-actions[bot] 2025-02-28 12:51:16 +00:00
3 changed files with 19 additions and 11 deletions

View file

@ -7,7 +7,6 @@
#include <AK/ByteString.h>
#include <AK/String.h>
#include <LibCore/Resource.h>
#include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibURL/URL.h>
#include <LibWebView/ViewImplementation.h>
@ -348,17 +347,8 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
- (void)onFaviconChange:(Gfx::Bitmap const&)bitmap
{
auto png = Gfx::PNGWriter::encode(bitmap);
if (png.is_error()) {
return;
}
auto* data = [NSData dataWithBytes:png.value().data()
length:png.value().size()];
auto* favicon = [[NSImage alloc] initWithData:data];
auto* favicon = Ladybird::gfx_bitmap_to_ns_image(bitmap);
[favicon setResizingMode:NSImageResizingModeStretch];
self.favicon = favicon;
[self updateTabTitleAndFavicon];
}

View file

@ -9,6 +9,7 @@
#include <AK/ByteString.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Color.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
@ -40,4 +41,6 @@ NSColor* gfx_color_to_ns_color(Gfx::Color);
Gfx::IntPoint compute_origin_relative_to_window(NSWindow*, Gfx::IntPoint);
NSImage* gfx_bitmap_to_ns_image(Gfx::Bitmap const&);
}

View file

@ -1,9 +1,12 @@
/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/ImageFormats/PNGWriter.h>
#import <Utilities/Conversions.h>
namespace Ladybird {
@ -128,4 +131,16 @@ Gfx::IntPoint compute_origin_relative_to_window(NSWindow* window, Gfx::IntPoint
return position;
}
NSImage* gfx_bitmap_to_ns_image(Gfx::Bitmap const& bitmap)
{
auto png = Gfx::PNGWriter::encode(bitmap);
if (png.is_error())
return nullptr;
auto* data = [NSData dataWithBytes:png.value().data()
length:png.value().size()];
return [[NSImage alloc] initWithData:data];
}
}