Browse Source

menu: smarter context menu spawning

K. Lange 2 years ago
parent
commit
c4ffc28b1f
3 changed files with 18 additions and 6 deletions
  1. 1 6
      apps/file-browser.c
  2. 1 0
      base/usr/include/toaru/menu.h
  3. 16 0
      lib/menu.c

+ 1 - 6
apps/file-browser.c

@@ -2016,12 +2016,7 @@ int main(int argc, char * argv[]) {
 										if (f && !f->selected) {
 											toggle_selected(hilighted_offset, me->modifiers);
 										}
-										menu_show(context_menu, main_window->ctx);
-										if (me->new_x + context_menu->window->width + main_window->x > main_window->ctx->display_width) {
-											yutani_window_move(main_window->ctx, context_menu->window, me->new_x + main_window->x - context_menu->window->width, me->new_y + main_window->y);
-										} else {
-											yutani_window_move(main_window->ctx, context_menu->window, me->new_x + main_window->x, me->new_y + main_window->y);
-										}
+										menu_show_at(context_menu, main_window, me->new_x, me->new_y);
 									}
 								}
 

+ 1 - 0
base/usr/include/toaru/menu.h

@@ -75,6 +75,7 @@ extern struct MenuSet * menu_set_from_description(const char * path, void (*call
 
 extern void menu_insert(struct MenuList * menu, struct MenuEntry * entry);
 extern void menu_show(struct MenuList * menu, yutani_t * yctx);
+extern void menu_show_at(struct MenuList * menu, yutani_window_t * parent, int x, int y);
 extern int menu_process_event(yutani_t * yctx, yutani_msg_t * m);
 extern struct MenuList * menu_set_get_root(struct MenuSet * menu);
 extern struct MenuList * menu_set_get_menu(struct MenuSet * menu, char * submenu);

+ 16 - 0
lib/menu.c

@@ -549,6 +549,22 @@ void menu_show(struct MenuList * menu, yutani_t * yctx) {
 	hashmap_set(menu_windows, (void*)menu_window->wid, menu_window);
 }
 
+void menu_show_at(struct MenuList * menu, yutani_window_t * parent, int x, int y) {
+
+	int final_x;
+	int final_y;
+
+	menu_show(menu, parent->ctx);
+
+	final_x = x + parent->x;
+	final_y = y + parent->y;
+
+	if (final_x + menu->window->width > parent->ctx->display_width) final_x -= menu->window->width;
+	if (final_y + menu->window->height > parent->ctx->display_height) final_y -= menu->window->height;
+
+	yutani_window_move(parent->ctx, menu->window, final_x, final_y);
+}
+
 int menu_has_eventual_child(struct MenuList * root, struct MenuList * child) {
 
 	if (!child) return 0;