2019-03-05 08:36:59 -03:00
|
|
|
/* jslint esversion: 6 */
|
2020-09-07 13:56:48 -03:00
|
|
|
/* exported Tools, DrawingArea */
|
2019-03-05 08:36:59 -03:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 Abakkk
|
|
|
|
|
*
|
2019-12-28 09:25:41 -03:00
|
|
|
* This file is part of DrawOnYourScreen, a drawing extension for GNOME Shell.
|
2019-03-05 08:36:59 -03:00
|
|
|
* https://framagit.org/abakkk/DrawOnYourScreen
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const Cairo = imports.cairo;
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
2019-03-29 12:35:18 -03:00
|
|
|
const Gio = imports.gi.Gio;
|
2019-03-05 08:36:59 -03:00
|
|
|
const GLib = imports.gi.GLib;
|
2019-03-26 18:28:24 -03:00
|
|
|
const GObject = imports.gi.GObject;
|
2019-03-05 08:36:59 -03:00
|
|
|
const Gtk = imports.gi.Gtk;
|
|
|
|
|
const Lang = imports.lang;
|
2020-06-26 07:19:53 -03:00
|
|
|
const Pango = imports.gi.Pango;
|
2019-03-05 08:36:59 -03:00
|
|
|
const St = imports.gi.St;
|
2020-07-30 06:13:23 -03:00
|
|
|
const System = imports.system;
|
2019-03-26 18:28:24 -03:00
|
|
|
|
2020-07-11 10:59:08 -03:00
|
|
|
const ExtensionUtils = imports.misc.extensionUtils;
|
|
|
|
|
const Main = imports.ui.main;
|
2019-03-05 08:36:59 -03:00
|
|
|
const Screenshot = imports.ui.screenshot;
|
|
|
|
|
|
2020-01-05 11:44:51 -03:00
|
|
|
const Me = ExtensionUtils.getCurrentExtension();
|
2020-09-04 19:03:05 -03:00
|
|
|
const Convenience = ExtensionUtils.getSettings ? ExtensionUtils : Me.imports.convenience;
|
2020-07-03 18:37:09 -03:00
|
|
|
const Elements = Me.imports.elements;
|
2020-07-30 06:13:23 -03:00
|
|
|
const Files = Me.imports.files;
|
2020-07-03 17:51:23 -03:00
|
|
|
const Menu = Me.imports.menu;
|
2020-01-06 11:29:01 -03:00
|
|
|
const _ = imports.gettext.domain(Me.metadata['gettext-domain']).gettext;
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2020-06-17 10:43:23 -03:00
|
|
|
const CAIRO_DEBUG_EXTENDS = false;
|
|
|
|
|
const SVG_DEBUG_EXTENDS = false;
|
2020-06-18 20:35:25 -03:00
|
|
|
const TEXT_CURSOR_TIME = 600; // ms
|
2020-08-31 09:03:24 -03:00
|
|
|
const ELEMENT_GRABBER_TIME = 80; // ms, default is about 16 ms
|
2020-08-31 04:43:00 -03:00
|
|
|
const GRID_TILES_HORIZONTAL_NUMBER = 30;
|
2019-10-11 04:22:37 -03:00
|
|
|
|
2020-09-07 13:56:48 -03:00
|
|
|
const { Shapes, Transformations } = Elements;
|
|
|
|
|
const { DisplayStrings } = Menu;
|
|
|
|
|
|
|
|
|
|
const FontGenericFamilies = ['Sans-Serif', 'Serif', 'Monospace', 'Cursive', 'Fantasy'];
|
2020-07-30 06:13:23 -03:00
|
|
|
const Manipulations = { MOVE: 100, RESIZE: 101, MIRROR: 102 };
|
2020-07-03 17:51:23 -03:00
|
|
|
var Tools = Object.assign({}, Shapes, Manipulations);
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2020-08-31 04:43:00 -03:00
|
|
|
const getClutterColorFromString = function(string, fallback) {
|
|
|
|
|
let [success, color] = Clutter.Color.from_string(string);
|
|
|
|
|
color.string = string;
|
|
|
|
|
if (success)
|
|
|
|
|
return color;
|
|
|
|
|
|
|
|
|
|
log(`${Me.metadata.uuid}: "${string}" color cannot be parsed.`);
|
|
|
|
|
return Clutter.Color.get_static(Clutter.StaticColor[fallback]);
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
// DrawingArea is the widget in which we draw, thanks to Cairo.
|
|
|
|
|
// It creates and manages a DrawingElement for each "brushstroke".
|
|
|
|
|
// It handles pointer/mouse/(touch?) events and some keyboard events.
|
|
|
|
|
var DrawingArea = new Lang.Class({
|
2019-03-08 07:37:59 -03:00
|
|
|
Name: 'DrawOnYourScreenDrawingArea',
|
2019-03-05 08:36:59 -03:00
|
|
|
Extends: St.DrawingArea,
|
2020-06-28 08:10:31 -03:00
|
|
|
Signals: { 'show-osd': { param_types: [GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_DOUBLE, GObject.TYPE_BOOLEAN] },
|
2020-07-30 06:13:23 -03:00
|
|
|
'show-osd-gicon': { param_types: [Gio.Icon.$gtype, GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_DOUBLE, GObject.TYPE_BOOLEAN] },
|
2020-06-25 06:41:15 -03:00
|
|
|
'update-action-mode': {},
|
2020-06-22 06:56:34 -03:00
|
|
|
'leave-drawing-mode': {} },
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2020-01-03 20:16:20 -03:00
|
|
|
_init: function(params, monitor, helper, loadPersistent) {
|
2020-03-01 13:33:41 -03:00
|
|
|
this.parent({ style_class: 'draw-on-your-screen', name: params.name});
|
2019-03-07 12:32:06 -03:00
|
|
|
this.monitor = monitor;
|
2019-03-05 08:36:59 -03:00
|
|
|
this.helper = helper;
|
|
|
|
|
|
|
|
|
|
this.elements = [];
|
|
|
|
|
this.undoneElements = [];
|
|
|
|
|
this.currentElement = null;
|
2020-06-13 08:53:52 -03:00
|
|
|
this.currentTool = Shapes.NONE;
|
2020-07-30 06:13:23 -03:00
|
|
|
this.currentImage = 0;
|
2020-09-04 19:03:05 -03:00
|
|
|
this.currentTextRightAligned = Clutter.get_default_text_direction() == Clutter.TextDirection.RTL;
|
|
|
|
|
let fontName = St.Settings && St.Settings.get().font_name || Convenience.getSettings('org.gnome.desktop.interface').get_string('font-name');
|
|
|
|
|
this.currentFont = Pango.FontDescription.from_string(fontName);
|
|
|
|
|
this.currentFont.unset_fields(Pango.FontMask.SIZE);
|
|
|
|
|
this.defaultFontFamily = this.currentFont.get_family();
|
2020-08-31 04:43:00 -03:00
|
|
|
this.currentLineWidth = 5;
|
|
|
|
|
this.currentLineJoin = Cairo.LineJoin.ROUND;
|
|
|
|
|
this.currentLineCap = Cairo.LineCap.ROUND;
|
|
|
|
|
this.currentFillRule = Cairo.FillRule.WINDING;
|
2019-03-07 12:32:06 -03:00
|
|
|
this.isSquareArea = false;
|
2020-06-05 10:58:58 -03:00
|
|
|
this.hasGrid = false;
|
2019-03-05 08:36:59 -03:00
|
|
|
this.hasBackground = false;
|
|
|
|
|
this.textHasCursor = false;
|
|
|
|
|
this.dashedLine = false;
|
2019-03-26 18:28:24 -03:00
|
|
|
this.fill = false;
|
2019-03-11 13:53:35 -03:00
|
|
|
|
2020-08-31 04:43:00 -03:00
|
|
|
this.connect('destroy', this._onDestroy.bind(this));
|
|
|
|
|
this.connect('notify::reactive', this._onReactiveChanged.bind(this));
|
|
|
|
|
this.drawingSettingsChangedHandler = Me.drawingSettings.connect('changed', this._onDrawingSettingsChanged.bind(this));
|
|
|
|
|
this._onDrawingSettingsChanged();
|
|
|
|
|
|
2020-01-03 20:16:20 -03:00
|
|
|
if (loadPersistent)
|
|
|
|
|
this._loadPersistent();
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2019-10-11 04:39:17 -03:00
|
|
|
get menu() {
|
|
|
|
|
if (!this._menu)
|
2020-09-07 13:56:48 -03:00
|
|
|
this._menu = new Menu.DrawingMenu(this, this.monitor, Tools);
|
2019-10-11 04:39:17 -03:00
|
|
|
return this._menu;
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-22 06:56:34 -03:00
|
|
|
closeMenu: function() {
|
|
|
|
|
if (this._menu)
|
|
|
|
|
this._menu.close();
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-25 06:41:15 -03:00
|
|
|
get isWriting() {
|
|
|
|
|
return this.textEntry ? true : false;
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
get currentTool() {
|
|
|
|
|
return this._currentTool;
|
2020-06-11 22:45:51 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
set currentTool(tool) {
|
|
|
|
|
this._currentTool = tool;
|
2020-06-27 17:35:43 -03:00
|
|
|
if (this.hasManipulationTool)
|
2020-06-17 19:57:54 -03:00
|
|
|
this._startElementGrabber();
|
2020-06-13 08:53:52 -03:00
|
|
|
else
|
2020-06-17 19:57:54 -03:00
|
|
|
this._stopElementGrabber();
|
2020-06-11 22:45:51 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-31 04:43:00 -03:00
|
|
|
get currentPalette() {
|
|
|
|
|
return this._currentPalette;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
set currentPalette(palette) {
|
|
|
|
|
this._currentPalette = palette;
|
|
|
|
|
this.colors = palette[1].map(colorString => getClutterColorFromString(colorString, 'WHITE'));
|
|
|
|
|
if (!this.colors[0])
|
|
|
|
|
this.colors.push(Clutter.Color.get_static(Clutter.StaticColor.WHITE));
|
|
|
|
|
},
|
|
|
|
|
|
2020-09-04 19:03:05 -03:00
|
|
|
get currentFontFamily() {
|
|
|
|
|
return this.currentFont.get_family();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
set currentFontFamily(family) {
|
|
|
|
|
this.currentFont.set_family(family);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get currentFontStyle() {
|
|
|
|
|
return this.currentFont.get_style();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
set currentFontStyle(style) {
|
|
|
|
|
this.currentFont.set_style(style);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get currentFontWeight() {
|
|
|
|
|
return this.currentFont.get_weight();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
set currentFontWeight(weight) {
|
|
|
|
|
this.currentFont.set_weight(weight);
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-27 17:35:43 -03:00
|
|
|
get hasManipulationTool() {
|
|
|
|
|
// No Object.values method in GS 3.24.
|
|
|
|
|
return Object.keys(Manipulations).map(key => Manipulations[key]).indexOf(this.currentTool) != -1;
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-27 08:40:34 -03:00
|
|
|
// Boolean wrapper for switch menu item.
|
|
|
|
|
get currentEvenodd() {
|
|
|
|
|
return this.currentFillRule == Cairo.FillRule.EVEN_ODD;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
set currentEvenodd(evenodd) {
|
|
|
|
|
this.currentFillRule = evenodd ? Cairo.FillRule.EVEN_ODD : Cairo.FillRule.WINDING;
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-30 06:13:23 -03:00
|
|
|
getImages() {
|
|
|
|
|
let images = Files.getImages();
|
|
|
|
|
if (!images[this.currentImage])
|
|
|
|
|
this.currentImage = Math.max(images.length - 1, 0);
|
|
|
|
|
return images;
|
|
|
|
|
},
|
|
|
|
|
|
2020-08-05 18:30:25 -03:00
|
|
|
get fontFamilies() {
|
|
|
|
|
if (!this._fontFamilies) {
|
|
|
|
|
let pangoFontFamilies = Elements.getPangoFontFamilies().filter(family => {
|
2020-08-31 04:43:00 -03:00
|
|
|
return family != this.defaultFontFamily && FontGenericFamilies.indexOf(family) == -1;
|
2020-08-05 18:30:25 -03:00
|
|
|
});
|
2020-08-31 04:43:00 -03:00
|
|
|
this._fontFamilies = [this.defaultFontFamily].concat(FontGenericFamilies, pangoFontFamilies);
|
2020-08-05 18:30:25 -03:00
|
|
|
}
|
|
|
|
|
return this._fontFamilies;
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-26 15:08:36 -03:00
|
|
|
vfunc_repaint: function() {
|
|
|
|
|
let cr = this.get_context();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
this._repaint(cr);
|
|
|
|
|
} catch(e) {
|
|
|
|
|
logError(e, "An error occured while painting");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cr.$dispose();
|
2020-07-30 06:13:23 -03:00
|
|
|
if (this.elements.some(element => element.shape == Shapes.IMAGE) || this.currentElement && this.currentElement.shape == Shapes.IMAGE)
|
|
|
|
|
System.gc();
|
2020-06-26 15:08:36 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
_redisplay: function() {
|
|
|
|
|
// force area to emit 'repaint'
|
|
|
|
|
this.queue_repaint();
|
|
|
|
|
},
|
|
|
|
|
|
2020-08-31 04:43:00 -03:00
|
|
|
_onDrawingSettingsChanged: function() {
|
|
|
|
|
this.palettes = Me.drawingSettings.get_value('palettes').deep_unpack();
|
|
|
|
|
if (!this.colors) {
|
|
|
|
|
if (this.palettes[0])
|
|
|
|
|
this.currentPalette = this.palettes[0];
|
|
|
|
|
else
|
|
|
|
|
this.currentPalette = ['Palette', ['White']];
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
2020-08-31 04:43:00 -03:00
|
|
|
if (!this.currentColor)
|
|
|
|
|
this.currentColor = this.colors[0];
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2020-08-31 04:43:00 -03:00
|
|
|
if (Me.drawingSettings.get_boolean('square-area-auto')) {
|
|
|
|
|
this.squareAreaSize = Math.pow(2, 6);
|
|
|
|
|
while (this.squareAreaSize * 2 < Math.min(this.monitor.width, this.monitor.height))
|
|
|
|
|
this.squareAreaSize *= 2;
|
|
|
|
|
} else {
|
|
|
|
|
this.squareAreaSize = Math.max(64, Me.drawingSettings.get_uint('square-area-size'));
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
2020-08-31 04:43:00 -03:00
|
|
|
|
2020-09-01 08:43:30 -03:00
|
|
|
this.areaBackgroundColor = getClutterColorFromString(Me.drawingSettings.get_string('background-color'), 'BLACK');
|
2020-08-31 04:43:00 -03:00
|
|
|
|
|
|
|
|
this.gridColor = getClutterColorFromString(Me.drawingSettings.get_string('grid-color'), 'GRAY');
|
|
|
|
|
if (Me.drawingSettings.get_boolean('grid-line-auto')) {
|
|
|
|
|
this.gridLineSpacing = Math.round(this.monitor.width / (5 * GRID_TILES_HORIZONTAL_NUMBER));
|
|
|
|
|
this.gridLineWidth = this.gridLineSpacing / 20;
|
|
|
|
|
} else {
|
|
|
|
|
this.gridLineSpacing = Math.max(1, Me.drawingSettings.get_uint('grid-line-spacing'));
|
|
|
|
|
this.gridLineWidth = Math.round(Math.max(0.1, Me.drawingSettings.get_double('grid-line-width')) * 100) / 100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.dashOffset = Math.round(Me.drawingSettings.get_double('dash-offset') * 100) / 100;
|
|
|
|
|
if (Me.drawingSettings.get_boolean('dash-array-auto')) {
|
|
|
|
|
this.dashArray = [0, 0];
|
|
|
|
|
} else {
|
|
|
|
|
let on = Math.round(Math.max(0, Me.drawingSettings.get_double('dash-array-on')) * 100) / 100;
|
|
|
|
|
let off = Math.round(Math.max(0, Me.drawingSettings.get_double('dash-array-off')) * 100) / 100;
|
|
|
|
|
this.dashArray = [on, off];
|
2020-06-23 03:46:28 -03:00
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-26 15:08:36 -03:00
|
|
|
_repaint: function(cr) {
|
2020-06-17 10:43:23 -03:00
|
|
|
if (CAIRO_DEBUG_EXTENDS) {
|
|
|
|
|
cr.scale(0.5, 0.5);
|
|
|
|
|
cr.translate(this.monitor.width, this.monitor.height);
|
|
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
|
|
|
|
|
for (let i = 0; i < this.elements.length; i++) {
|
2020-06-17 10:27:18 -03:00
|
|
|
cr.save();
|
2019-03-29 18:31:13 -03:00
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
this.elements[i].buildCairo(cr, { showTextRectangle: this.grabbedElement && this.grabbedElement == this.elements[i],
|
|
|
|
|
drawTextRectangle: this.grabPoint ? true : false });
|
2020-06-11 22:45:51 -03:00
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
if (this.grabPoint)
|
|
|
|
|
this._searchElementToGrab(cr, this.elements[i]);
|
2020-06-11 22:45:51 -03:00
|
|
|
|
2020-06-17 20:03:50 -03:00
|
|
|
if (this.elements[i].fill && !this.elements[i].isStraightLine) {
|
2020-06-17 10:37:37 -03:00
|
|
|
cr.fillPreserve();
|
2019-03-29 18:31:13 -03:00
|
|
|
if (this.elements[i].shape == Shapes.NONE || this.elements[i].shape == Shapes.LINE)
|
|
|
|
|
cr.closePath();
|
2020-06-17 10:37:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cr.stroke();
|
2020-06-17 10:27:18 -03:00
|
|
|
cr.restore();
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.currentElement) {
|
2020-06-17 10:27:18 -03:00
|
|
|
cr.save();
|
2020-06-15 17:13:03 -03:00
|
|
|
this.currentElement.buildCairo(cr, { showTextCursor: this.textHasCursor,
|
2020-07-30 06:13:23 -03:00
|
|
|
showTextRectangle: this.currentElement.shape != Shapes.TEXT || !this.isWriting,
|
2020-06-19 15:58:23 -03:00
|
|
|
dummyStroke: this.currentElement.fill && this.currentElement.line.lineWidth == 0 });
|
2020-06-07 15:37:13 -03:00
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
cr.stroke();
|
2020-06-17 10:27:18 -03:00
|
|
|
cr.restore();
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
|
2020-08-31 04:43:00 -03:00
|
|
|
if (this.reactive && this.hasGrid) {
|
2020-06-17 10:27:18 -03:00
|
|
|
cr.save();
|
2020-06-05 10:58:58 -03:00
|
|
|
Clutter.cairo_set_source_color(cr, this.gridColor);
|
|
|
|
|
|
2020-07-11 17:42:48 -03:00
|
|
|
let [gridX, gridY] = [0, 0];
|
|
|
|
|
while (gridX < this.monitor.width / 2) {
|
2020-08-31 04:43:00 -03:00
|
|
|
cr.setLineWidth((gridX / this.gridLineSpacing) % 5 ? this.gridLineWidth / 2 : this.gridLineWidth);
|
2020-07-11 17:42:48 -03:00
|
|
|
cr.moveTo(this.monitor.width / 2 + gridX, 0);
|
|
|
|
|
cr.lineTo(this.monitor.width / 2 + gridX, this.monitor.height);
|
|
|
|
|
cr.moveTo(this.monitor.width / 2 - gridX, 0);
|
|
|
|
|
cr.lineTo(this.monitor.width / 2 - gridX, this.monitor.height);
|
2020-08-31 04:43:00 -03:00
|
|
|
gridX += this.gridLineSpacing;
|
2020-06-05 10:58:58 -03:00
|
|
|
cr.stroke();
|
|
|
|
|
}
|
2020-07-11 17:42:48 -03:00
|
|
|
while (gridY < this.monitor.height / 2) {
|
2020-08-31 04:43:00 -03:00
|
|
|
cr.setLineWidth((gridY / this.gridLineSpacing) % 5 ? this.gridLineWidth / 2 : this.gridLineWidth);
|
2020-07-11 17:42:48 -03:00
|
|
|
cr.moveTo(0, this.monitor.height / 2 + gridY);
|
|
|
|
|
cr.lineTo(this.monitor.width, this.monitor.height / 2 + gridY);
|
|
|
|
|
cr.moveTo(0, this.monitor.height / 2 - gridY);
|
|
|
|
|
cr.lineTo(this.monitor.width, this.monitor.height / 2 - gridY);
|
2020-08-31 04:43:00 -03:00
|
|
|
gridY += this.gridLineSpacing;
|
2020-06-05 10:58:58 -03:00
|
|
|
cr.stroke();
|
|
|
|
|
}
|
2020-06-17 10:27:18 -03:00
|
|
|
cr.restore();
|
2020-06-05 10:58:58 -03:00
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_onButtonPressed: function(actor, event) {
|
2020-03-01 09:25:48 -03:00
|
|
|
if (this.spaceKeyPressed)
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
let button = event.get_button();
|
|
|
|
|
let [x, y] = event.get_coords();
|
2020-06-15 17:13:03 -03:00
|
|
|
let controlPressed = event.has_control_modifier();
|
2019-03-05 08:36:59 -03:00
|
|
|
let shiftPressed = event.has_shift_modifier();
|
|
|
|
|
|
2020-06-25 06:41:15 -03:00
|
|
|
if (this.currentElement && this.currentElement.shape == Shapes.TEXT && this.isWriting)
|
2020-06-05 19:13:21 -03:00
|
|
|
// finish writing
|
2019-03-05 08:36:59 -03:00
|
|
|
this._stopWriting();
|
|
|
|
|
|
2019-03-28 18:15:31 -03:00
|
|
|
if (this.helper.visible) {
|
2020-06-05 19:13:21 -03:00
|
|
|
// hide helper
|
2020-06-25 06:41:15 -03:00
|
|
|
this.toggleHelp();
|
2019-03-05 08:36:59 -03:00
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (button == 1) {
|
2020-06-27 17:35:43 -03:00
|
|
|
if (this.hasManipulationTool) {
|
2020-06-17 19:57:54 -03:00
|
|
|
if (this.grabbedElement)
|
2020-06-15 17:13:03 -03:00
|
|
|
this._startTransforming(x, y, controlPressed, shiftPressed);
|
2020-06-13 08:53:52 -03:00
|
|
|
} else {
|
|
|
|
|
this._startDrawing(x, y, shiftPressed);
|
|
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
} else if (button == 2) {
|
2020-08-05 19:04:12 -03:00
|
|
|
this.switchFill();
|
2019-03-05 08:36:59 -03:00
|
|
|
} else if (button == 3) {
|
2019-03-27 10:23:34 -03:00
|
|
|
this._stopDrawing();
|
2019-03-26 18:28:24 -03:00
|
|
|
this.menu.open(x, y);
|
2019-03-05 08:36:59 -03:00
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-27 10:23:34 -03:00
|
|
|
_onKeyboardPopupMenu: function() {
|
|
|
|
|
this._stopDrawing();
|
|
|
|
|
if (this.helper.visible)
|
2020-06-25 06:41:15 -03:00
|
|
|
this.toggleHelp();
|
2019-03-27 10:23:34 -03:00
|
|
|
this.menu.popup();
|
|
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
},
|
|
|
|
|
|
2020-03-01 09:25:48 -03:00
|
|
|
_onStageKeyPressed: function(actor, event) {
|
|
|
|
|
if (event.get_key_symbol() == Clutter.KEY_space)
|
|
|
|
|
this.spaceKeyPressed = true;
|
|
|
|
|
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_onStageKeyReleased: function(actor, event) {
|
|
|
|
|
if (event.get_key_symbol() == Clutter.KEY_space)
|
|
|
|
|
this.spaceKeyPressed = false;
|
|
|
|
|
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
_onKeyPressed: function(actor, event) {
|
2020-06-25 06:41:15 -03:00
|
|
|
if (this.currentElement && this.currentElement.shape == Shapes.LINE) {
|
|
|
|
|
if (event.get_key_symbol() == Clutter.KEY_Return ||
|
|
|
|
|
event.get_key_symbol() == Clutter.KEY_KP_Enter ||
|
|
|
|
|
event.get_key_symbol() == Clutter.KEY_Control_L) {
|
2020-06-20 06:17:56 -03:00
|
|
|
if (this.currentElement.points.length == 2)
|
2020-06-28 08:10:31 -03:00
|
|
|
this.emit('show-osd', null, _("Press <i>%s</i> to get\na fourth control point")
|
|
|
|
|
.format(Gtk.accelerator_get_label(Clutter.KEY_Return, 0)), "", -1, true);
|
2020-06-19 10:52:12 -03:00
|
|
|
this.currentElement.addPoint();
|
|
|
|
|
this.updatePointerCursor(true);
|
|
|
|
|
this._redisplay();
|
|
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
} else {
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 15:05:30 -03:00
|
|
|
} else if (this.currentElement &&
|
|
|
|
|
(this.currentElement.shape == Shapes.POLYGON || this.currentElement.shape == Shapes.POLYLINE) &&
|
2020-06-25 06:41:15 -03:00
|
|
|
(event.get_key_symbol() == Clutter.KEY_Return || event.get_key_symbol() == Clutter.KEY_KP_Enter)) {
|
2020-06-19 10:52:12 -03:00
|
|
|
this.currentElement.addPoint();
|
2020-06-07 13:57:11 -03:00
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
|
2020-06-25 06:41:15 -03:00
|
|
|
} else if (event.get_key_symbol() == Clutter.KEY_Escape) {
|
2020-06-05 19:13:21 -03:00
|
|
|
if (this.helper.visible)
|
2020-06-25 06:41:15 -03:00
|
|
|
this.toggleHelp();
|
2020-06-05 19:13:21 -03:00
|
|
|
else
|
2020-06-22 06:56:34 -03:00
|
|
|
this.emit('leave-drawing-mode');
|
2020-06-05 19:13:21 -03:00
|
|
|
return Clutter.EVENT_STOP;
|
2019-03-11 15:07:01 -03:00
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
} else {
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_onScroll: function(actor, event) {
|
|
|
|
|
if (this.helper.visible)
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
let direction = event.get_scroll_direction();
|
|
|
|
|
if (direction == Clutter.ScrollDirection.UP)
|
|
|
|
|
this.incrementLineWidth(1);
|
|
|
|
|
else if (direction == Clutter.ScrollDirection.DOWN)
|
|
|
|
|
this.incrementLineWidth(-1);
|
|
|
|
|
else
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
_searchElementToGrab: function(cr, element) {
|
|
|
|
|
if (element.getContainsPoint(cr, this.grabPoint[0], this.grabPoint[1]))
|
|
|
|
|
this.grabbedElement = element;
|
|
|
|
|
else if (this.grabbedElement == element)
|
|
|
|
|
this.grabbedElement = null;
|
2020-06-13 08:53:52 -03:00
|
|
|
|
2020-06-17 10:27:18 -03:00
|
|
|
if (element == this.elements[this.elements.length - 1])
|
2020-06-13 08:53:52 -03:00
|
|
|
// All elements have been tested, the winner is the last.
|
|
|
|
|
this.updatePointerCursor();
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
_startElementGrabber: function() {
|
2020-06-27 17:31:31 -03:00
|
|
|
if (this.elementGrabberHandler)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
this.elementGrabberHandler = this.connect('motion-event', (actor, event) => {
|
|
|
|
|
if (this.motionHandler || this.grabbedElementLocked) {
|
|
|
|
|
this.grabPoint = null;
|
2020-06-13 08:53:52 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 20:18:16 -03:00
|
|
|
// Reduce computing without notable effect.
|
2020-08-31 09:03:24 -03:00
|
|
|
if (event.get_time() - (this.elementGrabberTimestamp || 0) < ELEMENT_GRABBER_TIME)
|
2020-06-18 20:18:16 -03:00
|
|
|
return;
|
2020-08-31 09:03:24 -03:00
|
|
|
this.elementGrabberTimestamp = event.get_time();
|
2020-06-18 20:18:16 -03:00
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
let coords = event.get_coords();
|
|
|
|
|
let [s, x, y] = this.transform_stage_point(coords[0], coords[1]);
|
|
|
|
|
if (!s)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
this.grabPoint = [x, y];
|
|
|
|
|
this.grabbedElement = null;
|
|
|
|
|
// this._redisplay calls this._searchElementToGrab.
|
2020-06-13 08:53:52 -03:00
|
|
|
this._redisplay();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
_stopElementGrabber: function() {
|
|
|
|
|
if (this.elementGrabberHandler) {
|
|
|
|
|
this.disconnect(this.elementGrabberHandler);
|
|
|
|
|
this.grabPoint = null;
|
|
|
|
|
this.elementGrabberHandler = null;
|
2020-06-13 08:53:52 -03:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-15 17:13:03 -03:00
|
|
|
_startTransforming: function(stageX, stageY, controlPressed, duplicate) {
|
2020-06-13 08:53:52 -03:00
|
|
|
let [success, startX, startY] = this.transform_stage_point(stageX, stageY);
|
|
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-17 13:30:57 -03:00
|
|
|
if (this.currentTool == Manipulations.MIRROR) {
|
2020-06-17 19:57:54 -03:00
|
|
|
this.grabbedElementLocked = !this.grabbedElementLocked;
|
|
|
|
|
if (this.grabbedElementLocked) {
|
2020-06-17 13:30:57 -03:00
|
|
|
this.updatePointerCursor();
|
2020-06-20 06:17:56 -03:00
|
|
|
let label = controlPressed ? _("Mark a point of symmetry") : _("Draw a line of symmetry");
|
2020-06-28 08:10:31 -03:00
|
|
|
this.emit('show-osd', null, label, "", -1, true);
|
2020-06-17 13:30:57 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
this.grabPoint = null;
|
2020-06-17 13:30:57 -03:00
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
this.buttonReleasedHandler = this.connect('button-release-event', (actor, event) => {
|
|
|
|
|
this._stopTransforming();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (duplicate) {
|
|
|
|
|
// deep cloning
|
2020-07-08 06:47:51 -03:00
|
|
|
let copy = new this.grabbedElement.constructor(JSON.parse(JSON.stringify(this.grabbedElement)));
|
2020-07-30 06:13:23 -03:00
|
|
|
if (this.grabbedElement.image)
|
|
|
|
|
copy.image = this.grabbedElement.image;
|
2020-06-13 08:53:52 -03:00
|
|
|
this.elements.push(copy);
|
2020-06-17 19:57:54 -03:00
|
|
|
this.grabbedElement = copy;
|
2020-06-13 08:53:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.currentTool == Manipulations.MOVE)
|
2020-06-17 19:57:54 -03:00
|
|
|
this.grabbedElement.startTransformation(startX, startY, controlPressed ? Transformations.ROTATION : Transformations.TRANSLATION);
|
2020-06-15 17:13:03 -03:00
|
|
|
else if (this.currentTool == Manipulations.RESIZE)
|
2020-06-18 20:24:42 -03:00
|
|
|
this.grabbedElement.startTransformation(startX, startY, controlPressed ? Transformations.STRETCH : Transformations.SCALE_PRESERVE);
|
2020-06-17 13:30:57 -03:00
|
|
|
else if (this.currentTool == Manipulations.MIRROR) {
|
2020-06-17 19:57:54 -03:00
|
|
|
this.grabbedElement.startTransformation(startX, startY, controlPressed ? Transformations.INVERSION : Transformations.REFLECTION);
|
2020-06-17 13:30:57 -03:00
|
|
|
this._redisplay();
|
|
|
|
|
}
|
2020-06-13 08:53:52 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
this.motionHandler = this.connect('motion-event', (actor, event) => {
|
|
|
|
|
if (this.spaceKeyPressed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
let coords = event.get_coords();
|
|
|
|
|
let [s, x, y] = this.transform_stage_point(coords[0], coords[1]);
|
|
|
|
|
if (!s)
|
|
|
|
|
return;
|
|
|
|
|
let controlPressed = event.has_control_modifier();
|
|
|
|
|
this._updateTransforming(x, y, controlPressed);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_updateTransforming: function(x, y, controlPressed) {
|
2020-06-17 19:57:54 -03:00
|
|
|
if (controlPressed && this.grabbedElement.lastTransformation.type == Transformations.TRANSLATION) {
|
|
|
|
|
this.grabbedElement.stopTransformation();
|
|
|
|
|
this.grabbedElement.startTransformation(x, y, Transformations.ROTATION);
|
|
|
|
|
} else if (!controlPressed && this.grabbedElement.lastTransformation.type == Transformations.ROTATION) {
|
|
|
|
|
this.grabbedElement.stopTransformation();
|
|
|
|
|
this.grabbedElement.startTransformation(x, y, Transformations.TRANSLATION);
|
2020-06-13 08:53:52 -03:00
|
|
|
}
|
|
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
if (controlPressed && this.grabbedElement.lastTransformation.type == Transformations.SCALE_PRESERVE) {
|
|
|
|
|
this.grabbedElement.stopTransformation();
|
2020-06-18 20:24:42 -03:00
|
|
|
this.grabbedElement.startTransformation(x, y, Transformations.STRETCH);
|
|
|
|
|
} else if (!controlPressed && this.grabbedElement.lastTransformation.type == Transformations.STRETCH) {
|
2020-06-17 19:57:54 -03:00
|
|
|
this.grabbedElement.stopTransformation();
|
|
|
|
|
this.grabbedElement.startTransformation(x, y, Transformations.SCALE_PRESERVE);
|
2020-06-15 17:13:03 -03:00
|
|
|
}
|
|
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
if (controlPressed && this.grabbedElement.lastTransformation.type == Transformations.REFLECTION) {
|
|
|
|
|
this.grabbedElement.transformations.pop();
|
|
|
|
|
this.grabbedElement.startTransformation(x, y, Transformations.INVERSION);
|
|
|
|
|
} else if (!controlPressed && this.grabbedElement.lastTransformation.type == Transformations.INVERSION) {
|
|
|
|
|
this.grabbedElement.transformations.pop();
|
|
|
|
|
this.grabbedElement.startTransformation(x, y, Transformations.REFLECTION);
|
2020-06-17 13:30:57 -03:00
|
|
|
}
|
|
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
this.grabbedElement.updateTransformation(x, y);
|
2020-06-13 08:53:52 -03:00
|
|
|
this._redisplay();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_stopTransforming: function() {
|
|
|
|
|
if (this.motionHandler) {
|
|
|
|
|
this.disconnect(this.motionHandler);
|
|
|
|
|
this.motionHandler = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.buttonReleasedHandler) {
|
|
|
|
|
this.disconnect(this.buttonReleasedHandler);
|
|
|
|
|
this.buttonReleasedHandler = null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 19:57:54 -03:00
|
|
|
this.grabbedElement.stopTransformation();
|
|
|
|
|
this.grabbedElement = null;
|
|
|
|
|
this.grabbedElementLocked = false;
|
2020-06-13 08:53:52 -03:00
|
|
|
this._redisplay();
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-26 20:59:48 -03:00
|
|
|
_startDrawing: function(stageX, stageY, eraser) {
|
2019-03-05 08:36:59 -03:00
|
|
|
let [success, startX, startY] = this.transform_stage_point(stageX, stageY);
|
|
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.buttonReleasedHandler = this.connect('button-release-event', (actor, event) => {
|
|
|
|
|
this._stopDrawing();
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
if (this.currentTool == Shapes.TEXT) {
|
2020-07-08 06:47:51 -03:00
|
|
|
this.currentElement = new Elements.DrawingElement({
|
|
|
|
|
shape: this.currentTool,
|
|
|
|
|
color: this.currentColor.to_string(),
|
|
|
|
|
eraser: eraser,
|
2020-09-04 19:03:05 -03:00
|
|
|
font: this.currentFont.to_string(),
|
2020-07-08 06:47:51 -03:00
|
|
|
text: _("Text"),
|
|
|
|
|
textRightAligned: this.currentTextRightAligned,
|
|
|
|
|
points: []
|
|
|
|
|
});
|
2020-07-30 06:13:23 -03:00
|
|
|
} else if (this.currentTool == Shapes.IMAGE) {
|
|
|
|
|
let images = this.getImages();
|
|
|
|
|
if (!images.length)
|
|
|
|
|
return;
|
|
|
|
|
this.currentElement = new Elements.DrawingElement({
|
|
|
|
|
shape: this.currentTool,
|
|
|
|
|
color: this.currentColor.to_string(),
|
|
|
|
|
eraser: eraser,
|
|
|
|
|
image: images[this.currentImage],
|
|
|
|
|
operator: this.currentOperator,
|
|
|
|
|
points: []
|
|
|
|
|
});
|
2020-07-08 06:47:51 -03:00
|
|
|
} else {
|
|
|
|
|
this.currentElement = new Elements.DrawingElement({
|
|
|
|
|
shape: this.currentTool,
|
|
|
|
|
color: this.currentColor.to_string(),
|
|
|
|
|
eraser: eraser,
|
|
|
|
|
fill: this.fill,
|
|
|
|
|
fillRule: this.currentFillRule,
|
|
|
|
|
line: { lineWidth: this.currentLineWidth, lineJoin: this.currentLineJoin, lineCap: this.currentLineCap },
|
|
|
|
|
dash: { active: this.dashedLine, array: this.dashedLine ? [this.dashArray[0] || this.currentLineWidth, this.dashArray[1] || this.currentLineWidth * 3] : [0, 0] , offset: this.dashOffset },
|
|
|
|
|
points: []
|
|
|
|
|
});
|
2019-03-27 22:00:57 -03:00
|
|
|
}
|
|
|
|
|
|
2020-06-11 22:45:51 -03:00
|
|
|
this.currentElement.startDrawing(startX, startY);
|
|
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
if (this.currentTool == Shapes.POLYGON || this.currentTool == Shapes.POLYLINE)
|
2020-06-28 08:10:31 -03:00
|
|
|
this.emit('show-osd', null, _("Press <i>%s</i> to mark vertices")
|
|
|
|
|
.format(Gtk.accelerator_get_label(Clutter.KEY_Return, 0)), "", -1, true);
|
2020-06-07 13:57:11 -03:00
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
this.motionHandler = this.connect('motion-event', (actor, event) => {
|
2020-03-01 09:25:48 -03:00
|
|
|
if (this.spaceKeyPressed)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
let coords = event.get_coords();
|
|
|
|
|
let [s, x, y] = this.transform_stage_point(coords[0], coords[1]);
|
|
|
|
|
if (!s)
|
|
|
|
|
return;
|
2019-03-07 10:28:35 -03:00
|
|
|
let controlPressed = event.has_control_modifier();
|
|
|
|
|
this._updateDrawing(x, y, controlPressed);
|
2019-03-05 08:36:59 -03:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-11 22:45:51 -03:00
|
|
|
_updateDrawing: function(x, y, controlPressed) {
|
|
|
|
|
if (!this.currentElement)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.currentElement.updateDrawing(x, y, controlPressed);
|
|
|
|
|
|
|
|
|
|
this._redisplay();
|
|
|
|
|
this.updatePointerCursor(controlPressed);
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
_stopDrawing: function() {
|
|
|
|
|
if (this.motionHandler) {
|
|
|
|
|
this.disconnect(this.motionHandler);
|
|
|
|
|
this.motionHandler = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.buttonReleasedHandler) {
|
|
|
|
|
this.disconnect(this.buttonReleasedHandler);
|
|
|
|
|
this.buttonReleasedHandler = null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 13:57:11 -03:00
|
|
|
// skip when a polygon has not at least 3 points
|
|
|
|
|
if (this.currentElement && this.currentElement.shape == Shapes.POLYGON && this.currentElement.points.length < 3)
|
|
|
|
|
this.currentElement = null;
|
|
|
|
|
|
2020-06-11 22:45:51 -03:00
|
|
|
if (this.currentElement)
|
|
|
|
|
this.currentElement.stopDrawing();
|
2020-06-07 13:57:11 -03:00
|
|
|
|
|
|
|
|
if (this.currentElement && this.currentElement.points.length >= 2) {
|
2020-06-25 06:41:15 -03:00
|
|
|
if (this.currentElement.shape == Shapes.TEXT && !this.isWriting) {
|
|
|
|
|
this._startWriting();
|
2019-03-08 08:01:19 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
|
|
|
|
|
this.elements.push(this.currentElement);
|
|
|
|
|
}
|
2019-03-08 08:01:19 -03:00
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
this.currentElement = null;
|
|
|
|
|
this._redisplay();
|
2019-11-27 02:04:37 -03:00
|
|
|
this.updatePointerCursor();
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-25 06:41:15 -03:00
|
|
|
_startWriting: function() {
|
2020-07-11 10:59:08 -03:00
|
|
|
let [x, y] = [this.currentElement.x, this.currentElement.y];
|
2020-06-25 06:41:15 -03:00
|
|
|
this.currentElement.text = '';
|
|
|
|
|
this.currentElement.cursorPosition = 0;
|
2020-06-28 08:10:31 -03:00
|
|
|
this.emit('show-osd', null, _("Type your text and press <i>%s</i>")
|
|
|
|
|
.format(Gtk.accelerator_get_label(Clutter.KEY_Escape, 0)), "", -1, true);
|
2020-06-25 06:41:15 -03:00
|
|
|
this._updateTextCursorTimeout();
|
|
|
|
|
this.textHasCursor = true;
|
|
|
|
|
this._redisplay();
|
|
|
|
|
|
2020-07-11 10:59:08 -03:00
|
|
|
this.textEntry = new St.Entry({ visible: false, x, y });
|
2020-06-25 06:41:15 -03:00
|
|
|
this.get_parent().add_child(this.textEntry);
|
|
|
|
|
this.textEntry.grab_key_focus();
|
|
|
|
|
this.updateActionMode();
|
2020-06-28 13:37:42 -03:00
|
|
|
this.updatePointerCursor();
|
2020-06-25 06:41:15 -03:00
|
|
|
|
2020-07-11 10:59:08 -03:00
|
|
|
let ibusCandidatePopup = Main.layoutManager.uiGroup.get_children().filter(child =>
|
|
|
|
|
child.has_style_class_name && child.has_style_class_name('candidate-popup-boxpointer'))[0] || null;
|
|
|
|
|
if (ibusCandidatePopup) {
|
|
|
|
|
this.ibusHandler = ibusCandidatePopup.connect('notify::visible', popup => popup.visible && (this.textEntry.visible = true));
|
|
|
|
|
this.textEntry.connect('destroy', () => ibusCandidatePopup.disconnect(this.ibusHandler));
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 06:41:15 -03:00
|
|
|
this.textEntry.clutterText.connect('activate', (clutterText) => {
|
|
|
|
|
let startNewLine = true;
|
|
|
|
|
this._stopWriting(startNewLine);
|
|
|
|
|
clutterText.text = "";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.textEntry.clutterText.connect('text-changed', (clutterText) => {
|
|
|
|
|
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
|
|
|
|
|
this.currentElement.text = clutterText.text;
|
|
|
|
|
this.currentElement.cursorPosition = clutterText.cursorPosition;
|
|
|
|
|
this._updateTextCursorTimeout();
|
|
|
|
|
this._redisplay();
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-06-13 08:53:52 -03:00
|
|
|
|
2020-06-25 06:41:15 -03:00
|
|
|
this.textEntry.clutterText.connect('key-press-event', (clutterText, event) => {
|
|
|
|
|
if (event.get_key_symbol() == Clutter.KEY_Escape) {
|
|
|
|
|
this._stopWriting();
|
|
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 'cursor-changed' signal is not emitted if the text entry is not visible.
|
|
|
|
|
// So key events related to the cursor must be listened.
|
|
|
|
|
if (event.get_key_symbol() == Clutter.KEY_Left || event.get_key_symbol() == Clutter.KEY_Right ||
|
|
|
|
|
event.get_key_symbol() == Clutter.KEY_Home || event.get_key_symbol() == Clutter.KEY_End) {
|
|
|
|
|
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
|
|
|
|
|
this.currentElement.cursorPosition = clutterText.cursorPosition;
|
|
|
|
|
this._updateTextCursorTimeout();
|
|
|
|
|
this.textHasCursor = true;
|
|
|
|
|
this._redisplay();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_stopWriting: function(startNewLine) {
|
2019-03-06 00:29:46 -03:00
|
|
|
if (this.currentElement.text.length > 0)
|
|
|
|
|
this.elements.push(this.currentElement);
|
2020-06-25 06:41:15 -03:00
|
|
|
|
2020-06-05 19:13:21 -03:00
|
|
|
if (startNewLine && this.currentElement.points.length == 2) {
|
2020-06-13 08:53:52 -03:00
|
|
|
this.currentElement.lineIndex = this.currentElement.lineIndex || 0;
|
2020-06-05 19:13:21 -03:00
|
|
|
// copy object, the original keep existing in this.elements
|
|
|
|
|
this.currentElement = Object.create(this.currentElement);
|
2020-06-13 08:53:52 -03:00
|
|
|
this.currentElement.lineIndex ++;
|
2020-06-05 19:13:21 -03:00
|
|
|
// define a new 'points' array, the original keep existing in this.elements
|
|
|
|
|
this.currentElement.points = [
|
2020-07-11 10:59:08 -03:00
|
|
|
[this.currentElement.points[0][0], this.currentElement.points[0][1] + this.currentElement.height],
|
|
|
|
|
[this.currentElement.points[1][0], this.currentElement.points[1][1] + this.currentElement.height]
|
2020-06-05 19:13:21 -03:00
|
|
|
];
|
|
|
|
|
this.currentElement.text = "";
|
2020-07-11 10:59:08 -03:00
|
|
|
this.textEntry.set_y(this.currentElement.y);
|
2020-06-05 19:13:21 -03:00
|
|
|
} else {
|
|
|
|
|
this.currentElement = null;
|
|
|
|
|
this._stopTextCursorTimeout();
|
2020-06-25 06:41:15 -03:00
|
|
|
this.textEntry.destroy();
|
|
|
|
|
delete this.textEntry;
|
|
|
|
|
this.grab_key_focus();
|
|
|
|
|
this.updateActionMode();
|
2020-06-28 13:37:42 -03:00
|
|
|
this.updatePointerCursor();
|
2020-06-05 19:13:21 -03:00
|
|
|
}
|
2020-06-25 06:41:15 -03:00
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
this._redisplay();
|
|
|
|
|
},
|
|
|
|
|
|
2019-11-27 02:04:37 -03:00
|
|
|
setPointerCursor: function(pointerCursorName) {
|
|
|
|
|
if (!this.currentPointerCursorName || this.currentPointerCursorName != pointerCursorName) {
|
|
|
|
|
this.currentPointerCursorName = pointerCursorName;
|
2020-08-31 08:05:41 -03:00
|
|
|
Me.stateObj.areaManager.setCursor(pointerCursorName);
|
2019-11-27 02:04:37 -03:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updatePointerCursor: function(controlPressed) {
|
2020-06-17 19:57:54 -03:00
|
|
|
if (this.currentTool == Manipulations.MIRROR && this.grabbedElementLocked)
|
2020-06-17 13:30:57 -03:00
|
|
|
this.setPointerCursor('CROSSHAIR');
|
2020-06-27 17:35:43 -03:00
|
|
|
else if (this.hasManipulationTool)
|
2020-06-17 19:57:54 -03:00
|
|
|
this.setPointerCursor(this.grabbedElement ? 'MOVE_OR_RESIZE_WINDOW' : 'DEFAULT');
|
2020-06-28 13:37:42 -03:00
|
|
|
else if (this.currentElement && this.currentElement.shape == Shapes.TEXT && this.isWriting)
|
|
|
|
|
this.setPointerCursor('IBEAM');
|
|
|
|
|
else if (!this.currentElement)
|
2020-06-13 08:53:52 -03:00
|
|
|
this.setPointerCursor(this.currentTool == Shapes.NONE ? 'POINTING_HAND' : 'CROSSHAIR');
|
2019-11-27 02:04:37 -03:00
|
|
|
else if (this.currentElement.shape != Shapes.NONE && controlPressed)
|
|
|
|
|
this.setPointerCursor('MOVE_OR_RESIZE_WINDOW');
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-20 20:08:45 -03:00
|
|
|
initPointerCursor: function() {
|
|
|
|
|
this.currentPointerCursorName = null;
|
|
|
|
|
this.updatePointerCursor();
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-09 13:56:40 -03:00
|
|
|
_stopTextCursorTimeout: function() {
|
|
|
|
|
if (this.textCursorTimeoutId) {
|
2020-06-18 20:35:25 -03:00
|
|
|
GLib.source_remove(this.textCursorTimeoutId);
|
2020-02-09 13:56:40 -03:00
|
|
|
this.textCursorTimeoutId = null;
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
this.textHasCursor = false;
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-09 13:56:40 -03:00
|
|
|
_updateTextCursorTimeout: function() {
|
|
|
|
|
this._stopTextCursorTimeout();
|
2020-06-18 20:35:25 -03:00
|
|
|
this.textCursorTimeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, TEXT_CURSOR_TIME, () => {
|
2019-03-05 08:36:59 -03:00
|
|
|
this.textHasCursor = !this.textHasCursor;
|
|
|
|
|
this._redisplay();
|
2020-01-01 21:44:08 -03:00
|
|
|
return GLib.SOURCE_CONTINUE;
|
2019-03-05 08:36:59 -03:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
erase: function() {
|
2020-06-25 06:41:15 -03:00
|
|
|
this.deleteLastElement();
|
2019-03-05 08:36:59 -03:00
|
|
|
this.elements = [];
|
|
|
|
|
this.undoneElements = [];
|
|
|
|
|
this._redisplay();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
deleteLastElement: function() {
|
|
|
|
|
if (this.currentElement) {
|
|
|
|
|
if (this.motionHandler) {
|
|
|
|
|
this.disconnect(this.motionHandler);
|
|
|
|
|
this.motionHandler = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.buttonReleasedHandler) {
|
|
|
|
|
this.disconnect(this.buttonReleasedHandler);
|
|
|
|
|
this.buttonReleasedHandler = null;
|
|
|
|
|
}
|
2020-06-25 06:41:15 -03:00
|
|
|
if (this.isWriting)
|
|
|
|
|
this._stopWriting();
|
2019-03-05 08:36:59 -03:00
|
|
|
this.currentElement = null;
|
|
|
|
|
} else {
|
|
|
|
|
this.elements.pop();
|
|
|
|
|
}
|
|
|
|
|
this._redisplay();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
undo: function() {
|
|
|
|
|
if (this.elements.length > 0)
|
|
|
|
|
this.undoneElements.push(this.elements.pop());
|
|
|
|
|
this._redisplay();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
redo: function() {
|
|
|
|
|
if (this.undoneElements.length > 0)
|
|
|
|
|
this.elements.push(this.undoneElements.pop());
|
|
|
|
|
this._redisplay();
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 17:08:43 -03:00
|
|
|
smoothLastElement: function() {
|
|
|
|
|
if (this.elements.length > 0 && this.elements[this.elements.length - 1].shape == Shapes.NONE) {
|
|
|
|
|
this.elements[this.elements.length - 1].smoothAll();
|
|
|
|
|
this._redisplay();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
toggleBackground: function() {
|
|
|
|
|
this.hasBackground = !this.hasBackground;
|
2020-08-31 04:43:00 -03:00
|
|
|
this.get_parent().set_background_color(this.hasBackground ? this.areaBackgroundColor : null);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-05 10:58:58 -03:00
|
|
|
toggleGrid: function() {
|
|
|
|
|
this.hasGrid = !this.hasGrid;
|
|
|
|
|
this._redisplay();
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-07 12:32:06 -03:00
|
|
|
toggleSquareArea: function() {
|
|
|
|
|
this.isSquareArea = !this.isSquareArea;
|
|
|
|
|
if (this.isSquareArea) {
|
2020-08-31 04:43:00 -03:00
|
|
|
this.set_position((this.monitor.width - this.squareAreaSize) / 2, (this.monitor.height - this.squareAreaSize) / 2);
|
|
|
|
|
this.set_size(this.squareAreaSize, this.squareAreaSize);
|
2019-03-07 12:32:06 -03:00
|
|
|
this.add_style_class_name('draw-on-your-screen-square-area');
|
|
|
|
|
} else {
|
2019-03-11 14:08:19 -03:00
|
|
|
this.set_position(0, 0);
|
2019-03-07 12:32:06 -03:00
|
|
|
this.set_size(this.monitor.width, this.monitor.height);
|
|
|
|
|
this.remove_style_class_name('draw-on-your-screen-square-area');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
selectColor: function(index) {
|
2020-08-31 04:43:00 -03:00
|
|
|
if (!this.colors[index])
|
|
|
|
|
return;
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
this.currentColor = this.colors[index];
|
|
|
|
|
if (this.currentElement) {
|
2019-03-11 13:53:35 -03:00
|
|
|
this.currentElement.color = this.currentColor.to_string();
|
2019-03-05 08:36:59 -03:00
|
|
|
this._redisplay();
|
|
|
|
|
}
|
2020-06-01 14:28:06 -03:00
|
|
|
// Foreground color markup is not displayed since 3.36, use style instead but the transparency is lost.
|
2020-08-31 04:43:00 -03:00
|
|
|
this.emit('show-osd', null, this.currentColor.string || this.currentColor.to_string(), this.currentColor.to_string().slice(0, 7), -1, false);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-13 08:53:52 -03:00
|
|
|
selectTool: function(tool) {
|
|
|
|
|
this.currentTool = tool;
|
2020-09-07 13:56:48 -03:00
|
|
|
this.emit('show-osd', null, DisplayStrings.Tool[tool], "", -1, false);
|
2019-11-27 02:04:37 -03:00
|
|
|
this.updatePointerCursor();
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-05 19:04:12 -03:00
|
|
|
switchFill: function() {
|
2019-03-26 20:59:48 -03:00
|
|
|
this.fill = !this.fill;
|
2020-09-07 13:56:48 -03:00
|
|
|
this.emit('show-osd', null, DisplayStrings.getFill(this.fill), "", -1, false);
|
2019-03-26 20:59:48 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-31 04:43:00 -03:00
|
|
|
switchFillRule: function() {
|
|
|
|
|
this.currentFillRule = this.currentFillRule == 1 ? 0 : this.currentFillRule + 1;
|
2020-09-07 13:56:48 -03:00
|
|
|
this.emit('show-osd', null, DisplayStrings.FillRule[this.currentFillRule], "", -1, false);
|
2020-08-31 04:43:00 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
switchColorPalette: function(reverse) {
|
|
|
|
|
let index = this.palettes.indexOf(this.currentPalette);
|
|
|
|
|
if (reverse)
|
|
|
|
|
this.currentPalette = index <= 0 ? this.palettes[this.palettes.length - 1] : this.palettes[index - 1];
|
|
|
|
|
else
|
|
|
|
|
this.currentPalette = index == this.palettes.length - 1 ? this.palettes[0] : this.palettes[index + 1];
|
|
|
|
|
this.emit('show-osd', null, this.currentPalette[0], "", -1, false);
|
|
|
|
|
},
|
|
|
|
|
|
2020-08-05 19:04:12 -03:00
|
|
|
switchDash: function() {
|
2019-03-05 08:36:59 -03:00
|
|
|
this.dashedLine = !this.dashedLine;
|
2020-09-07 13:56:48 -03:00
|
|
|
this.emit('show-osd', null, DisplayStrings.getDashedLine(this.dashedLine), "", -1, false);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
incrementLineWidth: function(increment) {
|
2019-10-11 08:26:08 -03:00
|
|
|
this.currentLineWidth = Math.max(this.currentLineWidth + increment, 0);
|
2020-09-07 13:56:48 -03:00
|
|
|
this.emit('show-osd', null, DisplayStrings.getPixels(this.currentLineWidth), "", 2 * this.currentLineWidth, false);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-05 19:04:12 -03:00
|
|
|
switchLineJoin: function() {
|
2019-03-05 08:36:59 -03:00
|
|
|
this.currentLineJoin = this.currentLineJoin == 2 ? 0 : this.currentLineJoin + 1;
|
2020-09-07 13:56:48 -03:00
|
|
|
this.emit('show-osd', null, DisplayStrings.LineJoin[this.currentLineJoin], "", -1, false);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-05 19:04:12 -03:00
|
|
|
switchLineCap: function() {
|
2019-03-05 08:36:59 -03:00
|
|
|
this.currentLineCap = this.currentLineCap == 2 ? 0 : this.currentLineCap + 1;
|
2020-09-07 13:56:48 -03:00
|
|
|
this.emit('show-osd', null, DisplayStrings.LineCap[this.currentLineCap], "", -1, false);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-05 19:04:12 -03:00
|
|
|
switchFontWeight: function() {
|
2020-09-07 13:56:48 -03:00
|
|
|
let fontWeights = Object.keys(DisplayStrings.FontWeight).map(key => Number(key));
|
2020-06-26 07:19:53 -03:00
|
|
|
let index = fontWeights.indexOf(this.currentFontWeight);
|
|
|
|
|
this.currentFontWeight = index == fontWeights.length - 1 ? fontWeights[0] : fontWeights[index + 1];
|
|
|
|
|
if (this.currentElement && this.currentElement.font) {
|
2020-09-04 19:03:05 -03:00
|
|
|
this.currentElement.font.set_weight(this.currentFontWeight);
|
2019-03-05 08:36:59 -03:00
|
|
|
this._redisplay();
|
|
|
|
|
}
|
2020-06-28 08:10:31 -03:00
|
|
|
this.emit('show-osd', null, `<span font_weight="${this.currentFontWeight}">` +
|
2020-09-07 13:56:48 -03:00
|
|
|
`${DisplayStrings.FontWeight[this.currentFontWeight]}</span>`, "", -1, false);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-05 19:04:12 -03:00
|
|
|
switchFontStyle: function() {
|
2019-03-05 08:36:59 -03:00
|
|
|
this.currentFontStyle = this.currentFontStyle == 2 ? 0 : this.currentFontStyle + 1;
|
2020-06-26 07:19:53 -03:00
|
|
|
if (this.currentElement && this.currentElement.font) {
|
2020-09-04 19:03:05 -03:00
|
|
|
this.currentElement.font.set_style(this.currentFontStyle);
|
2019-03-05 08:36:59 -03:00
|
|
|
this._redisplay();
|
|
|
|
|
}
|
2020-09-07 13:56:48 -03:00
|
|
|
this.emit('show-osd', null, `<span font_style="${DisplayStrings.FontStyleMarkup[this.currentFontStyle]}">` +
|
|
|
|
|
`${DisplayStrings.FontStyle[this.currentFontStyle]}</span>`, "", -1, false);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-05 19:26:10 -03:00
|
|
|
switchFontFamily: function(reverse) {
|
2020-08-05 18:30:25 -03:00
|
|
|
let index = Math.max(0, this.fontFamilies.indexOf(this.currentFontFamily));
|
2020-08-05 19:26:10 -03:00
|
|
|
if (reverse)
|
|
|
|
|
this.currentFontFamily = (index == 0) ? this.fontFamilies[this.fontFamilies.length - 1] : this.fontFamilies[index - 1];
|
|
|
|
|
else
|
|
|
|
|
this.currentFontFamily = (index == this.fontFamilies.length - 1) ? this.fontFamilies[0] : this.fontFamilies[index + 1];
|
2020-06-26 07:19:53 -03:00
|
|
|
if (this.currentElement && this.currentElement.font) {
|
2020-09-04 19:03:05 -03:00
|
|
|
this.currentElement.font.set_family(this.currentFontFamily);
|
2019-03-05 08:36:59 -03:00
|
|
|
this._redisplay();
|
|
|
|
|
}
|
2020-09-07 13:56:48 -03:00
|
|
|
this.emit('show-osd', null, `<span font_family="${this.currentFontFamily}">${DisplayStrings.getFontFamily(this.currentFontFamily)}</span>`, "", -1, false);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-05 19:04:12 -03:00
|
|
|
switchTextAlignment: function() {
|
2020-06-27 10:10:01 -03:00
|
|
|
this.currentTextRightAligned = !this.currentTextRightAligned;
|
|
|
|
|
if (this.currentElement && this.currentElement.textRightAligned !== undefined) {
|
|
|
|
|
this.currentElement.textRightAligned = this.currentTextRightAligned;
|
|
|
|
|
this._redisplay();
|
|
|
|
|
}
|
2020-09-07 13:56:48 -03:00
|
|
|
this.emit('show-osd', null, DisplayStrings.getTextAlignment(this.currentTextRightAligned), "", -1, false);
|
2020-06-27 10:10:01 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-05 19:04:12 -03:00
|
|
|
switchImageFile: function() {
|
2020-07-30 06:13:23 -03:00
|
|
|
let images = this.getImages();
|
|
|
|
|
if (!images.length)
|
|
|
|
|
return;
|
|
|
|
|
if (images.length > 1)
|
|
|
|
|
this.currentImage = this.currentImage == images.length - 1 ? 0 : this.currentImage + 1;
|
|
|
|
|
this.emit('show-osd-gicon', images[this.currentImage].gicon, images[this.currentImage].toString(), "", -1, false);
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
toggleHelp: function() {
|
2020-06-25 06:41:15 -03:00
|
|
|
if (this.helper.visible) {
|
2019-03-05 08:36:59 -03:00
|
|
|
this.helper.hideHelp();
|
2020-06-25 06:41:15 -03:00
|
|
|
if (this.textEntry)
|
|
|
|
|
this.textEntry.grab_key_focus();
|
|
|
|
|
} else {
|
2019-03-05 08:36:59 -03:00
|
|
|
this.helper.showHelp();
|
2020-06-25 06:41:15 -03:00
|
|
|
this.grab_key_focus();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2020-06-22 06:56:34 -03:00
|
|
|
// The area is reactive when it is modal.
|
|
|
|
|
_onReactiveChanged: function() {
|
|
|
|
|
if (this.hasGrid)
|
|
|
|
|
this._redisplay();
|
|
|
|
|
if (this.helper.visible)
|
2020-06-25 06:41:15 -03:00
|
|
|
this.toggleHelp();
|
|
|
|
|
if (this.textEntry && this.reactive)
|
|
|
|
|
this.textEntry.grab_key_focus();
|
2020-06-22 06:56:34 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_onDestroy: function() {
|
2020-08-31 04:43:00 -03:00
|
|
|
Me.drawingSettings.disconnect(this.drawingSettingsChangedHandler);
|
2020-06-22 06:56:34 -03:00
|
|
|
this.erase();
|
|
|
|
|
if (this._menu)
|
|
|
|
|
this._menu.disable();
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-25 06:41:15 -03:00
|
|
|
updateActionMode: function() {
|
|
|
|
|
this.emit('update-action-mode');
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
enterDrawingMode: function() {
|
2020-03-01 09:25:48 -03:00
|
|
|
this.stageKeyPressedHandler = global.stage.connect('key-press-event', this._onStageKeyPressed.bind(this));
|
|
|
|
|
this.stageKeyReleasedHandler = global.stage.connect('key-release-event', this._onStageKeyReleased.bind(this));
|
|
|
|
|
this.keyPressedHandler = this.connect('key-press-event', this._onKeyPressed.bind(this));
|
2019-03-05 08:36:59 -03:00
|
|
|
this.buttonPressedHandler = this.connect('button-press-event', this._onButtonPressed.bind(this));
|
2020-08-05 18:30:25 -03:00
|
|
|
this.keyboardPopupMenuHandler = this.connect('popup-menu', this._onKeyboardPopupMenu.bind(this));
|
2019-03-05 08:36:59 -03:00
|
|
|
this.scrollHandler = this.connect('scroll-event', this._onScroll.bind(this));
|
2020-08-31 04:43:00 -03:00
|
|
|
this.get_parent().set_background_color(this.reactive && this.hasBackground ? this.areaBackgroundColor : null);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-11 13:53:35 -03:00
|
|
|
leaveDrawingMode: function(save) {
|
2020-03-01 09:25:48 -03:00
|
|
|
if (this.stageKeyPressedHandler) {
|
|
|
|
|
global.stage.disconnect(this.stageKeyPressedHandler);
|
|
|
|
|
this.stageKeyPressedHandler = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.stageKeyReleasedHandler) {
|
|
|
|
|
global.stage.disconnect(this.stageKeyReleasedHandler);
|
|
|
|
|
this.stageKeyReleasedHandler = null;
|
|
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
if (this.keyPressedHandler) {
|
|
|
|
|
this.disconnect(this.keyPressedHandler);
|
|
|
|
|
this.keyPressedHandler = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.buttonPressedHandler) {
|
|
|
|
|
this.disconnect(this.buttonPressedHandler);
|
|
|
|
|
this.buttonPressedHandler = null;
|
|
|
|
|
}
|
2020-08-05 18:30:25 -03:00
|
|
|
if (this.keyboardPopupMenuHandler) {
|
|
|
|
|
this.disconnect(this.keyboardPopupMenuHandler);
|
|
|
|
|
this.keyboardPopupMenuHandler = null;
|
2019-03-27 10:23:34 -03:00
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
if (this.motionHandler) {
|
|
|
|
|
this.disconnect(this.motionHandler);
|
|
|
|
|
this.motionHandler = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.buttonReleasedHandler) {
|
|
|
|
|
this.disconnect(this.buttonReleasedHandler);
|
|
|
|
|
this.buttonReleasedHandler = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.scrollHandler) {
|
|
|
|
|
this.disconnect(this.scrollHandler);
|
|
|
|
|
this.scrollHandler = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.currentElement = null;
|
2020-02-09 13:56:40 -03:00
|
|
|
this._stopTextCursorTimeout();
|
2019-03-05 08:36:59 -03:00
|
|
|
this._redisplay();
|
2020-06-22 06:56:34 -03:00
|
|
|
this.closeMenu();
|
2019-03-05 08:36:59 -03:00
|
|
|
this.get_parent().set_background_color(null);
|
2019-03-11 13:53:35 -03:00
|
|
|
if (save)
|
2020-01-03 20:16:20 -03:00
|
|
|
this.savePersistent();
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-11 13:53:35 -03:00
|
|
|
saveAsSvg: function() {
|
2019-03-05 08:36:59 -03:00
|
|
|
// stop drawing or writing
|
2020-06-25 06:41:15 -03:00
|
|
|
if (this.currentElement && this.currentElement.shape == Shapes.TEXT && this.isWriting) {
|
2019-03-05 08:36:59 -03:00
|
|
|
this._stopWriting();
|
2019-03-27 22:00:57 -03:00
|
|
|
} else if (this.currentElement && this.currentElement.shape != Shapes.TEXT) {
|
2019-03-05 08:36:59 -03:00
|
|
|
this._stopDrawing();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 06:13:23 -03:00
|
|
|
let prefixes = 'xmlns="http://www.w3.org/2000/svg"';
|
|
|
|
|
if (this.elements.some(element => element.shape == Shapes.IMAGE))
|
|
|
|
|
prefixes += ' xmlns:xlink="http://www.w3.org/1999/xlink"';
|
|
|
|
|
let content = `<svg viewBox="0 0 ${this.width} ${this.height}" ${prefixes}>`;
|
2020-06-17 10:43:23 -03:00
|
|
|
if (SVG_DEBUG_EXTENDS)
|
|
|
|
|
content = `<svg viewBox="${-this.width} ${-this.height} ${2 * this.width} ${2 * this.height}" xmlns="http://www.w3.org/2000/svg">`;
|
2020-08-31 04:43:00 -03:00
|
|
|
let backgroundColorString = this.hasBackground ? this.areaBackgroundColor.to_string() : 'transparent';
|
2019-03-05 08:36:59 -03:00
|
|
|
if (backgroundColorString != 'transparent') {
|
|
|
|
|
content += `\n <rect id="background" width="100%" height="100%" fill="${backgroundColorString}"/>`;
|
|
|
|
|
}
|
2020-06-17 10:43:23 -03:00
|
|
|
if (SVG_DEBUG_EXTENDS) {
|
|
|
|
|
content += `\n <line stroke="black" x1="0" y1="${-this.height}" x2="0" y2="${this.height}"/>`;
|
|
|
|
|
content += `\n <line stroke="black" x1="${-this.width}" y1="0" x2="${this.width}" y2="0"/>`;
|
|
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
for (let i = 0; i < this.elements.length; i++) {
|
|
|
|
|
content += this.elements[i].buildSVG(backgroundColorString);
|
|
|
|
|
}
|
|
|
|
|
content += "\n</svg>";
|
|
|
|
|
|
2020-08-04 21:00:20 -03:00
|
|
|
let filename = `${Me.metadata['svg-file-name']} ${Files.getDateString()}.svg`;
|
2019-03-05 08:36:59 -03:00
|
|
|
let dir = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_PICTURES);
|
|
|
|
|
let path = GLib.build_filenamev([dir, filename]);
|
|
|
|
|
if (GLib.file_test(path, GLib.FileTest.EXISTS))
|
|
|
|
|
return false;
|
|
|
|
|
let success = GLib.file_set_contents(path, content);
|
|
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
|
// pass the parent (bgContainer) to Flashspot because coords of this are relative
|
|
|
|
|
let flashspot = new Screenshot.Flashspot(this.get_parent());
|
|
|
|
|
flashspot.fire();
|
2019-03-16 20:59:49 -03:00
|
|
|
if (global.play_theme_sound) {
|
|
|
|
|
global.play_theme_sound(0, 'screen-capture', "Save as SVG", null);
|
|
|
|
|
} else if (global.display && global.display.get_sound_player) {
|
|
|
|
|
let player = global.display.get_sound_player();
|
|
|
|
|
player.play_from_theme('screen-capture', "Save as SVG", null);
|
|
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-08-04 21:49:52 -03:00
|
|
|
_saveAsJson: function(name, notify, callback) {
|
2020-01-03 20:16:20 -03:00
|
|
|
// stop drawing or writing
|
2020-06-25 06:41:15 -03:00
|
|
|
if (this.currentElement && this.currentElement.shape == Shapes.TEXT && this.isWriting) {
|
2020-01-03 20:16:20 -03:00
|
|
|
this._stopWriting();
|
|
|
|
|
} else if (this.currentElement && this.currentElement.shape != Shapes.TEXT) {
|
|
|
|
|
this._stopDrawing();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 21:00:20 -03:00
|
|
|
let json = new Files.Json({ name });
|
2019-03-11 13:53:35 -03:00
|
|
|
let oldContents;
|
|
|
|
|
|
2020-03-03 09:48:31 -03:00
|
|
|
if (name == Me.metadata['persistent-file-name']) {
|
2020-08-04 21:00:20 -03:00
|
|
|
let oldContents = json.contents;
|
2020-03-03 09:48:31 -03:00
|
|
|
// do not create a file to write just an empty array
|
|
|
|
|
if (!oldContents && this.elements.length == 0)
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-03-11 20:29:14 -03:00
|
|
|
|
2019-03-11 13:53:35 -03:00
|
|
|
// do not use "content = JSON.stringify(this.elements, null, 2);", neither "content = JSON.stringify(this.elements);"
|
2020-08-04 21:00:20 -03:00
|
|
|
// do compromise between disk usage and human readability
|
2019-03-11 13:53:35 -03:00
|
|
|
let contents = `[\n ` + new Array(...this.elements.map(element => JSON.stringify(element))).join(`,\n\n `) + `\n]`;
|
|
|
|
|
|
2020-03-03 09:48:31 -03:00
|
|
|
if (name == Me.metadata['persistent-file-name'] && contents == oldContents)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-22 07:16:17 -03:00
|
|
|
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
|
2020-08-04 21:00:20 -03:00
|
|
|
json.contents = contents;
|
2020-06-22 07:16:17 -03:00
|
|
|
if (notify)
|
2020-06-28 08:10:31 -03:00
|
|
|
this.emit('show-osd', 'document-save-symbolic', name, "", -1, false);
|
2020-06-22 07:16:17 -03:00
|
|
|
if (name != Me.metadata['persistent-file-name']) {
|
|
|
|
|
this.jsonName = name;
|
|
|
|
|
this.lastJsonContents = contents;
|
|
|
|
|
}
|
2020-08-04 21:49:52 -03:00
|
|
|
if (callback)
|
|
|
|
|
callback();
|
2020-06-22 07:16:17 -03:00
|
|
|
});
|
2020-01-03 20:16:20 -03:00
|
|
|
},
|
|
|
|
|
|
2020-08-04 21:49:52 -03:00
|
|
|
saveAsJsonWithName: function(name, callback) {
|
|
|
|
|
this._saveAsJson(name, false, callback);
|
2020-01-03 20:16:20 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
saveAsJson: function() {
|
2020-08-04 21:00:20 -03:00
|
|
|
this._saveAsJson(Files.getDateString(), true);
|
2020-01-03 20:16:20 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
savePersistent: function() {
|
2020-01-10 08:32:28 -03:00
|
|
|
this._saveAsJson(Me.metadata['persistent-file-name']);
|
2019-03-11 13:53:35 -03:00
|
|
|
},
|
|
|
|
|
|
2020-01-10 09:01:26 -03:00
|
|
|
syncPersistent: function() {
|
|
|
|
|
// do not override peristent.json with an empty drawing when changing persistency setting
|
|
|
|
|
if (!this.elements.length)
|
|
|
|
|
this._loadPersistent();
|
|
|
|
|
else
|
|
|
|
|
this.savePersistent();
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
2020-01-03 20:16:20 -03:00
|
|
|
_loadJson: function(name, notify) {
|
2020-06-25 06:41:15 -03:00
|
|
|
// stop drawing or writing
|
|
|
|
|
if (this.currentElement && this.currentElement.shape == Shapes.TEXT && this.isWriting) {
|
|
|
|
|
this._stopWriting();
|
|
|
|
|
} else if (this.currentElement && this.currentElement.shape != Shapes.TEXT) {
|
|
|
|
|
this._stopDrawing();
|
|
|
|
|
}
|
|
|
|
|
this.elements = [];
|
|
|
|
|
this.currentElement = null;
|
|
|
|
|
|
2020-08-04 21:00:20 -03:00
|
|
|
let contents = (new Files.Json({ name })).contents;
|
|
|
|
|
if (!contents)
|
2019-03-11 13:53:35 -03:00
|
|
|
return;
|
2020-08-04 21:00:20 -03:00
|
|
|
|
2020-07-30 06:13:23 -03:00
|
|
|
this.elements.push(...JSON.parse(contents).map(object => {
|
|
|
|
|
if (object.image)
|
|
|
|
|
object.image = new Files.Image(object.image);
|
|
|
|
|
return new Elements.DrawingElement(object);
|
|
|
|
|
}));
|
2020-01-03 20:16:20 -03:00
|
|
|
|
|
|
|
|
if (notify)
|
2020-06-28 08:10:31 -03:00
|
|
|
this.emit('show-osd', 'document-open-symbolic', name, "", -1, false);
|
2020-01-10 08:32:28 -03:00
|
|
|
if (name != Me.metadata['persistent-file-name']) {
|
2020-01-03 20:16:20 -03:00
|
|
|
this.jsonName = name;
|
2020-01-04 12:07:53 -03:00
|
|
|
this.lastJsonContents = contents;
|
|
|
|
|
}
|
2020-01-03 20:16:20 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_loadPersistent: function() {
|
2020-01-10 08:32:28 -03:00
|
|
|
this._loadJson(Me.metadata['persistent-file-name']);
|
2020-01-03 20:16:20 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
loadJson: function(name, notify) {
|
|
|
|
|
this._loadJson(name, notify);
|
|
|
|
|
this._redisplay();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
loadNextJson: function() {
|
2020-08-04 21:00:20 -03:00
|
|
|
let names = Files.getJsons().map(json => json.name);
|
2020-01-03 20:16:20 -03:00
|
|
|
|
|
|
|
|
if (!names.length)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
let nextName = names[this.jsonName && names.indexOf(this.jsonName) != names.length - 1 ? names.indexOf(this.jsonName) + 1 : 0];
|
|
|
|
|
this.loadJson(nextName, true);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
loadPreviousJson: function() {
|
2020-08-04 21:00:20 -03:00
|
|
|
let names = Files.getJsons().map(json => json.name);
|
2020-01-03 20:16:20 -03:00
|
|
|
|
|
|
|
|
if (!names.length)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
let previousName = names[this.jsonName && names.indexOf(this.jsonName) > 0 ? names.indexOf(this.jsonName) - 1 : names.length - 1];
|
|
|
|
|
this.loadJson(previousName, true);
|
2019-03-11 13:53:35 -03:00
|
|
|
},
|
|
|
|
|
|
2020-01-04 08:50:43 -03:00
|
|
|
get drawingContentsHasChanged() {
|
|
|
|
|
let contents = `[\n ` + new Array(...this.elements.map(element => JSON.stringify(element))).join(`,\n\n `) + `\n]`;
|
|
|
|
|
return contents != this.lastJsonContents;
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|