mirror of
https://github.com/davatorium/rofi.git
synced 2024-11-25 13:55:34 -05:00
Fix and optimize 20200908 (#1184)
* combi: Reuse own get_num_entries in self-init * combi_mode_parse_switchers: Remove duplicated mode pointer * combi_mode_parse_switchers: Regroup mode search lines * combi_mode_result: Simplify switcher search quit * mode_result: Check for mode switch actions before hook These are over-mode actions, which should not be handled in mode hooks at all.
This commit is contained in:
parent
88ac249d36
commit
96cd34d24f
8 changed files with 43 additions and 110 deletions
|
@ -76,27 +76,37 @@ static void combi_mode_parse_switchers ( Mode *sw )
|
|||
sizeof ( CombiMode ) * ( pd->num_switchers + 1 ) );
|
||||
|
||||
Mode *mode = rofi_collect_modi_search ( token );
|
||||
if ( mode ) {
|
||||
if ( mode != NULL ) {
|
||||
pd->switchers[pd->num_switchers].disable = FALSE;
|
||||
pd->switchers[pd->num_switchers++].mode = mode;
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
// If not build in, use custom switchers.
|
||||
Mode *sw = script_switcher_parse_setup ( token );
|
||||
if ( sw != NULL ) {
|
||||
pd->switchers[pd->num_switchers].disable = FALSE;
|
||||
pd->switchers[pd->num_switchers++].mode = sw;
|
||||
}
|
||||
else {
|
||||
// Report error, don't continue.
|
||||
g_warning ( "Invalid script switcher: %s", token );
|
||||
token = NULL;
|
||||
}
|
||||
// If not build in, use custom switchers.
|
||||
mode = script_switcher_parse_setup ( token );
|
||||
if ( mode != NULL ) {
|
||||
pd->switchers[pd->num_switchers].disable = FALSE;
|
||||
pd->switchers[pd->num_switchers++].mode = mode;
|
||||
continue;
|
||||
}
|
||||
// Report error, don't continue.
|
||||
g_warning ( "Invalid script switcher: %s", token );
|
||||
token = NULL;
|
||||
}
|
||||
// Free string that was modified by strtok_r
|
||||
g_free ( switcher_str );
|
||||
}
|
||||
static unsigned int combi_mode_get_num_entries ( const Mode *sw )
|
||||
{
|
||||
const CombiModePrivateData *pd = (const CombiModePrivateData *) mode_get_private_data ( sw );
|
||||
unsigned int length = 0;
|
||||
for ( unsigned int i = 0; i < pd->num_switchers; i++ ) {
|
||||
unsigned int entries = mode_get_num_entries ( pd->switchers[i].mode );
|
||||
pd->starts[i] = length;
|
||||
pd->lengths[i] = entries;
|
||||
length += entries;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
static int combi_mode_init ( Mode *sw )
|
||||
{
|
||||
|
@ -112,29 +122,11 @@ static int combi_mode_init ( Mode *sw )
|
|||
}
|
||||
}
|
||||
if ( pd->cmd_list_length == 0 ) {
|
||||
pd->cmd_list_length = 0;
|
||||
for ( unsigned int i = 0; i < pd->num_switchers; i++ ) {
|
||||
unsigned int length = mode_get_num_entries ( pd->switchers[i].mode );
|
||||
pd->starts[i] = pd->cmd_list_length;
|
||||
pd->lengths[i] = length;
|
||||
pd->cmd_list_length += length;
|
||||
}
|
||||
pd->cmd_list_length = combi_mode_get_num_entries ( sw );
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
static unsigned int combi_mode_get_num_entries ( const Mode *sw )
|
||||
{
|
||||
const CombiModePrivateData *pd = (const CombiModePrivateData *) mode_get_private_data ( sw );
|
||||
unsigned int length = 0;
|
||||
for ( unsigned int i = 0; i < pd->num_switchers; i++ ) {
|
||||
unsigned int entries = mode_get_num_entries ( pd->switchers[i].mode );
|
||||
pd->starts[i] = length;
|
||||
pd->lengths[i] = entries;
|
||||
length += entries;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
static void combi_mode_destroy ( Mode *sw )
|
||||
{
|
||||
CombiModePrivateData *pd = (CombiModePrivateData *) mode_get_private_data ( sw );
|
||||
|
@ -163,11 +155,12 @@ static ModeMode combi_mode_result ( Mode *sw, int mretv, char **input, unsigned
|
|||
}
|
||||
ssize_t bang_len = g_utf8_pointer_to_offset ( input[0], eob ) - 1;
|
||||
if ( bang_len > 0 ) {
|
||||
for ( unsigned i = 0; switcher == -1 && i < pd->num_switchers; i++ ) {
|
||||
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
||||
const char *mode_name = mode_get_name ( pd->switchers[i].mode );
|
||||
size_t mode_name_len = g_utf8_strlen ( mode_name, -1 );
|
||||
if ( (size_t) bang_len <= mode_name_len && utf8_strncmp ( &input[0][1], mode_name, bang_len ) == 0 ) {
|
||||
switcher = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -180,9 +173,6 @@ static ModeMode combi_mode_result ( Mode *sw, int mretv, char **input, unsigned
|
|||
return MODE_EXIT;
|
||||
}
|
||||
}
|
||||
if ( mretv & MENU_QUICK_SWITCH ) {
|
||||
return mretv & MENU_LOWER_MASK;
|
||||
}
|
||||
|
||||
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
||||
if ( selected_line >= pd->starts[i] &&
|
||||
|
|
|
@ -1012,16 +1012,7 @@ static ModeMode drun_mode_result ( Mode *sw, int mretv, char **input, unsigned i
|
|||
DRunModePrivateData *rmpd = (DRunModePrivateData *) mode_get_private_data ( sw );
|
||||
ModeMode retv = MODE_EXIT;
|
||||
|
||||
if ( mretv & MENU_NEXT ) {
|
||||
retv = NEXT_DIALOG;
|
||||
}
|
||||
else if ( mretv & MENU_PREVIOUS ) {
|
||||
retv = PREVIOUS_DIALOG;
|
||||
}
|
||||
else if ( mretv & MENU_QUICK_SWITCH ) {
|
||||
retv = ( mretv & MENU_LOWER_MASK );
|
||||
}
|
||||
else if ( ( mretv & MENU_OK ) ) {
|
||||
if ( ( mretv & MENU_OK ) ) {
|
||||
switch ( rmpd->entry_list[selected_line].type )
|
||||
{
|
||||
case DRUN_DESKTOP_ENTRY_TYPE_APPLICATION:
|
||||
|
|
|
@ -71,18 +71,7 @@ static ModeMode help_keys_mode_result ( G_GNUC_UNUSED Mode *sw,
|
|||
G_GNUC_UNUSED char **input,
|
||||
G_GNUC_UNUSED unsigned int selected_line )
|
||||
{
|
||||
ModeMode retv = MODE_EXIT;
|
||||
|
||||
if ( mretv & MENU_NEXT ) {
|
||||
retv = NEXT_DIALOG;
|
||||
}
|
||||
else if ( mretv & MENU_PREVIOUS ) {
|
||||
retv = PREVIOUS_DIALOG;
|
||||
}
|
||||
else if ( mretv & MENU_QUICK_SWITCH ) {
|
||||
retv = ( mretv & MENU_LOWER_MASK );
|
||||
}
|
||||
return retv;
|
||||
return MODE_EXIT;
|
||||
}
|
||||
static void help_keys_mode_destroy ( Mode *sw )
|
||||
{
|
||||
|
|
|
@ -370,16 +370,7 @@ static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned in
|
|||
|
||||
gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION );
|
||||
|
||||
if ( mretv & MENU_NEXT ) {
|
||||
retv = NEXT_DIALOG;
|
||||
}
|
||||
else if ( mretv & MENU_PREVIOUS ) {
|
||||
retv = PREVIOUS_DIALOG;
|
||||
}
|
||||
else if ( mretv & MENU_QUICK_SWITCH ) {
|
||||
retv = ( mretv & MENU_LOWER_MASK );
|
||||
}
|
||||
else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) {
|
||||
if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) {
|
||||
exec_cmd ( rmpd->cmd_list[selected_line], run_in_term );
|
||||
}
|
||||
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
|
||||
|
|
|
@ -278,28 +278,7 @@ static ModeMode script_mode_result ( Mode *sw, int mretv, char **input, unsigned
|
|||
DmenuScriptEntry *new_list = NULL;
|
||||
unsigned int new_length = 0;
|
||||
|
||||
if ( ( mretv & MENU_NEXT ) ) {
|
||||
retv = NEXT_DIALOG;
|
||||
}
|
||||
else if ( ( mretv & MENU_PREVIOUS ) ) {
|
||||
retv = PREVIOUS_DIALOG;
|
||||
}
|
||||
else if ( ( mretv & MENU_QUICK_SWITCH ) ) {
|
||||
//retv = 1+( mretv & MENU_LOWER_MASK );
|
||||
script_mode_reset_highlight ( sw );
|
||||
if ( selected_line != UINT32_MAX ) {
|
||||
new_list = execute_executor ( sw, rmpd->cmd_list[selected_line].entry, &new_length, 10 + ( mretv & MENU_LOWER_MASK ), &( rmpd->cmd_list[selected_line] ) );
|
||||
}
|
||||
else {
|
||||
if ( rmpd->no_custom == FALSE ) {
|
||||
new_list = execute_executor ( sw, *input, &new_length, 10 + ( mretv & MENU_LOWER_MASK ), NULL );
|
||||
}
|
||||
else {
|
||||
return RELOAD_DIALOG;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line].entry != NULL ) {
|
||||
if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line].entry != NULL ) {
|
||||
if ( rmpd->cmd_list[selected_line].nonselectable ) {
|
||||
return RELOAD_DIALOG;
|
||||
}
|
||||
|
|
|
@ -593,16 +593,7 @@ static ModeMode ssh_mode_result ( Mode *sw, int mretv, char **input, unsigned in
|
|||
{
|
||||
ModeMode retv = MODE_EXIT;
|
||||
SSHModePrivateData *rmpd = (SSHModePrivateData *) mode_get_private_data ( sw );
|
||||
if ( mretv & MENU_NEXT ) {
|
||||
retv = NEXT_DIALOG;
|
||||
}
|
||||
else if ( mretv & MENU_PREVIOUS ) {
|
||||
retv = PREVIOUS_DIALOG;
|
||||
}
|
||||
else if ( mretv & MENU_QUICK_SWITCH ) {
|
||||
retv = ( mretv & MENU_LOWER_MASK );
|
||||
}
|
||||
else if ( ( mretv & MENU_OK ) && rmpd->hosts_list[selected_line].hostname != NULL ) {
|
||||
if ( ( mretv & MENU_OK ) && rmpd->hosts_list[selected_line].hostname != NULL ) {
|
||||
exec_ssh ( &( rmpd->hosts_list[selected_line] ) );
|
||||
}
|
||||
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
|
||||
|
|
|
@ -650,16 +650,7 @@ static ModeMode window_mode_result ( Mode *sw, int mretv, G_GNUC_UNUSED char **i
|
|||
{
|
||||
ModeModePrivateData *rmpd = (ModeModePrivateData *) mode_get_private_data ( sw );
|
||||
ModeMode retv = MODE_EXIT;
|
||||
if ( mretv & MENU_NEXT ) {
|
||||
retv = NEXT_DIALOG;
|
||||
}
|
||||
else if ( mretv & MENU_PREVIOUS ) {
|
||||
retv = PREVIOUS_DIALOG;
|
||||
}
|
||||
else if ( ( mretv & MENU_QUICK_SWITCH ) == MENU_QUICK_SWITCH ) {
|
||||
retv = ( mretv & MENU_LOWER_MASK );
|
||||
}
|
||||
else if ( ( mretv & ( MENU_OK ) ) ) {
|
||||
if ( ( mretv & ( MENU_OK ) ) ) {
|
||||
if ( mretv & MENU_CUSTOM_ACTION ) {
|
||||
act_on_window ( rmpd->ids->array[selected_line] );
|
||||
}
|
||||
|
|
|
@ -96,9 +96,20 @@ char * mode_get_completion ( const Mode *mode, unsigned int selected_line )
|
|||
|
||||
ModeMode mode_result ( Mode *mode, int menu_retv, char **input, unsigned int selected_line )
|
||||
{
|
||||
if ( menu_retv & MENU_NEXT ) {
|
||||
return NEXT_DIALOG;
|
||||
}
|
||||
else if ( menu_retv & MENU_PREVIOUS ) {
|
||||
return PREVIOUS_DIALOG;
|
||||
}
|
||||
else if ( menu_retv & MENU_QUICK_SWITCH ) {
|
||||
return ( menu_retv & MENU_LOWER_MASK );
|
||||
}
|
||||
|
||||
g_assert ( mode != NULL );
|
||||
g_assert ( mode->_result != NULL );
|
||||
g_assert ( input != NULL );
|
||||
|
||||
return mode->_result ( mode, menu_retv, input, selected_line );
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue