Replace multi_rect with lab_scene_rect

lab_scene_rect accepts the arbitrary number of borders and a background
color.
This commit is contained in:
tokyo4j 2025-06-09 16:27:17 +09:00 committed by Hiroaki Yamamoto
parent 4cc6b354b0
commit ffd400503e
12 changed files with 187 additions and 140 deletions

View file

@ -1,85 +1,8 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include <cairo.h>
#include <stdlib.h>
#include <string.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/util/box.h>
#include "buffer.h"
#include "common/graphic-helpers.h"
#include "common/macros.h"
#include "common/mem.h"
static void
multi_rect_destroy_notify(struct wl_listener *listener, void *data)
{
struct multi_rect *rect = wl_container_of(listener, rect, destroy);
wl_list_remove(&rect->destroy.link);
free(rect);
}
struct multi_rect *
multi_rect_create(struct wlr_scene_tree *parent, float *colors[3], int line_width)
{
struct multi_rect *rect = znew(*rect);
rect->line_width = line_width;
rect->tree = wlr_scene_tree_create(parent);
rect->destroy.notify = multi_rect_destroy_notify;
wl_signal_add(&rect->tree->node.events.destroy, &rect->destroy);
for (size_t i = 0; i < 3; i++) {
rect->top[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
rect->right[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
rect->bottom[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
rect->left[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
wlr_scene_node_set_position(&rect->top[i]->node,
i * line_width, i * line_width);
wlr_scene_node_set_position(&rect->left[i]->node,
i * line_width, (i + 1) * line_width);
}
return rect;
}
void
multi_rect_set_size(struct multi_rect *rect, int width, int height)
{
assert(rect);
int line_width = rect->line_width;
/*
* The outmost outline is drawn like below:
*
* |--width--|
*
* +---------+ ---
* +-+-----+-+ |
* | | | | height
* | | | | |
* +-+-----+-+ |
* +---------+ ---
*/
for (int i = 0; i < 3; i++) {
/* Reposition, top and left don't ever change */
wlr_scene_node_set_position(&rect->right[i]->node,
width - (i + 1) * line_width, (i + 1) * line_width);
wlr_scene_node_set_position(&rect->bottom[i]->node,
i * line_width, height - (i + 1) * line_width);
/* Update sizes */
wlr_scene_rect_set_size(rect->top[i],
MAX(width - i * line_width * 2, 0),
line_width);
wlr_scene_rect_set_size(rect->bottom[i],
MAX(width - i * line_width * 2, 0),
line_width);
wlr_scene_rect_set_size(rect->left[i],
line_width,
MAX(height - (i + 1) * line_width * 2, 0));
wlr_scene_rect_set_size(rect->right[i],
line_width,
MAX(height - (i + 1) * line_width * 2, 0));
}
}
/* Draws a border with a specified line width */
void

103
src/common/lab-scene-rect.c Normal file
View file

@ -0,0 +1,103 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include <wlr/types/wlr_scene.h>
#include "common/lab-scene-rect.h"
#include "common/mem.h"
struct border_scene {
struct wlr_scene_tree *tree;
struct wlr_scene_rect *top, *bottom, *left, *right;
};
static void
handle_node_destroy(struct wl_listener *listener, void *data)
{
struct lab_scene_rect *rect = wl_container_of(listener, rect, node_destroy);
wl_list_remove(&rect->node_destroy.link);
free(rect->borders);
free(rect);
}
struct lab_scene_rect *
lab_scene_rect_create(struct wlr_scene_tree *parent,
struct lab_scene_rect_options *opts)
{
struct lab_scene_rect *rect = znew(*rect);
rect->border_width = opts->border_width;
rect->nr_borders = opts->nr_borders;
rect->borders = znew_n(rect->borders[0], opts->nr_borders);
rect->tree = wlr_scene_tree_create(parent);
if (opts->bg_color) {
rect->fill = wlr_scene_rect_create(rect->tree, 0, 0, opts->bg_color);
}
for (int i = 0; i < rect->nr_borders; i++) {
struct border_scene *border = &rect->borders[i];
float *color = opts->border_colors[i];
border->tree = wlr_scene_tree_create(rect->tree);
border->top = wlr_scene_rect_create(border->tree, 0, 0, color);
border->right = wlr_scene_rect_create(border->tree, 0, 0, color);
border->bottom = wlr_scene_rect_create(border->tree, 0, 0, color);
border->left = wlr_scene_rect_create(border->tree, 0, 0, color);
}
rect->node_destroy.notify = handle_node_destroy;
wl_signal_add(&rect->tree->node.events.destroy, &rect->node_destroy);
lab_scene_rect_set_size(rect, opts->width, opts->height);
return rect;
}
static void
resize_border(struct border_scene *border, int border_width, int width, int height)
{
/*
* The border is drawn like below:
*
* <--width-->
* +---------+ ^
* +-+-----+-+ |
* | | | | height
* | | | | |
* +-+-----+-+ |
* +---------+ v
*/
if ((width < border_width * 2) || (height < border_width * 2)) {
wlr_scene_node_set_enabled(&border->tree->node, false);
return;
}
wlr_scene_node_set_enabled(&border->tree->node, true);
wlr_scene_node_set_position(&border->top->node, 0, 0);
wlr_scene_node_set_position(&border->bottom->node, 0, height - border_width);
wlr_scene_node_set_position(&border->left->node, 0, border_width);
wlr_scene_node_set_position(&border->right->node, width - border_width, border_width);
wlr_scene_rect_set_size(border->top, width, border_width);
wlr_scene_rect_set_size(border->bottom, width, border_width);
wlr_scene_rect_set_size(border->left, border_width, height - border_width * 2);
wlr_scene_rect_set_size(border->right, border_width, height - border_width * 2);
}
void
lab_scene_rect_set_size(struct lab_scene_rect *rect, int width, int height)
{
assert(rect);
int border_width = rect->border_width;
for (int i = 0; i < rect->nr_borders; i++) {
struct border_scene *border = &rect->borders[i];
resize_border(border, border_width,
width - 2 * border_width * i,
height - 2 * border_width * i);
wlr_scene_node_set_position(&border->tree->node,
i * border_width, i * border_width);
}
if (rect->fill) {
wlr_scene_rect_set_size(rect->fill, width, height);
}
}

View file

@ -8,6 +8,7 @@ labwc_sources += files(
'font.c',
'grab-file.c',
'graphic-helpers.c',
'lab-scene-rect.c',
'match.c',
'mem.c',
'nodename.c',