better line passing in preparation and drawing

This commit is contained in:
FelixKratz 2021-09-05 16:42:52 +02:00
parent 461d4ef923
commit 8000a3dfcc
4 changed files with 30 additions and 26 deletions

View file

@ -26,7 +26,7 @@ static CTFontRef bar_create_font(char *cstring) {
return font; return font;
} }
static CGPoint bar_align_line(struct bar *bar, struct bar_line line, int align_x, int align_y) { static CGPoint bar_align_line(struct bar *bar, struct bar_line* line, int align_x, int align_y) {
float x = 0, y = 0; float x = 0, y = 0;
if (align_x == ALIGN_NONE) { if (align_x == ALIGN_NONE) {
@ -34,9 +34,9 @@ static CGPoint bar_align_line(struct bar *bar, struct bar_line line, int align_x
} else if (align_x == ALIGN_LEFT) { } else if (align_x == ALIGN_LEFT) {
x = 20; x = 20;
} else if (align_x == ALIGN_CENTER) { } else if (align_x == ALIGN_CENTER) {
x = (bar->frame.size.width / 2) - (line.bounds.size.width / 2); x = (bar->frame.size.width / 2) - (line->bounds.size.width / 2);
} else if (align_x == ALIGN_RIGHT) { } else if (align_x == ALIGN_RIGHT) {
x = bar->frame.size.width - line.bounds.size.width - 20; x = bar->frame.size.width - line->bounds.size.width - 20;
} }
if (align_y == ALIGN_NONE) { if (align_y == ALIGN_NONE) {
@ -44,9 +44,9 @@ static CGPoint bar_align_line(struct bar *bar, struct bar_line line, int align_x
} else if (align_y == ALIGN_TOP) { } else if (align_y == ALIGN_TOP) {
y = bar->frame.size.height; y = bar->frame.size.height;
} else if (align_y == ALIGN_CENTER) { } else if (align_y == ALIGN_CENTER) {
y = (bar->frame.size.height / 2) - ((line.ascent - line.descent) / 2); y = (bar->frame.size.height / 2) - ((line->ascent - line->descent) / 2);
} else if (align_y == ALIGN_BOTTOM) { } else if (align_y == ALIGN_BOTTOM) {
y = line.descent; y = line->descent;
} }
return (CGPoint) { x, y }; return (CGPoint) { x, y };
@ -62,30 +62,22 @@ static void bar_destroy_line(struct bar_line* line) {
CFRelease(line->line); CFRelease(line->line);
} }
static struct bar_line bar_prepare_line(CTFontRef font, char *cstring, struct rgba_color color) { void bar_prepare_line(struct bar_line* bar_line, CTFontRef font, char* cstring, struct rgba_color color) {
const void *keys[] = { kCTFontAttributeName, kCTForegroundColorFromContextAttributeName }; const void *keys[] = { kCTFontAttributeName, kCTForegroundColorFromContextAttributeName };
const void *values[] = { font, kCFBooleanTrue }; const void *values[] = { font, kCFBooleanTrue };
CFDictionaryRef attributes = CFDictionaryCreate(NULL, keys, values, array_count(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionaryRef attributes = CFDictionaryCreate(NULL, keys, values, array_count(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFStringRef string = CFStringCreateWithCString(NULL, cstring, kCFStringEncodingUTF8); CFStringRef string = CFStringCreateWithCString(NULL, cstring, kCFStringEncodingUTF8);
if (!string) string = CFStringCreateWithCString(NULL, "Warning: Malformed UTF-8 string", kCFStringEncodingUTF8); if (!string) string = CFStringCreateWithCString(NULL, "Warning: Malformed UTF-8 string", kCFStringEncodingUTF8);
CFAttributedStringRef attr_string = CFAttributedStringCreate(NULL, string, attributes); CFAttributedStringRef attr_string = CFAttributedStringCreate(NULL, string, attributes);
CTLineRef line = CTLineCreateWithAttributedString(attr_string); bar_line->line = CTLineCreateWithAttributedString(attr_string);
CGFloat ascent, descent; CTLineGetTypographicBounds(bar_line->line, &bar_line->ascent, &bar_line->descent, NULL);
CTLineGetTypographicBounds(line, &ascent, &descent, NULL); bar_line->bounds = CTLineGetBoundsWithOptions(bar_line->line, kCTLineBoundsUseGlyphPathBounds);
CGRect bounds = CTLineGetBoundsWithOptions(line, kCTLineBoundsUseGlyphPathBounds); bar_line->color = color;
CFRelease(string); CFRelease(string);
CFRelease(attributes); CFRelease(attributes);
CFRelease(attr_string); CFRelease(attr_string);
return (struct bar_line) {
.line = line,
.ascent = ascent,
.descent = descent,
.bounds = bounds,
.color = color
};
} }
void bar_draw_graph_line(struct bar *bar, struct graph_data* graph_data, uint32_t x, uint32_t y, bool right_to_left) { void bar_draw_graph_line(struct bar *bar, struct graph_data* graph_data, uint32_t x, uint32_t y, bool right_to_left) {
@ -213,8 +205,8 @@ void bar_refresh(struct bar* bar) {
if (!bar_item->drawing) continue; if (!bar_item->drawing) continue;
struct bar_line* label = &bar_item->label_line; struct bar_line* label = &bar_item->label_line;
struct bar_line* icon = &bar_item->icon_line; struct bar_line* icon = &bar_item->icon_line;
CGPoint icon_position = bar_align_line(bar, *icon, ALIGN_CENTER, ALIGN_CENTER); CGPoint icon_position = bar_align_line(bar, icon, ALIGN_CENTER, ALIGN_CENTER);
CGPoint label_position = bar_align_line(bar, *label, ALIGN_CENTER, ALIGN_CENTER); CGPoint label_position = bar_align_line(bar, label, ALIGN_CENTER, ALIGN_CENTER);
uint32_t graph_x = 0; uint32_t graph_x = 0;
bool graph_rtl = false; bool graph_rtl = false;

View file

@ -115,12 +115,16 @@ void bar_item_set_icon(struct bar_item* bar_item, char* icon) {
if (icon != bar_item->icon && !bar_item->icon) if (icon != bar_item->icon && !bar_item->icon)
free(bar_item->icon); free(bar_item->icon);
bar_item->icon = icon; bar_item->icon = icon;
bar_item->icon_line = bar_prepare_line(bar_item->icon_font, bar_item->icon, bar_item->icon_highlight ? bar_item->icon_highlight_color : bar_item->icon_color); bar_prepare_line(&bar_item->icon_line, bar_item->icon_font, bar_item->icon, bar_item->icon_highlight ? bar_item->icon_highlight_color : bar_item->icon_color);
}
void bar_item_update_icon_color(struct bar_item *bar_item) {
bar_item->icon_line.color = bar_item->icon_highlight ? bar_item->icon_highlight_color : bar_item->icon_color;
} }
void bar_item_set_icon_color(struct bar_item* bar_item, uint32_t color) { void bar_item_set_icon_color(struct bar_item* bar_item, uint32_t color) {
bar_item->icon_color = rgba_color_from_hex(color); bar_item->icon_color = rgba_color_from_hex(color);
bar_item_set_icon(bar_item, bar_item->icon); bar_item->icon_line.color = rgba_color_from_hex(color);
} }
void bar_item_set_label(struct bar_item* bar_item, char* label) { void bar_item_set_label(struct bar_item* bar_item, char* label) {
@ -130,13 +134,19 @@ void bar_item_set_label(struct bar_item* bar_item, char* label) {
if (label != bar_item->label && !bar_item->label) if (label != bar_item->label && !bar_item->label)
free(bar_item->label); free(bar_item->label);
bar_item->label = label; bar_item->label = label;
bar_item->label_line = bar_prepare_line(bar_item->label_font, bar_item->label, bar_item->label_highlight ? bar_item->label_highlight_color : bar_item->label_color); bar_prepare_line(&bar_item->label_line, bar_item->label_font, bar_item->label, bar_item->label_highlight ? bar_item->label_highlight_color : bar_item->label_color);
} }
void bar_item_set_label_color(struct bar_item* bar_item, uint32_t color) { void bar_item_set_label_color(struct bar_item* bar_item, uint32_t color) {
bar_item->label_color = rgba_color_from_hex(color); bar_item->label_color = rgba_color_from_hex(color);
bar_item_set_label(bar_item, bar_item->label); bar_item->label_line.color = rgba_color_from_hex(color);
} }
void bar_item_update_label_color(struct bar_item *bar_item) {
bar_item->label_line.color = bar_item->label_highlight ? bar_item->label_highlight_color : bar_item->label_color;
}
void bar_item_set_icon_font(struct bar_item* bar_item, char *font_string) { void bar_item_set_icon_font(struct bar_item* bar_item, char *font_string) {
if (!font_string) return; if (!font_string) return;
if (bar_item->icon_font) if (bar_item->icon_font)

View file

@ -85,8 +85,10 @@ void bar_item_set_padding_left(struct bar_item* bar_item, uint32_t pad);
void bar_item_set_padding_right(struct bar_item* bar_item, uint32_t pad); void bar_item_set_padding_right(struct bar_item* bar_item, uint32_t pad);
void bar_item_set_icon(struct bar_item* bar_item, char* icon); void bar_item_set_icon(struct bar_item* bar_item, char* icon);
void bar_item_set_icon_color(struct bar_item* bar_item, uint32_t color); void bar_item_set_icon_color(struct bar_item* bar_item, uint32_t color);
void bar_item_update_icon_color(struct bar_item* bar_item);
void bar_item_set_label(struct bar_item* bar_item, char* label); void bar_item_set_label(struct bar_item* bar_item, char* label);
void bar_item_set_label_color(struct bar_item* bar_item, uint32_t color); void bar_item_set_label_color(struct bar_item* bar_item, uint32_t color);
void bar_item_update_label_color(struct bar_item* bar_item);
void bar_item_set_label_font(struct bar_item* bar_item, char *font_string); void bar_item_set_label_font(struct bar_item* bar_item, char *font_string);
void bar_item_set_icon_font(struct bar_item* bar_item, char *font_string); void bar_item_set_icon_font(struct bar_item* bar_item, char *font_string);

View file

@ -420,11 +420,11 @@ static void handle_domain_set(FILE* rsp, struct token domain, char* message) {
} else if (token_equals(property, COMMAND_SET_LABEL_HIGHLIGHT)) { } else if (token_equals(property, COMMAND_SET_LABEL_HIGHLIGHT)) {
struct token value = get_token(&message); struct token value = get_token(&message);
bar_item->label_highlight = token_equals(value, ARGUMENT_COMMON_VAL_ON) ? true : false; bar_item->label_highlight = token_equals(value, ARGUMENT_COMMON_VAL_ON) ? true : false;
bar_item_set_label(bar_item, bar_item->label); bar_item_update_label_color(bar_item);
} else if (token_equals(property, COMMAND_SET_ICON_HIGHLIGHT)) { } else if (token_equals(property, COMMAND_SET_ICON_HIGHLIGHT)) {
struct token value = get_token(&message); struct token value = get_token(&message);
bar_item->icon_highlight = token_equals(value, ARGUMENT_COMMON_VAL_ON) ? true : false; bar_item->icon_highlight = token_equals(value, ARGUMENT_COMMON_VAL_ON) ? true : false;
bar_item_set_icon(bar_item, bar_item->icon); bar_item_update_icon_color(bar_item);
} }
// DEPRECATED // DEPRECATED
else if (token_equals(property, COMMAND_SET_ENABLED)) { else if (token_equals(property, COMMAND_SET_ENABLED)) {