mirror of
https://github.com/labwc/labwc.git
synced 2026-03-03 01:40:37 -05:00
img: remove "theme" member from lab_img
lab_img.theme member was referenced by "modifier" functions when drawing a hover effect on button buffers or rounding the button buffer for corner buttons, but this can be prone to use-after-free and was not very clean because theme and lab_img were referencing each other. Instead, let's just reference rc.theme in the modifier functions and remove theme from lab_img.
This commit is contained in:
parent
956b271f9b
commit
90a8c3e793
3 changed files with 15 additions and 20 deletions
|
|
@ -16,7 +16,6 @@ enum lab_img_type {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct lab_img {
|
struct lab_img {
|
||||||
struct theme *theme; /* Used by modifier functions */
|
|
||||||
struct wl_array modifiers; /* lab_img_modifier_func_t */
|
struct wl_array modifiers; /* lab_img_modifier_func_t */
|
||||||
struct lab_img_cache *cache;
|
struct lab_img_cache *cache;
|
||||||
};
|
};
|
||||||
|
|
@ -33,8 +32,7 @@ struct lab_img *lab_img_load(enum lab_img_type type, const char *path,
|
||||||
*/
|
*/
|
||||||
struct lab_img *lab_img_load_from_bitmap(const char *bitmap, float *rgba);
|
struct lab_img *lab_img_load_from_bitmap(const char *bitmap, float *rgba);
|
||||||
|
|
||||||
typedef void (*lab_img_modifier_func_t)(struct theme *theme, cairo_t *cairo,
|
typedef void (*lab_img_modifier_func_t)(cairo_t *cairo, int w, int h);
|
||||||
int w, int h);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* lab_img_copy() - Copy lab_img
|
* lab_img_copy() - Copy lab_img
|
||||||
|
|
@ -55,8 +53,7 @@ struct lab_img *lab_img_copy(struct lab_img *img);
|
||||||
* after the image is rendered on a buffer with lab_img_render(). For example,
|
* after the image is rendered on a buffer with lab_img_render(). For example,
|
||||||
* hover effects for window buttons can be drawn over the rendered image.
|
* hover effects for window buttons can be drawn over the rendered image.
|
||||||
*/
|
*/
|
||||||
void lab_img_add_modifier(struct lab_img *img, lab_img_modifier_func_t modifier,
|
void lab_img_add_modifier(struct lab_img *img, lab_img_modifier_func_t modifier);
|
||||||
struct theme *theme);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* lab_img_render() - Render lab_img to a buffer
|
* lab_img_render() - Render lab_img to a buffer
|
||||||
|
|
|
||||||
|
|
@ -106,10 +106,8 @@ lab_img_copy(struct lab_img *img)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
lab_img_add_modifier(struct lab_img *img, lab_img_modifier_func_t modifier,
|
lab_img_add_modifier(struct lab_img *img, lab_img_modifier_func_t modifier)
|
||||||
struct theme *theme)
|
|
||||||
{
|
{
|
||||||
img->theme = theme;
|
|
||||||
lab_img_modifier_func_t *mod = wl_array_add(&img->modifiers, sizeof(*mod));
|
lab_img_modifier_func_t *mod = wl_array_add(&img->modifiers, sizeof(*mod));
|
||||||
*mod = modifier;
|
*mod = modifier;
|
||||||
}
|
}
|
||||||
|
|
@ -185,7 +183,7 @@ lab_img_render(struct lab_img *img, int width, int height, int padding,
|
||||||
lab_img_modifier_func_t *modifier;
|
lab_img_modifier_func_t *modifier;
|
||||||
wl_array_for_each(modifier, &img->modifiers) {
|
wl_array_for_each(modifier, &img->modifiers) {
|
||||||
cairo_save(cairo);
|
cairo_save(cairo);
|
||||||
(*modifier)(img->theme, cairo, width, height);
|
(*modifier)(cairo, width, height);
|
||||||
cairo_restore(cairo);
|
cairo_restore(cairo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
22
src/theme.c
22
src/theme.c
|
|
@ -76,12 +76,12 @@ zdrop(struct lab_data_buffer **buffer)
|
||||||
|
|
||||||
/* Draw rounded-rectangular hover overlay on the button buffer */
|
/* Draw rounded-rectangular hover overlay on the button buffer */
|
||||||
static void
|
static void
|
||||||
draw_hover_overlay_on_button(struct theme *theme, cairo_t *cairo, int w, int h)
|
draw_hover_overlay_on_button(cairo_t *cairo, int w, int h)
|
||||||
{
|
{
|
||||||
/* Overlay (pre-multiplied alpha) */
|
/* Overlay (pre-multiplied alpha) */
|
||||||
float overlay_color[4] = { 0.15f, 0.15f, 0.15f, 0.3f};
|
float overlay_color[4] = { 0.15f, 0.15f, 0.15f, 0.3f};
|
||||||
set_cairo_color(cairo, overlay_color);
|
set_cairo_color(cairo, overlay_color);
|
||||||
int r = theme->window_button_hover_bg_corner_radius;
|
int r = rc.theme->window_button_hover_bg_corner_radius;
|
||||||
|
|
||||||
cairo_new_sub_path(cairo);
|
cairo_new_sub_path(cairo);
|
||||||
cairo_arc(cairo, r, r, r, 180 * deg, 270 * deg);
|
cairo_arc(cairo, r, r, r, 180 * deg, 270 * deg);
|
||||||
|
|
@ -97,16 +97,16 @@ draw_hover_overlay_on_button(struct theme *theme, cairo_t *cairo, int w, int h)
|
||||||
|
|
||||||
/* Round the buffer for the leftmost button in the titlebar */
|
/* Round the buffer for the leftmost button in the titlebar */
|
||||||
static void
|
static void
|
||||||
round_left_corner_button(struct theme *theme, cairo_t *cairo, int w, int h)
|
round_left_corner_button(cairo_t *cairo, int w, int h)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Position of the topleft corner of the titlebar relative to the
|
* Position of the topleft corner of the titlebar relative to the
|
||||||
* leftmost button
|
* leftmost button
|
||||||
*/
|
*/
|
||||||
double x = -theme->window_titlebar_padding_width;
|
double x = -rc.theme->window_titlebar_padding_width;
|
||||||
double y = -(theme->titlebar_height - theme->window_button_height) / 2;
|
double y = -(rc.theme->titlebar_height - rc.theme->window_button_height) / 2;
|
||||||
|
|
||||||
double r = rc.corner_radius - (double)theme->border_width / 2.0;
|
double r = rc.corner_radius - (double)rc.theme->border_width / 2.0;
|
||||||
|
|
||||||
cairo_new_sub_path(cairo);
|
cairo_new_sub_path(cairo);
|
||||||
cairo_arc(cairo, x + r, y + r, r, deg * 180, deg * 270);
|
cairo_arc(cairo, x + r, y + r, r, deg * 180, deg * 270);
|
||||||
|
|
@ -122,7 +122,7 @@ round_left_corner_button(struct theme *theme, cairo_t *cairo, int w, int h)
|
||||||
|
|
||||||
/* Round the buffer for the rightmost button in the titlebar */
|
/* Round the buffer for the rightmost button in the titlebar */
|
||||||
static void
|
static void
|
||||||
round_right_corner_button(struct theme *theme, cairo_t *cairo, int w, int h)
|
round_right_corner_button(cairo_t *cairo, int w, int h)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Horizontally flip the cairo context so we can reuse
|
* Horizontally flip the cairo context so we can reuse
|
||||||
|
|
@ -130,7 +130,7 @@ round_right_corner_button(struct theme *theme, cairo_t *cairo, int w, int h)
|
||||||
*/
|
*/
|
||||||
cairo_scale(cairo, -1, 1);
|
cairo_scale(cairo, -1, 1);
|
||||||
cairo_translate(cairo, -w, 0);
|
cairo_translate(cairo, -w, 0);
|
||||||
round_left_corner_button(theme, cairo, w, h);
|
round_left_corner_button(cairo, w, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -223,7 +223,7 @@ load_button(struct theme *theme, struct button *b, int active)
|
||||||
button_imgs[b->type][b->state_set & ~LAB_BS_HOVERD];
|
button_imgs[b->type][b->state_set & ~LAB_BS_HOVERD];
|
||||||
*img = lab_img_copy(non_hover_img);
|
*img = lab_img_copy(non_hover_img);
|
||||||
lab_img_add_modifier(*img,
|
lab_img_add_modifier(*img,
|
||||||
draw_hover_overlay_on_button, theme);
|
draw_hover_overlay_on_button);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -239,7 +239,7 @@ load_button(struct theme *theme, struct button *b, int active)
|
||||||
if (leftmost_button->type == b->type) {
|
if (leftmost_button->type == b->type) {
|
||||||
*rounded_img = lab_img_copy(*img);
|
*rounded_img = lab_img_copy(*img);
|
||||||
lab_img_add_modifier(*rounded_img,
|
lab_img_add_modifier(*rounded_img,
|
||||||
round_left_corner_button, theme);
|
round_left_corner_button);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -249,7 +249,7 @@ load_button(struct theme *theme, struct button *b, int active)
|
||||||
if (rightmost_button->type == b->type) {
|
if (rightmost_button->type == b->type) {
|
||||||
*rounded_img = lab_img_copy(*img);
|
*rounded_img = lab_img_copy(*img);
|
||||||
lab_img_add_modifier(*rounded_img,
|
lab_img_add_modifier(*rounded_img,
|
||||||
round_right_corner_button, theme);
|
round_right_corner_button);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue