Skip to content

Commit

Permalink
osd: use theme->osd_border_width for focused item
Browse files Browse the repository at this point in the history
  • Loading branch information
johanmalm committed Jun 29, 2023
1 parent df53c28 commit b200dd2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
4 changes: 2 additions & 2 deletions include/common/graphic-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

struct wlr_scene_tree;
struct wlr_scene_rect;
struct wlr_fbox;

struct multi_rect {
struct wlr_scene_tree *tree;
Expand Down Expand Up @@ -44,7 +45,6 @@ void multi_rect_set_size(struct multi_rect *rect, int width, int height);
void set_cairo_color(cairo_t *cairo, float *color);

/* Draws a border with a specified line width */
void draw_cairo_border(cairo_t *cairo, double width, double height,
double line_width);
void draw_cairo_border(cairo_t *cairo, struct wlr_fbox fbox, double line_width);

#endif /* LABWC_GRAPHIC_HELPERS_H */
14 changes: 7 additions & 7 deletions src/common/graphic-helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cairo.h>
#include <stdlib.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/util/box.h>
#include "common/graphic-helpers.h"
#include "common/mem.h"

Expand Down Expand Up @@ -62,18 +63,17 @@ multi_rect_set_size(struct multi_rect *rect, int width, int height)

/* Draws a border with a specified line width */
void
draw_cairo_border(cairo_t *cairo, double width, double height, double line_width)
draw_cairo_border(cairo_t *cairo, struct wlr_fbox fbox, double line_width)
{
cairo_save(cairo);

double x, y, w, h;
/* The anchor point of a line is in the center */
x = line_width / 2;
y = x;
w = width - line_width;
h = height - line_width;
fbox.x += line_width / 2.0;
fbox.y += line_width / 2.0;
fbox.width -= line_width;
fbox.height -= line_width;
cairo_set_line_width(cairo, line_width);
cairo_rectangle(cairo, x, y, w, h);
cairo_rectangle(cairo, fbox.x, fbox.y, fbox.width, fbox.height);
cairo_stroke(cairo);

cairo_restore(cairo);
Expand Down
65 changes: 54 additions & 11 deletions src/osd.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <drm_fourcc.h>
#include <pango/pangocairo.h>
#include <wlr/util/log.h>
#include <wlr/util/box.h>
#include "buffer.h"
#include "common/buf.h"
#include "common/font.h"
Expand Down Expand Up @@ -69,8 +70,13 @@ get_osd_height(struct wl_list *node_list)
if (!isfocusable(view) || skip == LAB_PROP_TRUE) {
continue;
}
height += rc.theme->osd_window_switcher_item_height;

/* Include item border width */
height += rc.theme->osd_window_switcher_item_height
+ rc.theme->osd_border_width * 2;
}

/* Add OSD border width */
height += 2 * rc.theme->osd_border_width;
return height;
}
Expand Down Expand Up @@ -294,10 +300,13 @@ render_osd(struct server *server, cairo_t *cairo, int w, int h,

/* Draw border */
set_cairo_color(cairo, theme->osd_border_color);
draw_cairo_border(cairo, w, h, theme->osd_border_width);
struct wlr_fbox fbox = {
.width = w,
.height = h,
};
draw_cairo_border(cairo, fbox, theme->osd_border_width);

/* Set up text rendering */
int item_width = w - 2 * theme->osd_border_width;
set_cairo_color(cairo, theme->osd_label_text_color);
PangoLayout *layout = pango_cairo_create_layout(cairo);
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
Expand Down Expand Up @@ -329,6 +338,12 @@ render_osd(struct server *server, cairo_t *cairo, int w, int h,
struct buf buf;
buf_init(&buf);

/*
* Subtract 4x border-width to allow for both the OSD border and the
* item border. This is the width of the area available for text fields.
*/
int available_width = w - 4 * theme->osd_border_width;

/* Draw text for each node */
wl_list_for_each_reverse(node, node_list, link) {
if (!node->data) {
Expand All @@ -341,11 +356,32 @@ render_osd(struct server *server, cairo_t *cairo, int w, int h,
continue;
}

int x = theme->osd_border_width + theme->osd_window_switcher_item_padding_x;
/*
* OSD border
* +---------------------------------+
* | |
* | item border |
* |+-------------------------------+|
* || ||
* ||padding between each field ||
* ||| field-1 | field-2 | field-n |||
* || ||
* || ||
* |+-------------------------------+|
* | |
* | |
* +---------------------------------+
*/
y += theme->osd_border_width;
int x = theme->osd_window_switcher_item_padding_x
+ 2 * theme->osd_border_width;

int nr_fields = wl_list_length(&rc.window_switcher.fields);
struct window_switcher_field *field;
wl_list_for_each(field, &rc.window_switcher.fields, link) {
buf.len = 0;
cairo_move_to(cairo, x, y + theme->osd_window_switcher_item_padding_y - 1);
cairo_move_to(cairo, x,
y + theme->osd_window_switcher_item_padding_y - 1);

switch (field->content) {
case LAB_FIELD_TYPE:
Expand All @@ -360,8 +396,9 @@ render_osd(struct server *server, cairo_t *cairo, int w, int h,
default:
break;
}
int field_width = field->width / 100.0 * item_width
- 2 * theme->osd_window_switcher_item_padding_x;
int field_width = (available_width - (nr_fields + 1)
* theme->osd_window_switcher_item_padding_x)
* field->width / 100.0;
pango_layout_set_width(layout, field_width * PANGO_SCALE);
pango_layout_set_text(layout, buf.buf, -1);
pango_cairo_show_layout(cairo, layout);
Expand All @@ -370,13 +407,19 @@ render_osd(struct server *server, cairo_t *cairo, int w, int h,

if (view == cycle_view) {
/* Highlight current window */
cairo_rectangle(cairo, theme->osd_border_width, y,
theme->osd_window_switcher_width - 2 * theme->osd_border_width,
theme->osd_window_switcher_item_height);
struct wlr_fbox fbox = {
.x = theme->osd_border_width,
.y = y - theme->osd_border_width,
.width = theme->osd_window_switcher_width
- 2 * theme->osd_border_width,
.height = theme->osd_window_switcher_item_height
+ 2 * theme->osd_border_width,
};
draw_cairo_border(cairo, fbox, theme->osd_border_width);
cairo_stroke(cairo);
}

y += theme->osd_window_switcher_item_height;
y += theme->osd_border_width + theme->osd_window_switcher_item_height;
}
free(buf.buf);
g_object_unref(layout);
Expand Down
6 changes: 5 additions & 1 deletion src/workspaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ _osd_update(struct server *server)

/* Border */
set_cairo_color(cairo, theme->osd_border_color);
draw_cairo_border(cairo, width, height, theme->osd_border_width);
struct wlr_fbox fbox = {
.width = width,
.height = height,
};
draw_cairo_border(cairo, fbox, theme->osd_border_width);

uint16_t x = (width - marker_width) / 2;
wl_list_for_each(workspace, &server->workspaces, link) {
Expand Down

0 comments on commit b200dd2

Please sign in to comment.