Skip to content

Commit

Permalink
iOS: Display app icon in app icon picker in materialui (#16520)
Browse files Browse the repository at this point in the history
  • Loading branch information
warmenhoven committed May 13, 2024
1 parent f3b6093 commit 7a44fb9
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 24 deletions.
53 changes: 45 additions & 8 deletions menu/drivers/materialui.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#endif

#include "../../frontend/frontend_driver.h"
#include "../../ui/ui_companion_driver.h"

#include "../menu_driver.h"
#include "../menu_screensaver.h"
Expand Down Expand Up @@ -175,7 +176,8 @@ enum materialui_node_icon_type
MUI_ICON_TYPE_MENU_EXPLORE,
MUI_ICON_TYPE_PLAYLIST,
MUI_ICON_TYPE_MENU_CONTENTLESS_CORE,
MUI_ICON_TYPE_ACHIEVEMENT
MUI_ICON_TYPE_ACHIEVEMENT,
MUI_ICON_TYPE_APPICON
};

/* Defines all standard menu textures */
Expand Down Expand Up @@ -4011,6 +4013,14 @@ static void materialui_render_menu_entry_default(
uintptr_t icon_texture = 0;
bool draw_text_outside = (x_offset != 0);
gfx_display_t *p_disp = disp_get_ptr();
uico_driver_state_t *uico_st = uico_state_get_ptr();

static float color_white[16] = {
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
};

if (!p_disp->dispctx->handles_transform)
{
Expand Down Expand Up @@ -4076,12 +4086,6 @@ static void materialui_render_menu_entry_default(
if (icon_texture)
{
/* draw the icon ourselves - the draw_icon below tints it to match the theme */
static float color_white[16] = {
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
};
materialui_draw_icon(
userdata, p_disp,
video_width,
Expand Down Expand Up @@ -4116,6 +4120,33 @@ static void materialui_render_menu_entry_default(
}
break;
#endif
case MUI_ICON_TYPE_APPICON:
if (uico_st->drv && uico_st->drv->get_app_icon_texture)
{
icon_texture = uico_st->drv->get_app_icon_texture(entry_label);
if (icon_texture)
{
/* draw the icon ourselves - the draw_icon below tints it to match the theme */
materialui_draw_icon(
userdata, p_disp,
video_width,
video_height,
mui->icon_size,
(uintptr_t)icon_texture,
entry_x + (int)mui->landscape_optimization.entry_margin,
entry_y + (node->entry_height / 2.0f) - (mui->icon_size / 2.0f),
0,
1,
color_white,
&mymat);

entry_margin += mui->icon_size;
usable_width -= mui->icon_size;

icon_texture = 0; /* prevent drawing tinted icon */
}
}
break;
default:
switch (entry_file_type)
{
Expand Down Expand Up @@ -10481,6 +10512,13 @@ static void materialui_list_insert(
case MENU_SETTING_ACTION_CONTENTLESS_CORE_RUN:
node->icon_type = MUI_ICON_TYPE_MENU_CONTENTLESS_CORE;
break;
case MENU_SETTING_DROPDOWN_SETTING_STRING_OPTIONS_ITEM:
if (atoi(fullpath) == MENU_ENUM_LABEL_APPICON_SETTINGS)
{
node->icon_type = MUI_ICON_TYPE_APPICON;
}
/* for other types we don't have an icon */
break;
case FILE_TYPE_RPL_ENTRY:
case MENU_SETTING_DROPDOWN_ITEM:
case MENU_SETTING_DROPDOWN_ITEM_RESOLUTION:
Expand All @@ -10498,7 +10536,6 @@ static void materialui_list_insert(
case MENU_SETTING_DROPDOWN_ITEM_INPUT_DESCRIPTION:
case MENU_SETTING_DROPDOWN_ITEM_INPUT_DESCRIPTION_KBD:
case MENU_SETTING_DROPDOWN_SETTING_CORE_OPTIONS_ITEM:
case MENU_SETTING_DROPDOWN_SETTING_STRING_OPTIONS_ITEM:
case MENU_SETTING_DROPDOWN_SETTING_FLOAT_ITEM:
case MENU_SETTING_DROPDOWN_SETTING_INT_ITEM:
case MENU_SETTING_DROPDOWN_SETTING_UINT_ITEM:
Expand Down
1 change: 1 addition & 0 deletions ui/drivers/ui_cocoa.m
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,7 @@ static void ui_companion_cocoa_event_command(void *data, enum event_command cmd)
NULL, /* is_active */
NULL, /* get_app_icons */
NULL, /* set_app_icon */
NULL, /* get_app_icon_texture */
&ui_browser_window_cocoa,
&ui_msg_window_cocoa,
&ui_window_cocoa,
Expand Down
51 changes: 35 additions & 16 deletions ui/drivers/ui_cocoatouch.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,40 @@ static void ui_companion_cocoatouch_set_app_icon(const char *iconName)
[[UIApplication sharedApplication] setAlternateIconName:str completionHandler:nil];
}

static uintptr_t ui_companion_cocoatouch_get_app_icon_texture(const char *icon)
{
static NSMutableDictionary<NSString *, NSNumber *> *textures = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
textures = [NSMutableDictionary dictionaryWithCapacity:6];
});

NSString *iconName = [NSString stringWithUTF8String:icon];
if (!textures[iconName])
{
UIImage *img = [UIImage imageNamed:iconName];
if (!img)
{
RARCH_LOG("could not load %s\n", icon);
return NULL;
}
NSData *png = UIImagePNGRepresentation(img);
if (!png)
{
RARCH_LOG("could not get png for %s\n", icon);
return NULL;
}

uintptr_t item;
gfx_display_reset_textures_list_buffer(&item, TEXTURE_FILTER_MIPMAP_LINEAR,
(void*)[png bytes], (unsigned int)[png length], IMAGE_TYPE_PNG,
NULL, NULL);
textures[iconName] = [NSNumber numberWithUnsignedLong:item];
}

return [textures[iconName] unsignedLongValue];
}

static void rarch_draw_observer(CFRunLoopObserverRef observer,
CFRunLoopActivity activity, void *info)
{
Expand Down Expand Up @@ -506,22 +540,6 @@ - (void)handleAudioSessionInterruption:(NSNotification *)notification
}
}

- (NSData *)pngForIcon:(NSString *)iconName
{
UIImage *img;
NSData *png;
img = [UIImage imageNamed:iconName];
if (!img)
NSLog(@"could not load %@\n", iconName);
else
{
png = UIImagePNGRepresentation(img);
if (!png)
NSLog(@"could not get png for %@\n", iconName);
}
return png;
}

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
char arguments[] = "retroarch";
Expand Down Expand Up @@ -763,6 +781,7 @@ - (void)didReceiveDiagnosticPayloads:(NSArray<MXDiagnosticPayload *> *)payloads
NULL, /* is_active */
ui_companion_cocoatouch_get_app_icons,
ui_companion_cocoatouch_set_app_icon,
ui_companion_cocoatouch_get_app_icon_texture,
NULL, /* browser_window */
NULL, /* msg_window */
NULL, /* window */
Expand Down
1 change: 1 addition & 0 deletions ui/drivers/ui_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4992,6 +4992,7 @@ ui_companion_driver_t ui_companion_qt = {
ui_companion_qt_is_active,
NULL, /* get_app_icons */
NULL, /* set_app_icon */
NULL, /* get_app_icon_texture */
&ui_browser_window_qt,
&ui_msg_window_qt,
&ui_window_qt,
Expand Down
1 change: 1 addition & 0 deletions ui/drivers/ui_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ ui_companion_driver_t ui_companion_win32 = {
NULL, /* is_active */
NULL, /* get_app_icons */
NULL, /* set_app_icon */
NULL, /* get_app_icon_texture */
&ui_browser_window_win32,
&ui_msg_window_win32,
&ui_window_win32,
Expand Down
1 change: 1 addition & 0 deletions ui/ui_companion_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static ui_companion_driver_t ui_companion_null = {
NULL, /* is_active */
NULL, /* get_app_icons */
NULL, /* set_app_icon */
NULL, /* get_app_icon_texture */
NULL, /* browser_window */
NULL, /* msg_window */
NULL, /* window */
Expand Down
1 change: 1 addition & 0 deletions ui/ui_companion_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ typedef struct ui_companion_driver
bool (*is_active)(void *data);
struct string_list *(*get_app_icons)(void);
void (*set_app_icon)(const char *icon);
uintptr_t (*get_app_icon_texture)(const char *icon);
ui_browser_window_t *browser_window;
ui_msg_window_t *msg_window;
ui_window_t *window;
Expand Down

0 comments on commit 7a44fb9

Please sign in to comment.