add -c center mode on wayland

This commit is contained in:
Stacy Harper 2021-08-14 12:07:22 +02:00 committed by Jari Vetoniemi
parent a84eeb770e
commit a42fa97a49
8 changed files with 76 additions and 3 deletions

View File

@ -192,6 +192,7 @@ usage(FILE *out, const char *name)
" (...) At end of help indicates the backend support for option.\n\n"
" -b, --bottom appears at the bottom of the screen. (wx)\n"
" -c, --center appears at the center of the screen. (w)\n"
" -f, --grab show the menu before reading stdin. (wx)\n"
" -n, --no-overlap adjust geometry to not overlap with panels. (w)\n"
" -m, --monitor index of monitor where menu will appear. (wx)\n"
@ -244,6 +245,7 @@ do_getopt(struct client *client, int *argc, char **argv[])
{ "filter", required_argument, 0, 'F' },
{ "wrap", no_argument, 0, 'w' },
{ "list", required_argument, 0, 'l' },
{ "center", no_argument, 0, 'c' },
{ "prompt", required_argument, 0, 'p' },
{ "index", required_argument, 0, 'I' },
{ "prefix", required_argument, 0, 'P' },
@ -286,7 +288,7 @@ do_getopt(struct client *client, int *argc, char **argv[])
for (optind = 0;;) {
int32_t opt;
if ((opt = getopt_long(*argc, *argv, "hviwxl:I:p:P:I:bfm:H:n", opts, NULL)) < 0)
if ((opt = getopt_long(*argc, *argv, "hviwxcl:I:p:P:I:bfm:H:n", opts, NULL)) < 0)
break;
switch (opt) {
@ -309,6 +311,9 @@ do_getopt(struct client *client, int *argc, char **argv[])
case 'l':
client->lines = strtol(optarg, NULL, 10);
break;
case 'c':
client->center = true;
break;
case 'p':
client->title = optarg;
break;
@ -433,13 +438,18 @@ menu_with_options(struct client *client)
bm_menu_set_filter_mode(menu, client->filter_mode);
bm_menu_set_lines(menu, client->lines);
bm_menu_set_wrap(menu, client->wrap);
bm_menu_set_bottom(menu, client->bottom);
bm_menu_set_monitor(menu, client->monitor);
bm_menu_set_monitor_name(menu, client->monitor_name);
bm_menu_set_scrollbar(menu, client->scrollbar);
bm_menu_set_panel_overlap(menu, !client->no_overlap);
bm_menu_set_password(menu, client->password);
if (client->center) {
bm_menu_set_center(menu, client->center);
} else if (client->bottom) {
bm_menu_set_bottom(menu, client->bottom);
}
for (uint32_t i = 0; i < BM_COLOR_LAST; ++i)
bm_menu_set_color(menu, i, client->colors[i]);

View File

@ -17,6 +17,7 @@ struct client {
uint32_t selected;
uint32_t monitor;
bool bottom;
bool center;
bool grab;
bool wrap;
bool ifne;

View File

@ -483,6 +483,15 @@ BM_PUBLIC void bm_menu_set_scrollbar(struct bm_menu *menu, enum bm_scrollbar_mod
*/
BM_PUBLIC enum bm_scrollbar_mode bm_menu_get_scrollbar(struct bm_menu *menu);
/**
* Display menu at center of the screen.
* This may be no-op on some renderers (curses, wayland)
*
* @param menu bm_menu instance to set center mode for.
* @param center true for center mode, false for top mode.
*/
BM_PUBLIC void bm_menu_set_center(struct bm_menu *menu, bool center);
/**
* Display menu at bottom of the screen.
* This may be no-op on some renderers (curses, wayland)

View File

@ -79,6 +79,11 @@ struct render_api {
*/
void (*set_bottom)(const struct bm_menu *menu, bool bottom);
/**
* Set menu to appear from center of the screen.
*/
void (*set_center)(const struct bm_menu *menu, bool center);
/**
* Set monitor indeax where menu will appear
*/
@ -311,6 +316,11 @@ struct bm_menu {
*/
bool wrap;
/**
* Is menu shown from center?
*/
bool center;
/**
* Is menu shown from bottom?
*/

View File

@ -340,6 +340,20 @@ bm_menu_get_scrollbar(struct bm_menu *menu)
return menu->scrollbar;
}
void
bm_menu_set_center(struct bm_menu *menu, bool center)
{
assert(menu);
if (menu->center == center)
return;
menu->center = center;
if (menu->renderer->api.set_center)
menu->renderer->api.set_center(menu, center);
}
void
bm_menu_set_bottom(struct bm_menu *menu, bool bottom)
{

View File

@ -240,6 +240,18 @@ set_bottom(const struct bm_menu *menu, bool bottom)
}
}
static void
set_center(const struct bm_menu *menu, bool center)
{
struct wayland *wayland = menu->renderer->internal;
assert(wayland);
struct window *window;
wl_list_for_each(window, &wayland->windows, link) {
bm_wl_window_set_center(window, wayland->display, center);
}
}
static void
grab_keyboard(const struct bm_menu *menu, bool grab)
{
@ -432,6 +444,7 @@ register_renderer(struct render_api *api)
api->get_displayed_count = get_displayed_count;
api->poll_key = poll_key;
api->render = render;
api->set_center = set_center;
api->set_bottom = set_bottom;
api->grab_keyboard = grab_keyboard;
api->set_overlap = set_overlap;

View File

@ -88,6 +88,7 @@ struct window {
uint32_t displayed;
struct wl_list link;
bool bottom;
bool center;
bool render_pending;
struct {
@ -129,6 +130,7 @@ void bm_wl_registry_destroy(struct wayland *wayland);
void bm_wl_window_schedule_render(struct window *window);
void bm_wl_window_render(struct window *window, struct wl_display *display, const struct bm_menu *menu);
void bm_wl_window_set_bottom(struct window *window, struct wl_display *display, bool bottom);
void bm_wl_window_set_center(struct window *window, struct wl_display *display, bool center);
void bm_wl_window_grab_keyboard(struct window *window, struct wl_display *display, bool grab);
void bm_wl_window_set_overlap(struct window *window, struct wl_display *display, bool overlap);
bool bm_wl_window_create(struct window *window, struct wl_display *display, struct wl_shm *shm, struct wl_output *output, struct zwlr_layer_shell_v1 *layer_shell, struct wl_surface *surface);

View File

@ -240,7 +240,7 @@ bm_wl_window_render(struct window *window, struct wl_display *display, const str
break;
window->height = result.height / window->scale;
zwlr_layer_surface_v1_set_size(window->layer_surface, 0, window->height);
zwlr_layer_surface_v1_set_size(window->layer_surface, window->width, window->height);
wl_surface_commit(window->surface);
wl_display_roundtrip(display);
destroy_buffer(buffer);
@ -304,6 +304,20 @@ bm_wl_window_set_bottom(struct window *window, struct wl_display *display, bool
wl_display_roundtrip(display);
}
void
bm_wl_window_set_center(struct window *window, struct wl_display *display, bool center)
{
if (window->center == center)
return;
window->center = center;
zwlr_layer_surface_v1_set_anchor(window->layer_surface, ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
zwlr_layer_surface_v1_set_size(window->layer_surface, 3 * window->width / 4, window->height);
wl_surface_commit(window->surface);
wl_display_roundtrip(display);
}
void
bm_wl_window_grab_keyboard(struct window *window, struct wl_display *display, bool grab)
{