deco: refactor button code

- Make the height+width of buttons the same as the title bar.
- Center icons within the 'button space'
- Show button background color on hover (just hard-coded grey for now)
This commit is contained in:
Johan Malm 2020-09-21 19:24:27 +01:00
parent 3c90cb7945
commit 4ecc5efa73
3 changed files with 56 additions and 21 deletions

View file

@ -90,9 +90,7 @@ static void process_cursor_motion(struct server *server, uint32_t time)
/* TODO: Could we use wlr_xcursor_get_resize_name() here?? */ /* TODO: Could we use wlr_xcursor_get_resize_name() here?? */
switch (view_area) { switch (view_area) {
case LAB_DECO_PART_TITLE: case LAB_DECO_NONE:
wlr_xcursor_manager_set_cursor_image(
server->cursor_mgr, XCURSOR_DEFAULT, server->cursor);
break; break;
case LAB_DECO_PART_TOP: case LAB_DECO_PART_TOP:
wlr_xcursor_manager_set_cursor_image( wlr_xcursor_manager_set_cursor_image(
@ -110,6 +108,10 @@ static void process_cursor_motion(struct server *server, uint32_t time)
wlr_xcursor_manager_set_cursor_image( wlr_xcursor_manager_set_cursor_image(
server->cursor_mgr, "left_side", server->cursor); server->cursor_mgr, "left_side", server->cursor);
break; break;
default:
wlr_xcursor_manager_set_cursor_image(
server->cursor_mgr, XCURSOR_DEFAULT, server->cursor);
break;
} }
if (surface) { if (surface) {
bool focus_changed = seat->pointer_state.focused_surface != bool focus_changed = seat->pointer_state.focused_surface !=

View file

@ -37,31 +37,26 @@ struct wlr_box deco_max_extents(struct view *view)
struct wlr_box deco_box(struct view *view, enum deco_part deco_part) struct wlr_box deco_box(struct view *view, enum deco_part deco_part)
{ {
int margin;
struct wlr_box box = { .x = 0, .y = 0, .width = 0, .height = 0 }; struct wlr_box box = { .x = 0, .y = 0, .width = 0, .height = 0 };
BUG_ON(!view); BUG_ON(!view);
switch (deco_part) { switch (deco_part) {
case LAB_DECO_BUTTON_CLOSE: case LAB_DECO_BUTTON_CLOSE:
wlr_texture_get_size(theme.xbm_close_active_unpressed, box.width = rc.title_height;
&box.width, &box.height); box.height = rc.title_height;
margin = (rc.title_height - box.height) / 2; box.x = view->x + view->w - rc.title_height;
box.x = view->x + view->w + margin - rc.title_height; box.y = view->y - rc.title_height;
box.y = view->y - rc.title_height + margin;
break; break;
case LAB_DECO_BUTTON_MAXIMIZE: case LAB_DECO_BUTTON_MAXIMIZE:
wlr_texture_get_size(theme.xbm_maximize_active_unpressed, box.width = rc.title_height;
&box.width, &box.height); box.height = rc.title_height;
margin = (rc.title_height - box.height) / 2; box.x = view->x + view->w - rc.title_height * 2;
box.x = view->x + view->w + margin - rc.title_height * 2; box.y = view->y - rc.title_height;
box.y = view->y - rc.title_height + margin;
break; break;
case LAB_DECO_BUTTON_ICONIFY: case LAB_DECO_BUTTON_ICONIFY:
wlr_texture_get_size(theme.xbm_iconify_active_unpressed, box.width = rc.title_height;
&box.width, &box.height); box.height = rc.title_height;
margin = (rc.title_height - box.height) / 2; box.x = view->x + view->w - rc.title_height * 3;
box.x = view->x + view->w + margin - rc.title_height * 3; box.y = view->y - rc.title_height;
box.y = view->y - rc.title_height + margin;
break; break;
case LAB_DECO_PART_TITLE: case LAB_DECO_PART_TITLE:
box.x = view->x; box.x = view->x;

View file

@ -79,11 +79,35 @@ static void render_icon(struct draw_data *d, struct wlr_box box,
if (!texture) if (!texture)
return; return;
float matrix[9]; float matrix[9];
wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0,
/* centre-align icon if smaller than designated box */
struct wlr_box button;
wlr_texture_get_size(texture, &button.width, &button.height);
if (box.width > button.width) {
button.x = box.x + (box.width - button.width) / 2;
} else {
button.x = box.x;
button.width = box.width;
}
if (box.height > button.height) {
button.y = box.y + (box.height - button.height) / 2;
} else {
button.y = box.y;
button.height = box.height;
}
wlr_matrix_project_box(matrix, &button, WL_OUTPUT_TRANSFORM_NORMAL, 0,
d->transform_matrix); d->transform_matrix);
wlr_render_texture_with_matrix(d->renderer, texture, matrix, 1); wlr_render_texture_with_matrix(d->renderer, texture, matrix, 1);
} }
static bool isbutton(enum deco_part deco_part)
{
return deco_part == LAB_DECO_BUTTON_CLOSE ||
deco_part == LAB_DECO_BUTTON_MAXIMIZE ||
deco_part == LAB_DECO_BUTTON_ICONIFY;
}
static void render_decorations(struct wlr_output *output, struct view *view) static void render_decorations(struct wlr_output *output, struct view *view)
{ {
if (!view->server_side_deco) if (!view->server_side_deco)
@ -93,18 +117,32 @@ static void render_decorations(struct wlr_output *output, struct view *view)
.transform_matrix = output->transform_matrix, .transform_matrix = output->transform_matrix,
}; };
/* border */
ddata.rgba = theme.window_active_handle_bg_color; ddata.rgba = theme.window_active_handle_bg_color;
draw_rect(&ddata, deco_box(view, LAB_DECO_PART_TOP)); draw_rect(&ddata, deco_box(view, LAB_DECO_PART_TOP));
draw_rect(&ddata, deco_box(view, LAB_DECO_PART_RIGHT)); draw_rect(&ddata, deco_box(view, LAB_DECO_PART_RIGHT));
draw_rect(&ddata, deco_box(view, LAB_DECO_PART_BOTTOM)); draw_rect(&ddata, deco_box(view, LAB_DECO_PART_BOTTOM));
draw_rect(&ddata, deco_box(view, LAB_DECO_PART_LEFT)); draw_rect(&ddata, deco_box(view, LAB_DECO_PART_LEFT));
/* title */
if (view->surface == seat_focused_surface()) if (view->surface == seat_focused_surface())
ddata.rgba = theme.window_active_title_bg_color; ddata.rgba = theme.window_active_title_bg_color;
else else
ddata.rgba = theme.window_inactive_title_bg_color; ddata.rgba = theme.window_inactive_title_bg_color;
draw_rect(&ddata, deco_box(view, LAB_DECO_PART_TITLE)); draw_rect(&ddata, deco_box(view, LAB_DECO_PART_TITLE));
/* button background */
struct wlr_cursor *cur = view->server->cursor;
enum deco_part deco_part = deco_at(view, cur->x, cur->y);
struct wlr_box box = deco_box(view, deco_part);
if (isbutton(deco_part) &&
wlr_box_contains_point(&box, cur->x, cur->y)) {
ddata.rgba = (float[4]){ 0.5, 0.5, 0.5, 0.5 };
draw_rect(&ddata, deco_box(view, deco_part));
}
/* buttons */
if (view->surface == seat_focused_surface()) { if (view->surface == seat_focused_surface()) {
render_icon(&ddata, deco_box(view, LAB_DECO_BUTTON_CLOSE), render_icon(&ddata, deco_box(view, LAB_DECO_BUTTON_CLOSE),
theme.xbm_close_active_unpressed); theme.xbm_close_active_unpressed);