From 749d1455e9022b99d0d34da8dea1a0b32592b77f Mon Sep 17 00:00:00 2001 From: abakkk Date: Wed, 30 Sep 2020 19:16:55 +0200 Subject: [PATCH] motion timeout Add intermediate points to make quick free drawings smoother. Do not redisplay the area at this step (crashes). Fix #45. --- area.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/area.js b/area.js index 3b5444c..498a627 100644 --- a/area.js +++ b/area.js @@ -47,6 +47,7 @@ const pgettext = imports.gettext.domain(Me.metadata['gettext-domain']).pgettext; const CAIRO_DEBUG_EXTENDS = false; const SVG_DEBUG_EXTENDS = false; +const MOTION_TIME = 5; // ms, time accuracy for free drawing, max is about 33 ms. The lower it is, the smoother the drawing is. const TEXT_CURSOR_TIME = 600; // ms const ELEMENT_GRABBER_TIME = 80; // ms, default is about 16 ms const GRID_TILES_HORIZONTAL_NUMBER = 30; @@ -664,6 +665,31 @@ var DrawingArea = new Lang.Class({ return; let controlPressed = event.has_control_modifier(); this._updateDrawing(x, y, controlPressed); + + if (this.motionTimeout) + GLib.source_remove(this.motionTimeout); + + if (this.currentTool == Shapes.NONE) { + // Minimum time between two motion events is about 33 ms. + // Add intermediate points to make quick free drawings smoother. + this.motionTimeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, MOTION_TIME, () => { + if (this.spaceKeyPressed) + return GLib.SOURCE_CONTINUE; + + let [stageX, stageY, mods] = global.get_pointer(); + let [s, x, y] = this.transform_stage_point(stageX, stageY); + if (!s) + return GLib.SOURCE_CONTINUE; + + let controlPressed = !!(mods & Clutter.ModifierType.CONTROL_MASK); + + // Important: do not call this._updateDrawing because the area MUST NOT BE REDISPLAYED at this step. + // It would lead to critical issues (bad performances and shell crashes). + this.currentElement.updateDrawing(x, y, controlPressed); + + return GLib.SOURCE_CONTINUE; + }); + } }); }, @@ -678,6 +704,10 @@ var DrawingArea = new Lang.Class({ }, _stopDrawing: function() { + if (this.motionTimeout) { + GLib.source_remove(this.motionTimeout); + this.motionTimeout = null; + } if (this.motionHandler) { this.disconnect(this.motionHandler); this.motionHandler = null;