Skip to content

Commit

Permalink
fix popup menus for multi monitors (closes #132)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Dec 20, 2021
1 parent 4c218db commit 3f6cc9d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
10 changes: 4 additions & 6 deletions src/bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "graph.h"
#include "group.h"
#include "misc/helpers.h"
#include "popup.h"
#include "text.h"
#include <_types/_uint32_t.h>
#include <stdint.h>
Expand All @@ -26,7 +27,7 @@ bool bar_draws_item(struct bar* bar, struct bar_item* bar_item) {
}

void bar_calculate_popup_anchor_for_bar_item(struct bar* bar, struct bar_item* bar_item) {
if (bar->adid != display_arrangement(display_active_display_id())) return;
if (bar->adid != g_bar_manager.active_adid) return;
bar_item->popup.cell_size = bar->frame.size.height;
popup_calculate_bounds(&bar_item->popup);
CGPoint anchor = bar->origin;
Expand All @@ -38,12 +39,7 @@ void bar_calculate_popup_anchor_for_bar_item(struct bar* bar, struct bar_item* b
anchor.x += bar_item->icon.bounds.origin.x + bar_item_get_length(bar_item, false) - bar_item->popup.background.bounds.size.width;
}
anchor.y += (g_bar_manager.position == POSITION_BOTTOM ? (-bar->frame.size.height - bar_item->popup.background.bounds.size.height) : bar->frame.size.height);
if (anchor.x + bar_item->popup.background.bounds.size.width > bar->frame.size.width) {
anchor.x = bar->frame.size.width - bar_item->popup.background.bounds.size.width;
popup_calculate_bounds(&bar_item->popup);
}
popup_set_anchor(&bar_item->popup, anchor, bar->adid);
popup_calculate_bounds(&bar_item->popup);
}

void bar_draw_bar_items(struct bar* bar) {
Expand All @@ -66,6 +62,8 @@ void bar_draw_bar_items(struct bar* bar) {

bar_item_set_bounding_rect_for_display(bar_item, bar->adid, bar->origin, bar->frame.size.height);
bar_item_draw(bar_item, bar->context);
if (bar_item->popup.drawing && bar->adid == g_bar_manager.active_adid)
popup_draw(&bar_item->popup);
}

CGContextFlush(bar->context);
Expand Down
1 change: 1 addition & 0 deletions src/bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern CGError SLSAddActivationRegion(uint32_t cid, uint32_t wid, CFTypeRef regi
extern CGError SLSAddTrackingRect(uint32_t cid, uint32_t wid, CGRect rect);
extern CGError SLSClearActivationRegion(uint32_t cid, uint32_t wid);
extern CGError SLSRemoveAllTrackingAreas(uint32_t cid, uint32_t wid);
extern CGError SLSMoveWindow(int cid, uint32_t wid, CGPoint *point);
extern CGError SLSWindowSetShadowProperties(uint32_t wid, CFDictionaryRef properties);

#define kCGSDisableShadowTagBit (1ULL << 3)
Expand Down
2 changes: 0 additions & 2 deletions src/bar_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,6 @@ void bar_item_draw(struct bar_item* bar_item, CGContextRef context) {
alias_draw(&bar_item->alias, context);
if (bar_item->has_graph)
graph_draw(&bar_item->graph, context);
if (bar_item->popup.drawing)
popup_draw(&bar_item->popup);
}

void bar_item_destroy(struct bar_item* bar_item) {
Expand Down
4 changes: 4 additions & 0 deletions src/bar_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void bar_manager_init(struct bar_manager* bar_manager) {
bar_manager->topmost = false;
bar_manager->picky_redraw = false;
bar_manager->notch_width = 200;
bar_manager->active_adid = display_arrangement(display_active_display_id());

background_init(&bar_manager->background);
bar_manager->background.bounds.size.height = 25;
Expand Down Expand Up @@ -370,6 +371,8 @@ void bar_manager_custom_events_trigger(struct bar_manager* bar_manager, char* na
}

void bar_manager_display_changed(struct bar_manager* bar_manager) {
bar_manager->active_adid = display_arrangement(display_active_display_id());

for (int i = 0; i < bar_manager->bar_count; ++i)
bar_destroy(bar_manager->bars[i]);

Expand Down Expand Up @@ -402,6 +405,7 @@ void bar_manager_handle_space_change(struct bar_manager* bar_manager) {
}

void bar_manager_handle_display_change(struct bar_manager* bar_manager) {
bar_manager->active_adid = display_arrangement(display_active_display_id());
bar_manager_custom_events_trigger(bar_manager, COMMAND_SUBSCRIBE_DISPLAY_CHANGE, NULL);
}

Expand Down
2 changes: 2 additions & 0 deletions src/bar_manager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef BAR_MANAGER_H
#define BAR_MANAGER_H
#include <_types/_uint32_t.h>
#include <stdint.h>

#define TIMER_CALLBACK(name) void name(CFRunLoopTimerRef timer, void *context)
Expand Down Expand Up @@ -31,6 +32,7 @@ struct bar_manager {
uint32_t notch_width;
int y_offset;
bool shadow;
uint32_t active_adid;

struct background background;
struct custom_events custom_events;
Expand Down
26 changes: 6 additions & 20 deletions src/popup.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ void popup_init(struct popup* popup) {
background_init(&popup->background);
popup->background.border_color = rgba_color_from_hex(0xffff0000);
popup->background.color = rgba_color_from_hex(0x44000000);

}

void popup_calculate_bounds(struct popup* popup) {
Expand All @@ -44,34 +43,24 @@ void popup_calculate_bounds(struct popup* popup) {
y += popup->cell_size;
}

if ((popup->background.bounds.size.width != width + 2) || (popup->background.bounds.size.height != y - popup->cell_size / 2)) {
popup->background.bounds.size.width = width + 2;
popup->background.bounds.size.height = y - popup->cell_size / 2;

popup_resize(popup);
}
popup->background.bounds.size.width = width + 2;
popup->background.bounds.size.height = y - popup->cell_size / 2;
}

void popup_create_frame(struct popup *popup, CFTypeRef *frame_region) {
popup->frame.size = popup->background.bounds.size;
CGSNewRegionWithRect(&popup->frame, frame_region);
CGSNewRegionWithRect(&popup->background.bounds, frame_region);
}

void popup_resize(struct popup* popup) {
CFTypeRef frame_region;
popup_create_frame(popup, &frame_region);

SLSDisableUpdate(g_connection);
SLSOrderWindow(g_connection, popup->id, -1, 0);
SLSSetWindowShape(g_connection, popup->id, popup->anchor.x, popup->anchor.y, frame_region);

SLSClearActivationRegion(g_connection, popup->id);
SLSAddActivationRegion(g_connection, popup->id, frame_region);
SLSRemoveAllTrackingAreas(g_connection, popup->id);

popup_draw(popup);
SLSOrderWindow(g_connection, popup->id, 1, 0);
SLSReenableUpdate(g_connection);
CFRelease(frame_region);
}

Expand Down Expand Up @@ -99,9 +88,6 @@ void popup_create_window(struct popup* popup) {
context_set_font_smoothing(popup->context, g_bar_manager.font_smoothing);

popup->drawing = true;
SLSDisableUpdate(g_connection);
popup_draw(popup);
SLSReenableUpdate(g_connection);
}

void popup_close_window(struct popup* popup) {
Expand All @@ -123,7 +109,7 @@ void popup_set_anchor(struct popup* popup, CGPoint anchor, uint32_t adid) {
if (popup->anchor.x != anchor.x || popup->anchor.y != anchor.y + popup->y_offset) {
popup->anchor = anchor;
popup->anchor.y += popup->y_offset;
if (popup->drawing) popup_resize(popup);
SLSMoveWindow(g_connection, popup->id, &popup->anchor);
}
popup->adid = adid;
}
Expand All @@ -137,7 +123,8 @@ void popup_draw(struct popup* popup) {
if (!popup->drawing) return;

SLSOrderWindow(g_connection, popup->id, -1, 0);
draw_rect(popup->context, popup->frame, &popup->background.color, popup->background.corner_radius, popup->background.border_width, &popup->background.border_color, true);
popup_resize(popup);
draw_rect(popup->context, popup->background.bounds, &popup->background.color, popup->background.corner_radius, popup->background.border_width, &popup->background.border_color, true);

for (int i = 0; i < popup->num_items; i++) {
struct bar_item* bar_item = popup->items[i];
Expand Down Expand Up @@ -176,7 +163,6 @@ static bool popup_parse_sub_domain(struct popup* popup, FILE* rsp, struct token
return true;
} else if (token_equals(property, PROPERTY_ALIGN)) {
popup->align = get_token(&message).text[0];
printf("Align: %c \n", popup->align);
return true;
}
else {
Expand Down

0 comments on commit 3f6cc9d

Please sign in to comment.