mirror of
https://github.com/labwc/labwc.git
synced 2026-03-15 05:33:53 -04:00
Support a very simple root-menu
This commit is contained in:
parent
f49a3a0395
commit
dc5d1ab976
9 changed files with 255 additions and 36 deletions
|
|
@ -38,9 +38,10 @@
|
||||||
#define XCURSOR_MOVE "grabbing"
|
#define XCURSOR_MOVE "grabbing"
|
||||||
|
|
||||||
enum cursor_mode {
|
enum cursor_mode {
|
||||||
LAB_CURSOR_PASSTHROUGH,
|
LAB_CURSOR_PASSTHROUGH = 0,
|
||||||
LAB_CURSOR_MOVE,
|
LAB_CURSOR_MOVE,
|
||||||
LAB_CURSOR_RESIZE,
|
LAB_CURSOR_RESIZE,
|
||||||
|
LAB_INPUT_STATE_MENU,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct input {
|
struct input {
|
||||||
|
|
@ -106,6 +107,8 @@ struct server {
|
||||||
|
|
||||||
/* Set when in cycle (alt-tab) mode */
|
/* Set when in cycle (alt-tab) mode */
|
||||||
struct view *cycle_view;
|
struct view *cycle_view;
|
||||||
|
|
||||||
|
struct menu *rootmenu;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct output {
|
struct output {
|
||||||
|
|
|
||||||
35
include/menu/menu.h
Normal file
35
include/menu/menu.h
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
#ifndef __LABWC_MENU_H
|
||||||
|
#define __LABWC_MENU_H
|
||||||
|
|
||||||
|
#include <wayland-server.h>
|
||||||
|
#include <wlr/render/wlr_renderer.h>
|
||||||
|
|
||||||
|
struct menuitem {
|
||||||
|
char *action;
|
||||||
|
char *command;
|
||||||
|
struct wlr_box geo_box;
|
||||||
|
struct wlr_texture *active_texture;
|
||||||
|
struct wlr_texture *inactive_texture;
|
||||||
|
bool selected;
|
||||||
|
struct wl_list link;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct menu {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
struct wl_list menuitems;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* menu_create - create menu */
|
||||||
|
void menu_init(struct server *server, struct menu *menu);
|
||||||
|
|
||||||
|
/* menu_move - move to position (x, y) */
|
||||||
|
void menu_move(struct menu *menu, int x, int y);
|
||||||
|
|
||||||
|
/* menu_set_selected - select item at (x, y) */
|
||||||
|
void menu_set_selected(struct menu *menu, int x, int y);
|
||||||
|
|
||||||
|
/* menu_action_selected - select item at (x, y) */
|
||||||
|
void menu_action_selected(struct server *server, struct menu *menu);
|
||||||
|
|
||||||
|
#endif /* __LABWC_MENU_H */
|
||||||
|
|
@ -25,7 +25,7 @@ action(struct server *server, const char *action, const char *command)
|
||||||
desktop_next_view(server, server->cycle_view);
|
desktop_next_view(server, server->cycle_view);
|
||||||
} else if (!strcasecmp(action, "Reconfigure")) {
|
} else if (!strcasecmp(action, "Reconfigure")) {
|
||||||
reconfigure();
|
reconfigure();
|
||||||
} else if (!strcasecmp(action, "debug-views")) {
|
} else if (!strcasecmp(action, "Debug")) {
|
||||||
dbg_show_views(server);
|
dbg_show_views(server);
|
||||||
} else {
|
} else {
|
||||||
warn("action (%s) not supported", action);
|
warn("action (%s) not supported", action);
|
||||||
|
|
|
||||||
94
src/cursor.c
94
src/cursor.c
|
|
@ -1,4 +1,5 @@
|
||||||
#include "labwc.h"
|
#include "labwc.h"
|
||||||
|
#include "menu/menu.h"
|
||||||
|
|
||||||
static void
|
static void
|
||||||
request_cursor_notify(struct wl_listener *listener, void *data)
|
request_cursor_notify(struct wl_listener *listener, void *data)
|
||||||
|
|
@ -108,6 +109,10 @@ process_cursor_motion(struct server *server, uint32_t time)
|
||||||
} else if (server->cursor_mode == LAB_CURSOR_RESIZE) {
|
} else if (server->cursor_mode == LAB_CURSOR_RESIZE) {
|
||||||
process_cursor_resize(server, time);
|
process_cursor_resize(server, time);
|
||||||
return;
|
return;
|
||||||
|
} else if (server->cursor_mode == LAB_INPUT_STATE_MENU) {
|
||||||
|
menu_set_selected(server->rootmenu,
|
||||||
|
server->seat.cursor->x, server->seat.cursor->y);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Otherwise, find the view under the pointer and send the event along.
|
/* Otherwise, find the view under the pointer and send the event along.
|
||||||
|
|
@ -240,42 +245,63 @@ cursor_button(struct wl_listener *listener, void *data)
|
||||||
double sx, sy;
|
double sx, sy;
|
||||||
struct wlr_surface *surface;
|
struct wlr_surface *surface;
|
||||||
int view_area;
|
int view_area;
|
||||||
struct view *view =
|
struct view *view = desktop_view_at(server, server->seat.cursor->x,
|
||||||
desktop_view_at(server, server->seat.cursor->x, server->seat.cursor->y,
|
server->seat.cursor->y, &surface, &sx, &sy, &view_area);
|
||||||
&surface, &sx, &sy, &view_area);
|
|
||||||
|
/* handle _release_ */
|
||||||
if (event->state == WLR_BUTTON_RELEASED) {
|
if (event->state == WLR_BUTTON_RELEASED) {
|
||||||
/* Exit interactive move/resize mode. */
|
if (server->cursor_mode == LAB_INPUT_STATE_MENU) {
|
||||||
server->cursor_mode = LAB_CURSOR_PASSTHROUGH;
|
return;
|
||||||
} else {
|
|
||||||
/* Focus that client if the button was _pressed_ */
|
|
||||||
desktop_focus_view(&server->seat, view);
|
|
||||||
switch (view_area) {
|
|
||||||
case LAB_DECO_BUTTON_CLOSE:
|
|
||||||
view->impl->close(view);
|
|
||||||
break;
|
|
||||||
case LAB_DECO_BUTTON_ICONIFY:
|
|
||||||
view_minimize(view);
|
|
||||||
break;
|
|
||||||
case LAB_DECO_PART_TITLE:
|
|
||||||
interactive_begin(view, LAB_CURSOR_MOVE, 0);
|
|
||||||
break;
|
|
||||||
case LAB_DECO_PART_TOP:
|
|
||||||
interactive_begin(view, LAB_CURSOR_RESIZE,
|
|
||||||
WLR_EDGE_TOP);
|
|
||||||
break;
|
|
||||||
case LAB_DECO_PART_RIGHT:
|
|
||||||
interactive_begin(view, LAB_CURSOR_RESIZE,
|
|
||||||
WLR_EDGE_RIGHT);
|
|
||||||
break;
|
|
||||||
case LAB_DECO_PART_BOTTOM:
|
|
||||||
interactive_begin(view, LAB_CURSOR_RESIZE,
|
|
||||||
WLR_EDGE_BOTTOM);
|
|
||||||
break;
|
|
||||||
case LAB_DECO_PART_LEFT:
|
|
||||||
interactive_begin(view, LAB_CURSOR_RESIZE,
|
|
||||||
WLR_EDGE_LEFT);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
/* Exit interactive move/resize/menu mode. */
|
||||||
|
server->cursor_mode = LAB_CURSOR_PASSTHROUGH;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (server->cursor_mode == LAB_INPUT_STATE_MENU) {
|
||||||
|
menu_action_selected(server, server->rootmenu);
|
||||||
|
server->cursor_mode = LAB_CURSOR_PASSTHROUGH;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* handle _press_ on desktop */
|
||||||
|
if (!view) {
|
||||||
|
/* launch root-menu */
|
||||||
|
server->cursor_mode = LAB_INPUT_STATE_MENU;
|
||||||
|
menu_move(server->rootmenu, server->seat.cursor->x,
|
||||||
|
server->seat.cursor->y);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Handle _press_ on view */
|
||||||
|
desktop_focus_view(&server->seat, view);
|
||||||
|
switch (view_area) {
|
||||||
|
case LAB_DECO_BUTTON_CLOSE:
|
||||||
|
view->impl->close(view);
|
||||||
|
break;
|
||||||
|
case LAB_DECO_BUTTON_ICONIFY:
|
||||||
|
view_minimize(view);
|
||||||
|
break;
|
||||||
|
case LAB_DECO_PART_TITLE:
|
||||||
|
interactive_begin(view, LAB_CURSOR_MOVE, 0);
|
||||||
|
break;
|
||||||
|
case LAB_DECO_PART_TOP:
|
||||||
|
interactive_begin(view, LAB_CURSOR_RESIZE,
|
||||||
|
WLR_EDGE_TOP);
|
||||||
|
break;
|
||||||
|
case LAB_DECO_PART_RIGHT:
|
||||||
|
interactive_begin(view, LAB_CURSOR_RESIZE,
|
||||||
|
WLR_EDGE_RIGHT);
|
||||||
|
break;
|
||||||
|
case LAB_DECO_PART_BOTTOM:
|
||||||
|
interactive_begin(view, LAB_CURSOR_RESIZE,
|
||||||
|
WLR_EDGE_BOTTOM);
|
||||||
|
break;
|
||||||
|
case LAB_DECO_PART_LEFT:
|
||||||
|
interactive_begin(view, LAB_CURSOR_RESIZE,
|
||||||
|
WLR_EDGE_LEFT);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#include "labwc.h"
|
#include "labwc.h"
|
||||||
#include "theme/theme.h"
|
#include "theme/theme.h"
|
||||||
#include "xbm/xbm.h"
|
#include "xbm/xbm.h"
|
||||||
|
#include "menu/menu.h"
|
||||||
|
|
||||||
#include <cairo.h>
|
#include <cairo.h>
|
||||||
#include <pango/pangocairo.h>
|
#include <pango/pangocairo.h>
|
||||||
|
|
@ -61,6 +62,10 @@ main(int argc, char *argv[])
|
||||||
theme_read(rc.theme_name);
|
theme_read(rc.theme_name);
|
||||||
xbm_load(server.renderer);
|
xbm_load(server.renderer);
|
||||||
|
|
||||||
|
struct menu menu = { 0 };
|
||||||
|
menu_init(&server, &menu);
|
||||||
|
server.rootmenu = &menu;
|
||||||
|
|
||||||
session_autostart_init();
|
session_autostart_init();
|
||||||
if (startup_cmd) {
|
if (startup_cmd) {
|
||||||
spawn_async_no_shell(startup_cmd);
|
spawn_async_no_shell(startup_cmd);
|
||||||
|
|
|
||||||
117
src/menu/menu.c
Normal file
117
src/menu/menu.c
Normal file
|
|
@ -0,0 +1,117 @@
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
#include <cairo/cairo.h>
|
||||||
|
#include <pango/pangocairo.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "labwc.h"
|
||||||
|
#include "menu/menu.h"
|
||||||
|
|
||||||
|
static float background[4] = { 0.3f, 0.1f, 0.1f, 1.0f };
|
||||||
|
static float foreground[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
|
static const char font[] = "Sans 11";
|
||||||
|
|
||||||
|
struct wlr_texture *
|
||||||
|
texture_create(struct server *server, struct wlr_box *geo, const char *text,
|
||||||
|
float *bg, float *fg)
|
||||||
|
{
|
||||||
|
cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
|
||||||
|
geo->width, geo->height);
|
||||||
|
cairo_t *cairo = cairo_create(surf);
|
||||||
|
|
||||||
|
cairo_set_source_rgb(cairo, bg[0], bg[1], bg[2]);
|
||||||
|
cairo_paint(cairo);
|
||||||
|
cairo_set_source_rgba(cairo, fg[0], fg[1], fg[2], fg[3]);
|
||||||
|
cairo_move_to(cairo, 0, 0);
|
||||||
|
|
||||||
|
PangoLayout *layout = pango_cairo_create_layout(cairo);
|
||||||
|
pango_layout_set_width(layout, geo->width * PANGO_SCALE);
|
||||||
|
pango_layout_set_text(layout, text, -1);
|
||||||
|
|
||||||
|
PangoFontDescription *desc = pango_font_description_from_string(font);
|
||||||
|
pango_layout_set_font_description(layout, desc);
|
||||||
|
pango_font_description_free(desc);
|
||||||
|
pango_cairo_update_layout(cairo, layout);
|
||||||
|
pango_cairo_show_layout(cairo, layout);
|
||||||
|
g_object_unref(layout);
|
||||||
|
|
||||||
|
cairo_surface_flush(surf);
|
||||||
|
unsigned char *data = cairo_image_surface_get_data(surf);
|
||||||
|
struct wlr_texture *texture = wlr_texture_from_pixels(server->renderer,
|
||||||
|
WL_SHM_FORMAT_ARGB8888, cairo_image_surface_get_stride(surf),
|
||||||
|
geo->width, geo->height, data);
|
||||||
|
|
||||||
|
cairo_destroy(cairo);
|
||||||
|
cairo_surface_destroy(surf);
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MENUWIDTH (100)
|
||||||
|
#define MENUHEIGHT (25)
|
||||||
|
|
||||||
|
struct menuitem *
|
||||||
|
menuitem_create(struct server *server, struct menu *menu, const char *text,
|
||||||
|
const char *action, const char *command)
|
||||||
|
{
|
||||||
|
struct menuitem *menuitem = calloc(1, sizeof(struct menuitem));
|
||||||
|
if (!menuitem) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
menuitem->action = action ? strdup(action) : NULL;
|
||||||
|
menuitem->command = command ? strdup(command) : NULL;
|
||||||
|
menuitem->geo_box.width = MENUWIDTH;
|
||||||
|
menuitem->geo_box.height = MENUHEIGHT;
|
||||||
|
menuitem->active_texture = texture_create(server, &menuitem->geo_box,
|
||||||
|
text, background, foreground);
|
||||||
|
menuitem->inactive_texture = texture_create(server, &menuitem->geo_box,
|
||||||
|
text, foreground, background);
|
||||||
|
wl_list_insert(&menu->menuitems, &menuitem->link);
|
||||||
|
return menuitem;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
menu_init(struct server *server, struct menu *menu)
|
||||||
|
{
|
||||||
|
wl_list_init(&menu->menuitems);
|
||||||
|
menuitem_create(server, menu, "Terminal", "Execute", "sakura");
|
||||||
|
menuitem_create(server, menu, "Reconfigure", "Reconfigure", NULL);
|
||||||
|
menuitem_create(server, menu, "Exit", "Exit", NULL);
|
||||||
|
menu_move(menu, 100, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
menu_move(struct menu *menu, int x, int y)
|
||||||
|
{
|
||||||
|
menu->x = x;
|
||||||
|
menu->y = y;
|
||||||
|
|
||||||
|
int offset = 0;
|
||||||
|
struct menuitem *menuitem;
|
||||||
|
wl_list_for_each (menuitem, &menu->menuitems, link) {
|
||||||
|
menuitem->geo_box.x = menu->x;
|
||||||
|
menuitem->geo_box.y = menu->y + offset;
|
||||||
|
offset += menuitem->geo_box.height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
menu_set_selected(struct menu *menu, int x, int y)
|
||||||
|
{
|
||||||
|
struct menuitem *menuitem;
|
||||||
|
wl_list_for_each (menuitem, &menu->menuitems, link) {
|
||||||
|
menuitem->selected =
|
||||||
|
wlr_box_contains_point(&menuitem->geo_box, x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
menu_action_selected(struct server *server, struct menu *menu)
|
||||||
|
{
|
||||||
|
struct menuitem *menuitem;
|
||||||
|
wl_list_for_each (menuitem, &menu->menuitems, link) {
|
||||||
|
if (menuitem->selected) {
|
||||||
|
action(server, menuitem->action, menuitem->command);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/menu/meson.build
Normal file
3
src/menu/meson.build
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
labwc_sources += files(
|
||||||
|
'menu.c',
|
||||||
|
)
|
||||||
|
|
@ -21,3 +21,4 @@ subdir('common')
|
||||||
subdir('config')
|
subdir('config')
|
||||||
subdir('theme')
|
subdir('theme')
|
||||||
subdir('xbm')
|
subdir('xbm')
|
||||||
|
subdir('menu')
|
||||||
|
|
|
||||||
29
src/output.c
29
src/output.c
|
|
@ -1,6 +1,7 @@
|
||||||
#define _POSIX_C_SOURCE 200809L
|
#define _POSIX_C_SOURCE 200809L
|
||||||
#include <wlr/types/wlr_xdg_output_v1.h>
|
#include <wlr/types/wlr_xdg_output_v1.h>
|
||||||
#include "labwc.h"
|
#include "labwc.h"
|
||||||
|
#include "menu/menu.h"
|
||||||
#include "theme/theme.h"
|
#include "theme/theme.h"
|
||||||
#include "layers.h"
|
#include "layers.h"
|
||||||
|
|
||||||
|
|
@ -80,6 +81,29 @@ render_it:
|
||||||
draw_rect_unfilled(&dd, box);
|
draw_rect_unfilled(&dd, box);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
render_rootmenu(struct output *output)
|
||||||
|
{
|
||||||
|
struct server *server = output->server;
|
||||||
|
struct draw_data ddata = {
|
||||||
|
.renderer = server->renderer,
|
||||||
|
.transform_matrix = output->wlr_output->transform_matrix,
|
||||||
|
};
|
||||||
|
float matrix[9];
|
||||||
|
|
||||||
|
ddata.rgba = (float[4]){ 0.9, 0.3, 0.3, 0.5 };
|
||||||
|
struct menuitem *menuitem;
|
||||||
|
wl_list_for_each (menuitem, &server->rootmenu->menuitems, link) {
|
||||||
|
struct wlr_texture *t;
|
||||||
|
t = menuitem->selected ? menuitem->active_texture :
|
||||||
|
menuitem->inactive_texture;
|
||||||
|
wlr_matrix_project_box(matrix, &menuitem->geo_box,
|
||||||
|
WL_OUTPUT_TRANSFORM_NORMAL, 0, ddata.transform_matrix);
|
||||||
|
wlr_render_texture_with_matrix(ddata.renderer, t, matrix, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
render_icon(struct draw_data *d, struct wlr_box box,
|
render_icon(struct draw_data *d, struct wlr_box box,
|
||||||
struct wlr_texture *texture)
|
struct wlr_texture *texture)
|
||||||
|
|
@ -337,6 +361,11 @@ output_frame_notify(struct wl_listener *listener, void *data)
|
||||||
|
|
||||||
render_layer(&now, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]);
|
render_layer(&now, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]);
|
||||||
render_layer(&now, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
|
render_layer(&now, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
|
||||||
|
|
||||||
|
if (output->server->cursor_mode == LAB_INPUT_STATE_MENU) {
|
||||||
|
render_rootmenu(output);
|
||||||
|
}
|
||||||
|
|
||||||
/* Just in case hardware cursors not supported by GPU */
|
/* Just in case hardware cursors not supported by GPU */
|
||||||
wlr_output_render_software_cursors(output->wlr_output, NULL);
|
wlr_output_render_software_cursors(output->wlr_output, NULL);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue