Skip to content

Commit ce52790

Browse files
committed
Add cross-platform Image API and update icon handling
Introduces a new Image class for unified image management across platforms, with C API bindings in image_c.h/cpp. Refactors menu and tray icon APIs to use Image objects instead of file paths or base64 strings, updating related headers and documentation for type safety and improved resource management.
1 parent b0257d2 commit ce52790

File tree

13 files changed

+625
-91
lines changed

13 files changed

+625
-91
lines changed

src/capi/image_c.cpp

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#include "image_c.h"
2+
3+
#include <cstring>
4+
#include "../image.h"
5+
#include "string_utils_c.h"
6+
7+
using namespace nativeapi;
8+
9+
// Create an image from a file path
10+
native_image_t native_image_from_file(const char* file_path) {
11+
if (!file_path) {
12+
return nullptr;
13+
}
14+
15+
try {
16+
auto image = Image::FromFile(file_path);
17+
if (image) {
18+
auto size = image->GetSize();
19+
if (size.width > 0 && size.height > 0) {
20+
return new std::shared_ptr<Image>(image);
21+
}
22+
}
23+
} catch (...) {
24+
// Handle exceptions
25+
}
26+
27+
return nullptr;
28+
}
29+
30+
// Create an image from base64-encoded data
31+
native_image_t native_image_from_base64(const char* base64_data) {
32+
if (!base64_data) {
33+
return nullptr;
34+
}
35+
36+
try {
37+
auto image = Image::FromBase64(base64_data);
38+
if (image) {
39+
auto size = image->GetSize();
40+
if (size.width > 0 && size.height > 0) {
41+
return new std::shared_ptr<Image>(image);
42+
}
43+
}
44+
} catch (...) {
45+
// Handle exceptions
46+
}
47+
48+
return nullptr;
49+
}
50+
51+
// Create an image from a platform-specific system icon
52+
native_image_t native_image_from_system_icon(const char* icon_name) {
53+
if (!icon_name) {
54+
return nullptr;
55+
}
56+
57+
try {
58+
auto image = Image::FromSystemIcon(icon_name);
59+
if (image) {
60+
auto size = image->GetSize();
61+
if (size.width > 0 && size.height > 0) {
62+
return new std::shared_ptr<Image>(image);
63+
}
64+
}
65+
} catch (...) {
66+
// Handle exceptions
67+
}
68+
69+
return nullptr;
70+
}
71+
72+
// Destroy an image and release its resources
73+
void native_image_destroy(native_image_t image) {
74+
if (image) {
75+
delete static_cast<std::shared_ptr<Image>*>(image);
76+
}
77+
}
78+
79+
// Get the size of an image in pixels
80+
void native_image_get_size(native_image_t image,
81+
double* width,
82+
double* height) {
83+
if (!image || !width || !height) {
84+
if (width)
85+
*width = 0.0;
86+
if (height)
87+
*height = 0.0;
88+
return;
89+
}
90+
91+
try {
92+
auto img = static_cast<std::shared_ptr<Image>*>(image);
93+
auto size = (*img)->GetSize();
94+
*width = size.width;
95+
*height = size.height;
96+
} catch (...) {
97+
*width = 0.0;
98+
*height = 0.0;
99+
}
100+
}
101+
102+
// Get the image format string for debugging purposes
103+
char* native_image_get_format(native_image_t image) {
104+
if (!image) {
105+
return nullptr;
106+
}
107+
108+
try {
109+
auto img = static_cast<std::shared_ptr<Image>*>(image);
110+
std::string format = (*img)->GetFormat();
111+
112+
return to_c_str(format);
113+
} catch (...) {
114+
// Handle exceptions
115+
}
116+
117+
return nullptr;
118+
}
119+
120+
// Convert an image to base64-encoded PNG data
121+
char* native_image_to_base64(native_image_t image) {
122+
if (!image) {
123+
return nullptr;
124+
}
125+
126+
try {
127+
auto img = static_cast<std::shared_ptr<Image>*>(image);
128+
std::string base64 = (*img)->ToBase64();
129+
130+
return to_c_str(base64);
131+
} catch (...) {
132+
// Handle exceptions
133+
}
134+
135+
return nullptr;
136+
}
137+
138+
// Save an image to a file
139+
bool native_image_save_to_file(native_image_t image, const char* file_path) {
140+
if (!image || !file_path) {
141+
return false;
142+
}
143+
144+
try {
145+
auto img = static_cast<std::shared_ptr<Image>*>(image);
146+
return (*img)->SaveToFile(file_path);
147+
} catch (...) {
148+
return false;
149+
}
150+
}

src/capi/image_c.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
7+
#if _WIN32
8+
#define FFI_PLUGIN_EXPORT __declspec(dllexport)
9+
#else
10+
#define FFI_PLUGIN_EXPORT
11+
#endif
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
/**
18+
* Opaque handle for image objects
19+
*/
20+
typedef void* native_image_t;
21+
22+
/**
23+
* Image operations
24+
*/
25+
26+
/**
27+
* Create an image from a file path
28+
* @param file_path Path to the image file
29+
* @return Image handle, or NULL if loading failed
30+
*/
31+
FFI_PLUGIN_EXPORT
32+
native_image_t native_image_from_file(const char* file_path);
33+
34+
/**
35+
* Create an image from base64-encoded data
36+
* @param base64_data Base64-encoded image data, with or without data URI prefix
37+
* @return Image handle, or NULL if decoding failed
38+
*/
39+
FFI_PLUGIN_EXPORT
40+
native_image_t native_image_from_base64(const char* base64_data);
41+
42+
/**
43+
* Create an image from a platform-specific system icon
44+
* @param icon_name Platform-specific system icon name/identifier
45+
* @return Image handle, or NULL if icon not found
46+
*/
47+
FFI_PLUGIN_EXPORT
48+
native_image_t native_image_from_system_icon(const char* icon_name);
49+
50+
/**
51+
* Destroy an image and release its resources
52+
* @param image The image to destroy
53+
*/
54+
FFI_PLUGIN_EXPORT
55+
void native_image_destroy(native_image_t image);
56+
57+
/**
58+
* Get the size of an image in pixels
59+
* @param image The image
60+
* @param width Pointer to store the width (will be set to 0 if invalid)
61+
* @param height Pointer to store the height (will be set to 0 if invalid)
62+
*/
63+
FFI_PLUGIN_EXPORT
64+
void native_image_get_size(native_image_t image, double* width, double* height);
65+
66+
/**
67+
* Get the image format string for debugging purposes
68+
* @param image The image
69+
* @return The image format (e.g., "PNG", "JPEG", "GIF"), or NULL if unknown
70+
* (caller must free)
71+
*/
72+
FFI_PLUGIN_EXPORT
73+
char* native_image_get_format(native_image_t image);
74+
75+
/**
76+
* Convert an image to base64-encoded PNG data
77+
* @param image The image
78+
* @return Base64-encoded PNG data with data URI prefix (caller must free), or
79+
* NULL on error
80+
*/
81+
FFI_PLUGIN_EXPORT
82+
char* native_image_to_base64(native_image_t image);
83+
84+
/**
85+
* Save an image to a file
86+
* @param image The image
87+
* @param file_path Path where the image should be saved
88+
* @return true if saved successfully, false otherwise
89+
*/
90+
FFI_PLUGIN_EXPORT
91+
bool native_image_save_to_file(native_image_t image, const char* file_path);
92+
93+
#ifdef __cplusplus
94+
}
95+
#endif

src/capi/menu_c.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,8 +1067,6 @@ bool native_menu_remove_listener(native_menu_t menu, int listener_id) {
10671067
}
10681068
}
10691069

1070-
1071-
10721070
// Utility functions
10731071

10741072
void native_menu_item_list_free(native_menu_item_list_t list) {

src/capi/menu_c.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern "C" {
1515
#endif
1616

1717
#include "geometry_c.h"
18+
#include "image_c.h"
1819

1920
/**
2021
* Opaque handles for menu objects
@@ -194,20 +195,21 @@ FFI_PLUGIN_EXPORT
194195
char* native_menu_item_get_label(native_menu_item_t item);
195196

196197
/**
197-
* Set the icon of a menu item
198+
* Set the icon of a menu item using an Image object
198199
* @param item The menu item
199-
* @param icon Path to icon file or base64 data
200+
* @param image The Image object to set as the icon, or NULL to clear the icon
200201
*/
201202
FFI_PLUGIN_EXPORT
202-
void native_menu_item_set_icon(native_menu_item_t item, const char* icon);
203+
void native_menu_item_set_icon(native_menu_item_t item, native_image_t image);
203204

204205
/**
205-
* Get the icon of a menu item
206+
* Get the current icon image of the menu item
206207
* @param item The menu item
207-
* @return The icon path/data string (caller must free), or NULL if item is invalid or no icon set
208+
* @return The Image object, or NULL if no icon is set. Caller must call
209+
* native_image_destroy() when done.
208210
*/
209211
FFI_PLUGIN_EXPORT
210-
char* native_menu_item_get_icon(native_menu_item_t item);
212+
native_image_t native_menu_item_get_icon(native_menu_item_t item);
211213

212214
/**
213215
* Set the tooltip of a menu item
@@ -577,8 +579,6 @@ int native_menu_add_listener(native_menu_t menu,
577579
FFI_PLUGIN_EXPORT
578580
bool native_menu_remove_listener(native_menu_t menu, int listener_id);
579581

580-
581-
582582
/**
583583
* Utility functions
584584
*/
@@ -600,4 +600,4 @@ char* native_keyboard_accelerator_to_string(const native_keyboard_accelerator_t*
600600

601601
#ifdef __cplusplus
602602
}
603-
#endif
603+
#endif

src/capi/tray_icon_c.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern "C" {
1515
#endif
1616

1717
#include "geometry_c.h"
18+
#include "image_c.h"
1819
#include "menu_c.h"
1920

2021
/**
@@ -98,12 +99,21 @@ FFI_PLUGIN_EXPORT
9899
native_tray_icon_id_t native_tray_icon_get_id(native_tray_icon_t tray_icon);
99100

100101
/**
101-
* Set the icon image for the tray icon
102+
* Set the icon image for the tray icon using an Image object
102103
* @param tray_icon The tray icon
103-
* @param icon Path to icon file or base64 encoded image data
104+
* @param image The Image object to set as the icon, or NULL to clear the icon
104105
*/
105106
FFI_PLUGIN_EXPORT
106-
void native_tray_icon_set_icon(native_tray_icon_t tray_icon, const char* icon);
107+
void native_tray_icon_set_icon(native_tray_icon_t tray_icon, native_image_t image);
108+
109+
/**
110+
* Get the current icon image of the tray icon
111+
* @param tray_icon The tray icon
112+
* @return The Image object, or NULL if no icon is set. Caller must call
113+
* native_image_destroy() when done.
114+
*/
115+
FFI_PLUGIN_EXPORT
116+
native_image_t native_tray_icon_get_icon(native_tray_icon_t tray_icon);
107117

108118
/**
109119
* Set the title text for the tray icon
@@ -116,8 +126,8 @@ void native_tray_icon_set_title(native_tray_icon_t tray_icon, const char* title)
116126
/**
117127
* Get the title text of the tray icon
118128
* @param tray_icon The tray icon
119-
* @return The title text, or NULL if no title is set or error. Caller must free the returned
120-
* string.
129+
* @return The title text, or NULL if no title is set or error. Caller must free
130+
* the returned string.
121131
*/
122132
FFI_PLUGIN_EXPORT
123133
char* native_tray_icon_get_title(native_tray_icon_t tray_icon);
@@ -133,8 +143,8 @@ void native_tray_icon_set_tooltip(native_tray_icon_t tray_icon, const char* tool
133143
/**
134144
* Get the tooltip text of the tray icon
135145
* @param tray_icon The tray icon
136-
* @return The tooltip text, or NULL if no tooltip is set or error. Caller must free the returned
137-
* string.
146+
* @return The tooltip text, or NULL if no tooltip is set or error. Caller must
147+
* free the returned string.
138148
*/
139149
FFI_PLUGIN_EXPORT
140150
char* native_tray_icon_get_tooltip(native_tray_icon_t tray_icon);

0 commit comments

Comments
 (0)