Use Gio.SimpleAction as only keypress handler

This commit is contained in:
Rafostar
2021-04-25 20:19:44 +02:00
parent 32995fc6a6
commit a98ca53dfb
7 changed files with 123 additions and 170 deletions

View File

@@ -1,34 +1,86 @@
const Dialogs = imports.src.dialogs; const Dialogs = imports.src.dialogs;
var actions = { var actions = {
open_local: { open_local: ['<Ctrl>O'],
run: (window) => new Dialogs.FileChooser(window), open_uri: ['<Ctrl>U'],
accels: ['<Ctrl>O'], prefs: null,
}, about: null,
open_uri: { progress_forward: ['Right'],
run: (window) => new Dialogs.UriDialog(window), progress_backward: ['Left'],
accels: ['<Ctrl>U'], next_chapter: ['<Shift>Right'],
}, prev_chapter: ['<Shift>Left'],
prefs: { next_track: ['<Ctrl>Right'],
run: (window) => new Dialogs.PrefsDialog(window), prev_track: ['<Ctrl>Left'],
}, volume_up: ['Up'],
about: { volume_down: ['Down'],
run: (window) => new Dialogs.AboutDialog(window), toggle_play: ['space'],
}, reveal_controls: ['Return'],
next_track: { toggle_fullscreen: ['F11', 'f'],
run: (window) => window.child.player.playlistWidget.nextTrack(), quit: ['<Ctrl>q', 'q'],
accels: ['<Ctrl>Right'],
},
prev_track: {
run: (window) => window.child.player.playlistWidget.prevTrack(),
accels: ['<Ctrl>Left'],
},
next_chapter: {
run: (window) => window.child.player.next_chapter(),
accels: ['<Shift>Right'],
},
prev_chapter: {
run: (window) => window.child.player.prev_chapter(),
accels: ['<Shift>Left'],
}
}; };
function handleAction(action, window)
{
const clapperWidget = window.child;
if(!clapperWidget) return;
const { player } = clapperWidget;
let bool = false;
switch(action.name) {
case 'open_local':
new Dialogs.FileChooser(window);
break;
case 'open_uri':
new Dialogs.UriDialog(window);
break;
case 'prefs':
new Dialogs.PrefsDialog(window);
break;
case 'about':
new Dialogs.AboutDialog(window);
break;
case 'progress_forward':
bool = true;
case 'progress_backward':
player.adjust_position(bool);
if(
clapperWidget.isReleaseKeyEnabled
&& clapperWidget.isFullscreenMode
)
clapperWidget.revealControls();
/* Actual seek is handled on release */
clapperWidget.isReleaseKeyEnabled = true;
if(!clapperWidget.has_focus)
clapperWidget.grab_focus();
break;
case 'volume_up':
bool = true;
case 'volume_down':
player.adjust_volume(bool);
break;
case 'next_track':
player.playlistWidget.nextTrack();
break;
case 'prev_track':
player.playlistWidget.prevTrack();
break;
case 'reveal_controls':
if(clapperWidget.isFullscreenMode)
clapperWidget.revealControls();
break;
case 'toggle_fullscreen':
clapperWidget.toggleFullscreen();
break;
case 'quit':
clapperWidget.root.emit('close-request');
break;
case 'toggle_play':
case 'next_chapter':
case 'prev_chapter':
player[action.name]();
break;
default:
break;
}
}

View File

@@ -2,7 +2,7 @@ const { Gio, GLib, GObject, Gtk } = imports.gi;
const Debug = imports.src.debug; const Debug = imports.src.debug;
const FileOps = imports.src.fileOps; const FileOps = imports.src.fileOps;
const Misc = imports.src.misc; const Misc = imports.src.misc;
const { actions } = imports.src.actions; const Actions = imports.src.actions;
const { debug } = Debug; const { debug } = Debug;
const { settings } = Misc; const { settings } = Misc;
@@ -35,15 +35,16 @@ class ClapperAppBase extends Gtk.Application
if(!settings.get_boolean('render-shadows')) if(!settings.get_boolean('render-shadows'))
window.add_css_class('gpufriendly'); window.add_css_class('gpufriendly');
for(let name in actions) { for(let name in Actions.actions) {
const simpleAction = new Gio.SimpleAction({ name }); const simpleAction = new Gio.SimpleAction({ name });
simpleAction.connect( simpleAction.connect('activate', (action) =>
'activate', () => actions[name].run(this.active_window) Actions.handleAction(action, this.active_window)
); );
this.add_action(simpleAction); this.add_action(simpleAction);
if(actions[name].accels) const accels = Actions.actions[name];
this.set_accels_for_action(`app.${name}`, actions[name].accels); if(accels)
this.set_accels_for_action(`app.${name}`, accels);
} }
} }

View File

@@ -31,8 +31,6 @@ class ClapperCustomButton extends Gtk.Button
if(this.isFullscreen === isFullscreen) if(this.isFullscreen === isFullscreen)
return; return;
this.can_focus = isFullscreen;
/* Redraw icon after style class change */ /* Redraw icon after style class change */
if(this.icon_name) if(this.icon_name)
this.set_icon_name(this.icon_name); this.set_icon_name(this.icon_name);
@@ -110,8 +108,6 @@ class ClapperPopoverButtonBase extends Gtk.ToggleButton
if(this.isFullscreen === isFullscreen) if(this.isFullscreen === isFullscreen)
return; return;
this.can_focus = isFullscreen;
/* Redraw icon after style class change */ /* Redraw icon after style class change */
if(this.icon_name) if(this.icon_name)
this.set_icon_name(this.icon_name); this.set_icon_name(this.icon_name);
@@ -152,8 +148,6 @@ class ClapperPopoverButtonBase extends Gtk.ToggleButton
{ {
const clapperWidget = this.get_ancestor(Gtk.Grid); const clapperWidget = this.get_ancestor(Gtk.Grid);
clapperWidget.player.widget.grab_focus();
/* Set again timeout as popover is now closed */ /* Set again timeout as popover is now closed */
if(clapperWidget.isFullscreenMode) if(clapperWidget.isFullscreenMode)
clapperWidget.revealControls(); clapperWidget.revealControls();

30
src/controls.js vendored
View File

@@ -85,11 +85,6 @@ class ClapperControls extends Gtk.Box
this.unfullscreenButton.connect('clicked', this._onUnfullscreenClicked.bind(this)); this.unfullscreenButton.connect('clicked', this._onUnfullscreenClicked.bind(this));
this.unfullscreenButton.set_visible(false); this.unfullscreenButton.set_visible(false);
const keyController = new Gtk.EventControllerKey();
keyController.connect('key-pressed', this._onControlsKeyPressed.bind(this));
keyController.connect('key-released', this._onControlsKeyReleased.bind(this));
this.add_controller(keyController);
this.add_css_class('playercontrols'); this.add_css_class('playercontrols');
this.realizeSignal = this.connect('realize', this._onRealize.bind(this)); this.realizeSignal = this.connect('realize', this._onRealize.bind(this));
} }
@@ -103,8 +98,6 @@ class ClapperControls extends Gtk.Box
button.setFullscreenMode(isFullscreen); button.setFullscreenMode(isFullscreen);
this.unfullscreenButton.visible = isFullscreen; this.unfullscreenButton.visible = isFullscreen;
this.can_focus = isFullscreen;
this.isFullscreen = isFullscreen; this.isFullscreen = isFullscreen;
} }
@@ -621,29 +614,6 @@ class ClapperControls extends Gtk.Box
} }
} }
/* Only happens when navigating through controls panel */
_onControlsKeyPressed(controller, keyval, keycode, state)
{
const clapperWidget = this.get_ancestor(Gtk.Grid);
clapperWidget._setHideControlsTimeout();
}
_onControlsKeyReleased(controller, keyval, keycode, state)
{
switch(keyval) {
case Gdk.KEY_space:
case Gdk.KEY_Return:
case Gdk.KEY_Escape:
case Gdk.KEY_Right:
case Gdk.KEY_Left:
break;
default:
const { player } = this.get_ancestor(Gtk.Grid);
player._onWidgetKeyReleased(controller, keyval, keycode, state);
break;
}
}
_onCloseRequest() _onCloseRequest()
{ {
debug('controls close request'); debug('controls close request');

View File

@@ -37,6 +37,7 @@ class ClapperHeaderBarBase extends Gtk.Box
this.menuButton = new Gtk.MenuButton({ this.menuButton = new Gtk.MenuButton({
icon_name: 'open-menu-symbolic', icon_name: 'open-menu-symbolic',
valign: Gtk.Align.CENTER, valign: Gtk.Align.CENTER,
can_focus: false,
}); });
const mainMenuModel = uiBuilder.get_object('mainMenu'); const mainMenuModel = uiBuilder.get_object('mainMenu');
const mainMenuPopover = new HeaderBarPopover(mainMenuModel); const mainMenuPopover = new HeaderBarPopover(mainMenuModel);
@@ -53,6 +54,7 @@ class ClapperHeaderBarBase extends Gtk.Box
const floatButton = new Gtk.Button({ const floatButton = new Gtk.Button({
icon_name: 'go-bottom-symbolic', icon_name: 'go-bottom-symbolic',
can_focus: false,
}); });
floatButton.add_css_class('circular'); floatButton.add_css_class('circular');
floatButton.add_css_class('linkedleft'); floatButton.add_css_class('linkedleft');
@@ -69,6 +71,7 @@ class ClapperHeaderBarBase extends Gtk.Box
const fullscreenButton = new Gtk.Button({ const fullscreenButton = new Gtk.Button({
icon_name: 'view-fullscreen-symbolic', icon_name: 'view-fullscreen-symbolic',
can_focus: false,
}); });
fullscreenButton.add_css_class('circular'); fullscreenButton.add_css_class('circular');
fullscreenButton.add_css_class('linkedright'); fullscreenButton.add_css_class('linkedright');
@@ -196,6 +199,7 @@ class ClapperHeaderBarBase extends Gtk.Box
const button = new Gtk.Button({ const button = new Gtk.Button({
icon_name: `window-${name}-symbolic`, icon_name: `window-${name}-symbolic`,
valign: Gtk.Align.CENTER, valign: Gtk.Align.CENTER,
can_focus: false,
}); });
button.add_css_class('circular'); button.add_css_class('circular');
@@ -263,7 +267,5 @@ class ClapperHeaderBarPopover extends Gtk.PopoverMenu
child.revealControls(); child.revealControls();
child.isPopoverOpen = false; child.isPopoverOpen = false;
child.player.widget.grab_focus();
} }
}); });

View File

@@ -38,6 +38,7 @@ class ClapperPlayer extends GstClapper.Clapper
this.webserver = null; this.webserver = null;
this.webapp = null; this.webapp = null;
this.ytClient = null;
this.playlistWidget = new PlaylistWidget(); this.playlistWidget = new PlaylistWidget();
this.seek_done = true; this.seek_done = true;
@@ -50,14 +51,6 @@ class ClapperPlayer extends GstClapper.Clapper
this.quitOnStop = false; this.quitOnStop = false;
this.needsTocUpdate = true; this.needsTocUpdate = true;
this.keyPressCount = 0;
this.ytClient = null;
const keyController = new Gtk.EventControllerKey();
keyController.connect('key-pressed', this._onWidgetKeyPressed.bind(this));
keyController.connect('key-released', this._onWidgetKeyReleased.bind(this));
this.widget.add_controller(keyController);
this.set_all_plugins_ranks(); this.set_all_plugins_ranks();
this.set_initial_config(); this.set_initial_config();
this.set_and_bind_settings(); this.set_and_bind_settings();
@@ -638,75 +631,6 @@ class ClapperPlayer extends GstClapper.Clapper
); );
} }
/* Widget only - does not happen when using controls navigation */
_onWidgetKeyPressed(controller, keyval, keycode, state)
{
const clapperWidget = this.widget.get_ancestor(Gtk.Grid);
let bool = false;
switch(keyval) {
case Gdk.KEY_Up:
bool = true;
case Gdk.KEY_Down:
this.adjust_volume(bool);
break;
case Gdk.KEY_Right:
bool = true;
case Gdk.KEY_Left:
this.adjust_position(bool);
if(this.keyPressCount > 1)
clapperWidget.revealControls();
break;
case Gdk.KEY_space:
this.toggle_play();
break;
case Gdk.KEY_Return:
if(clapperWidget.isFullscreenMode)
clapperWidget.revealControls(true);
break;
case Gdk.KEY_F11:
case Gdk.KEY_f:
case Gdk.KEY_F:
clapperWidget.toggleFullscreen();
break;
case Gdk.KEY_q:
case Gdk.KEY_Q:
this.widget.root.emit('close-request');
break;
default:
return;
}
this.keyPressCount++;
}
/* Also happens after using controls navigation for selected keys */
_onWidgetKeyReleased(controller, keyval, keycode, state)
{
/* Ignore releases that did not trigger keypress
* e.g. while holding left "Super" key */
if(!this.keyPressCount)
return;
const clapperWidget = this.widget.get_ancestor(Gtk.Grid);
let value;
this.keyPressCount = 0;
switch(keyval) {
case Gdk.KEY_Right:
case Gdk.KEY_Left:
value = Math.round(
clapperWidget.controls.positionScale.get_value()
);
this.seek_seconds(value);
clapperWidget._setHideControlsTimeout();
break;
default:
break;
}
}
_onWindowMap(window) _onWindowMap(window)
{ {
this.windowMapped = true; this.windowMapped = true;

View File

@@ -31,6 +31,7 @@ class ClapperWidget extends Gtk.Grid
this.isDragAllowed = false; this.isDragAllowed = false;
this.isSwipePerformed = false; this.isSwipePerformed = false;
this.isReleaseKeyEnabled = false;
this.isCursorInPlayer = false; this.isCursorInPlayer = false;
this.isPopoverOpen = false; this.isPopoverOpen = false;
@@ -103,18 +104,20 @@ class ClapperWidget extends Gtk.Grid
const dropTarget = this._getDropTarget(); const dropTarget = this._getDropTarget();
playerWidget.add_controller(dropTarget); playerWidget.add_controller(dropTarget);
/* Applied only for widget to detect simple action key releases */
const keyController = new Gtk.EventControllerKey();
keyController.connect('key-released', this._onKeyReleased.bind(this));
this.add_controller(keyController);
} }
revealControls(isAllowInput) revealControls()
{ {
this.revealerTop.revealChild(true); this.revealerTop.revealChild(true);
this.revealerBottom.revealChild(true); this.revealerBottom.revealChild(true);
this._checkSetUpdateTimeInterval(); this._checkSetUpdateTimeInterval();
if(isAllowInput)
this.setControlsCanFocus(true);
/* Reset timeout if already revealed, otherwise /* Reset timeout if already revealed, otherwise
* timeout will be set after reveal finishes */ * timeout will be set after reveal finishes */
if(this.revealerTop.child_revealed) if(this.revealerTop.child_revealed)
@@ -157,8 +160,6 @@ class ClapperWidget extends Gtk.Grid
if(this.revealerTop.child_revealed) if(this.revealerTop.child_revealed)
this._checkSetUpdateTimeInterval(); this._checkSetUpdateTimeInterval();
this.setControlsCanFocus(false);
if(this.player.playOnFullscreen && isFullscreen) { if(this.player.playOnFullscreen && isFullscreen) {
this.player.playOnFullscreen = false; this.player.playOnFullscreen = false;
this.player.play(); this.player.play();
@@ -167,18 +168,6 @@ class ClapperWidget extends Gtk.Grid
debug(`interface in fullscreen mode: ${isFullscreen}`); debug(`interface in fullscreen mode: ${isFullscreen}`);
} }
setControlsCanFocus(isControlsFocus)
{
this.revealerBottom.can_focus = isControlsFocus;
this.player.widget.can_focus = !isControlsFocus;
const focusWidget = (isControlsFocus)
? this.controls.togglePlayButton
: this.player.widget;
focusWidget.grab_focus();
}
_changeControlsPlacement(isOnTop) _changeControlsPlacement(isOnTop)
{ {
if(isOnTop) { if(isOnTop) {
@@ -619,7 +608,6 @@ class ClapperWidget extends Gtk.Grid
this.revealerTop.revealChild(false); this.revealerTop.revealChild(false);
this.revealerBottom.revealChild(false); this.revealerBottom.revealChild(false);
} }
this.setControlsCanFocus(false);
return GLib.SOURCE_REMOVE; return GLib.SOURCE_REMOVE;
}); });
@@ -767,6 +755,28 @@ class ClapperWidget extends Gtk.Grid
} }
} }
_onKeyReleased(controller, keyval, keycode, state)
{
/* Ignore releases that did not trigger keypress
* e.g. while holding left "Super" key */
if(!this.isReleaseKeyEnabled)
return;
switch(keyval) {
case Gdk.KEY_Right:
case Gdk.KEY_Left:
const value = Math.round(
this.controls.positionScale.get_value()
);
this.player.seek_seconds(value);
this._setHideControlsTimeout();
this.isReleaseKeyEnabled = false;
break;
default:
break;
}
}
_onDragUpdate(gesture, offsetX, offsetY) _onDragUpdate(gesture, offsetX, offsetY)
{ {
if(!this.isDragAllowed || this.isFullscreenMode) if(!this.isDragAllowed || this.isFullscreenMode)