further modularize core logic

This commit is contained in:
Felix Kratz 2021-11-11 17:56:44 +01:00
parent 28729302e3
commit 2815a6e325
14 changed files with 188 additions and 174 deletions

View file

@ -114,22 +114,6 @@ void alias_find_window(struct alias* alias) {
CFRelease(window_list);
}
/*
void alias_click(struct alias* alias) {
printf("Click \n");
CGEventSourceRef event_source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef click_event = CGEventCreateMouseEvent(event_source, kCGEventLeftMouseDown, CGPointMake(alias->bounds.origin.x + 5,alias->bounds.origin.y + 5), kCGMouseButtonLeft);
CGEventPostToPid(alias->pid, click_event);
//CGEventPost(kCGHIDEventTap, click_event);
CGEventSetType(click_event, kCGEventLeftMouseUp);
CGEventPostToPid(alias->pid, click_event);
//CGEventPost(kCGHIDEventTap, click_event);
CFRelease(click_event);
CFRelease(event_source);
}*/
bool alias_update_image(struct alias* alias) {
if (alias->wid == 0) alias_find_window(alias);
if (alias->wid == 0) {
@ -139,11 +123,9 @@ bool alias_update_image(struct alias* alias) {
SLSGetScreenRectForWindow(g_connection, alias->wid, &alias->bounds);
alias->bounds.size.width = (uint32_t) (alias->bounds.size.width + 0.5);
alias->bounds.origin.x = (uint32_t) (alias->bounds.origin.x + 0.5);
CGImageRef tmp_ref = NULL;
SLSCaptureWindowsContentsToRectWithOptions(g_connection, &alias->wid, true, CGRectNull, 1 << 8, &tmp_ref);
// alias_click(alias);
if (!tmp_ref) { alias->wid = 0; return false; }
@ -152,3 +134,13 @@ bool alias_update_image(struct alias* alias) {
return true;
}
void alias_draw(struct alias* alias, CGContextRef context) {
if (!alias->image_ref) return;
CGContextDrawImage(context, alias->bounds, alias->image_ref);
}
void alias_calculate_bounds(struct alias* alias, uint32_t x, uint32_t y) {
alias->bounds.origin.x = x;
alias->bounds.origin.y = y - alias->bounds.size.height / 2;
}

View file

@ -22,4 +22,7 @@ void alias_find_window(struct alias* alias);
uint32_t alias_get_length(struct alias* alias);
uint32_t alias_get_height(struct alias* alias);
void alias_calculate_bounds(struct alias* alias, uint32_t x, uint32_t y);
void alias_draw(struct alias* alias, CGContextRef context);
#endif

View file

@ -5,8 +5,8 @@ void background_init(struct background* background) {
background->enabled = false;
background->overrides_height = false;
background->height = 0;
background->width = 0;
background->bounds.size.height = 25;
background->bounds.size.width = 0;
background->border_width = 0;
background->padding_left = 0;
background->padding_right = 0;
@ -44,8 +44,8 @@ bool background_set_enabled(struct background* background, bool enabled) {
}
bool background_set_height(struct background* background, uint32_t height) {
if (background->height == height) return false;
background->height = height;
if (background->bounds.size.height == height) return false;
background->bounds.size.height = height;
background->overrides_height = height != 0;
return true;
}
@ -74,10 +74,14 @@ bool background_set_padding_right(struct background* background, uint32_t pad) {
return true;
}
void background_draw(struct background* background, CGPoint origin, CGContextRef context) {
void background_calculate_bounds(struct background* background, uint32_t x, uint32_t y) {
background->bounds.origin.x = x;
background->bounds.origin.y = y - background->bounds.size.height / 2;
}
void background_draw(struct background* background, CGContextRef context) {
if (!background->enabled) return;
CGRect draw_region = {{origin.x, origin.y}, {background->width, background->height}};
draw_rect(context, draw_region, &background->color, background->corner_radius, background->border_width, &background->border_color, false);
draw_rect(context, background->bounds, &background->color, background->corner_radius, background->border_width, &background->border_color, false);
}
static bool background_parse_sub_domain(struct background* background, FILE* rsp, struct token property, char* message) {

View file

@ -5,8 +5,7 @@
struct background {
bool enabled;
bool overrides_height;
uint32_t height;
uint32_t width;
CGRect bounds;
uint32_t corner_radius;
uint32_t border_width;
int padding_left;
@ -25,7 +24,8 @@ bool background_set_corner_radius(struct background* background, uint32_t corner
bool background_set_padding_left(struct background* background, uint32_t pad);
bool background_set_padding_right(struct background* background, uint32_t pad);
void background_draw(struct background* background, CGPoint origin, CGContextRef context);
void background_calculate_bounds(struct background* background, uint32_t x, uint32_t y);
void background_draw(struct background* background, CGContextRef context);
static bool background_parse_sub_domain(struct background* background, FILE* rsp, struct token property, char* message);

187
src/bar.c
View file

@ -1,8 +1,10 @@
#include "bar.h"
#include "alias.h"
#include "background.h"
#include "bar_item.h"
#include "display.h"
#include "graph.h"
#include "group.h"
#include "misc/helpers.h"
#include "text.h"
#include <_types/_uint32_t.h>
@ -10,84 +12,9 @@
extern struct bar_manager g_bar_manager;
static CGPoint bar_align_line(struct bar *bar, struct text_line* line, int align_x, int align_y) {
float x = 0, y = 0;
if (align_x == ALIGN_NONE) {
x = CGContextGetTextPosition(bar->context).x;
} else if (align_x == ALIGN_LEFT) {
x = 20;
} else if (align_x == ALIGN_CENTER) {
x = (bar->frame.size.width / 2) - (line->bounds.size.width / 2);
} else if (align_x == ALIGN_RIGHT) {
x = bar->frame.size.width - line->bounds.size.width - 20;
}
if (align_y == ALIGN_NONE) {
y = CGContextGetTextPosition(bar->context).y;
} else if (align_y == ALIGN_TOP) {
y = bar->frame.size.height;
} else if (align_y == ALIGN_CENTER) {
y = (bar->frame.size.height / 2) - ((line->ascent - line->descent) / 2);
} else if (align_y == ALIGN_BOTTOM) {
y = line->descent;
}
return (CGPoint) { x, y };
}
void bar_draw_graph_line(struct bar *bar, struct graph* graph, uint32_t x, uint32_t y, uint32_t height, bool right_to_left) {
y-= height / 2;
uint32_t sample_width = 1;
bool fill = graph->fill;
CGContextSaveGState(bar->context);
CGContextSetRGBStrokeColor(bar->context, graph->line_color.r, graph->line_color.g, graph->line_color.b, graph->line_color.a);
if (graph->overrides_fill_color) CGContextSetRGBFillColor(bar->context, graph->fill_color.r, graph->fill_color.g, graph->fill_color.b, graph->fill_color.a);
else CGContextSetRGBFillColor(bar->context, graph->line_color.r, graph->line_color.g, graph->line_color.b, 0.2 * graph->line_color.a);
CGContextSetLineWidth(bar->context, graph->line_width);
CGMutablePathRef p = CGPathCreateMutable();
uint32_t start_x = x;
if (right_to_left) {
CGPathMoveToPoint(p, NULL, x, y + graph_get_y(graph, graph->width - 1) * height);
for (int i = graph->width - 1; i > 0; --i, x -= sample_width) {
CGPathAddLineToPoint(p, NULL, x, y + graph_get_y(graph, i) * height);
}
}
else {
CGPathMoveToPoint(p, NULL, x, y + graph_get_y(graph, 0) * height);
for (int i = graph->width - 1; i > 0; --i, x += sample_width) {
CGPathAddLineToPoint(p, NULL, x, y + graph_get_y(graph, i) * height);
}
}
CGContextAddPath(bar->context, p);
CGContextStrokePath(bar->context);
if (fill) {
if (right_to_left) {
CGPathAddLineToPoint(p, NULL, x + sample_width, y);
}
else {
CGPathAddLineToPoint(p, NULL, x - sample_width, y);
}
CGPathAddLineToPoint(p, NULL, start_x, y);
CGPathCloseSubpath(p);
CGContextAddPath(bar->context, p);
CGContextFillPath(bar->context);
}
CGPathRelease(p);
CGContextRestoreGState(bar->context);
}
void bar_draw_graph(struct bar* bar, struct bar_item* bar_item, uint32_t x, bool right_to_left) {
if (!bar_item->has_graph) return;
bar_draw_graph_line(bar, &bar_item->graph, x, bar->frame.size.height / 2 + bar_item->graph.line_width + bar_item->y_offset, bar_item->background.enabled ? (bar_item->background.height - bar_item->background.border_width - 1) : (bar->frame.size.height - g_bar_manager.background.border_width - 1), right_to_left);
}
void bar_draw_item_background(struct bar* bar, struct bar_item* bar_item, uint32_t adid) {
if (!bar_item->background.enabled) return;
bool custom_height = bar_item->background.height != 0;
CGRect draw_region = {{bar_item->bounding_rects[adid - 1]->origin.x - bar->origin.x, custom_height ? ((bar->frame.size.height - bar_item->background.height) / 2 + bar_item->y_offset) : (g_bar_manager.background.border_width + 1)},
{bar_item->bounding_rects[adid - 1]->size.width, custom_height ? bar_item->background.height : (bar->frame.size.height - 2*(g_bar_manager.background.border_width + 1))}};
draw_rect(bar->context, draw_region, &bar_item->background.color, bar_item->background.corner_radius, bar_item->background.border_width, &bar_item->background.border_color, false);
}
void bar_draw_group(struct bar* bar, struct bar_item* item, uint32_t adid) {
@ -95,18 +22,16 @@ void bar_draw_group(struct bar* bar, struct bar_item* item, uint32_t adid) {
struct bar_item* bar_item = item->group->members[0];
if (!bar_item->background.enabled) return;
bool custom_height = bar_item->background.height != 0;
bool custom_height = bar_item->background.bounds.size.height != 0;
uint32_t group_length = group_get_length(bar_item->group);
CGRect draw_region = {{item->bounding_rects[adid - 1]->origin.x - bar->origin.x - (item->position == POSITION_RIGHT ? group_length - bar_item_get_length(item) : 0), custom_height ? ((bar->frame.size.height - bar_item->background.height)) / 2 : (g_bar_manager.background.border_width + 1)},
{group_length, custom_height ? bar_item->background.height : (bar->frame.size.height - 2*(g_bar_manager.background.border_width + 1))}};
draw_rect(bar->context, draw_region, &bar_item->background.color, bar_item->background.corner_radius, bar_item->background.border_width, &bar_item->background.border_color, false);
}
}
CGRect draw_region = {{item->bounding_rects[adid - 1]->origin.x - bar->origin.x - (item->position == POSITION_RIGHT ? group_length - bar_item_get_length(item) : 0), custom_height ? ((bar->frame.size.height - bar_item->background.bounds.size.height)) / 2 : (g_bar_manager.background.border_width + 1)},
{group_length, custom_height ? bar_item->background.bounds.size.height : (bar->frame.size.height - (g_bar_manager.background.border_width + 1))}};
void bar_draw_alias(struct bar* bar, struct bar_item* bar_item, uint32_t x) {
if (!bar_item->has_alias || !bar_item->alias.image_ref) return;
CGRect bounds = {{x, (bar->frame.size.height - bar_item->alias.bounds.size.height) / 2 + bar_item->y_offset},{bar_item->alias.bounds.size.width, bar_item->alias.bounds.size.height}};
CGContextDrawImage(bar->context, bounds, bar_item->alias.image_ref);
bar_item->background.bounds.size.height = draw_region.size.height;
bar_item->background.bounds.size.width = group_get_length(bar_item->group);
bar_item->background.bounds.origin = draw_region.origin;
background_draw(&bar_item->background, bar->context);
}
}
bool bar_draws_item(struct bar* bar, struct bar_item* bar_item) {
@ -136,14 +61,17 @@ void bar_draw_bar_items(struct bar* bar) {
bar_item_set_bounding_rect_for_display(bar_item, bar->adid, bar->origin);
bar_draw_group(bar, bar_item, bar->adid);
bar_draw_item_background(bar, bar_item, bar->adid);
if (bar_item->group && group_is_first_member(bar_item->group, bar_item))
group_draw(bar_item->group, bar->context);
text_draw(&bar_item->icon, bar_item->icon.line.bounds.origin, bar->context);
text_draw(&bar_item->label, bar_item->label.line.bounds.origin, bar->context);
background_draw(&bar_item->background, bar->context);
text_draw(&bar_item->icon, bar->context);
text_draw(&bar_item->label, bar->context);
bar_draw_alias(bar, bar_item, bar_item->sandwich_position);
bar_draw_graph(bar, bar_item, bar_item->sandwich_position, bar_item->graph.rtl);
if (bar_item->has_alias)
alias_draw(&bar_item->alias, bar->context);
if (bar_item->has_graph)
graph_draw(&bar_item->graph, bar->context);
}
CGContextFlush(bar->context);
@ -159,91 +87,102 @@ void bar_redraw(struct bar* bar) {
int bar_right_first_item_x = bar->frame.size.width - g_bar_manager.background.padding_right;
int bar_center_first_item_x = (bar->frame.size.width - bar_manager_get_center_length_for_bar(&g_bar_manager, bar)) / 2;
uint32_t y = bar->frame.size.height / 2;
for (int i = 0; i < g_bar_manager.bar_item_count; i++) {
struct bar_item* bar_item = g_bar_manager.bar_items[i];
if (!bar_draws_item(bar, bar_item)) continue;
struct text_line* label = &bar_item->label.line;
struct text_line* icon = &bar_item->icon.line;
CGPoint icon_position = bar_align_line(bar, icon, ALIGN_CENTER, ALIGN_CENTER);
icon_position.y += bar_item->y_offset;
CGPoint label_position = bar_align_line(bar, label, ALIGN_CENTER, ALIGN_CENTER);
label_position.y += bar_item->y_offset;
uint32_t icon_position = 0;
uint32_t label_position = 0;
uint32_t sandwich_position = 0;
bool rtl = false;
if (bar_item->position == POSITION_LEFT) {
icon_position.x = bar_left_final_item_x + bar_item->background.padding_left + 1;
label_position.x = icon_position.x + text_get_length(&bar_item->icon);
icon_position = bar_left_final_item_x + bar_item->background.padding_left + 1;
label_position = icon_position + text_get_length(&bar_item->icon);
if (!bar_item->has_const_width)
bar_left_final_item_x = label_position.x + text_get_length(&bar_item->label) + bar_item->background.padding_right;
bar_left_final_item_x = label_position + text_get_length(&bar_item->label) + bar_item->background.padding_right;
else
bar_left_final_item_x += bar_item->custom_width;
sandwich_position = label_position.x;
sandwich_position = label_position;
if (bar_item->has_graph) {
if (!bar_item->has_const_width)
bar_left_final_item_x += graph_get_length(&bar_item->graph);
label_position.x += graph_get_length(&bar_item->graph);
label_position += graph_get_length(&bar_item->graph);
} else if (bar_item->has_alias) {
if (!bar_item->has_const_width)
bar_left_final_item_x += alias_get_length(&bar_item->alias);
label_position.x += alias_get_length(&bar_item->alias);
label_position += alias_get_length(&bar_item->alias);
}
}
else if (bar_item->position == POSITION_RIGHT) {
rtl = true;
label_position.x = bar_right_first_item_x - text_get_length(&bar_item->label) - bar_item->background.padding_right;
icon_position.x = label_position.x - text_get_length(&bar_item->icon) - 1;
label_position = bar_right_first_item_x - text_get_length(&bar_item->label) - bar_item->background.padding_right;
icon_position = label_position - text_get_length(&bar_item->icon) - 1;
if (!bar_item->has_const_width)
bar_right_first_item_x = icon_position.x - bar_item->background.padding_left;
bar_right_first_item_x = icon_position - bar_item->background.padding_left;
else
bar_right_first_item_x -= bar_item->custom_width;
if (bar_item->has_graph) {
if (!bar_item->has_const_width)
bar_right_first_item_x -= graph_get_length(&bar_item->graph);
sandwich_position = icon_position.x + text_get_length(&bar_item->icon);
icon_position.x -= graph_get_length(&bar_item->graph);
sandwich_position = icon_position + text_get_length(&bar_item->icon);
icon_position -= graph_get_length(&bar_item->graph);
} else if (bar_item->has_alias) {
if (!bar_item->has_const_width)
icon_position.x -= alias_get_length(&bar_item->alias);
icon_position -= alias_get_length(&bar_item->alias);
bar_right_first_item_x -= alias_get_length(&bar_item->alias);
sandwich_position = icon_position.x + text_get_length(&bar_item->icon);
sandwich_position = icon_position + text_get_length(&bar_item->icon);
}
}
else if (bar_item->position == POSITION_CENTER) {
icon_position.x = bar_center_first_item_x + bar_item->background.padding_left + 1;
label_position.x = icon_position.x + text_get_length(&bar_item->icon);
icon_position = bar_center_first_item_x + bar_item->background.padding_left + 1;
label_position = icon_position + text_get_length(&bar_item->icon);
if (!bar_item->has_const_width)
bar_center_first_item_x = label_position.x + text_get_length(&bar_item->label) + bar_item->background.padding_right;
bar_center_first_item_x = label_position + text_get_length(&bar_item->label) + bar_item->background.padding_right;
else
bar_center_first_item_x += bar_item->custom_width;
sandwich_position = label_position.x;
sandwich_position = label_position;
if (bar_item->has_graph) {
if (!bar_item->has_const_width)
bar_center_first_item_x += graph_get_length(&bar_item->graph);
label_position.x += graph_get_length(&bar_item->graph);
label_position += graph_get_length(&bar_item->graph);
} else if (bar_item->has_alias) {
if (!bar_item->has_const_width)
bar_center_first_item_x += alias_get_length(&bar_item->alias);
label_position.x += alias_get_length(&bar_item->alias);
label_position += alias_get_length(&bar_item->alias);
}
}
bar_item->icon.line.bounds.origin = icon_position;
bar_item->label.line.bounds.origin = label_position;
if (bar_item->group && group_is_first_member(bar_item->group, bar_item))
group_calculate_bounds(bar_item->group, icon_position, y);
bar_item->graph.rtl = rtl;
bar_item->sandwich_position = sandwich_position;
text_calculate_bounds(&bar_item->icon, icon_position, y + bar_item->y_offset);
text_calculate_bounds(&bar_item->label, label_position, y + bar_item->y_offset);
if (bar_item->has_alias)
alias_calculate_bounds(&bar_item->alias, sandwich_position, y + bar_item->y_offset);
if (bar_item->has_graph) {
bar_item->graph.bounds.size.height = bar_item->background.enabled ? (bar_item->background.bounds.size.height - bar_item->background.border_width - 1)
: (bar->frame.size.height - (g_bar_manager.background.border_width + 1));
bar_item->graph.rtl = rtl;
graph_calculate_bounds(&bar_item->graph, sandwich_position, y + bar_item->y_offset);
}
bar_item->background.bounds.size.height = bar_item->background.overrides_height ? bar_item->background.bounds.size.height
: (bar->frame.size.height - (g_bar_manager.background.border_width + 1));
bar_item->background.bounds.size.width = bar_item_get_length(bar_item);
background_calculate_bounds(&bar_item->background, icon_position, y + bar_item->y_offset);
}
bar_draw_bar_items(bar);
@ -258,13 +197,13 @@ void bar_create_frame(struct bar *bar, CFTypeRef *frame_region) {
if (g_bar_manager.position == POSITION_BOTTOM) {
origin.y = CGRectGetMaxY(bounds) - g_bar_manager.background.height - 2*g_bar_manager.y_offset;
origin.y = CGRectGetMaxY(bounds) - g_bar_manager.background.bounds.size.height - 2*g_bar_manager.y_offset;
} else if (display_menu_bar_visible() && !g_bar_manager.topmost) {
CGRect menu = display_menu_bar_rect(bar->did);
origin.y += menu.size.height;
}
bar->frame = (CGRect) {{0, 0},{bounds.size.width, g_bar_manager.background.height}};
bar->frame = (CGRect) {{0, 0},{bounds.size.width, g_bar_manager.background.bounds.size.height}};
bar->origin = origin;
CGSNewRegionWithRect(&bar->frame, frame_region);
}

View file

@ -294,8 +294,8 @@ void bar_item_remove_bounding_rect_for_display(struct bar_item* bar_item, uint32
CGRect bar_item_construct_bounding_rect(struct bar_item* bar_item) {
CGRect bounding_rect;
bounding_rect.origin = bar_item->icon.line.bounds.origin;
bounding_rect.origin.y = bar_item->icon.line.bounds.origin.y < bar_item->label.line.bounds.origin.y ? bar_item->icon.line.bounds.origin.y : bar_item->label.line.bounds.origin.y + bar_item->y_offset;
bounding_rect.origin = bar_item->icon.bounds.origin;
bounding_rect.origin.y = bar_item->icon.bounds.origin.y < bar_item->label.bounds.origin.y ? bar_item->icon.bounds.origin.y : bar_item->label.bounds.origin.y + bar_item->y_offset;
bounding_rect.size.width = bar_item_get_length(bar_item);
bounding_rect.size.height = bar_item_get_height(bar_item);
return bounding_rect;
@ -401,7 +401,7 @@ void bar_item_serialize(struct bar_item* bar_item, FILE* rsp) {
hex_from_rgba_color(bar_item->label.color),
hex_from_rgba_color(bar_item->label.highlight_color),
bar_item->background.enabled,
bar_item->background.height,
(uint32_t)bar_item->background.bounds.size.height,
bar_item->background.corner_radius,
bar_item->background.border_width,
hex_from_rgba_color(bar_item->background.color),

View file

@ -49,7 +49,6 @@ struct bar_item {
bool has_graph;
struct graph graph;
uint32_t sandwich_position;
// Alias Data
bool has_alias;
struct alias alias;

View file

@ -31,7 +31,7 @@ void bar_manager_init(struct bar_manager* bar_manager) {
bar_manager->picky_redraw = false;
background_init(&bar_manager->background);
bar_manager->background.height = 25;
bar_manager->background.bounds.size.height = 25;
bar_manager->background.padding_left = 20;
bar_manager->background.padding_right = 20;
bar_manager->background.border_color = rgba_color_from_hex(0xffff0000);
@ -428,7 +428,7 @@ void bar_manager_serialize(struct bar_manager* bar_manager, FILE* rsp) {
"\t},\n"
"\t\"items\": [\n",
bar_manager->position,
bar_manager->background.height,
(uint32_t)bar_manager->background.bounds.size.height,
bar_manager->margin,
bar_manager->y_offset,
bar_manager->background.corner_radius,

View file

@ -1,4 +1,5 @@
#include "graph.h"
#include <_types/_uint32_t.h>
void graph_init(struct graph* graph, uint32_t width) {
graph->width = width;
@ -37,6 +38,55 @@ uint32_t graph_get_length(struct graph* graph) {
return 0;
}
void graph_calculate_bounds(struct graph* graph, uint32_t x, uint32_t y) {
graph->bounds.origin.x = x;
graph->bounds.origin.y = y - graph->bounds.size.height / 2 + graph->line_width;
}
void graph_draw(struct graph* graph, CGContextRef context) {
uint32_t x = graph->bounds.origin.x;
uint32_t y = graph->bounds.origin.y;
uint32_t height = graph->bounds.size.height;
uint32_t sample_width = 1;
bool fill = graph->fill;
CGContextSaveGState(context);
CGContextSetRGBStrokeColor(context, graph->line_color.r, graph->line_color.g, graph->line_color.b, graph->line_color.a);
if (graph->overrides_fill_color) CGContextSetRGBFillColor(context, graph->fill_color.r, graph->fill_color.g, graph->fill_color.b, graph->fill_color.a);
else CGContextSetRGBFillColor(context, graph->line_color.r, graph->line_color.g, graph->line_color.b, 0.2 * graph->line_color.a);
CGContextSetLineWidth(context, graph->line_width);
CGMutablePathRef p = CGPathCreateMutable();
uint32_t start_x = x;
if (graph->rtl) {
CGPathMoveToPoint(p, NULL, x, y + graph_get_y(graph, graph->width - 1) * height);
for (int i = graph->width - 1; i > 0; --i, x -= sample_width) {
CGPathAddLineToPoint(p, NULL, x, y + graph_get_y(graph, i) * height);
}
}
else {
CGPathMoveToPoint(p, NULL, x, y + graph_get_y(graph, 0) * height);
for (int i = graph->width - 1; i > 0; --i, x += sample_width) {
CGPathAddLineToPoint(p, NULL, x, y + graph_get_y(graph, i) * height);
}
}
CGContextAddPath(context, p);
CGContextStrokePath(context);
if (fill) {
if (graph->rtl) {
CGPathAddLineToPoint(p, NULL, x + sample_width, y);
}
else {
CGPathAddLineToPoint(p, NULL, x - sample_width, y);
}
CGPathAddLineToPoint(p, NULL, start_x, y);
CGPathCloseSubpath(p);
CGContextAddPath(context, p);
CGContextFillPath(context);
}
CGPathRelease(p);
CGContextRestoreGState(context);
}
static bool graph_parse_sub_domain(struct graph* graph, FILE* rsp, struct token property, char* message) {
if (token_equals(property, PROPERTY_COLOR)) {
graph->line_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));

View file

@ -9,6 +9,7 @@ struct graph {
uint32_t cursor;
uint32_t width;
float* y;
CGRect bounds;
// Visual
bool fill;
@ -25,6 +26,9 @@ void graph_destruct(struct graph* graph);
float graph_get_y(struct graph* graph, uint32_t i);
uint32_t graph_get_length(struct graph* graph);
void graph_calculate_bounds(struct graph* graph, uint32_t x, uint32_t y);
void graph_draw(struct graph* graph, CGContextRef context);
static bool graph_parse_sub_domain(struct graph* graph, FILE* rsp, struct token property, char* message);
#endif

View file

@ -69,3 +69,14 @@ void group_destroy(struct group* group) {
if (group->members) free(group->members);
free(group);
}
void group_calculate_bounds(struct group* group, uint32_t x, uint32_t y) {
group->members[0]->background.bounds.size.width = group_get_length(group);
group->members[0]->background.bounds.origin.x = x;
group->members[0]->background.bounds.origin.y = y - group->members[0]->background.bounds.size.height / 2;
}
void group_draw(struct group* group, CGContextRef context) {
background_draw(&group->members[0]->background, context);
}

View file

@ -4,6 +4,7 @@
#include <_types/_uint32_t.h>
struct group {
CGRect bounds;
uint32_t num_members;
struct bar_item** members;
};
@ -17,4 +18,7 @@ uint32_t group_get_length(struct group* group);
bool group_is_first_member(struct group* group, struct bar_item* item);
void group_destroy(struct group* group);
void group_calculate_bounds(struct group* group, uint32_t x, uint32_t y);
void group_draw(struct group* group, CGContextRef context);
#endif

View file

@ -43,20 +43,20 @@ void text_init(struct text* text) {
text_set_string(text, text->string, false);
}
void text_prepare_line(struct text_line* text_line, CTFontRef font, char* cstring, struct rgba_color color) {
void text_prepare_line(struct text* text) {
const void *keys[] = { kCTFontAttributeName, kCTForegroundColorFromContextAttributeName };
const void *values[] = { font, kCFBooleanTrue };
const void *values[] = { text->font, kCFBooleanTrue };
CFDictionaryRef attributes = CFDictionaryCreate(NULL, keys, values, array_count(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFStringRef string = CFStringCreateWithCString(NULL, cstring, kCFStringEncodingUTF8);
CFStringRef string = CFStringCreateWithCString(NULL, text->string, kCFStringEncodingUTF8);
if (!string) string = CFStringCreateWithCString(NULL, "Warning: Malformed UTF-8 string", kCFStringEncodingUTF8);
CFAttributedStringRef attr_string = CFAttributedStringCreate(NULL, string, attributes);
text_line->line = CTLineCreateWithAttributedString(attr_string);
text->line.line = CTLineCreateWithAttributedString(attr_string);
CTLineGetTypographicBounds(text_line->line, &text_line->ascent, &text_line->descent, NULL);
text_line->bounds = CTLineGetBoundsWithOptions(text_line->line, kCTLineBoundsUseGlyphPathBounds);
text_line->bounds.size.width = (uint32_t) (text_line->bounds.size.width + 0.5);
text_line->bounds.origin.x = (uint32_t) (text_line->bounds.origin.x + 0.5);
text_line->color = color;
CTLineGetTypographicBounds(text->line.line, &text->line.ascent, &text->line.descent, NULL);
text->bounds = CTLineGetBoundsWithOptions(text->line.line, kCTLineBoundsUseGlyphPathBounds);
text->bounds.size.width = (uint32_t) (text->bounds.size.width + 0.5);
text->bounds.origin.x = (uint32_t) (text->bounds.origin.x + 0.5);
text->line.color = text->highlight ? text->highlight_color : text->color;
CFRelease(string);
CFRelease(attributes);
@ -72,7 +72,7 @@ bool text_set_string(struct text* text, char* string, bool forced) {
if (text->line.line) text_destroy_line(text);
if (string != text->string && !text->string) free(text->string);
text->string = string;
text_prepare_line(&text->line, text->font, text->string, text->highlight ? text->highlight_color : text->color);
text_prepare_line(text);
return true;
}
@ -111,11 +111,11 @@ void text_clear_pointers(struct text* text) {
uint32_t text_get_length(struct text* text) {
if (!text->drawing) return 0;
if (text->has_const_width) return text->custom_width;
return (text->line.bounds.size.width + text->padding_left + text->padding_right) > 0 ? (text->line.bounds.size.width + text->padding_left + text->padding_right) : 0;
return (text->bounds.size.width + text->padding_left + text->padding_right) > 0 ? (text->bounds.size.width + text->padding_left + text->padding_right) : 0;
}
uint32_t text_get_height(struct text* text) {
return text->line.bounds.size.height;
return text->bounds.size.height;
}
void text_destroy_line(struct text* text) {
@ -131,10 +131,15 @@ void text_destroy(struct text* text) {
text_clear_pointers(text);
}
void text_draw(struct text* text, CGPoint origin, CGContextRef context) {
void text_calculate_bounds(struct text* text, uint32_t x, uint32_t y) {
text->bounds.origin.x = x;
text->bounds.origin.y = y - ((text->line.ascent - text->line.descent) / 2);
}
void text_draw(struct text* text, CGContextRef context) {
if (!text->drawing) return;
CGContextSetRGBFillColor(context, text->line.color.r, text->line.color.g, text->line.color.b, text->line.color.a);
CGContextSetTextPosition(context, origin.x + text->padding_left, origin.y + text->y_offset);
CGContextSetTextPosition(context, text->bounds.origin.x + text->padding_left, text->bounds.origin.y + text->y_offset);
CTLineDraw(text->line.line, context);
}

View file

@ -1,15 +1,17 @@
#ifndef TEXT_H
#define TEXT_H
#include <_types/_uint32_t.h>
#include <stdint.h>
struct text_line {
CTLineRef line;
CGFloat ascent;
CGFloat descent;
CGRect bounds;
struct rgba_color color;
};
struct text {
CGRect bounds;
int y_offset;
bool highlight;
bool drawing;
@ -38,7 +40,8 @@ uint32_t text_get_length(struct text* text);
uint32_t text_get_height(struct text* text);
bool text_update_color(struct text* text);
void text_draw(struct text* text, CGPoint origin, CGContextRef context);
void text_calculate_bounds(struct text* text, uint32_t x, uint32_t y);
void text_draw(struct text* text, CGContextRef context);
static bool text_parse_sub_domain(struct text* text, FILE* rsp, struct token property, char* message);