Skip to content

Commit

Permalink
properly handle image inheritance (closes #141)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Dec 31, 2021
1 parent b37618f commit 32d2cf3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/background.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ void background_draw(struct background* background, CGContextRef context) {
draw_rect(context, background->bounds, &background->color, background->corner_radius, background->border_width, &background->border_color, false);
}

void background_clear_pointers(struct background* background) {
image_clear_pointers(&background->image);
}

void background_destroy(struct background* background) {
image_destroy(&background->image);
background_clear_pointers(background);
}

static bool background_parse_sub_domain(struct background* background, FILE* rsp, struct token property, char* message) {
Expand Down
3 changes: 3 additions & 0 deletions src/background.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ bool background_set_padding_right(struct background* background, uint32_t pad);
void background_calculate_bounds(struct background* background, uint32_t x, uint32_t y);
void background_draw(struct background* background, CGContextRef context);

void background_clear_pointers(struct background* background);
void background_destroy(struct background* background);

static bool background_parse_sub_domain(struct background* background, FILE* rsp, struct token property, char* message);

#endif // !BACKGROUND_H
5 changes: 5 additions & 0 deletions src/bar_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void bar_item_clear_pointers(struct bar_item* bar_item) {
bar_item->signal_args.env_vars.count = 0;
text_clear_pointers(&bar_item->icon);
text_clear_pointers(&bar_item->label);
background_clear_pointers(&bar_item->background);
}

void bar_item_inherit_from_item(struct bar_item* bar_item, struct bar_item* ancestor) {
Expand All @@ -52,6 +53,10 @@ void bar_item_inherit_from_item(struct bar_item* bar_item, struct bar_item* ance
bar_item_set_script(bar_item, string_copy(ancestor->script));
bar_item_set_click_script(bar_item, string_copy(ancestor->click_script));

image_copy(&bar_item->background.image, ancestor->background.image.image_ref);
image_copy(&bar_item->icon.background.image, ancestor->icon.background.image.image_ref);
image_copy(&bar_item->label.background.image, ancestor->label.background.image.image_ref);

if (bar_item->type == BAR_COMPONENT_SPACE) {
env_vars_set(&bar_item->signal_args.env_vars, string_copy("SELECTED"), string_copy("false"));
env_vars_set(&bar_item->signal_args.env_vars, string_copy("SID"), string_copy(env_vars_get_value_for_key(&ancestor->signal_args.env_vars, "DID")));
Expand Down
10 changes: 10 additions & 0 deletions src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ bool image_data_equals(struct image* image, CFDataRef new_data_ref) {
return equals;
}

void image_copy(struct image* image, CGImageRef source) {
if (source) image->image_ref = CGImageCreateCopy(source);
}

bool image_set_image(struct image* image, CGImageRef new_image_ref, CGRect bounds, bool forced) {
CFDataRef new_data_ref = CGDataProviderCopyData(CGImageGetDataProvider(new_image_ref));

Expand Down Expand Up @@ -100,9 +104,15 @@ void image_draw(struct image* image, CGContextRef context) {
CGContextDrawImage(context, image->bounds, image->image_ref);
}

void image_clear_pointers(struct image* image) {
image->image_ref = NULL;
image->data_ref = NULL;
}

void image_destroy(struct image* image) {
CGImageRelease(image->image_ref);
if (image->data_ref) CFRelease(image->data_ref);
image_clear_pointers(image);
}

static bool image_parse_sub_domain(struct image* image, FILE* rsp, struct token property, char* message) {
Expand Down
3 changes: 3 additions & 0 deletions src/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ struct image {
void image_init(struct image* image);
bool image_set_enabled(struct image* image, bool enabled);
bool image_data_equals(struct image* image, CFDataRef new_data_ref);
void image_copy(struct image* image, CGImageRef source);
bool image_set_image(struct image* image, CGImageRef new_image_ref, CGRect bounds, bool forced);
bool image_load(struct image* image, char* path, FILE* rsp);
void image_calculate_bounds(struct image* image, uint32_t x, uint32_t y);
void image_draw(struct image* image, CGContextRef context);

void image_clear_pointers(struct image* image);
void image_destroy(struct image* image);

#endif
1 change: 1 addition & 0 deletions src/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ void text_clear_pointers(struct text* text) {
text->font_name = NULL;
text->font = NULL;
text->line.line = NULL;
background_clear_pointers(&text->background);
}

uint32_t text_get_length(struct text* text, bool override) {
Expand Down

0 comments on commit 32d2cf3

Please sign in to comment.