Pixel-perfect rendering

This commit is contained in:
Drew DeVault 2018-03-29 11:58:54 -04:00
parent 37b61eff2d
commit 1e8faeec02
4 changed files with 75 additions and 37 deletions

View file

@ -24,6 +24,7 @@ struct swaybar_config {
char *font; char *font;
char *sep_symbol; char *sep_symbol;
char *mode; char *mode;
bool mode_pango_markup;
bool strip_workspace_numbers; bool strip_workspace_numbers;
bool binding_mode_indicator; bool binding_mode_indicator;
bool wrap_scroll; bool wrap_scroll;

View file

@ -41,6 +41,7 @@ struct swaybar_config *init_config() {
/* colors */ /* colors */
config->colors.background = 0x000000FF; config->colors.background = 0x000000FF;
config->colors.focused_background = 0x000000FF;
config->colors.statusline = 0xFFFFFFFF; config->colors.statusline = 0xFFFFFFFF;
config->colors.separator = 0x666666FF; config->colors.separator = 0x666666FF;

View file

@ -48,58 +48,76 @@ static void ipc_parse_colors(
json_object_object_get_ex(colors, "binding_mode_bg", &binding_mode_bg); json_object_object_get_ex(colors, "binding_mode_bg", &binding_mode_bg);
json_object_object_get_ex(colors, "binding_mode_text", &binding_mode_text); json_object_object_get_ex(colors, "binding_mode_text", &binding_mode_text);
if (background) { if (background) {
config->colors.background = parse_color(json_object_get_string(background)); config->colors.background = parse_color(
json_object_get_string(background));
} }
if (statusline) { if (statusline) {
config->colors.statusline = parse_color(json_object_get_string(statusline)); config->colors.statusline = parse_color(
json_object_get_string(statusline));
} }
if (separator) { if (separator) {
config->colors.separator = parse_color(json_object_get_string(separator)); config->colors.separator = parse_color(
json_object_get_string(separator));
} }
if (focused_background) { if (focused_background) {
config->colors.focused_background = parse_color(json_object_get_string(focused_background)); config->colors.focused_background = parse_color(
json_object_get_string(focused_background));
} }
if (focused_statusline) { if (focused_statusline) {
config->colors.focused_statusline = parse_color(json_object_get_string(focused_statusline)); config->colors.focused_statusline = parse_color(
json_object_get_string(focused_statusline));
} }
if (focused_separator) { if (focused_separator) {
config->colors.focused_separator = parse_color(json_object_get_string(focused_separator)); config->colors.focused_separator = parse_color(
json_object_get_string(focused_separator));
} }
if (focused_workspace_border) { if (focused_workspace_border) {
config->colors.focused_workspace.border = parse_color(json_object_get_string(focused_workspace_border)); config->colors.focused_workspace.border = parse_color(
json_object_get_string(focused_workspace_border));
} }
if (focused_workspace_bg) { if (focused_workspace_bg) {
config->colors.focused_workspace.background = parse_color(json_object_get_string(focused_workspace_bg)); config->colors.focused_workspace.background = parse_color(
json_object_get_string(focused_workspace_bg));
} }
if (focused_workspace_text) { if (focused_workspace_text) {
config->colors.focused_workspace.text = parse_color(json_object_get_string(focused_workspace_text)); config->colors.focused_workspace.text = parse_color(
json_object_get_string(focused_workspace_text));
} }
if (active_workspace_border) { if (active_workspace_border) {
config->colors.active_workspace.border = parse_color(json_object_get_string(active_workspace_border)); config->colors.active_workspace.border = parse_color(
json_object_get_string(active_workspace_border));
} }
if (active_workspace_bg) { if (active_workspace_bg) {
config->colors.active_workspace.background = parse_color(json_object_get_string(active_workspace_bg)); config->colors.active_workspace.background = parse_color(
json_object_get_string(active_workspace_bg));
} }
if (active_workspace_text) { if (active_workspace_text) {
config->colors.active_workspace.text = parse_color(json_object_get_string(active_workspace_text)); config->colors.active_workspace.text = parse_color(
json_object_get_string(active_workspace_text));
} }
if (inactive_workspace_border) { if (inactive_workspace_border) {
config->colors.inactive_workspace.border = parse_color(json_object_get_string(inactive_workspace_border)); config->colors.inactive_workspace.border = parse_color(
json_object_get_string(inactive_workspace_border));
} }
if (inactive_workspace_bg) { if (inactive_workspace_bg) {
config->colors.inactive_workspace.background = parse_color(json_object_get_string(inactive_workspace_bg)); config->colors.inactive_workspace.background = parse_color(
json_object_get_string(inactive_workspace_bg));
} }
if (inactive_workspace_text) { if (inactive_workspace_text) {
config->colors.inactive_workspace.text = parse_color(json_object_get_string(inactive_workspace_text)); config->colors.inactive_workspace.text = parse_color(
json_object_get_string(inactive_workspace_text));
} }
if (binding_mode_border) { if (binding_mode_border) {
config->colors.binding_mode.border = parse_color(json_object_get_string(binding_mode_border)); config->colors.binding_mode.border = parse_color(
json_object_get_string(binding_mode_border));
} }
if (binding_mode_bg) { if (binding_mode_bg) {
config->colors.binding_mode.background = parse_color(json_object_get_string(binding_mode_bg)); config->colors.binding_mode.background = parse_color(
json_object_get_string(binding_mode_bg));
} }
if (binding_mode_text) { if (binding_mode_text) {
config->colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text)); config->colors.binding_mode.text = parse_color(
json_object_get_string(binding_mode_text));
} }
} }
@ -306,14 +324,13 @@ bool handle_ipc_event(struct swaybar *bar) {
ipc_get_workspaces(bar); ipc_get_workspaces(bar);
break; break;
case IPC_EVENT_MODE: { case IPC_EVENT_MODE: {
// TODO: interpret "pango_markup" field
json_object *result = json_tokener_parse(resp->payload); json_object *result = json_tokener_parse(resp->payload);
if (!result) { if (!result) {
free_ipc_response(resp); free_ipc_response(resp);
wlr_log(L_ERROR, "failed to parse payload as json"); wlr_log(L_ERROR, "failed to parse payload as json");
return false; return false;
} }
json_object *json_change; json_object *json_change, *json_pango_markup;
if (json_object_object_get_ex(result, "change", &json_change)) { if (json_object_object_get_ex(result, "change", &json_change)) {
const char *change = json_object_get_string(json_change); const char *change = json_object_get_string(json_change);
free(bar->config->mode); free(bar->config->mode);
@ -328,6 +345,11 @@ bool handle_ipc_event(struct swaybar *bar) {
free_ipc_response(resp); free_ipc_response(resp);
return false; return false;
} }
if (json_object_object_get_ex(result,
"pango_markup", &json_pango_markup)) {
bar->config->mode_pango_markup = json_object_get_boolean(
json_pango_markup);
}
json_object_put(result); json_object_put(result);
break; break;
} }

View file

@ -13,33 +13,39 @@
static const int ws_horizontal_padding = 5; static const int ws_horizontal_padding = 5;
static const double ws_vertical_padding = 1.5; static const double ws_vertical_padding = 1.5;
static const int ws_spacing = 1; static const double border_width = 1;
static uint32_t render_binding_mode_indicator(cairo_t *cairo, static uint32_t render_binding_mode_indicator(cairo_t *cairo,
struct swaybar_config *config, const char *mode, double x, struct swaybar_config *config, const char *mode, double x,
uint32_t height) { uint32_t height) {
int text_width, text_height; int text_width, text_height;
get_text_size(cairo, config->font, &text_width, &text_height, get_text_size(cairo, config->font, &text_width, &text_height,
1, true, "⚡ %s", mode); 1, true, "%s", mode);
uint32_t ideal_height = text_height + ws_vertical_padding * 2; uint32_t ideal_height = text_height + ws_vertical_padding * 2
+ border_width * 2;
if (height < ideal_height) { if (height < ideal_height) {
height = ideal_height; height = ideal_height;
} }
uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
cairo_set_source_u32(cairo, config->colors.binding_mode.background); cairo_set_source_u32(cairo, config->colors.binding_mode.background);
cairo_rectangle(cairo, x, 0, text_width + ws_horizontal_padding * 2 - 1, cairo_rectangle(cairo, x, 0, width, height);
height + ws_vertical_padding * 2);
cairo_fill(cairo); cairo_fill(cairo);
cairo_set_source_u32(cairo, config->colors.binding_mode.border); cairo_set_source_u32(cairo, config->colors.binding_mode.border);
cairo_rectangle(cairo, x, 0, text_width + ws_horizontal_padding * 2 - 1, cairo_rectangle(cairo, x, 0, width, border_width);
height + ws_vertical_padding * 2); cairo_fill(cairo);
cairo_stroke(cairo); cairo_rectangle(cairo, x, 0, border_width, height);
cairo_fill(cairo);
cairo_rectangle(cairo, x + width - border_width, 0, border_width, height);
cairo_fill(cairo);
cairo_rectangle(cairo, x, height - border_width, width, border_width);
cairo_fill(cairo);
double text_y = height / 2.0 - text_height / 2.0; double text_y = height / 2.0 - text_height / 2.0;
cairo_set_source_u32(cairo, config->colors.binding_mode.text); cairo_set_source_u32(cairo, config->colors.binding_mode.text);
cairo_move_to(cairo, (int)x + ws_horizontal_padding, (int)floor(text_y)); cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y));
pango_printf(cairo, config->font, 1, true, "⚡ %s", mode); pango_printf(cairo, config->font, 1, true, "%s", mode);
return ideal_height; return ideal_height;
} }
@ -78,26 +84,33 @@ static uint32_t render_workspace_button(cairo_t *cairo,
int text_width, text_height; int text_width, text_height;
get_text_size(cairo, config->font, &text_width, &text_height, get_text_size(cairo, config->font, &text_width, &text_height,
1, true, "%s", name); 1, true, "%s", name);
uint32_t ideal_height = ws_vertical_padding * 2 + text_height; uint32_t ideal_height = ws_vertical_padding * 2 + text_height
+ border_width * 2;
if (height < ideal_height) { if (height < ideal_height) {
height = ideal_height; height = ideal_height;
} }
uint32_t width = ws_horizontal_padding * 2 + text_width; uint32_t width = ws_horizontal_padding * 2 + text_width + border_width * 2;
cairo_set_source_u32(cairo, box_colors.background); cairo_set_source_u32(cairo, box_colors.background);
cairo_rectangle(cairo, *x, 0, width - 1, height); cairo_rectangle(cairo, *x, 0, width, height);
cairo_fill(cairo); cairo_fill(cairo);
cairo_set_source_u32(cairo, box_colors.border); cairo_set_source_u32(cairo, box_colors.border);
cairo_rectangle(cairo, *x, 0, width - 1, height); cairo_rectangle(cairo, *x, 0, width, border_width);
cairo_stroke(cairo); cairo_fill(cairo);
cairo_rectangle(cairo, *x, 0, border_width, height);
cairo_fill(cairo);
cairo_rectangle(cairo, *x + width - border_width, 0, border_width, height);
cairo_fill(cairo);
cairo_rectangle(cairo, *x, height - border_width, width, border_width);
cairo_fill(cairo);
double text_y = height / 2.0 - text_height / 2.0; double text_y = height / 2.0 - text_height / 2.0;
cairo_set_source_u32(cairo, box_colors.text); cairo_set_source_u32(cairo, box_colors.text);
cairo_move_to(cairo, (int)*x + ws_horizontal_padding, (int)floor(text_y)); cairo_move_to(cairo, *x + width / 2 - text_width / 2, (int)floor(text_y));
pango_printf(cairo, config->font, 1, true, "%s", name); pango_printf(cairo, config->font, 1, true, "%s", name);
*x += width + ws_spacing; *x += width;
return ideal_height; return ideal_height;
} }
@ -167,6 +180,7 @@ void render_frame(struct swaybar *bar,
cairo_set_source_surface(shm, recorder, 0.0, 0.0); cairo_set_source_surface(shm, recorder, 0.0, 0.0);
cairo_paint(shm); cairo_paint(shm);
wl_surface_attach(output->surface, wl_surface_attach(output->surface,
output->current_buffer->buffer, 0, 0); output->current_buffer->buffer, 0, 0);
wl_surface_damage(output->surface, 0, 0, output->width, output->height); wl_surface_damage(output->surface, 0, 0, output->width, output->height);