new centering logic

This commit is contained in:
FelixKratz 2021-10-02 14:58:03 +02:00
parent d4948c429f
commit 82011a0ee5
15 changed files with 147 additions and 108 deletions

View file

@ -59,6 +59,11 @@ void alias_init(struct alias* alias, char* owner, char* name) {
alias_update_image(alias);
}
uint32_t alias_get_length(struct alias* alias) {
if (alias->image_ref) return alias->bounds.size.width;
return 0;
}
void alias_find_window(struct alias* alias) {
CFArrayRef window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
int window_count = CFArrayGetCount(window_list);

View file

@ -1,6 +1,7 @@
#ifndef ALIAS_H
#define ALIAS_H
#include <stdint.h>
#define MENUBAR_LAYER 0x19
struct alias {
@ -18,5 +19,6 @@ void print_all_menu_items(FILE* rsp);
void alias_init(struct alias* alias, char* owner, char* name);
bool alias_update_image(struct alias* alias);
void alias_find_window(struct alias* alias);
uint32_t alias_get_length(struct alias* alias);
#endif

View file

@ -40,27 +40,27 @@ static void bar_draw_line(struct bar *bar, struct text_line* line, float x, floa
CTLineDraw(line->line, bar->context);
}
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* graph, uint32_t x, uint32_t y, bool right_to_left) {
const float height = bar->frame.size.height * 0.9f;
uint32_t sample_width = 1;
bool fill = graph_data->fill;
bool fill = graph->fill;
CGContextSaveGState(bar->context);
CGContextSetRGBStrokeColor(bar->context, graph_data->line_color.r, graph_data->line_color.g, graph_data->line_color.b, graph_data->line_color.a);
if (graph_data->overrides_fill_color) CGContextSetRGBFillColor(bar->context, graph_data->fill_color.r, graph_data->fill_color.g, graph_data->fill_color.b, graph_data->fill_color.a);
else CGContextSetRGBFillColor(bar->context, graph_data->line_color.r, graph_data->line_color.g, graph_data->line_color.b, 0.2 * graph_data->line_color.a);
CGContextSetLineWidth(bar->context, graph_data->line_width);
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_data_get_y(graph_data, graph_data->graph_width - 1) * height);
for (int i = graph_data->graph_width - 1; i > 0; --i, x -= sample_width) {
CGPathAddLineToPoint(p, NULL, x, y + graph_data_get_y(graph_data, i) * height);
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_data_get_y(graph_data, 0) * height);
for (int i = graph_data->graph_width - 1; i > 0; --i, x += sample_width) {
CGPathAddLineToPoint(p, NULL, x, y + graph_data_get_y(graph_data, i) * height);
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);
@ -82,23 +82,9 @@ void bar_draw_graph_line(struct bar *bar, struct graph_data* graph_data, uint32_
CGContextRestoreGState(bar->context);
}
static int bar_get_center_length(struct bar_manager* bar_manager) {
int total_length = 0;
for (int i = 0; i < bar_manager->bar_item_count; i++) {
struct bar_item* bar_item = bar_manager->bar_items[i];
if (bar_item->position == BAR_POSITION_CENTER) {
total_length += bar_item->label.line.bounds.size.width + bar_item->icon.line.bounds.size.width + bar_item->icon.padding_right + bar_item->label.padding_left + (bar_item->has_graph ? bar_item->graph_data.graph_width : 0);
if (i > 0) {
total_length += bar_manager->bar_items[i-1]->label.padding_right + bar_item->icon.padding_left;
}
}
}
return total_length;
}
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_data, x, g_bar_manager.background.border_width + 1, right_to_left);
bar_draw_graph_line(bar, &bar_item->graph, x, 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) {
@ -124,7 +110,7 @@ void bar_redraw(struct bar* bar) {
int bar_left_final_item_x = g_bar_manager.background.padding_left;
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_get_center_length(&g_bar_manager)) / 2;
int bar_center_first_item_x = (bar->frame.size.width - bar_manager_get_center_length_for_bar(&g_bar_manager, bar)) / 2;
SLSDisableUpdate(g_connection);
SLSOrderWindow(g_connection, bar->id, -1, 0);
@ -153,7 +139,7 @@ void bar_redraw(struct bar* bar) {
if (bar_item->has_graph) {
graph_x = bar_item->nospace ? label_position.x + label->bounds.size.width + bar_item->label.padding_right + bar_item->background.padding_right : bar_left_final_item_x;
if (!bar_item->nospace)
bar_left_final_item_x += bar_item->graph_data.graph_width;
bar_left_final_item_x += bar_item->graph.width;
}
if (bar_item->has_alias) {
bar_left_final_item_x += bar_item->alias.bounds.size.width;
@ -169,7 +155,7 @@ void bar_redraw(struct bar* bar) {
graph_x = bar_item->nospace ? icon_position.x - bar_item->icon.padding_left - bar_item->background.padding_left : bar_right_first_item_x;
graph_rtl = true;
if (!bar_item->nospace)
bar_right_first_item_x -= bar_item->graph_data.graph_width;
bar_right_first_item_x -= bar_item->graph.width;
}
if (bar_item->has_alias) {
icon_position.x -= bar_item->alias.bounds.size.width;
@ -185,7 +171,7 @@ void bar_redraw(struct bar* bar) {
if (bar_item->has_graph) {
graph_x = bar_item->nospace ? label_position.x + label->bounds.size.width + bar_item->label.padding_right + bar_item->background.padding_right : bar_center_first_item_x;
if (!bar_item->nospace)
bar_center_first_item_x += bar_item->graph_data.graph_width;
bar_center_first_item_x += bar_item->graph.width;
}
if (bar_item->has_alias) {
bar_center_first_item_x += bar_item->alias.bounds.size.width;

View file

@ -1,6 +1,6 @@
#include "bar_item.h"
#include "alias.h"
#include "graph_data.h"
#include "graph.h"
#include "misc/helpers.h"
#include <stdint.h>
#include <string.h>
@ -202,15 +202,26 @@ void bar_item_set_yoffset(struct bar_item* bar_item, int offset) {
bar_item_needs_update(bar_item);
}
uint32_t bar_item_get_length(struct bar_item* bar_item) {
return text_get_length(&bar_item->icon)
+ text_get_length(&bar_item->label)
+ (bar_item->has_graph ? graph_get_length(&bar_item->graph) : 0)
+ (bar_item->has_alias ? alias_get_length(&bar_item->alias) : 0);
}
uint32_t bar_item_get_height(struct bar_item* bar_item) {
uint32_t label_height = text_get_height(&bar_item->label);
uint32_t icon_height = text_get_height(&bar_item->icon);
return label_height > icon_height ? label_height : icon_height;
}
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.x -= bar_item->icon.padding_left;
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;
bounding_rect.size.width = bar_item->label.line.bounds.size.width + bar_item->icon.line.bounds.size.width
+ bar_item->icon.padding_left + bar_item->icon.padding_right
+ bar_item->label.padding_right + bar_item->label.padding_left;
bounding_rect.size.height = bar_item->label.line.bounds.size.height > bar_item->icon.line.bounds.size.height ? bar_item->label.line.bounds.size.height : bar_item->icon.line.bounds.size.height;
bounding_rect.size.width = bar_item_get_length(bar_item);
bounding_rect.size.height = bar_item_get_height(bar_item);
return bounding_rect;
}
@ -245,7 +256,7 @@ void bar_item_destroy(struct bar_item* bar_item) {
free(bar_item->bounding_rects);
}
if (bar_item->has_graph) {
graph_data_destroy(&bar_item->graph_data);
graph_destroy(&bar_item->graph);
}
free(bar_item);
}

View file

@ -59,7 +59,7 @@ struct bar_item {
// Graph Data
bool has_graph;
struct graph_data graph_data;
struct graph graph;
// Alias Data
bool has_alias;
@ -93,6 +93,10 @@ void bar_item_set_script(struct bar_item* bar_item, char* script);
void bar_item_set_click_script(struct bar_item* bar_item, char* script);
void bar_item_set_drawing(struct bar_item* bar_item, bool state);
void bar_item_set_yoffset(struct bar_item* bar_item, int offset);
uint32_t bar_item_get_length(struct bar_item* bar_item);
uint32_t bar_item_get_height(struct bar_item* bar_item);
void bar_item_needs_update(struct bar_item* bar_item);
void bar_item_clear_needs_update(struct bar_item* bar_item);

View file

@ -64,12 +64,25 @@ void bar_manager_set_font_smoothing(struct bar_manager* bar_manager, bool smooth
bar_set_font_smoothing(bar_manager->bars[i], smoothing);
}
uint32_t bar_manager_get_center_length_for_bar(struct bar_manager* bar_manager, struct bar* bar) {
uint32_t total_length = 0;
for (int i = 0; i < bar_manager->bar_item_count; i++) {
struct bar_item* bar_item = bar_manager->bar_items[i];
bool is_associated_space_shown = (bar_item->associated_space & (1 << bar->sid)) || bar_item->associated_space == 0;
bool is_associated_display_shown = (bar_item->associated_display & (1 << bar->adid));
if (bar_item->position == BAR_POSITION_CENTER && bar_item->drawing && (is_associated_space_shown || is_associated_display_shown))
total_length += bar_item_get_length(bar_item);
}
return total_length;
}
bool bar_manager_bar_needs_redraw(struct bar_manager* bar_manager, struct bar* bar) {
for (int i = 0; i < bar_manager->bar_item_count; i++) {
struct bar_item* bar_item = bar_manager->bar_items[i];
bool is_associated_space_shown = (bar_item->associated_space & (1 << bar->sid)) || bar_item->associated_space == 0;
bool is_associated_display_shown = (bar_item->associated_display & (1 << bar->adid));
if (bar_item->needs_update && (is_associated_space_shown || is_associated_display_shown) && !bar_item->lazy) {
if (bar_item->drawing && bar_item->needs_update && (is_associated_space_shown || is_associated_display_shown) && !bar_item->lazy) {
return true;
}
}
@ -355,4 +368,3 @@ void bar_manager_serialize(struct bar_manager* bar_manager, FILE* rsp) {
else fprintf(rsp, "\n\t]\n}\n");
}
}

View file

@ -53,6 +53,7 @@ void bar_manager_set_topmost(struct bar_manager *bar_manager, bool topmost);
void bar_manager_set_font_smoothing(struct bar_manager* bar_manager, bool smoothing);
struct bar_item* bar_manager_get_item_by_point(struct bar_manager* bar_manager, CGPoint point, uint32_t adid);
uint32_t bar_manager_get_center_length_for_bar(struct bar_manager* bar_manager, struct bar* bar);
void bar_manager_freeze(struct bar_manager *bar_manager);
void bar_manager_unfreeze(struct bar_manager *bar_manager);

37
src/graph.c Normal file
View file

@ -0,0 +1,37 @@
#include "graph.h"
void graph_init(struct graph* graph, uint32_t width) {
graph->width = width;
graph->y = malloc(sizeof(float) * width);
memset(graph->y, 0, sizeof(float) * width);
graph->cursor = 0;
graph->line_color = rgba_color_from_hex(0xcccccc);
graph->fill_color = rgba_color_from_hex(0xcccccc);
graph->line_width = 0.5;
graph->fill = true;
graph->overrides_fill_color = false;
graph->enabled = true;
}
void graph_destroy(struct graph* graph) {
if (!graph->enabled) return;
free(graph->y);
}
float graph_get_y(struct graph* graph, uint32_t i) {
if (!graph->enabled) return 0.f;
return graph->y[ (graph->cursor + i)%graph->width ];
}
void graph_push_back(struct graph* graph, float y) {
if (!graph->enabled) return;
graph->y[graph->cursor] = y;
++graph->cursor;
graph->cursor %= graph->width;
}
uint32_t graph_get_length(struct graph* graph) {
if (graph->enabled) return graph->width;
return 0;
}

30
src/graph.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef GRAPH_H
#define GRAPH_H
#include <_types/_uint32_t.h>
struct graph {
// Functional
bool enabled;
uint32_t cursor;
uint32_t width;
float* y;
// Visual
bool fill;
bool overrides_fill_color;
struct rgba_color line_color;
struct rgba_color fill_color;
float line_width;
};
void graph_init(struct graph* graph, uint32_t width);
void graph_destroy(struct graph* graph);
void graph_push_back(struct graph* graph, float y);
void graph_destruct(struct graph* graph);
float graph_get_y(struct graph* graph, uint32_t i);
uint32_t graph_get_length(struct graph* graph);
#endif

View file

@ -1,32 +0,0 @@
#include "graph_data.h"
void graph_data_init(struct graph_data* graph_data, uint32_t graph_width) {
graph_data->graph_width = graph_width;
graph_data->y = malloc(sizeof(float) * graph_width);
memset(graph_data->y, 0, sizeof(float) * graph_width);
graph_data->cursor = 0;
graph_data->line_color = rgba_color_from_hex(0xcccccc);
graph_data->fill_color = rgba_color_from_hex(0xcccccc);
graph_data->line_width = 0.5;
graph_data->fill = true;
graph_data->overrides_fill_color = false;
graph_data->ready = true;
}
void graph_data_destroy(struct graph_data* graph_data) {
if (!graph_data->ready) return;
free(graph_data->y);
}
float graph_data_get_y(struct graph_data* graph_data, uint32_t i) {
if (!graph_data->ready) return 0.f;
return graph_data->y[ (graph_data->cursor + i)%graph_data->graph_width ];
}
void graph_data_push_back(struct graph_data* graph_data, float y) {
if (!graph_data->ready) return;
graph_data->y[graph_data->cursor] = y;
++graph_data->cursor;
graph_data->cursor %= graph_data->graph_width;
}

View file

@ -1,28 +0,0 @@
#ifndef GRAPH_DATA_H
#define GRAPH_DATA_H
struct graph_data {
// Functional
bool ready;
uint32_t cursor;
uint32_t graph_width;
float* y;
// Visual
bool fill;
bool overrides_fill_color;
struct rgba_color line_color;
struct rgba_color fill_color;
float line_width;
};
void graph_data_init(struct graph_data* graph_data, uint32_t graph_width);
void graph_data_destroy(struct graph_data* graph_data);
void graph_data_push_back(struct graph_data* graph_data, float y);
void graph_data_destruct(struct graph_data* graph_data);
float graph_data_get_y(struct graph_data* graph_data, uint32_t i);
#endif

View file

@ -27,7 +27,7 @@
#include "background.h"
#include "bar.h"
#include "text.h"
#include "graph_data.h"
#include "graph.h"
#include "alias.h"
#include "bar_item.h"
#include "custom_events.h"
@ -42,7 +42,7 @@
#include "background.c"
#include "bar.c"
#include "text.c"
#include "graph_data.c"
#include "graph.c"
#include "alias.c"
#include "bar_item.c"
#include "custom_events.c"

View file

@ -280,7 +280,7 @@ static void handle_domain_push(FILE* rsp, struct token domain, char* message) {
int item_index_for_name = bar_manager_get_item_index_for_name(&g_bar_manager, name.text);
if (item_index_for_name < 0) return;
struct bar_item* bar_item = g_bar_manager.bar_items[item_index_for_name];
graph_data_push_back(&bar_item->graph_data, token_to_float(y));
graph_push_back(&bar_item->graph, token_to_float(y));
bar_item_needs_update(bar_item);
if (bar_item_is_shown(bar_item)) bar_manager_refresh(&g_bar_manager, false);
}
@ -325,7 +325,7 @@ static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
bar_item_set_type(bar_item, identifier.text[0]);
if (bar_item->type == BAR_COMPONENT_GRAPH) {
struct token width = get_token(&message);
graph_data_init(&bar_item->graph_data, token_to_uint32t(width));
graph_init(&bar_item->graph, token_to_uint32t(width));
bar_item->has_graph = true;
}
else if (bar_item->type == BAR_COMPONENT_SPACE) {
@ -416,14 +416,14 @@ static void message_parse_set_message_for_bar_item(FILE* rsp, struct bar_item* b
} else if (token_equals(property, COMMAND_SET_UPDATE_FREQ)) {
bar_item->update_frequency = token_to_uint32t(get_token(&message));
} else if (token_equals(property, COMMAND_SET_GRAPH_COLOR)) {
bar_item->graph_data.line_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
bar_item->graph.line_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
bar_item_needs_update(bar_item);
} else if (token_equals(property, COMMAND_SET_GRAPH_FILL_COLOR)) {
bar_item->graph_data.fill_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
bar_item->graph_data.overrides_fill_color = true;
bar_item->graph.fill_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
bar_item->graph.overrides_fill_color = true;
needs_update = true;
} else if (token_equals(property, COMMAND_SET_GRAPH_LINE_WIDTH)) {
bar_item->graph_data.line_width = token_to_float(get_token(&message));
bar_item->graph.line_width = token_to_float(get_token(&message));
needs_update = true;
} else if (token_equals(property, COMMAND_SET_BACKGROUND_COLOR)) {
needs_update = background_set_color(&bar_item->background, token_to_uint32t(get_token(&message)));

View file

@ -1,5 +1,6 @@
#include "text.h"
#include "misc/helpers.h"
#include <stdint.h>
static CTFontRef text_create_font(char *cstring) {
float size = 10.0f;
@ -101,6 +102,14 @@ void text_clear_pointers(struct text* text) {
text->line.line = NULL;
}
uint32_t text_get_length(struct text* text) {
return text->line.bounds.size.width + text->padding_left + text->padding_right;
}
uint32_t text_get_height(struct text* text) {
return text->line.bounds.size.height;
}
void text_destroy_line(struct text* text) {
if (text->line.line) CFRelease(text->line.line);
text->line.line = NULL;

View file

@ -28,6 +28,8 @@ void text_destroy(struct text* text);
bool text_set_string(struct text* text, char* string, bool forced);
bool text_set_color(struct text* text, uint32_t color);
bool text_set_font(struct text* text, char* font_string, bool forced);
uint32_t text_get_length(struct text* text);
uint32_t text_get_height(struct text* text);
bool text_update_color(struct text* text);
#endif // !TEXT_H_