diff options
author | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2024-06-05 20:19:47 +0300 |
---|---|---|
committer | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2024-06-06 07:46:09 +0300 |
commit | 28ec2240c4be8235029cb02265cd7549e8324492 (patch) | |
tree | 6ba01c03743ec62efcd578a9174d61b16a408109 /platform/macos | |
parent | e96ad5af98547df71b50c4c4695ac348638113e0 (diff) | |
download | redot-engine-28ec2240c4be8235029cb02265cd7549e8324492.tar.gz |
Improve native menu and status indicator icons conversion and checks.
Diffstat (limited to 'platform/macos')
-rw-r--r-- | platform/macos/display_server_macos.mm | 74 | ||||
-rw-r--r-- | platform/macos/native_menu_macos.mm | 8 |
2 files changed, 12 insertions, 70 deletions
diff --git a/platform/macos/display_server_macos.mm b/platform/macos/display_server_macos.mm index 50313cfe67..f945ab53f0 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<Image> &p_icon) { DisplayServer::IndicatorID DisplayServerMacOS::create_status_indicator(const Ref<Texture2D> &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<Image> 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 Ref<T ERR_FAIL_COND(!indicators.has(p_id)); 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<Image> 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 1cf13a2d69..65c03dcf1b 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<Texture2D> &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<Texture2D> 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 Ref<Textu 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()) { @@ -1212,7 +1212,7 @@ void NativeMenuMacOS::set_item_icon(const RID &p_rid, int p_idx, const Ref<Textu GodotMenuItem *obj = [menu_item representedObject]; ERR_FAIL_NULL(obj); 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()) { |