retain app icon image after loading it (#423)

This commit is contained in:
Felix Kratz 2023-10-13 17:40:12 +02:00
parent 1433dd2837
commit 0acca7caae
4 changed files with 24 additions and 15 deletions

View file

@ -304,7 +304,7 @@ static void bar_item_set_script(struct bar_item* bar_item, char* script) {
free(bar_item->script);
char* path = resolve_path(script);
bar_item->script = path;
if (path) bar_item->script = path;
}
static void bar_item_set_click_script(struct bar_item* bar_item, char* script) {
@ -319,7 +319,7 @@ static void bar_item_set_click_script(struct bar_item* bar_item, char* script) {
free(bar_item->click_script);
char* path = resolve_path(script);
bar_item->click_script = path;
if (path) bar_item->click_script = path;
}
static bool bar_item_set_yoffset(struct bar_item* bar_item, int offset) {

View file

@ -21,6 +21,7 @@ bool image_set_enabled(struct image* image, bool enabled) {
}
bool image_load(struct image* image, char* path, FILE* rsp) {
if (!path) return false;
char* app = string_copy(path);
if (image->path) free(image->path);
image->path = string_copy(path);
@ -49,19 +50,25 @@ bool image_load(struct image* image, char* path, FILE* rsp) {
}
} else if (file_exists(res_path)) {
CGDataProviderRef data_provider = CGDataProviderCreateWithFilename(res_path);
if (strlen(res_path) > 3 && string_equals(&res_path[strlen(res_path) - 4], ".png"))
new_image_ref = CGImageCreateWithPNGDataProvider(data_provider,
NULL,
false,
kCGRenderingIntentDefault);
else {
new_image_ref = CGImageCreateWithJPEGDataProvider(data_provider,
NULL,
false,
kCGRenderingIntentDefault);
if (data_provider) {
if (strlen(res_path) > 3 && string_equals(&res_path[strlen(res_path) - 4], ".png"))
new_image_ref = CGImageCreateWithPNGDataProvider(data_provider,
NULL,
false,
kCGRenderingIntentDefault);
else {
new_image_ref = CGImageCreateWithJPEGDataProvider(data_provider,
NULL,
false,
kCGRenderingIntentDefault);
}
CFRelease(data_provider);
} else {
respond(rsp, "[!] Image: Invalid Image Format: '%s'\n", app_kv.value);
free(res_path);
free(app);
return false;
}
if (data_provider) CFRelease(data_provider);
}
else if (strlen(res_path) == 0) {
image_destroy(image);

View file

@ -394,6 +394,8 @@ static inline char* read_file(char* path) {
}
static inline char* resolve_path(char* path) {
if (!path) return NULL;
if (path[0] == '~') {
char* home = getenv("HOME");
char buf[512];

View file

@ -66,7 +66,7 @@ CGImageRef workspace_icon_for_app(char* app) {
if (!image) return NULL;
NSRect rect = NSMakeRect( 0, 0, [image size].width, [image size].height);
return [image CGImageForProposedRect: &rect context: NULL hints:NULL];
return (CGImageRef)CFRetain([image CGImageForProposedRect: &rect context: NULL hints:NULL]);
}
@implementation workspace_context