fix group padding (#351)

This commit is contained in:
Felix Kratz 2023-04-06 07:44:41 +02:00
parent 44cb73e94a
commit e442ac8515
3 changed files with 102 additions and 39 deletions

View file

@ -253,16 +253,33 @@ static void bar_calculate_bounds_top_bottom(struct bar* bar) {
int group_padding_left = 0; int group_padding_left = 0;
int group_padding_right = 0; int group_padding_right = 0;
struct bar_item* first_group_member = NULL;
struct bar_item* last_group_member = NULL;
if (bar_item->group && (bar_item->group->first_item == bar_item)) if (bar_item->group) {
group_padding_left = bar_item->group->members[0]->background.padding_left; first_group_member
= group_get_first_drawing_member_for_bar(bar_item->group,
bar,
g_bar_manager.bar_items,
g_bar_manager.bar_item_count);
if (bar_item->group && (bar_item->group->last_item == bar_item)) last_group_member
group_padding_right = bar_item->group->members[0]->background.padding_right; = group_get_last_drawing_member_for_bar(bar_item->group,
bar,
g_bar_manager.bar_items,
g_bar_manager.bar_item_count);
}
if (bar_item->position == POSITION_RIGHT if (bar_item->position == POSITION_RIGHT
|| bar_item->position == POSITION_CENTER_LEFT) { || bar_item->position == POSITION_CENTER_LEFT) {
if (first_group_member == bar_item)
group_padding_right = bar_item->group->members[0]->background.padding_right;
if (last_group_member == bar_item)
group_padding_left = bar_item->group->members[0]->background.padding_left;
*next_position = min(*next_position - bar_item_display_length *next_position = min(*next_position - bar_item_display_length
- bar_item->background.padding_right - bar_item->background.padding_right
- group_padding_right, - group_padding_right,
@ -270,6 +287,12 @@ static void bar_calculate_bounds_top_bottom(struct bar* bar) {
- bar_item_display_length ); - bar_item_display_length );
} }
else { else {
if (first_group_member == bar_item)
group_padding_left = bar_item->group->members[0]->background.padding_left;
if (last_group_member == bar_item)
group_padding_right = bar_item->group->members[0]->background.padding_right;
*next_position += max((int)-*next_position, *next_position += max((int)-*next_position,
bar_item->background.padding_left bar_item->background.padding_left
+ group_padding_left ); + group_padding_left );
@ -302,7 +325,6 @@ static void bar_calculate_bounds_top_bottom(struct bar* bar) {
*next_position += bar_item->has_const_width *next_position += bar_item->has_const_width
? bar_item_display_length ? bar_item_display_length
+ bar_item->background.padding_right + bar_item->background.padding_right
+ group_padding_right
- bar_item->custom_width - bar_item->custom_width
: (- bar_item->background.padding_left : (- bar_item->background.padding_left
- group_padding_left ); - group_padding_left );
@ -310,7 +332,6 @@ static void bar_calculate_bounds_top_bottom(struct bar* bar) {
*next_position += bar_item->has_const_width *next_position += bar_item->has_const_width
? bar_item->custom_width ? bar_item->custom_width
- bar_item->background.padding_left - bar_item->background.padding_left
- group_padding_left
: (bar_item_length : (bar_item_length
+ bar_item->background.padding_right + bar_item->background.padding_right
+ group_padding_right ); + group_padding_right );

View file

@ -1,39 +1,6 @@
#include "group.h" #include "group.h"
#include "bar.h" #include "bar.h"
struct group* group_create() {
struct group* group = malloc(sizeof(struct group));
memset(group, 0, sizeof(struct group));
return group;
}
void group_init(struct group* group) {
group->num_members = 0;
group->members = NULL;
}
bool group_is_item_member(struct group* group, struct bar_item* item) {
for (uint32_t i = 0; i < group->num_members; i++) {
if (group->members[i] == item) return true;
}
return false;
}
void group_add_member(struct group* group, struct bar_item* item) {
if (group_is_item_member(group, item)) return;
if (item->group && item->group->members && item->group->members[0] == item) {
for (int i = 1; i < item->group->num_members; i++) {
group_add_member(group, item->group->members[i]);
}
} else {
group->num_members++;
group->members = realloc(group->members,
sizeof(struct bar_item*)*group->num_members);
group->members[group->num_members - 1] = item;
item->group = group;
}
}
static struct bar_item* group_get_first_member(struct group* group, struct bar* bar) { static struct bar_item* group_get_first_member(struct group* group, struct bar* bar) {
if (group->num_members == 1) return NULL; if (group->num_members == 1) return NULL;
@ -74,6 +41,79 @@ static struct bar_item* group_get_last_member(struct group* group, struct bar* b
return last_item; return last_item;
} }
struct bar_item* group_get_first_drawing_member_for_bar(struct group* group, struct bar* bar, struct bar_item** bar_items, uint32_t length) {
if (group->num_members == 1) return NULL;
struct bar_item* first_item = NULL;
uint32_t min_index = length - 1;
for (int i = 1; i < group->num_members; i++) {
struct bar_item* member = group->members[i];
if (bar_draws_item(bar, member)) {
for (int i = 0; i < length; i++) {
if (i < min_index && bar_items[i] == member) {
min_index = i;
first_item = bar_items[i];
}
}
}
}
return first_item;
}
struct bar_item* group_get_last_drawing_member_for_bar(struct group* group, struct bar* bar, struct bar_item** bar_items, uint32_t length) {
if (group->num_members == 1) return NULL;
struct bar_item* last_item = NULL;
uint32_t max_index = 0;
for (int i = 1; i < group->num_members; i++) {
struct bar_item* member = group->members[i];
if (bar_draws_item(bar, member)) {
for (int i = 0; i < length; i++) {
if (i > max_index && bar_items[i] == member) {
max_index = i;
last_item = bar_items[i];
}
}
}
}
return last_item;
}
struct group* group_create() {
struct group* group = malloc(sizeof(struct group));
memset(group, 0, sizeof(struct group));
return group;
}
void group_init(struct group* group) {
group->num_members = 0;
group->members = NULL;
}
bool group_is_item_member(struct group* group, struct bar_item* item) {
for (uint32_t i = 0; i < group->num_members; i++) {
if (group->members[i] == item) return true;
}
return false;
}
void group_add_member(struct group* group, struct bar_item* item) {
if (group_is_item_member(group, item)) return;
if (item->group && item->group->members && item->group->members[0] == item) {
for (int i = 1; i < item->group->num_members; i++) {
group_add_member(group, item->group->members[i]);
}
} else {
group->num_members++;
group->members = realloc(group->members,
sizeof(struct bar_item*)*group->num_members);
group->members[group->num_members - 1] = item;
item->group = group;
}
}
uint32_t group_get_length(struct group* group, struct bar* bar) { uint32_t group_get_length(struct group* group, struct bar* bar) {
int len = group->last_window->origin.x int len = group->last_window->origin.x
+ group->last_window->frame.size.width + group->last_window->frame.size.width

View file

@ -23,6 +23,8 @@ void group_add_member(struct group* group, struct bar_item* item);
void group_remove_member(struct group* group, struct bar_item* bar_item); void group_remove_member(struct group* group, struct bar_item* bar_item);
uint32_t group_get_length(struct group* group, struct bar* bar); uint32_t group_get_length(struct group* group, struct bar* bar);
struct bar_item* group_get_first_drawing_member_for_bar(struct group* group, struct bar* bar, struct bar_item** bar_items, uint32_t length);
struct bar_item* group_get_last_drawing_member_for_bar(struct group* group, struct bar* bar, struct bar_item** bar_items, uint32_t length);
void group_calculate_bounds(struct group* group, struct bar* bar, uint32_t y); void group_calculate_bounds(struct group* group, struct bar* bar, uint32_t y);
void group_draw(struct group* group, CGContextRef context); void group_draw(struct group* group, CGContextRef context);
void group_destroy(struct group* group); void group_destroy(struct group* group);