From 4ecc5efa73a237eba7f27db36c073dadbec45180 Mon Sep 17 00:00:00 2001 From: Johan Malm Date: Mon, 21 Sep 2020 19:24:27 +0100 Subject: [PATCH] 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) --- src/cursor.c | 8 +++++--- src/deco.c | 29 ++++++++++++----------------- src/output.c | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/cursor.c b/src/cursor.c index faa75e10..14604310 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -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?? */ switch (view_area) { - case LAB_DECO_PART_TITLE: - wlr_xcursor_manager_set_cursor_image( - server->cursor_mgr, XCURSOR_DEFAULT, server->cursor); + case LAB_DECO_NONE: break; case LAB_DECO_PART_TOP: 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( server->cursor_mgr, "left_side", server->cursor); break; + default: + wlr_xcursor_manager_set_cursor_image( + server->cursor_mgr, XCURSOR_DEFAULT, server->cursor); + break; } if (surface) { bool focus_changed = seat->pointer_state.focused_surface != diff --git a/src/deco.c b/src/deco.c index 6974003b..8bba5d65 100644 --- a/src/deco.c +++ b/src/deco.c @@ -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) { - int margin; - struct wlr_box box = { .x = 0, .y = 0, .width = 0, .height = 0 }; BUG_ON(!view); switch (deco_part) { case LAB_DECO_BUTTON_CLOSE: - wlr_texture_get_size(theme.xbm_close_active_unpressed, - &box.width, &box.height); - margin = (rc.title_height - box.height) / 2; - box.x = view->x + view->w + margin - rc.title_height; - box.y = view->y - rc.title_height + margin; + box.width = rc.title_height; + box.height = rc.title_height; + box.x = view->x + view->w - rc.title_height; + box.y = view->y - rc.title_height; break; case LAB_DECO_BUTTON_MAXIMIZE: - wlr_texture_get_size(theme.xbm_maximize_active_unpressed, - &box.width, &box.height); - margin = (rc.title_height - box.height) / 2; - box.x = view->x + view->w + margin - rc.title_height * 2; - box.y = view->y - rc.title_height + margin; + box.width = rc.title_height; + box.height = rc.title_height; + box.x = view->x + view->w - rc.title_height * 2; + box.y = view->y - rc.title_height; break; case LAB_DECO_BUTTON_ICONIFY: - wlr_texture_get_size(theme.xbm_iconify_active_unpressed, - &box.width, &box.height); - margin = (rc.title_height - box.height) / 2; - box.x = view->x + view->w + margin - rc.title_height * 3; - box.y = view->y - rc.title_height + margin; + box.width = rc.title_height; + box.height = rc.title_height; + box.x = view->x + view->w - rc.title_height * 3; + box.y = view->y - rc.title_height; break; case LAB_DECO_PART_TITLE: box.x = view->x; diff --git a/src/output.c b/src/output.c index b51fad0b..773cbe29 100644 --- a/src/output.c +++ b/src/output.c @@ -79,11 +79,35 @@ static void render_icon(struct draw_data *d, struct wlr_box box, if (!texture) return; 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); 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) { 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, }; + /* border */ 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_RIGHT)); draw_rect(&ddata, deco_box(view, LAB_DECO_PART_BOTTOM)); draw_rect(&ddata, deco_box(view, LAB_DECO_PART_LEFT)); + /* title */ if (view->surface == seat_focused_surface()) ddata.rgba = theme.window_active_title_bg_color; else ddata.rgba = theme.window_inactive_title_bg_color; 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()) { render_icon(&ddata, deco_box(view, LAB_DECO_BUTTON_CLOSE), theme.xbm_close_active_unpressed);