Replace scaled_rect_buffer with border_buffer

This fixes the gap between menu items and the menu border in an output
with a fractional scale due to the semantic gap between cairo and
wlroots's position-independent scene renderer.
This commit is contained in:
tokyo4j 2025-05-05 02:00:19 +09:00
parent b503e48623
commit 5af20c7a1f
5 changed files with 12 additions and 165 deletions

View file

@ -1,31 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef LABWC_SCALED_RECT_BUFFER_H
#define LABWC_SCALED_RECT_BUFFER_H
#include <stdint.h>
struct wlr_scene_tree;
struct wlr_scene_buffer;
struct scaled_scene_buffer;
struct scaled_rect_buffer {
struct wlr_scene_buffer *scene_buffer;
struct scaled_scene_buffer *scaled_buffer;
int width;
int height;
int border_width;
float fill_color[4];
float border_color[4];
};
/*
* Create an auto scaling borderd-rectangle buffer, providing a wlr_scene_buffer
* node for display. It gets destroyed automatically when the backing
* scaled_scene_buffer is being destroyed which in turn happens automatically
* when the backing wlr_scene_buffer (or one of its parents) is being destroyed.
*/
struct scaled_rect_buffer *scaled_rect_buffer_create(
struct wlr_scene_tree *parent, int width, int height, int border_width,
float fill_color[4], float border_color[4]);
#endif /* LABWC_SCALED_RECT_BUFFER_H */

View file

@ -16,7 +16,6 @@ labwc_sources += files(
'scaled-font-buffer.c',
'scaled-icon-buffer.c',
'scaled-img-buffer.c',
'scaled-rect-buffer.c',
'scaled-scene-buffer.c',
'scene-helpers.c',
'set.c',

View file

@ -1,116 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-only
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-server-core.h>
#include <wlr/util/log.h>
#include "buffer.h"
#include "common/graphic-helpers.h"
#include "common/macros.h"
#include "common/mem.h"
#include "common/scaled-scene-buffer.h"
#include "common/scaled-rect-buffer.h"
static void
draw_rectangle_path(cairo_t *cairo, int width, int height, int border_width)
{
double offset = border_width / 2.0;
double right_x = width - offset;
double bottom_y = height - offset;
cairo_move_to(cairo, offset, offset);
cairo_line_to(cairo, right_x, offset);
cairo_line_to(cairo, right_x, bottom_y);
cairo_line_to(cairo, offset, bottom_y);
cairo_close_path(cairo);
}
static struct lab_data_buffer *
_create_buffer(struct scaled_scene_buffer *scaled_buffer, double scale)
{
struct scaled_rect_buffer *self = scaled_buffer->data;
struct lab_data_buffer *buffer = buffer_create_cairo(
self->width, self->height, scale);
if (!buffer) {
return NULL;
}
cairo_t *cairo = cairo_create(buffer->surface);
/* Clear background */
cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR);
cairo_paint(cairo);
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
/* Fill rectangle */
draw_rectangle_path(cairo, self->width, self->height, 0);
set_cairo_color(cairo, self->fill_color);
cairo_fill(cairo);
/* Draw borders */
draw_rectangle_path(cairo, self->width, self->height,
self->border_width);
cairo_set_line_width(cairo, self->border_width);
set_cairo_color(cairo, self->border_color);
cairo_stroke(cairo);
cairo_surface_flush(buffer->surface);
cairo_destroy(cairo);
return buffer;
}
static void
_destroy(struct scaled_scene_buffer *scaled_buffer)
{
struct scaled_rect_buffer *self = scaled_buffer->data;
scaled_buffer->data = NULL;
free(self);
}
static bool
_equal(struct scaled_scene_buffer *scaled_buffer_a, struct scaled_scene_buffer *scaled_buffer_b)
{
struct scaled_rect_buffer *a = scaled_buffer_a->data;
struct scaled_rect_buffer *b = scaled_buffer_b->data;
return a->width == b->width
&& a->height == b->height
&& a->border_width == b->border_width
&& !memcmp(a->fill_color, b->fill_color, sizeof(a->fill_color))
&& !memcmp(a->border_color, b->border_color, sizeof(a->border_color));
}
static const struct scaled_scene_buffer_impl impl = {
.create_buffer = _create_buffer,
.destroy = _destroy,
.equal = _equal,
};
struct scaled_rect_buffer *scaled_rect_buffer_create(
struct wlr_scene_tree *parent, int width, int height, int border_width,
float fill_color[4], float border_color[4])
{
/* TODO: support rounded corners for menus and OSDs */
assert(parent);
assert(width >= 0 && height >= 0);
struct scaled_rect_buffer *self = znew(*self);
struct scaled_scene_buffer *scaled_buffer = scaled_scene_buffer_create(
parent, &impl, /* drop_buffer */ true);
scaled_buffer->data = self;
self->scaled_buffer = scaled_buffer;
self->scene_buffer = scaled_buffer->scene_buffer;
self->width = width;
self->height = height;
self->border_width = border_width;
memcpy(self->fill_color, fill_color, sizeof(self->fill_color));
memcpy(self->border_color, border_color, sizeof(self->border_color));
scaled_scene_buffer_request_update(scaled_buffer,
self->width, self->height);
return self;
}

View file

@ -21,7 +21,6 @@
#include "common/nodename.h"
#include "common/scaled-font-buffer.h"
#include "common/scaled-icon-buffer.h"
#include "common/scaled-rect-buffer.h"
#include "common/scene-helpers.h"
#include "common/spawn.h"
#include "common/string-helpers.h"
@ -447,13 +446,11 @@ menu_create_scene(struct menu *menu)
}
menu->size.height = item_y + theme->menu_border_width;
float transparent[4] = {0};
struct scaled_rect_buffer *bg_buffer = scaled_rect_buffer_create(
menu->scene_tree, menu->size.width, menu->size.height,
theme->menu_border_width, transparent,
theme->menu_border_color);
assert(bg_buffer);
wlr_scene_node_lower_to_bottom(&bg_buffer->scene_buffer->node);
struct border_rect *bg_rect = border_rect_create(
menu->scene_tree, theme->menu_border_color, NULL,
theme->menu_border_width);
border_rect_set_size(bg_rect, menu->size.width, menu->size.height);
wlr_scene_node_lower_to_bottom(&bg_rect->tree->node);
}
/*

View file

@ -9,7 +9,6 @@
#include "common/macros.h"
#include "common/scaled-font-buffer.h"
#include "common/scaled-icon-buffer.h"
#include "common/scaled-rect-buffer.h"
#include "common/scene-helpers.h"
#include "common/string-helpers.h"
#include "config/rcxml.h"
@ -289,8 +288,9 @@ create_osd_scene(struct output *output, struct wl_array *views)
float *bg_color = theme->osd_bg_color;
/* Draw background */
scaled_rect_buffer_create(output->osd_scene.tree, w, h,
theme->osd_border_width, bg_color, theme->osd_border_color);
struct border_rect *bg_rect = border_rect_create(output->osd_scene.tree,
theme->osd_border_color, bg_color, theme->osd_border_width);
border_rect_set_size(bg_rect, w, h);
int y = theme->osd_border_width + theme->osd_window_switcher_padding;
@ -406,13 +406,11 @@ create_osd_scene(struct output *output, struct wl_array *views)
int highlight_x = theme->osd_border_width
+ theme->osd_window_switcher_padding;
int border_width = theme->osd_window_switcher_item_active_border_width;
float transparent[4] = {0};
struct scaled_rect_buffer *highlight_buffer = scaled_rect_buffer_create(
output->osd_scene.tree, highlight_w, highlight_h,
border_width, transparent, text_color);
assert(highlight_buffer);
item->highlight_outline = &highlight_buffer->scene_buffer->node;
struct border_rect *highlight_rect = border_rect_create(
output->osd_scene.tree, text_color, NULL, border_width);
border_rect_set_size(highlight_rect, highlight_w, highlight_h);
item->highlight_outline = &highlight_rect->tree->node;
wlr_scene_node_set_position(item->highlight_outline, highlight_x, y);
wlr_scene_node_set_enabled(item->highlight_outline, false);