Allow switching between modi more easily

This commit is contained in:
Qball Cow 2014-01-16 09:21:48 +01:00
parent 408d317ae2
commit c6e6faf08e
4 changed files with 54 additions and 15 deletions

View File

@ -34,6 +34,10 @@ e.g.
bindsym $mod+Tab exec simpleswitcher -now -font "snap-10" -fg "#505050" -bg "#000000" -hlfg "#ffb964" -hlbg "#000000" -o 85 bindsym $mod+Tab exec simpleswitcher -now -font "snap-10" -fg "#505050" -bg "#000000" -hlfg "#ffb964" -hlbg "#000000" -o 85
Switching Between Modi
----------------------
Type '?' (enter) to switch from Window list mode to Run mode and back.
Compilation Compilation
----------- -----------

View File

@ -136,6 +136,8 @@ Set the width of the menu as a percentage of the screen width (default: 60).
.RS .RS
simpleswitcher -width 60 simpleswitcher -width 60
.RE .RE
.SH Switch between modi
Type '?'<enter> to switch between window list and run mode.
.SH SEE ALSO .SH SEE ALSO
.BR simpleswitcher (1) .BR simpleswitcher (1)
.SH AUTHOR .SH AUTHOR

View File

@ -796,7 +796,7 @@ static int calculate_common_prefix( char **filtered, int max_lines )
int menu( char **lines, char **input, char *prompt, int selected, Time *time, int *shift ) int menu( char **lines, char **input, char *prompt, int selected, Time *time, int *shift )
{ {
int line = -1, i, j, chosen = 0, aborted = 0; int line = -1, i, j, chosen = 0;
workarea mon; workarea mon;
monitor_active( &mon ); monitor_active( &mon );
@ -840,7 +840,8 @@ int menu( char **lines, char **input, char *prompt, int selected, Time *time, in
// search text input // search text input
textbox *text = textbox_create( box, TB_AUTOHEIGHT|TB_EDITABLE, INNER_MARGIN, INNER_MARGIN, textbox *text = textbox_create( box, TB_AUTOHEIGHT|TB_EDITABLE, INNER_MARGIN, INNER_MARGIN,
w-( 2*INNER_MARGIN ), 1, w-( 2*INNER_MARGIN ), 1,
config_menu_font, config_menu_fg, config_menu_bg, "", prompt ); config_menu_font, config_menu_fg, config_menu_bg,
(input!= NULL)?*input:"", prompt );
textbox_show( text ); textbox_show( text );
int line_height = text->font->ascent + text->font->descent; int line_height = text->font->ascent + text->font->descent;
@ -865,11 +866,33 @@ int menu( char **lines, char **input, char *prompt, int selected, Time *time, in
int jin = 0; int jin = 0;
for ( i = 0; i < max_lines; i++ ) { if(input && *input) {
filtered[jin] = lines[i]; char **tokens = tokenize( *input );
line_map[jin] = i;
jin++; // input changed
filtered_lines++; for ( i = 0, j = 0; i < num_lines && j < max_lines; i++ ) {
int match = 1;
// Do a tokenized match.
if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) {
match = ( strcasestr( lines[i], tokens[j] ) != NULL );
}
// If each token was matched, add it to list.
if ( match ) {
line_map[j] = i;
filtered[j++] = lines[i];
filtered_lines++;
}
}
tokenize_free(tokens);
}else{
for ( i = 0; i < max_lines; i++ ) {
filtered[jin] = lines[i];
line_map[jin] = i;
jin++;
filtered_lines++;
}
} }
// resize window vertically to suit // resize window vertically to suit
@ -895,6 +918,12 @@ int menu( char **lines, char **input, char *prompt, int selected, Time *time, in
if ( time ) if ( time )
*time = ev.xkey.time; *time = ev.xkey.time;
KeySym key = XkbKeycodeToKeysym( display, ev.xkey.keycode, 0, 0 );
if(((ev.xkey.state&ShiftMask) == ShiftMask) &&
key == XK_slash ) {
line = -2;
break;
}
int rc = textbox_keypress( text, &ev ); int rc = textbox_keypress( text, &ev );
if ( rc < 0 ) { if ( rc < 0 ) {
@ -944,7 +973,6 @@ int menu( char **lines, char **input, char *prompt, int selected, Time *time, in
|| ( ( windows_modmask == AnyModifier || ev.xkey.state & windows_modmask ) && key == windows_keysym ) || ( ( windows_modmask == AnyModifier || ev.xkey.state & windows_modmask ) && key == windows_keysym )
|| ( ( rundialog_modmask == AnyModifier || ev.xkey.state & rundialog_modmask ) && key == rundialog_keysym ) || ( ( rundialog_modmask == AnyModifier || ev.xkey.state & rundialog_modmask ) && key == rundialog_keysym )
) { ) {
aborted = 1;
break; break;
} }
@ -986,8 +1014,12 @@ int menu( char **lines, char **input, char *prompt, int selected, Time *time, in
if ( chosen && filtered[selected] ) if ( chosen && filtered[selected] )
line = line_map[selected]; line = line_map[selected];
if ( line < 0 && !aborted && input ) if ( line < 0 && input )
{
if(*input != NULL) free(*input);
*input = strdup( text->text ); *input = strdup( text->text );
}
textbox_free( text ); textbox_free( text );
@ -1087,6 +1119,7 @@ void run_switcher( int fmode )
XSync( display, True ); XSync( display, True );
} }
char *input = NULL;
do { do {
if ( mode == WINDOW_SWITCHER ) { if ( mode == WINDOW_SWITCHER ) {
@ -1149,11 +1182,10 @@ void run_switcher( int fmode )
list[lines++] = line; list[lines++] = line;
} }
} }
char *input = NULL;
Time time; Time time;
int n = menu( list, &input, "> ", 1, &time, NULL ); int n = menu( list, &input, "> ", 0, &time, NULL );
if ( input != NULL && input[0] == '!' ) { if ( n == -2 ) {
mode = RUN_DIALOG; mode = RUN_DIALOG;
} else if ( n >= 0 && list[n] ) { } else if ( n >= 0 && list[n] ) {
// Normally we want to exit. // Normally we want to exit.
@ -1187,7 +1219,6 @@ void run_switcher( int fmode )
winlist_free( ids ); winlist_free( ids );
} else if ( mode == RUN_DIALOG ) { } else if ( mode == RUN_DIALOG ) {
Time time; Time time;
char *input = NULL;
mode = MODE_EXIT; mode = MODE_EXIT;
// act as a launcher // act as a launcher
@ -1202,7 +1233,7 @@ void run_switcher( int fmode )
int shift; int shift;
int n = menu( cmd_list, &input, "$ ", 0, &time, &shift ); int n = menu( cmd_list, &input, "$ ", 0, &time, &shift );
if ( input != NULL && *input == '>' ) { if ( n == -2 ) {
mode = WINDOW_SWITCHER; mode = WINDOW_SWITCHER;
} else if ( n >=0 && cmd_list[n] != NULL ) { } else if ( n >=0 && cmd_list[n] != NULL ) {
@ -1216,7 +1247,7 @@ void run_switcher( int fmode )
free( cmd_list ); free( cmd_list );
} }
} while ( mode != MODE_EXIT ); } while ( mode != MODE_EXIT );
if(input != NULL) free(input);
if ( fmode == FORK ) if ( fmode == FORK )
exit( EXIT_SUCCESS ); exit( EXIT_SUCCESS );
} }

View File

@ -49,6 +49,7 @@ typedef struct {
void textbox_font( textbox *tb, char *font, char *fg, char *bg ); void textbox_font( textbox *tb, char *font, char *fg, char *bg );
void textbox_text( textbox *tb, char *text ); void textbox_text( textbox *tb, char *text );
void textbox_moveresize( textbox *tb, int x, int y, int w, int h ); void textbox_moveresize( textbox *tb, int x, int y, int w, int h );
void textbox_cursor_end( textbox *tb );
// Xft text box, optionally editable // Xft text box, optionally editable
textbox* textbox_create( Window parent, unsigned long flags, short x, short y, short w, short h, char *font, char *fg, char *bg, char *text, char *prompt ) textbox* textbox_create( Window parent, unsigned long flags, short x, short y, short w, short h, char *font, char *fg, char *bg, char *text, char *prompt )
@ -74,6 +75,7 @@ textbox* textbox_create( Window parent, unsigned long flags, short x, short y, s
tb->prompt = strdup( prompt ? prompt: "" ); tb->prompt = strdup( prompt ? prompt: "" );
textbox_text( tb, text ? text: "" ); textbox_text( tb, text ? text: "" );
textbox_cursor_end(tb);
// auto height/width modes get handled here // auto height/width modes get handled here
textbox_moveresize( tb, tb->x, tb->y, tb->w, tb->h ); textbox_moveresize( tb, tb->x, tb->y, tb->w, tb->h );