Skip to content

Commit

Permalink
Implements pinch gesture
Browse files Browse the repository at this point in the history
- Implements pinch actions
- Update keypress handler
- Add pinch scale config
- Add pinch enable/disable config
  • Loading branch information
amarullz committed Oct 10, 2023
1 parent 0e9ceec commit 1c6fa47
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 5 deletions.
157 changes: 152 additions & 5 deletions [email protected]/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ class Manager {

// Get horizontal swipe mode
_getHorizontalSwipeMode() {
// horiz-swap-switch
// false. Change workspace, true. Switch Window
return this._settings.get_boolean("horiz-swap-switch");
}
Expand Down Expand Up @@ -217,6 +216,18 @@ class Manager {
return this._settings.get_boolean("fn-move-snap");
}

_getPinchInScale() {
return this._settings.get_int('pinch-in-scale');
}

_getPinchOutScale() {
return this._settings.get_int('pinch-out-scale');
}

_getPinchEnabled() {
return this._settings.get_boolean("pinch-enable");
}

// Check edge flags
_isEdge(edge) {
return ((this._edgeAction & edge) == edge);
Expand All @@ -230,7 +241,8 @@ class Manager {
global.window_manager.emit("show-tile-preview",
this._targetWindow, new Meta.Rectangle(
{ x: rx, y: ry, width: rw, height: rh }
), this._monitorId
)
, this._monitorId
);
}

Expand All @@ -241,13 +253,12 @@ class Manager {

// Simulate keypress (up -> down)
_sendKeyPress(combination) {
const currentTime = global.get_current_time();
combination.forEach(key => this._virtualKeyboard.notify_keyval(
currentTime, key, Clutter.KeyState.PRESSED)
Clutter.get_current_event_time(), key, Clutter.KeyState.PRESSED)
);
combination.reverse().forEach(key =>
this._virtualKeyboard.notify_keyval(
currentTime, key, Clutter.KeyState.RELEASED
Clutter.get_current_event_time(), key, Clutter.KeyState.RELEASED
));
}

Expand Down Expand Up @@ -323,6 +334,14 @@ class Manager {
this._edgeAction = WindowEdgeAction.NONE;
this._edgeGestured = false;

// Pinch
this._pinch = {
begin: false,
canceled: false,
fingers: 0,
action: 0
};

// Clear window tile preview
this._hidePreview();
}
Expand Down Expand Up @@ -997,8 +1016,136 @@ class Manager {
return Clutter.EVENT_STOP;
}

setTimeout(func, delay, ...args) {
return GLib.timeout_add(GLib.PRIORITY_DEFAULT, delay, () => {
func(...args);
return GLib.SOURCE_REMOVE;
});
}

clearTimeout(id) {
GLib.source_remove(id);
}

// Run Action
_pinchAction(id) {
const _LCASE = 32;
if (id == 1) {
// Minimize
let activeWin = global.display.get_focus_window();
if (activeWin) {
if (activeWin.can_minimize()) {
activeWin.minimize();
}
}
}
else if (id == 2) {
// Close Window (ALT+F4)
this._sendKeyPress([Clutter.KEY_Alt_L, Clutter.KEY_F4]);
}
else if (id == 3) {
// Show Desktop (Super+D) Clutter.KEY_D
this._sendKeyPress([Clutter.KEY_Super_L, Clutter.KEY_D + _LCASE]);
}
else if (id == 4) {
// Overview (Super)
this._sendKeyPress([Clutter.KEY_Super_L]);
}
else if (id == 5) {
// Show Apps (Super+A)
this._sendKeyPress([Clutter.KEY_Super_L, Clutter.KEY_A + _LCASE]);
}
}

// Update Pinch
_pinchUpdate(pinch_scale) {
if (this._pinch.begin) {
let pIn = (this._getPinchInScale() / 100.0);
let pOut = (this._getPinchOutScale() / 100.0);
if (this._pinch.action == 0) {
if (pinch_scale <= pIn) {
this._pinch.action = 1;
}
else if (pinch_scale >= pOut) {
this._pinch.action = 2;
}
}
else if (this._pinch.action == 1) {
// Pinch-In
this._pinch.canceled = (pinch_scale <= pIn) ? false : true;
}
else if (this._pinch.action == 2) {
// Pinch-Out
this._pinch.canceled = (pinch_scale >= pOut) ? false : true;
}
}
return Clutter.EVENT_STOP;
}

// End Pinch
_pinchEnd() {
if (!this._pinch.canceled && this._pinch.begin &&
this._pinch.action != 0 && this._pinch.fingers >= 3 &&
this._pinch.fingers <= 4) {
try {
let cfg_name = "pinch" + this._pinch.fingers + "-" +
((this._pinch.action == 1) ? "in" : "out");
let action_id = this._settings.get_int(cfg_name);

log(
"WGS - pinchEnd: " + cfg_name + " / actionid: " +
action_id
);

if (action_id > 0) {
// Execute action
this._pinchAction(action_id);
}
} catch (err) {
log("Error Action = " + err);
}
}
this._clearVars();
return Clutter.EVENT_STOP;
}

// Pinch Handler
_pinchEventHandler(actor, event) {
if (!this._getPinchEnabled()) {
return Clutter.EVENT_PROPAGATE;
}
let numfingers = event.get_touchpad_gesture_finger_count();
if (numfingers != 3 && numfingers != 4) {
return Clutter.EVENT_PROPAGATE;
}
const pinch_scale = event.get_gesture_pinch_scale();

// Process gestures state
switch (event.get_gesture_phase()) {
case Clutter.TouchpadGesturePhase.BEGIN:
this._pinch.fingers = numfingers;
this._pinch.begin = true;
this._pinch.canceled = false;
this._pinch.action = 0;
return Clutter.EVENT_STOP;

case Clutter.TouchpadGesturePhase.UPDATE:
return this._pinchUpdate(pinch_scale);

default:
return this._pinchEnd();
}

return Clutter.EVENT_STOP;
}

// Touch Event Handler
_touchEvent(actor, event) {

// Only process swipe event
if (event.type() == Clutter.EventType.TOUCHPAD_PINCH)
return this._pinchEventHandler(actor, event);

// Only process swipe event
if (event.type() != Clutter.EventType.TOUCHPAD_SWIPE)
return Clutter.EVENT_PROPAGATE;
Expand Down
15 changes: 15 additions & 0 deletions [email protected]/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export default class extends ExtensionPreferences {
"App (Super+A)"
];
const pinch = new Adw.PreferencesGroup({ title: "Pinch" });
this._createSwitch(
pinch, "pinch-enable",
"Enable Pinch",
"Enable pinch tracking"
);
this._createCombo(pinch, "pinch3-in",
"Pinch-In 3 Fingers", "", pinch_actions);
this._createCombo(pinch, "pinch3-out",
Expand All @@ -97,6 +102,16 @@ export default class extends ExtensionPreferences {
this._createCombo(pinch, "pinch4-out",
"Pinch-Out 4 Fingers", "", pinch_actions);

this._createSpin(pinch, "pinch-in-scale",
"Pinch-In Trigger Scale Perentage",
"Trigger pinch-in if pinch scale lower than this value",
30, 80, 10);
this._createSpin(pinch, "pinch-out-scale",
"Pinch-Out Trigger Scale Perentage",
"Trigger pinch-out if pinch scale bigger than this value",
120, 200, 10);


// Tweaks Settings
const tweaks = new Adw.PreferencesGroup({ title: "Tweaks" });
this._createSpin(tweaks, "edge-size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@



<key name="pinch-enable" type="b">
<default>true</default>
<summary>Enable Pinch</summary>
<description>
Enable pinch gesture
</description>
</key>
<key type="i" name="pinch3-in">
<default>0</default>
<summary>Pinch-In 3 Fingers</summary>
Expand All @@ -90,6 +97,17 @@
<range min="0" max="15"/>
</key>

<key type="i" name="pinch-in-scale">
<default>60</default>
<summary>Pinch-In Scale</summary>
<range min="30" max="80"/>
</key>
<key type="i" name="pinch-out-scale">
<default>140</default>
<summary>Pinch-In Scale</summary>
<range min="120" max="200"/>
</key>




Expand Down

1 comment on commit 1c6fa47

@amarullz
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing pinch gesture request #3

Please sign in to comment.