1
0
mirror of https://github.com/Cloudef/bemenu synced 2024-11-23 09:21:59 +01:00

support hiding filter input (#150)

* support hiding filter input

"password mode"

Signed-off-by: Robert Günzler <r@gnzler.io>
This commit is contained in:
Robert Günzler 2021-02-05 04:12:48 +00:00 committed by GitHub
parent 1dfa72b8bc
commit 52547807b0
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 3 deletions

@ -174,6 +174,7 @@ usage(FILE *out, const char *name)
" -p, --prompt defines the prompt text to be displayed.\n"
" -P, --prefix text to show before highlighted item.\n"
" -I, --index select item at index automatically.\n"
" -x, --password hide input.\n"
" --scrollbar display scrollbar. (none (default), always, autohide)\n"
" --ifne only display menu if there are items.\n"
" --fork always fork. (bemenu-run)\n"
@ -228,6 +229,7 @@ do_getopt(struct client *client, int *argc, char **argv[])
{ "prompt", required_argument, 0, 'p' },
{ "index", required_argument, 0, 'I' },
{ "prefix", required_argument, 0, 'P' },
{ "password", no_argument, 0, 'x' },
{ "scrollbar", required_argument, 0, 0x100 },
{ "ifne", no_argument, 0, 0x115 },
{ "fork", no_argument, 0, 0x116 },
@ -262,7 +264,7 @@ do_getopt(struct client *client, int *argc, char **argv[])
for (optind = 0;;) {
int32_t opt;
if ((opt = getopt_long(*argc, *argv, "hviwl:I:p:P:I:bfm:H:n", opts, NULL)) < 0)
if ((opt = getopt_long(*argc, *argv, "hviwxl:I:p:P:I:bfm:H:n", opts, NULL)) < 0)
break;
switch (opt) {
@ -306,6 +308,9 @@ do_getopt(struct client *client, int *argc, char **argv[])
case 0x117:
client->no_exec = true;
break;
case 'x':
client->password = true;
break;
case 'b':
client->bottom = true;
@ -410,6 +415,7 @@ menu_with_options(struct client *client)
bm_menu_set_monitor(menu, client->monitor);
bm_menu_set_scrollbar(menu, client->scrollbar);
bm_menu_set_panel_overlap(menu, !client->no_overlap);
bm_menu_set_password(menu, client->password);
for (uint32_t i = 0; i < BM_COLOR_LAST; ++i)
bm_menu_set_color(menu, i, client->colors[i]);

@ -23,6 +23,7 @@ struct client {
bool no_overlap;
bool force_fork, fork;
bool no_exec;
bool password;
};
char* cstrcopy(const char *str, size_t size);

@ -540,6 +540,23 @@ BM_PUBLIC bool bm_menu_is_keyboard_grabbed(struct bm_menu *menu);
*/
BM_PUBLIC void bm_menu_set_panel_overlap(struct bm_menu *menu, bool overlap);
/**
* Replace input with asterisks.
*
* @param menu bm_menu instance to set password mode for.
* @param password true for password mode, false for deafault behavior.
*/
BM_PUBLIC void bm_menu_set_password(struct bm_menu *menu, bool password);
/**
* Is password mode activated and input hidden?
*
* @param menu bm_menu instance where to get password mode from.
* @return true if password mode, false otherwise.
*/
BM_PUBLIC bool bm_menu_get_password(struct bm_menu *menu);
/** @} Properties */
/**

@ -315,6 +315,11 @@ struct bm_menu {
* Should the menu overlap panels
*/
bool overlap;
/**
* Should the input be hidden
*/
bool password;
};
/* library.c */

@ -414,6 +414,24 @@ bm_menu_set_panel_overlap(struct bm_menu *menu, bool overlap)
menu->renderer->api.set_overlap(menu, overlap);
}
void
bm_menu_set_password(struct bm_menu *menu, bool password)
{
assert(menu);
if (menu->password == password)
return;
menu->password = password;
}
bool
bm_menu_get_password(struct bm_menu *menu)
{
assert(menu);
return menu->password;
}
bool
bm_menu_add_item_at(struct bm_menu *menu, struct bm_item *item, uint32_t index)
{

@ -247,7 +247,14 @@ bm_cairo_paint(struct cairo *cairo, uint32_t width, uint32_t max_height, const s
paint.cursor = menu->cursor;
paint.pos = (struct pos){ (menu->title ? 2 : 0) + result.x_advance, vpadding };
paint.box = (struct box){ (menu->title ? 2 : 4), 0, vpadding, vpadding, width - paint.pos.x, ascii_height };
bm_cairo_draw_line(cairo, &paint, &result, "%s", (menu->filter ? menu->filter : ""));
const char *filter_text = (menu->filter ? menu->filter : "");
if (menu->password) {
bm_cairo_draw_line(cairo, &paint, &result, "");
} else {
bm_cairo_draw_line(cairo, &paint, &result, "%s", filter_text);
}
paint.draw_cursor = false;
const uint32_t titleh = result.height;
out_result->height = titleh;

@ -221,7 +221,12 @@ render(const struct bm_menu *menu)
doffset -= (prev ? prev : 1);
}
draw_line(0, 0, "%*s%s", title_len, "", (menu->filter ? menu->filter + doffset : ""));
const char *filter_text = (menu->filter ? menu->filter + doffset : "");
if (menu->password) {
draw_line(0, 0, "%*s%s", title_len, "");
} else {
draw_line(0, 0, "%*s%s", title_len, "", filter_text);
}
if (menu->title && title_len > 0) {
attron(COLOR_PAIR(1));