mirror of
https://github.com/Cloudef/bemenu
synced 2025-03-25 04:38:35 +00:00
created margin option
-M or --margin option sets the horizontal margin of the window
This commit is contained in:
parent
d31164db75
commit
bddeea05b6
@ -198,6 +198,7 @@ usage(FILE *out, const char *name)
|
||||
" -n, --no-overlap adjust geometry to not overlap with panels. (w)\n"
|
||||
" -m, --monitor index of monitor where menu will appear. (wx)\n"
|
||||
" -H, --line-height defines the height to make each menu line (0 = default height). (wx)\n"
|
||||
" -M, --margin defines the empty space on either side of the menu. (wx)"
|
||||
" --ch defines the height of the cursor (0 = scales with line height). (wx)\n"
|
||||
" --fn defines the font to be used ('name [size]'). (wx)\n"
|
||||
" --tb defines the title background color. (wx)\n"
|
||||
@ -263,6 +264,7 @@ do_getopt(struct client *client, int *argc, char **argv[])
|
||||
{ "no-spacing", no_argument, 0, 's' },
|
||||
{ "monitor", required_argument, 0, 'm' },
|
||||
{ "line-height", required_argument, 0, 'H' },
|
||||
{ "margin", required_argument, 0, 'M' },
|
||||
{ "ch", required_argument, 0, 0x118 },
|
||||
{ "fn", required_argument, 0, 0x101 },
|
||||
{ "tb", required_argument, 0, 0x102 },
|
||||
@ -292,7 +294,7 @@ do_getopt(struct client *client, int *argc, char **argv[])
|
||||
for (optind = 0;;) {
|
||||
int32_t opt;
|
||||
|
||||
if ((opt = getopt_long(*argc, *argv, "hviwxcl:I:p:P:I:bfm:H:ns", opts, NULL)) < 0)
|
||||
if ((opt = getopt_long(*argc, *argv, "hviwxcl:I:p:P:I:bfm:H:M:ns", opts, NULL)) < 0)
|
||||
break;
|
||||
|
||||
switch (opt) {
|
||||
@ -362,6 +364,9 @@ do_getopt(struct client *client, int *argc, char **argv[])
|
||||
case 'H':
|
||||
client->line_height = strtol(optarg, NULL, 10);
|
||||
break;
|
||||
case 'M':
|
||||
client->hmargin_size = strtol(optarg, NULL, 10);
|
||||
break;
|
||||
case 0x118:
|
||||
client->cursor_height = strtol(optarg, NULL, 10);
|
||||
break;
|
||||
@ -455,6 +460,7 @@ menu_with_options(struct client *client)
|
||||
bm_menu_set_panel_overlap(menu, !client->no_overlap);
|
||||
bm_menu_set_spacing(menu, !client->no_spacing);
|
||||
bm_menu_set_password(menu, client->password);
|
||||
bm_menu_set_hmargin_size(menu, client->hmargin_size);
|
||||
|
||||
if (client->center) {
|
||||
bm_menu_set_center(menu, client->center);
|
||||
|
@ -17,6 +17,7 @@ struct client {
|
||||
uint32_t lines;
|
||||
uint32_t selected;
|
||||
uint32_t monitor;
|
||||
uint32_t hmargin_size;
|
||||
bool bottom;
|
||||
bool center;
|
||||
bool grab;
|
||||
|
18
lib/bemenu.h
18
lib/bemenu.h
@ -526,6 +526,24 @@ BM_PUBLIC void bm_menu_set_bottom(struct bm_menu *menu, bool bottom);
|
||||
*/
|
||||
BM_PUBLIC bool bm_menu_get_bottom(struct bm_menu *menu);
|
||||
|
||||
/**
|
||||
* Set the horizontal margin of the bar.
|
||||
*
|
||||
* @param menu bm_menu to set horizontal margin for.
|
||||
* @param margin margin to set.
|
||||
*/
|
||||
|
||||
BM_PUBLIC void bm_menu_set_hmargin_size(struct bm_menu *menu, uint32_t margin);
|
||||
|
||||
/**
|
||||
* Get the horizontal margin of the bar.
|
||||
*
|
||||
* @param menu bm_menu to get horizontal margin from.
|
||||
* @return horizontal margin of the menu
|
||||
*/
|
||||
|
||||
BM_PUBLIC uint32_t bm_menu_get_hmargin_size(struct bm_menu *menu);
|
||||
|
||||
/**
|
||||
* Display menu at monitor index.
|
||||
* Indices start at 0, a value of -1 can be passed for the active monitor (default).
|
||||
|
@ -18,6 +18,9 @@ extern char *secure_getenv(const char *name);
|
||||
#include <stddef.h> /* for size_t */
|
||||
#include <stdarg.h>
|
||||
|
||||
//minimum allowed window width when setting margin
|
||||
#define WINDOW_MIN_WIDTH 80
|
||||
|
||||
/**
|
||||
* Destructor function pointer for some list calls.
|
||||
*/
|
||||
@ -79,6 +82,11 @@ struct render_api {
|
||||
*/
|
||||
void (*set_bottom)(const struct bm_menu *menu, bool bottom);
|
||||
|
||||
/**
|
||||
* Set horizontal margin.
|
||||
*/
|
||||
void (*set_hmargin_size)(const struct bm_menu *menu, uint32_t margin);
|
||||
|
||||
/**
|
||||
* Set menu to appear from center of the screen.
|
||||
*/
|
||||
@ -331,6 +339,11 @@ struct bm_menu {
|
||||
*/
|
||||
bool bottom;
|
||||
|
||||
/**
|
||||
* Horizontal margin.
|
||||
*/
|
||||
uint32_t hmargin_size;
|
||||
|
||||
/**
|
||||
* Is menu grabbed?
|
||||
*/
|
||||
|
21
lib/menu.c
21
lib/menu.c
@ -389,6 +389,27 @@ bm_menu_get_bottom(struct bm_menu *menu)
|
||||
return menu->bottom;
|
||||
}
|
||||
|
||||
void
|
||||
bm_menu_set_hmargin_size(struct bm_menu *menu, uint32_t margin)
|
||||
{
|
||||
assert(menu);
|
||||
|
||||
if(menu->hmargin_size == margin)
|
||||
return;
|
||||
|
||||
menu->hmargin_size = margin;
|
||||
|
||||
if(menu->renderer->api.set_hmargin_size)
|
||||
menu->renderer->api.set_hmargin_size(menu, margin);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
bm_menu_get_hmargin_size(struct bm_menu *menu)
|
||||
{
|
||||
assert(menu);
|
||||
return menu->hmargin_size;
|
||||
}
|
||||
|
||||
void
|
||||
bm_menu_set_monitor(struct bm_menu *menu, int32_t monitor)
|
||||
{
|
||||
|
@ -240,6 +240,18 @@ set_bottom(const struct bm_menu *menu, bool bottom)
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
set_hmargin_size(const struct bm_menu *menu, uint32_t margin)
|
||||
{
|
||||
struct wayland *wayland = menu->renderer->internal;
|
||||
assert(wayland);
|
||||
|
||||
struct window *window;
|
||||
wl_list_for_each(window, &wayland->windows, link) {
|
||||
bm_wl_window_set_hmargin_size(window, wayland->display, margin);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
set_center(const struct bm_menu *menu, bool center)
|
||||
{
|
||||
@ -318,6 +330,7 @@ recreate_windows(const struct bm_menu *menu, struct wayland *wayland)
|
||||
|
||||
struct window *window = calloc(1, sizeof(struct window));
|
||||
window->bottom = menu->bottom;
|
||||
window->hmargin_size = menu->hmargin_size;
|
||||
|
||||
const char *scale = getenv("BEMENU_SCALE");
|
||||
if (scale) {
|
||||
@ -446,6 +459,7 @@ register_renderer(struct render_api *api)
|
||||
api->render = render;
|
||||
api->set_center = set_center;
|
||||
api->set_bottom = set_bottom;
|
||||
api->set_hmargin_size = set_hmargin_size;
|
||||
api->grab_keyboard = grab_keyboard;
|
||||
api->set_overlap = set_overlap;
|
||||
api->set_monitor = set_monitor;
|
||||
|
@ -84,6 +84,7 @@ struct window {
|
||||
struct wl_shm *shm;
|
||||
struct buffer buffers[2];
|
||||
uint32_t width, height, max_height;
|
||||
uint32_t hmargin_size;
|
||||
int32_t scale;
|
||||
uint32_t displayed;
|
||||
struct wl_list link;
|
||||
@ -130,6 +131,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_hmargin_size(struct window *window, struct wl_display *display, uint32_t margin);
|
||||
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);
|
||||
|
@ -286,6 +286,17 @@ layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *layer_surface)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static uint32_t
|
||||
get_window_width(struct window *window)
|
||||
{
|
||||
uint32_t width = window->width - 2 * window->hmargin_size;
|
||||
|
||||
if(width < WINDOW_MIN_WIDTH || 2 * window->hmargin_size > window->width)
|
||||
width = WINDOW_MIN_WIDTH;
|
||||
|
||||
return width;
|
||||
}
|
||||
|
||||
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
|
||||
.configure = layer_surface_configure,
|
||||
.closed = layer_surface_closed,
|
||||
@ -304,6 +315,21 @@ bm_wl_window_set_bottom(struct window *window, struct wl_display *display, bool
|
||||
wl_display_roundtrip(display);
|
||||
}
|
||||
|
||||
void
|
||||
bm_wl_window_set_hmargin_size(struct window *window, struct wl_display *display, uint32_t margin)
|
||||
{
|
||||
if(window->hmargin_size == margin)
|
||||
return;
|
||||
|
||||
window->hmargin_size = margin;
|
||||
|
||||
zwlr_layer_surface_v1_set_anchor(window->layer_surface, (window->bottom ? 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, get_window_width(window), window->height);
|
||||
|
||||
wl_surface_commit(window->surface);
|
||||
wl_display_roundtrip(display);
|
||||
}
|
||||
|
||||
void
|
||||
bm_wl_window_set_center(struct window *window, struct wl_display *display, bool center)
|
||||
{
|
||||
@ -313,7 +339,6 @@ bm_wl_window_set_center(struct window *window, struct wl_display *display, bool
|
||||
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);
|
||||
}
|
||||
@ -343,8 +368,11 @@ bm_wl_window_create(struct window *window, struct wl_display *display, struct wl
|
||||
zwlr_layer_surface_v1_add_listener(window->layer_surface, &layer_surface_listener, window);
|
||||
zwlr_layer_surface_v1_set_anchor(window->layer_surface, (window->bottom ? 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, 0, 32);
|
||||
|
||||
wl_surface_commit(surface);
|
||||
wl_display_roundtrip(display);
|
||||
|
||||
zwlr_layer_surface_v1_set_size(window->layer_surface, get_window_width(window), 32);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include "internal.h"
|
||||
#include "x11.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
@ -62,6 +63,17 @@ next_buffer(struct window *window)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static uint32_t
|
||||
get_window_width(struct window *window)
|
||||
{
|
||||
uint32_t width = window->width - 2 * window->hmargin_size;
|
||||
|
||||
if(width < WINDOW_MIN_WIDTH || 2 * window->hmargin_size > window->width)
|
||||
width = WINDOW_MIN_WIDTH;
|
||||
|
||||
return width;
|
||||
}
|
||||
|
||||
void
|
||||
bm_x11_window_render(struct window *window, const struct bm_menu *menu)
|
||||
{
|
||||
@ -95,7 +107,7 @@ bm_x11_window_render(struct window *window, const struct bm_menu *menu)
|
||||
if (window->bottom) {
|
||||
XMoveResizeWindow(window->display, window->drawable, window->x, window->max_height - window->height, window->width, window->height);
|
||||
} else {
|
||||
XResizeWindow(window->display, window->drawable, window->width, window->height);
|
||||
XMoveResizeWindow(window->display, window->drawable, window->x, 0, window->width, window->height);
|
||||
}
|
||||
}
|
||||
|
||||
@ -190,6 +202,11 @@ bm_x11_window_set_monitor(struct window *window, int32_t monitor)
|
||||
window->width = DisplayWidth(window->display, window->screen);
|
||||
}
|
||||
|
||||
window->orig_width = window->width;
|
||||
window->orig_x = window->x;
|
||||
window->width = get_window_width(window);
|
||||
window->x += (window->orig_width - window->width) / 2;
|
||||
|
||||
#undef INTERSECT
|
||||
}
|
||||
|
||||
@ -208,6 +225,20 @@ bm_x11_window_set_bottom(struct window *window, bool bottom)
|
||||
bm_x11_window_set_monitor(window, window->monitor);
|
||||
}
|
||||
|
||||
void
|
||||
bm_x11_window_set_hmargin_size(struct window *window, uint32_t margin)
|
||||
{
|
||||
if(window->hmargin_size == margin)
|
||||
return;
|
||||
|
||||
window->hmargin_size = margin;
|
||||
window->width = window->orig_width;
|
||||
window->x = window->orig_x;
|
||||
window->width = get_window_width(window);
|
||||
window->x += (window->orig_width - window->width) / 2;
|
||||
bm_x11_window_set_monitor(window, window->monitor);
|
||||
}
|
||||
|
||||
bool
|
||||
bm_x11_window_create(struct window *window, Display *display)
|
||||
{
|
||||
|
@ -206,6 +206,14 @@ set_bottom(const struct bm_menu *menu, bool bottom)
|
||||
bm_x11_window_set_bottom(&x11->window, bottom);
|
||||
}
|
||||
|
||||
static void
|
||||
set_hmargin_size(const struct bm_menu *menu, uint32_t margin)
|
||||
{
|
||||
struct x11 *x11 = menu->renderer->internal;
|
||||
assert(x11);
|
||||
bm_x11_window_set_hmargin_size(&x11->window, margin);
|
||||
}
|
||||
|
||||
static void
|
||||
set_monitor(const struct bm_menu *menu, int32_t monitor)
|
||||
{
|
||||
@ -288,6 +296,7 @@ register_renderer(struct render_api *api)
|
||||
api->poll_key = poll_key;
|
||||
api->render = render;
|
||||
api->set_bottom = set_bottom;
|
||||
api->set_hmargin_size = set_hmargin_size;
|
||||
api->set_monitor = set_monitor;
|
||||
api->grab_keyboard = grab_keyboard;
|
||||
api->priorty = BM_PRIO_GUI;
|
||||
|
@ -31,6 +31,8 @@ struct window {
|
||||
|
||||
struct buffer buffer;
|
||||
uint32_t x, y, width, height, max_height;
|
||||
uint32_t orig_width, orig_x;
|
||||
uint32_t hmargin_size;
|
||||
uint32_t displayed;
|
||||
|
||||
int32_t monitor;
|
||||
@ -50,6 +52,7 @@ void bm_x11_window_render(struct window *window, const struct bm_menu *menu);
|
||||
void bm_x11_window_key_press(struct window *window, XKeyEvent *ev);
|
||||
void bm_x11_window_set_monitor(struct window *window, int32_t monitor);
|
||||
void bm_x11_window_set_bottom(struct window *window, bool bottom);
|
||||
void bm_x11_window_set_hmargin_size(struct window *window, uint32_t margin);
|
||||
bool bm_x11_window_create(struct window *window, Display *display);
|
||||
void bm_x11_window_destroy(struct window *window);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user