From 28ec2240c4be8235029cb02265cd7549e8324492 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:19:47 +0300 Subject: [PATCH] Improve native menu and status indicator icons conversion and checks. --- platform/macos/display_server_macos.mm | 74 +++------------------ platform/macos/native_menu_macos.mm | 8 +-- platform/windows/display_server_windows.cpp | 10 ++- platform/windows/native_menu_windows.cpp | 40 ++++++----- 4 files changed, 44 insertions(+), 88 deletions(-) diff --git a/platform/macos/display_server_macos.mm b/platform/macos/display_server_macos.mm index 50313cfe67a..f945ab53f0a 100644 --- a/platform/macos/display_server_macos.mm +++ b/platform/macos/display_server_macos.mm @@ -3172,42 +3172,13 @@ void DisplayServerMacOS::set_icon(const Ref &p_icon) { DisplayServer::IndicatorID DisplayServerMacOS::create_status_indicator(const Ref &p_icon, const String &p_tooltip, const Callable &p_callback) { NSImage *nsimg = nullptr; - if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0) { + if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { Ref img = p_icon->get_image(); img = img->duplicate(); - img->convert(Image::FORMAT_RGBA8); - - NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc] - initWithBitmapDataPlanes:nullptr - pixelsWide:img->get_width() - pixelsHigh:img->get_height() - bitsPerSample:8 - samplesPerPixel:4 - hasAlpha:YES - isPlanar:NO - colorSpaceName:NSDeviceRGBColorSpace - bytesPerRow:img->get_width() * 4 - bitsPerPixel:32]; - if (imgrep) { - uint8_t *pixels = [imgrep bitmapData]; - - int len = img->get_width() * img->get_height(); - const uint8_t *r = img->get_data().ptr(); - - /* Premultiply the alpha channel */ - for (int i = 0; i < len; i++) { - uint8_t alpha = r[i * 4 + 3]; - pixels[i * 4 + 0] = (uint8_t)(((uint16_t)r[i * 4 + 0] * alpha) / 255); - pixels[i * 4 + 1] = (uint8_t)(((uint16_t)r[i * 4 + 1] * alpha) / 255); - pixels[i * 4 + 2] = (uint8_t)(((uint16_t)r[i * 4 + 2] * alpha) / 255); - pixels[i * 4 + 3] = alpha; - } - - nsimg = [[NSImage alloc] initWithSize:NSMakeSize(img->get_width(), img->get_height())]; - if (nsimg) { - [nsimg addRepresentation:imgrep]; - } + if (img->is_compressed()) { + img->decompress(); } + nsimg = _convert_to_nsimg(img); } IndicatorData idat; @@ -3235,42 +3206,13 @@ void DisplayServerMacOS::status_indicator_set_icon(IndicatorID p_id, const Refget_width() > 0 && p_icon->get_height() > 0) { + if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { Ref img = p_icon->get_image(); img = img->duplicate(); - img->convert(Image::FORMAT_RGBA8); - - NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc] - initWithBitmapDataPlanes:nullptr - pixelsWide:img->get_width() - pixelsHigh:img->get_height() - bitsPerSample:8 - samplesPerPixel:4 - hasAlpha:YES - isPlanar:NO - colorSpaceName:NSDeviceRGBColorSpace - bytesPerRow:img->get_width() * 4 - bitsPerPixel:32]; - if (imgrep) { - uint8_t *pixels = [imgrep bitmapData]; - - int len = img->get_width() * img->get_height(); - const uint8_t *r = img->get_data().ptr(); - - /* Premultiply the alpha channel */ - for (int i = 0; i < len; i++) { - uint8_t alpha = r[i * 4 + 3]; - pixels[i * 4 + 0] = (uint8_t)(((uint16_t)r[i * 4 + 0] * alpha) / 255); - pixels[i * 4 + 1] = (uint8_t)(((uint16_t)r[i * 4 + 1] * alpha) / 255); - pixels[i * 4 + 2] = (uint8_t)(((uint16_t)r[i * 4 + 2] * alpha) / 255); - pixels[i * 4 + 3] = alpha; - } - - nsimg = [[NSImage alloc] initWithSize:NSMakeSize(img->get_width(), img->get_height())]; - if (nsimg) { - [nsimg addRepresentation:imgrep]; - } + if (img->is_compressed()) { + img->decompress(); } + nsimg = _convert_to_nsimg(img); } NSStatusItem *item = indicators[p_id].item; diff --git a/platform/macos/native_menu_macos.mm b/platform/macos/native_menu_macos.mm index 1cf13a2d694..65c03dcf1bc 100644 --- a/platform/macos/native_menu_macos.mm +++ b/platform/macos/native_menu_macos.mm @@ -436,7 +436,7 @@ int NativeMenuMacOS::add_icon_item(const RID &p_rid, const Ref &p_ico obj->max_states = 0; obj->state = 0; DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); - if (ds && p_icon.is_valid()) { + if (ds && p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { obj->img = p_icon->get_image(); obj->img = obj->img->duplicate(); if (obj->img->is_compressed()) { @@ -467,7 +467,7 @@ int NativeMenuMacOS::add_icon_check_item(const RID &p_rid, const Ref obj->max_states = 0; obj->state = 0; DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); - if (ds && p_icon.is_valid()) { + if (ds && p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { obj->img = p_icon->get_image(); obj->img = obj->img->duplicate(); if (obj->img->is_compressed()) { @@ -518,7 +518,7 @@ int NativeMenuMacOS::add_icon_radio_check_item(const RID &p_rid, const Refmax_states = 0; obj->state = 0; DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); - if (ds && p_icon.is_valid()) { + if (ds && p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { obj->img = p_icon->get_image(); obj->img = obj->img->duplicate(); if (obj->img->is_compressed()) { @@ -1212,7 +1212,7 @@ void NativeMenuMacOS::set_item_icon(const RID &p_rid, int p_idx, const Refget_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { obj->img = p_icon->get_image(); obj->img = obj->img->duplicate(); if (obj->img->is_compressed()) { diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp index 4a482f560c2..e9f4539f390 100644 --- a/platform/windows/display_server_windows.cpp +++ b/platform/windows/display_server_windows.cpp @@ -3171,9 +3171,12 @@ void DisplayServerWindows::set_icon(const Ref &p_icon) { DisplayServer::IndicatorID DisplayServerWindows::create_status_indicator(const Ref &p_icon, const String &p_tooltip, const Callable &p_callback) { HICON hicon = nullptr; - if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0) { + if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { Ref img = p_icon->get_image(); img = img->duplicate(); + if (img->is_compressed()) { + img->decompress(); + } img->convert(Image::FORMAT_RGBA8); int w = img->get_width(); @@ -3241,9 +3244,12 @@ void DisplayServerWindows::status_indicator_set_icon(IndicatorID p_id, const Ref ERR_FAIL_COND(!indicators.has(p_id)); HICON hicon = nullptr; - if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0) { + if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { Ref img = p_icon->get_image(); img = img->duplicate(); + if (img->is_compressed()) { + img->decompress(); + } img->convert(Image::FORMAT_RGBA8); int w = img->get_width(); diff --git a/platform/windows/native_menu_windows.cpp b/platform/windows/native_menu_windows.cpp index 40a08f87df4..96bd422f032 100644 --- a/platform/windows/native_menu_windows.cpp +++ b/platform/windows/native_menu_windows.cpp @@ -35,6 +35,8 @@ #include "scene/resources/image_texture.h" HBITMAP NativeMenuWindows::_make_bitmap(const Ref &p_img) const { + p_img->convert(Image::FORMAT_RGBA8); + Vector2i texture_size = p_img->get_size(); UINT image_size = texture_size.width * texture_size.height; @@ -349,12 +351,14 @@ int NativeMenuWindows::add_icon_item(const RID &p_rid, const Ref &p_i item_data->checkable_type = CHECKABLE_TYPE_NONE; item_data->max_states = 0; item_data->state = 0; - item_data->img = p_icon->get_image(); - item_data->img = item_data->img->duplicate(); - if (item_data->img->is_compressed()) { - item_data->img->decompress(); + if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { + item_data->img = p_icon->get_image(); + item_data->img = item_data->img->duplicate(); + if (item_data->img->is_compressed()) { + item_data->img->decompress(); + } + item_data->bmp = _make_bitmap(item_data->img); } - item_data->bmp = _make_bitmap(item_data->img); Char16String label = p_label.utf16(); MENUITEMINFOW item; @@ -389,12 +393,14 @@ int NativeMenuWindows::add_icon_check_item(const RID &p_rid, const Refcheckable_type = CHECKABLE_TYPE_CHECK_BOX; item_data->max_states = 0; item_data->state = 0; - item_data->img = p_icon->get_image(); - item_data->img = item_data->img->duplicate(); - if (item_data->img->is_compressed()) { - item_data->img->decompress(); + if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { + item_data->img = p_icon->get_image(); + item_data->img = item_data->img->duplicate(); + if (item_data->img->is_compressed()) { + item_data->img->decompress(); + } + item_data->bmp = _make_bitmap(item_data->img); } - item_data->bmp = _make_bitmap(item_data->img); Char16String label = p_label.utf16(); MENUITEMINFOW item; @@ -462,12 +468,14 @@ int NativeMenuWindows::add_icon_radio_check_item(const RID &p_rid, const Refcheckable_type = CHECKABLE_TYPE_RADIO_BUTTON; item_data->max_states = 0; item_data->state = 0; - item_data->img = p_icon->get_image(); - item_data->img = item_data->img->duplicate(); - if (item_data->img->is_compressed()) { - item_data->img->decompress(); + if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { + item_data->img = p_icon->get_image(); + item_data->img = item_data->img->duplicate(); + if (item_data->img->is_compressed()) { + item_data->img->decompress(); + } + item_data->bmp = _make_bitmap(item_data->img); } - item_data->bmp = _make_bitmap(item_data->img); Char16String label = p_label.utf16(); MENUITEMINFOW item; @@ -1082,7 +1090,7 @@ void NativeMenuWindows::set_item_icon(const RID &p_rid, int p_idx, const Refbmp) { DeleteObject(item_data->bmp); } - if (p_icon.is_valid()) { + if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { item_data->img = p_icon->get_image(); item_data->img = item_data->img->duplicate(); if (item_data->img->is_compressed()) {