Skip to content

Commit

Permalink
!fixup Refactoring swaywm#2
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Weilbach <[email protected]>
  • Loading branch information
Felix Weilbach committed Aug 1, 2021
1 parent ce9ff50 commit 003853d
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions swaybar/tray/dbusmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
#include "swaybar/tray/item.h"
#include "swaybar/config.h"
#include "swaybar/tray/icon.h"
#include "swaybar/input.h"
#include "cairo_util.h"
#include "pango.h"
#include "background-image.h"

static const char *menu_interface = "com.canonical.dbusmenu";

static void swaybar_dbusmenu_get_layout_root(struct swaybar_dbusmenu *menu);
static void swaybar_dbusmenu_get_layout(struct swaybar_dbusmenu *menu, int id);

struct swaybar_dbusmenu_hotspot {
Expand Down Expand Up @@ -97,7 +99,7 @@ static int handle_items_properties_updated(sd_bus_message *msg, void *data,

// TODO: Optimize. Update only needed properties
if (sni->tray->menu) {
swaybar_dbusmenu_get_layout(sni->tray->menu, 0);
swaybar_dbusmenu_get_layout_root(sni->tray->menu);
}
return 0;
}
Expand Down Expand Up @@ -843,12 +845,12 @@ get_layout_callback(sd_bus_message *msg, void *data, sd_bus_error *error) {
return sd_bus_message_get_errno(msg);
}

// Parse the layout. The layout comes as a recursive structure as
// dbus message in the following form (ia{sv}av)

// Skip the menu revision
sd_bus_message_skip(msg, "u");

// Parse the layout. The layout comes as a recursive structure as
// dbus message in the following form (ia{sv}av)
int ret = 0;
sni->tray->menu_pointer_focus = NULL;

bool already_open = false;
Expand All @@ -872,6 +874,7 @@ get_layout_callback(sd_bus_message *msg, void *data, sd_bus_error *error) {
}
struct swaybar_dbusmenu_menu *menu = dbusmenu->menu;
menu->dbusmenu = dbusmenu;
int ret = 0;
while (!sd_bus_message_at_end(msg, 1)) {

sd_bus_message_enter_container(msg, 'r', "ia{sv}av");
Expand Down Expand Up @@ -1089,6 +1092,44 @@ static void close_child_menus(struct swaybar_dbusmenu_menu *menu) {
}
}

static void open_close_child_menu(struct swaybar_dbusmenu_menu *menu,
struct swaybar_dbusmenu_menu_item *item, int x, int y) {

bool in_hotspot = is_in_hotspot(&item->hotspot, x, y);

if (item->submenu && in_hotspot) {
if (item->id == 0) {
// No need to open the root menu
return;
}
open_menu_id(menu->dbusmenu, item->id);
} else if (in_hotspot && !item->submenu) {
close_child_menus(menu);
}
}

static void pointer_motion_process_item(
struct swaybar_dbusmenu_menu *focused_menu,
struct swaybar_dbusmenu_menu_item *item, struct swaybar_seat *seat) {

int scale = focused_menu->dbusmenu->output->scale;
double x = seat->pointer.x * scale;
double y = seat->pointer.y * scale;

if (is_in_hotspot(&item->hotspot, x, y) && item->enabled &&
!item->is_separator) {
struct swaybar_tray *tray = focused_menu->dbusmenu->sni->tray;
struct swaybar_sni *sni = tray->menu->sni;
sd_bus_call_method_async(tray->bus, NULL, sni->service, sni->menu,
menu_interface, "Event", NULL, NULL, "isvu", item->id,
"hovered", "y", 0, time(NULL));
sway_log(SWAY_DEBUG, "%s%s hovered id %d", sni->service, sni->menu,
item->id);
}

open_close_child_menu(focused_menu, item, x, y);
}

bool dbusmenu_pointer_motion(struct swaybar_seat *seat,
struct wl_pointer *wl_pointer, uint32_t time_, wl_fixed_t surface_x,
wl_fixed_t surface_y) {
Expand All @@ -1099,39 +1140,17 @@ bool dbusmenu_pointer_motion(struct swaybar_seat *seat,
return false;
}

double x = seat->pointer.x;
double y = seat->pointer.y;
int scale = focused_menu->dbusmenu->output->scale;

for (int i = 0; i < focused_menu->items->length; ++i) {
struct swaybar_dbusmenu_menu_item *item = focused_menu->items->items[i];

bool in_hotspot = is_in_hotspot(&item->hotspot, x * scale, y * scale);

if (in_hotspot && item->enabled && !item->is_separator) {
struct swaybar_sni *sni = tray->menu->sni;
sd_bus_call_method_async(tray->bus, NULL, sni->service, sni->menu,
menu_interface, "Event", NULL, NULL, "isvu", item->id,
"hovered", "y", 0, time(NULL));
sway_log(SWAY_DEBUG, "%s%s hovered id %d", sni->service, sni->menu,
item->id);
}

if (item->submenu && in_hotspot) {
if (item->id == 0) {
continue;
}
open_menu_id(focused_menu->dbusmenu, item->id);
} else if (in_hotspot && !item->submenu) {
close_child_menus(focused_menu);
}
pointer_motion_process_item(focused_menu, item, seat);
}

swaybar_dbusmenu_draw_menu(focused_menu, focused_menu->item_id, false);

return true;
}

static struct swaybar_dbusmenu_menu *dbusmenu_menu_find_menu(
static struct swaybar_dbusmenu_menu *dbusmenu_menu_find_menu_surface(
struct swaybar_dbusmenu_menu *menu, struct wl_surface *surface) {

if (menu->surface && menu->surface->surface == surface) {
Expand All @@ -1149,7 +1168,7 @@ static struct swaybar_dbusmenu_menu *dbusmenu_menu_find_menu(
}

struct swaybar_dbusmenu_menu *child_child_menu =
dbusmenu_menu_find_menu(child_menu, surface);
dbusmenu_menu_find_menu_surface(child_menu, surface);
if (child_child_menu != NULL) {
return child_child_menu;
}
Expand Down Expand Up @@ -1191,7 +1210,7 @@ bool dbusmenu_pointer_enter(void *data, struct wl_pointer *wl_pointer,
}

struct swaybar_dbusmenu_menu *new_focused_menu =
dbusmenu_menu_find_menu(tray->menu->menu, surface);
dbusmenu_menu_find_menu_surface(tray->menu->menu, surface);

if (new_focused_menu && new_focused_menu->child_menus) {
close_child_menus_outside_pointer(new_focused_menu, seat);
Expand Down

0 comments on commit 003853d

Please sign in to comment.