Permalink
Browse files

add "high priority" keyboard shortcuts (close #84)

High priority shortcuts can override the default shortcuts of widgets.
Warning: They may break some features or the keyboard navigation
  • Loading branch information...
squentin committed Apr 8, 2015
1 parent 321f009 commit e4efb6f4b065206c5f6b4f48d123a6728e7fee5f
Showing with 26 additions and 14 deletions.
  1. +17 −10 gmusicbrowser.pl
  2. +7 −4 gmusicbrowser_layout.pm
  3. +2 −0 layout_doc.mkd
View
@@ -22,6 +22,7 @@
package main;
use Gtk2 '-init';
use Glib qw/filename_from_unicode filename_to_unicode/;
use Gtk2::Pango; #for PANGO_WEIGHT_BOLD, PANGO_WEIGHT_NORMAL
use POSIX qw/setlocale LC_NUMERIC LC_MESSAGES LC_TIME strftime mktime getcwd _exit/;
use Encode qw/_utf8_on _utf8_off/;
{no warnings 'redefine'; #some work arounds for old versions of perl-Gtk2 and/or gtk2
@@ -1191,11 +1192,11 @@ sub make_keybindingshash
while (@list>1)
{ my $key=shift @list;
my $cmd=shift @list;
my $mod='';
$mod=$1 if $key=~s/^([caws]+-)//;
my @keys=($key);
@keys=(lc$key,uc$key) if $key=~m/^[A-Za-z]$/;
$h{$mod.$_}=$cmd for @keys;
my $priority= $key=~s/^\+//;
my $mod= $key=~s/^([caws]+-)// ? $1 : '';
$key= lc $key;
$h{$mod.$key}=$cmd;
$h{'+'.$mod.$key}=$cmd if $priority;
}
return \%h;
}
@@ -6122,10 +6123,10 @@ sub PrefDialog
sub PrefKeys
{ my $vbox=Gtk2::VBox->new;
my $store=Gtk2::ListStore->new(('Glib::String')x3);
my $store=Gtk2::ListStore->new(('Glib::String')x3,'Glib::Uint');
my $treeview=Gtk2::TreeView->new($store);
$treeview->append_column( Gtk2::TreeViewColumn->new_with_attributes
( _"Key",Gtk2::CellRendererText->new,text => 0
( _"Key",Gtk2::CellRendererText->new,text => 0, weight=> 3,
));
$treeview->append_column( Gtk2::TreeViewColumn->new_with_attributes
( _"Command",Gtk2::CellRendererText->new,text => 1
@@ -6143,8 +6144,9 @@ sub PrefKeys
{ my ($cmd,$arg)= $list->{$key}=~m/^(\w+)(?:\((.*)\))?$/;
$cmd=$Command{$cmd}[1];
$cmd.="($arg)" if defined $arg;
my $weight= $key=~s/^\+// ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL;
my $longkey=keybinding_longname($key);
$store->set($store->append,0,$longkey,1,$cmd,2,$key);
$store->set($store->append, 0,$longkey, 1,$cmd, 2,$key, 3,$weight);
}
%CustomBoundKeys=%{ make_keybindingshash($Options{CustomKeyBindings}) };
};
@@ -6201,6 +6203,9 @@ sub PrefKeys
};
$combo->signal_connect( changed => $combochanged );
my $priority= Gtk2::CheckButton->new(_"High priority");
$priority->set_tooltip_text(_"If checked, the shortcut has higher priority than the default shortcut of widgets. Warning: this can make some features inaccessible");
my $butadd= ::NewIconButton('gtk-add',_"Add shorcut key",sub
{ my $cmd=$combo->get_value;
return unless defined $cmd;
@@ -6210,6 +6215,8 @@ sub PrefKeys
{ my $extra= (ref $child eq 'Gtk2::Entry')? $child->get_text : $child->get_value;
$cmd.="($extra)" if $extra ne '';
}
delete $Options{CustomKeyBindings}{$_} for $key,"+$key";
$key= "+$key" if $priority->get_active;
$Options{CustomKeyBindings}{$key}=$cmd;
&$refresh_sub;
});
@@ -6239,8 +6246,8 @@ sub PrefKeys
$vbox->pack_start(
Vpack([ [ 0, Gtk2::Label->new(_"Key") , $key_entry ],
[ 0, Gtk2::Label->new(_"Command") , $combo ],
[ 0, Gtk2::Label->new(_"Arguments") , $entry_extra ],
],[$butadd,$butrm]
'_',[ 0, Gtk2::Label->new(_"Arguments") , $entry_extra ],
],[$butadd,$butrm,$priority]
),FALSE,FALSE,2);
&$refresh_sub;
&$combochanged;
View
@@ -1072,7 +1072,8 @@ sub CreateWidgets
{ $widgets->{$_}->signal_connect(scroll_event => \&::ChangeVol)
for grep $widgets->{$_}, split /\s+/,$l;
}
$self->signal_connect_after(key_press_event => \&KeyPressed);
$self->signal_connect(key_press_event => \&KeyPressed,0);
$self->signal_connect_after(key_press_event => \&KeyPressed,1);
for my $widget (values %$widgets) { my $postinit= delete $widget->{PostInit}; $postinit->($widget) if $postinit; }
@@ -1345,14 +1346,16 @@ sub ToggleFullscreen
}
sub KeyPressed
{ my ($self,$event)=@_;
{ my ($self,$event,$after)=@_;
my $key=Gtk2::Gdk->keyval_name( $event->keyval );
my $focused=$self->get_focus;
return 0 if !$after && $focused && ($focused->isa('Gtk2::Entry') || $focused->isa('Gtk2::SpinButton'));
my $mod;
$mod.='c' if $event->state >= 'control-mask';
$mod.='a' if $event->state >= 'mod1-mask';
$mod.='w' if $event->state >= 'mod4-mask';
$mod.='s' if $event->state >= 'shift-mask';
$key=$mod.'-'.$key if $mod;
$key= ($after? '':'+') . ($mod? "$mod-":'') . lc($key);
my ($cmd,$arg);
if ( exists $::CustomBoundKeys{$key} )
{ $cmd= $::CustomBoundKeys{$key};
@@ -1363,7 +1366,7 @@ sub KeyPressed
elsif ( exists $::GlobalBoundKeys{$key} )
{ $cmd= $::GlobalBoundKeys{$key};
}
elsif ($self->{fullscreen} && $key eq 'Escape') { $cmd='ToggleFullscreen' }
elsif ($after && $self->{fullscreen} && $key eq 'Escape') { $cmd='ToggleFullscreen' }
return 0 unless $cmd;
if ($self->isa('Gtk2::Window')) #try to find the focused widget (gmb widget, not gtk one), so that the cmd can act on it
{ my $widget=$self->get_focus;
View
@@ -258,6 +258,8 @@ Example :
KeyBindings = Insert OpenSearch c-q EnqueueSelected c-Insert OpenCustom(a_layout_id)
Key names are as shown in the key binding tab in the settings dialog.
Modifiers are one lower case letter : c for ctrl, s for shift, a for alt and w for win/super.
The modifier(s) and key name can be preceded by a '+' to make the shortcut a higher priority than the normal shorcuts for the focused widget (example: "+c-f" for a control-f that overrides the default ctrl-f action of some widgets). Warning: It may breaks some features or the keyboard navigation.
Commands can be found by running gmusicbrowser with command-line option `-listcmd`.
#### DefaultFocus

0 comments on commit e4efb6f

Please sign in to comment.