2019-03-05 08:36:59 -03:00
|
|
|
/* jslint esversion: 6 */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
const Mainloop = imports.mainloop;
|
2019-03-26 18:28:24 -03:00
|
|
|
const Shell = imports.gi.Shell;
|
2019-03-05 08:36:59 -03:00
|
|
|
const St = imports.gi.St;
|
2019-03-26 18:28:24 -03:00
|
|
|
|
|
|
|
|
const BoxPointer = imports.ui.boxpointer;
|
2019-10-11 04:22:37 -03:00
|
|
|
const Config = imports.misc.config;
|
2019-03-26 18:28:24 -03:00
|
|
|
const Main = imports.ui.main;
|
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
const Slider = imports.ui.slider;
|
2019-03-05 08:36:59 -03:00
|
|
|
const Screenshot = imports.ui.screenshot;
|
|
|
|
|
const Tweener = imports.ui.tweener;
|
|
|
|
|
|
|
|
|
|
const ExtensionUtils = imports.misc.extensionUtils;
|
|
|
|
|
const Extension = ExtensionUtils.getCurrentExtension();
|
|
|
|
|
const Convenience = Extension.imports.convenience;
|
2019-03-26 18:28:24 -03:00
|
|
|
const ExtensionJs = Extension.imports.extension;
|
2019-03-05 08:36:59 -03:00
|
|
|
const Prefs = Extension.imports.prefs;
|
|
|
|
|
const _ = imports.gettext.domain(Extension.metadata["gettext-domain"]).gettext;
|
|
|
|
|
|
2019-10-11 04:22:37 -03:00
|
|
|
const GS_VERSION = Config.PACKAGE_VERSION;
|
2020-01-01 21:22:25 -03:00
|
|
|
const DEFAULT_FILE_NAME = 'DrawOnYourScreen';
|
2019-10-11 04:22:37 -03:00
|
|
|
|
2019-03-26 18:28:24 -03:00
|
|
|
const FILL_ICON_PATH = Extension.dir.get_child('icons').get_child('fill-symbolic.svg').get_path();
|
2019-03-29 12:35:18 -03:00
|
|
|
const STROKE_ICON_PATH = Extension.dir.get_child('icons').get_child('stroke-symbolic.svg').get_path();
|
2019-03-26 18:28:24 -03:00
|
|
|
const LINEJOIN_ICON_PATH = Extension.dir.get_child('icons').get_child('linejoin-symbolic.svg').get_path();
|
|
|
|
|
const LINECAP_ICON_PATH = Extension.dir.get_child('icons').get_child('linecap-symbolic.svg').get_path();
|
|
|
|
|
const DASHED_LINE_ICON_PATH = Extension.dir.get_child('icons').get_child('dashed-line-symbolic.svg').get_path();
|
2019-03-29 12:35:18 -03:00
|
|
|
const FULL_LINE_ICON_PATH = Extension.dir.get_child('icons').get_child('full-line-symbolic.svg').get_path();
|
2019-03-26 18:28:24 -03:00
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
var Shapes = { NONE: 0, LINE: 1, ELLIPSE: 2, RECTANGLE: 3, TEXT: 4 };
|
2019-03-27 22:00:57 -03:00
|
|
|
var TextState = { DRAWING: 0, WRITING: 1 };
|
2019-03-07 10:28:35 -03:00
|
|
|
var ShapeNames = { 0: "Free drawing", 1: "Line", 2: "Ellipse", 3: "Rectangle", 4: "Text" };
|
2019-03-05 08:36:59 -03:00
|
|
|
var LineCapNames = { 0: 'Butt', 1: 'Round', 2: 'Square' };
|
|
|
|
|
var LineJoinNames = { 0: 'Miter', 1: 'Round', 2: 'Bevel' };
|
|
|
|
|
var FontWeightNames = { 0: 'Normal', 1: 'Bold' };
|
|
|
|
|
var FontStyleNames = { 0: 'Normal', 1: 'Italic', 2: 'Oblique' };
|
|
|
|
|
var FontFamilyNames = { 0: 'Default', 1: 'Sans-Serif', 2: 'Serif', 3: 'Monospace', 4: 'Cursive', 5: 'Fantasy' };
|
|
|
|
|
|
2020-01-01 21:22:25 -03:00
|
|
|
function getDateString() {
|
|
|
|
|
let date = GLib.DateTime.new_now_local();
|
|
|
|
|
return `${date.format("%F")} ${date.format("%X")}`;
|
|
|
|
|
}
|
|
|
|
|
|
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-01-01 20:56:40 -03:00
|
|
|
Signals: { 'show-osd': { param_types: [GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_DOUBLE] },
|
2019-11-27 15:01:07 -03:00
|
|
|
'stop-drawing': {} },
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2019-03-11 13:53:35 -03:00
|
|
|
_init: function(params, monitor, helper, loadJson) {
|
2019-03-05 08:36:59 -03:00
|
|
|
this.parent({ style_class: 'draw-on-your-screen', name: params && params.name ? params.name : ""});
|
|
|
|
|
|
|
|
|
|
this.connect('repaint', this._repaint.bind(this));
|
|
|
|
|
|
2019-03-05 17:08:43 -03:00
|
|
|
this.settings = Convenience.getSettings();
|
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;
|
|
|
|
|
this.currentShape = Shapes.NONE;
|
2019-03-07 12:32:06 -03:00
|
|
|
this.isSquareArea = 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-05 08:36:59 -03:00
|
|
|
this.colors = [Clutter.Color.new(0, 0, 0, 255)];
|
2019-03-11 13:53:35 -03:00
|
|
|
|
|
|
|
|
if (loadJson)
|
|
|
|
|
this._loadJson();
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2019-10-11 04:39:17 -03:00
|
|
|
get menu() {
|
|
|
|
|
if (!this._menu)
|
2019-12-29 11:26:20 -03:00
|
|
|
this._menu = new DrawingMenu(this, this.monitor);
|
2019-10-11 04:39:17 -03:00
|
|
|
return this._menu;
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
_redisplay: function() {
|
|
|
|
|
// force area to emit 'repaint'
|
|
|
|
|
this.queue_repaint();
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-16 08:35:14 -03:00
|
|
|
_updateStyle: function() {
|
2019-03-05 08:36:59 -03:00
|
|
|
try {
|
|
|
|
|
let themeNode = this.get_theme_node();
|
|
|
|
|
for (let i = 1; i < 10; i++) {
|
|
|
|
|
this.colors[i] = themeNode.get_color('-drawing-color' + i);
|
|
|
|
|
}
|
|
|
|
|
this.activeBackgroundColor = themeNode.get_color('-drawing-background-color');
|
|
|
|
|
this.currentLineWidth = themeNode.get_length('-drawing-line-width');
|
|
|
|
|
this.currentLineJoin = themeNode.get_double('-drawing-line-join');
|
|
|
|
|
this.currentLineCap = themeNode.get_double('-drawing-line-cap');
|
|
|
|
|
this.dashArray = [themeNode.get_length('-drawing-dash-array-on'), themeNode.get_length('-drawing-dash-array-off')];
|
|
|
|
|
this.dashOffset = themeNode.get_length('-drawing-dash-offset');
|
|
|
|
|
let font = themeNode.get_font();
|
|
|
|
|
this.fontFamily = font.get_family();
|
|
|
|
|
this.currentFontWeight = font.get_weight();
|
|
|
|
|
this.currentFontStyle = font.get_style();
|
|
|
|
|
} catch(e) {
|
|
|
|
|
logError(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let i = 1; i < 10; i++) {
|
|
|
|
|
this.colors[i] = this.colors[i].alpha ? this.colors[i] : this.colors[0];
|
|
|
|
|
}
|
|
|
|
|
this.currentColor = this.colors[1];
|
|
|
|
|
|
|
|
|
|
this.currentLineWidth = (this.currentLineWidth > 0) ? this.currentLineWidth : 3;
|
|
|
|
|
this.currentLineJoin = ([0, 1, 2].indexOf(this.currentLineJoin) != -1) ? this.currentLineJoin : Cairo.LineJoin.ROUND;
|
|
|
|
|
this.currentLineCap = ([0, 1, 2].indexOf(this.currentLineCap) != -1) ? this.currentLineCap : Cairo.LineCap.ROUND;
|
|
|
|
|
this.currentFontFamilyId = 0;
|
|
|
|
|
this.currentFontWeight = this.currentFontWeight > 500 ? 1 : 0 ;
|
|
|
|
|
// font style enum order of Cairo and Pango are different
|
|
|
|
|
this.currentFontStyle = this.currentFontStyle == 2 ? 1 : ( this.currentFontStyle == 1 ? 2 : 0);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_repaint: function(area) {
|
|
|
|
|
let cr = area.get_context();
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this.elements.length; i++) {
|
2019-03-07 10:28:35 -03:00
|
|
|
let isStraightLine = this.elements[i].shape == Shapes.LINE &&
|
|
|
|
|
(this.elements[i].points.length < 3 || this.elements[i].points[2] == this.elements[i].points[1] || this.elements[i].points[2] == this.elements[i].points[0]);
|
2019-03-29 18:31:13 -03:00
|
|
|
|
|
|
|
|
if (this.elements[i].fill && !isStraightLine) {
|
|
|
|
|
// first paint stroke
|
|
|
|
|
this.elements[i].buildCairo(cr, false);
|
|
|
|
|
if (this.elements[i].shape == Shapes.NONE || this.elements[i].shape == Shapes.LINE)
|
|
|
|
|
cr.closePath();
|
|
|
|
|
cr.stroke();
|
|
|
|
|
// secondly paint fill
|
|
|
|
|
this.elements[i].buildCairo(cr, false);
|
2019-03-05 08:36:59 -03:00
|
|
|
cr.fill();
|
2019-03-29 18:31:13 -03:00
|
|
|
} else {
|
|
|
|
|
this.elements[i].buildCairo(cr, false);
|
2019-03-05 08:36:59 -03:00
|
|
|
cr.stroke();
|
2019-03-29 18:31:13 -03:00
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.currentElement) {
|
|
|
|
|
this.currentElement.buildCairo(cr, this.textHasCursor);
|
|
|
|
|
cr.stroke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cr.$dispose();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_onButtonPressed: function(actor, event) {
|
|
|
|
|
let button = event.get_button();
|
|
|
|
|
let [x, y] = event.get_coords();
|
|
|
|
|
let shiftPressed = event.has_shift_modifier();
|
|
|
|
|
|
|
|
|
|
// stop writing
|
2019-03-27 22:00:57 -03:00
|
|
|
if (this.currentElement && this.currentElement.shape == Shapes.TEXT && this.currentElement.state == TextState.WRITING ) {
|
2019-03-05 08:36:59 -03:00
|
|
|
this._stopWriting();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// hide helper
|
2019-03-28 18:15:31 -03:00
|
|
|
if (this.helper.visible) {
|
2019-03-05 08:36:59 -03:00
|
|
|
this.helper.hideHelp();
|
|
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (button == 1) {
|
2019-03-26 20:59:48 -03:00
|
|
|
this._startDrawing(x, y, shiftPressed);
|
2019-03-05 08:36:59 -03:00
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
} else if (button == 2) {
|
2019-03-28 18:15:31 -03:00
|
|
|
this.toggleFill();
|
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)
|
|
|
|
|
this.helper.hideHelp();
|
|
|
|
|
this.menu.popup();
|
|
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
_onKeyPressed: function(actor, event) {
|
|
|
|
|
if (event.get_key_symbol() == Clutter.Escape) {
|
2019-11-27 15:01:07 -03:00
|
|
|
this.emit('stop-drawing');
|
2019-03-05 08:36:59 -03:00
|
|
|
return Clutter.EVENT_STOP;
|
2019-03-11 15:07:01 -03:00
|
|
|
|
2019-03-27 22:00:57 -03:00
|
|
|
} else if (this.currentElement && this.currentElement.shape == Shapes.TEXT && this.currentElement.state == TextState.WRITING) {
|
2019-03-05 08:36:59 -03:00
|
|
|
if (event.get_key_symbol() == Clutter.KEY_BackSpace) {
|
|
|
|
|
this.currentElement.text = this.currentElement.text.slice(0, -1);
|
|
|
|
|
this._updateCursorTimeout();
|
|
|
|
|
} else if (event.has_control_modifier() && event.get_key_symbol() == 118) {
|
|
|
|
|
// Ctrl + V
|
|
|
|
|
St.Clipboard.get_default().get_text(St.ClipboardType.CLIPBOARD, (clipBoard, clipText) => {
|
|
|
|
|
this.currentElement.text += clipText;
|
|
|
|
|
this._updateCursorTimeout();
|
|
|
|
|
this._redisplay();
|
|
|
|
|
});
|
|
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
} else if (event.get_key_symbol() == Clutter.KEY_Return || event.get_key_symbol() == 65421) {
|
|
|
|
|
// stop writing
|
|
|
|
|
// Clutter.KEY_Return is "Enter" and 65421 is KP_Enter
|
|
|
|
|
this._stopWriting();
|
2019-11-25 07:44:18 -03:00
|
|
|
} else if (event.has_control_modifier()){
|
|
|
|
|
// it's a shortcut, do not write text
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2019-03-05 08:36:59 -03:00
|
|
|
} else {
|
|
|
|
|
let unicode = event.get_key_unicode();
|
|
|
|
|
this.currentElement.text += unicode;
|
|
|
|
|
this._updateCursorTimeout();
|
|
|
|
|
}
|
|
|
|
|
this._redisplay();
|
|
|
|
|
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;
|
|
|
|
|
},
|
|
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
|
2019-03-05 17:08:43 -03:00
|
|
|
this.smoothedStroke = this.settings.get_boolean('smoothed-stroke');
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
this.currentElement = new DrawingElement ({
|
2019-03-27 22:00:57 -03:00
|
|
|
shape: this.currentShape,
|
2019-03-11 13:53:35 -03:00
|
|
|
color: this.currentColor.to_string(),
|
2019-03-05 08:36:59 -03:00
|
|
|
line: { lineWidth: this.currentLineWidth, lineJoin: this.currentLineJoin, lineCap: this.currentLineCap },
|
|
|
|
|
dash: { array: this.dashedLine ? this.dashArray : [0, 0] , offset: this.dashedLine ? this.dashOffset : 0 },
|
2019-03-26 20:59:48 -03:00
|
|
|
fill: this.fill,
|
2019-03-05 08:36:59 -03:00
|
|
|
eraser: eraser,
|
2019-03-07 10:28:35 -03:00
|
|
|
transform: { active: false, center: [0, 0], angle: 0, startAngle: 0, ratio: 1 },
|
2019-03-05 08:36:59 -03:00
|
|
|
text: '',
|
|
|
|
|
font: { family: (this.currentFontFamilyId == 0 ? this.fontFamily : FontFamilyNames[this.currentFontFamilyId]), weight: this.currentFontWeight, style: this.currentFontStyle },
|
|
|
|
|
points: [[startX, startY]]
|
|
|
|
|
});
|
|
|
|
|
|
2019-03-27 22:00:57 -03:00
|
|
|
if (this.currentShape == Shapes.TEXT) {
|
|
|
|
|
this.currentElement.line = { lineWidth: 1, lineJoin: 0, lineCap: 0 };
|
|
|
|
|
this.currentElement.dash = { array: [1, 1] , offset: 0 };
|
|
|
|
|
this.currentElement.fill = false;
|
|
|
|
|
this.currentElement.text = _("Text");
|
|
|
|
|
this.currentElement.state = TextState.DRAWING;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
this.motionHandler = this.connect('motion-event', (actor, event) => {
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_stopDrawing: function() {
|
|
|
|
|
if (this.motionHandler) {
|
|
|
|
|
this.disconnect(this.motionHandler);
|
|
|
|
|
this.motionHandler = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.buttonReleasedHandler) {
|
|
|
|
|
this.disconnect(this.buttonReleasedHandler);
|
|
|
|
|
this.buttonReleasedHandler = null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 09:04:45 -03:00
|
|
|
// skip when the size is too small to be visible (3px) (except for free drawing)
|
2019-03-08 08:01:19 -03:00
|
|
|
if (this.currentElement && this.currentElement.points.length >= 2 &&
|
2019-03-08 09:04:45 -03:00
|
|
|
(this.currentShape == Shapes.NONE ||
|
|
|
|
|
Math.hypot(this.currentElement.points[1][0] - this.currentElement.points[0][0], this.currentElement.points[1][1] - this.currentElement.points[0][1]) > 3)) {
|
2019-03-08 08:01:19 -03:00
|
|
|
|
|
|
|
|
// start writing
|
2019-03-27 22:00:57 -03:00
|
|
|
if (this.currentElement.shape == Shapes.TEXT && this.currentElement.state == TextState.DRAWING) {
|
|
|
|
|
this.currentElement.state = TextState.WRITING;
|
2019-03-08 08:01:19 -03:00
|
|
|
this.currentElement.text = '';
|
2020-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, _("Type your text\nand press Enter"), -1);
|
2019-03-08 08:01:19 -03:00
|
|
|
this._updateCursorTimeout();
|
2019-03-27 22:00:57 -03:00
|
|
|
this.textHasCursor = true;
|
2019-03-08 08:01:19 -03:00
|
|
|
this._redisplay();
|
2019-11-27 02:04:37 -03:00
|
|
|
this.updatePointerCursor();
|
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
|
|
|
},
|
|
|
|
|
|
2019-03-07 10:28:35 -03:00
|
|
|
_updateDrawing: function(x, y, controlPressed) {
|
2019-03-05 08:36:59 -03:00
|
|
|
if (!this.currentElement)
|
|
|
|
|
return;
|
|
|
|
|
if (this.currentElement.shape == Shapes.NONE)
|
2019-03-05 17:08:43 -03:00
|
|
|
this.currentElement.addPoint(x, y, this.smoothedStroke);
|
2019-03-27 22:00:57 -03:00
|
|
|
else if ((this.currentElement.shape == Shapes.RECTANGLE || this.currentElement.shape == Shapes.TEXT) && (controlPressed || this.currentElement.transform.active))
|
2019-03-07 10:28:35 -03:00
|
|
|
this.currentElement.transformRectangle(x, y);
|
|
|
|
|
else if (this.currentElement.shape == Shapes.ELLIPSE && (controlPressed || this.currentElement.transform.active))
|
|
|
|
|
this.currentElement.transformEllipse(x, y);
|
|
|
|
|
else if (this.currentElement.shape == Shapes.LINE && (controlPressed || this.currentElement.transform.active))
|
|
|
|
|
this.currentElement.transformLine(x, y);
|
2019-03-05 08:36:59 -03:00
|
|
|
else
|
|
|
|
|
this.currentElement.points[1] = [x, y];
|
2019-11-27 02:04:37 -03:00
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
this._redisplay();
|
2019-11-27 02:04:37 -03:00
|
|
|
this.updatePointerCursor(controlPressed);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_stopWriting: function() {
|
2019-03-06 00:29:46 -03:00
|
|
|
if (this.currentElement.text.length > 0)
|
|
|
|
|
this.elements.push(this.currentElement);
|
2019-03-05 08:36:59 -03:00
|
|
|
this.currentElement = null;
|
|
|
|
|
this._stopCursorTimeout();
|
|
|
|
|
this._redisplay();
|
|
|
|
|
},
|
|
|
|
|
|
2019-11-27 02:04:37 -03:00
|
|
|
setPointerCursor: function(pointerCursorName) {
|
|
|
|
|
if (!this.currentPointerCursorName || this.currentPointerCursorName != pointerCursorName) {
|
|
|
|
|
this.currentPointerCursorName = pointerCursorName;
|
|
|
|
|
ExtensionJs.setCursor(pointerCursorName);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updatePointerCursor: function(controlPressed) {
|
|
|
|
|
if (!this.currentElement || (this.currentElement.shape == Shapes.TEXT && this.currentElement.state == TextState.WRITING))
|
|
|
|
|
this.setPointerCursor(this.currentShape == Shapes.NONE ? 'POINTING_HAND' : 'CROSSHAIR');
|
|
|
|
|
else if (this.currentElement.shape != Shapes.NONE && controlPressed)
|
|
|
|
|
this.setPointerCursor('MOVE_OR_RESIZE_WINDOW');
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
_stopCursorTimeout: function() {
|
|
|
|
|
if (this.cursorTimeoutId) {
|
|
|
|
|
Mainloop.source_remove(this.cursorTimeoutId);
|
|
|
|
|
this.cursorTimeoutId = null;
|
|
|
|
|
}
|
|
|
|
|
this.textHasCursor = false;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_updateCursorTimeout: function() {
|
|
|
|
|
this._stopCursorTimeout();
|
|
|
|
|
this.cursorTimeoutId = Mainloop.timeout_add(600, () => {
|
|
|
|
|
this.textHasCursor = !this.textHasCursor;
|
|
|
|
|
this._redisplay();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
erase: function() {
|
|
|
|
|
this.elements = [];
|
|
|
|
|
this.undoneElements = [];
|
|
|
|
|
this.currentElement = null;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
this.currentElement = null;
|
|
|
|
|
this._stopCursorTimeout();
|
|
|
|
|
} 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;
|
|
|
|
|
this.get_parent().set_background_color(this.hasBackground ? this.activeBackgroundColor : null);
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-07 12:32:06 -03:00
|
|
|
toggleSquareArea: function() {
|
|
|
|
|
this.isSquareArea = !this.isSquareArea;
|
|
|
|
|
if (this.isSquareArea) {
|
|
|
|
|
let squareWidth = Math.min(this.monitor.width, this.monitor.height) * 3 / 4;
|
2019-03-11 14:08:19 -03:00
|
|
|
this.set_position(Math.floor(this.monitor.width / 2 - squareWidth / 2), Math.floor(this.monitor.height / 2 - squareWidth / 2));
|
2019-03-07 12:32:06 -03:00
|
|
|
this.set_size(squareWidth, squareWidth);
|
|
|
|
|
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
|
|
|
toggleColor: function() {
|
|
|
|
|
this.selectColor((this.currentColor == this.colors[1]) ? 2 : 1);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
selectColor: function(index) {
|
|
|
|
|
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-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, `<span foreground="${this.currentColor.to_string()}">${this.currentColor.to_string()}</span>`, -1);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
selectShape: function(shape) {
|
|
|
|
|
this.currentShape = shape;
|
2020-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, _(ShapeNames[shape]), -1);
|
2019-11-27 02:04:37 -03:00
|
|
|
this.updatePointerCursor();
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-26 20:59:48 -03:00
|
|
|
toggleFill: function() {
|
|
|
|
|
this.fill = !this.fill;
|
2020-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, this.fill ? _("Fill") : _("Stroke"), -1);
|
2019-03-26 20:59:48 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
toggleDash: function() {
|
|
|
|
|
this.dashedLine = !this.dashedLine;
|
2020-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, this.dashedLine ? _("Dashed line") : _("Full line"), -1);
|
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-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, this.currentLineWidth + " " + _("px"), 2 * this.currentLineWidth);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
toggleLineJoin: function() {
|
|
|
|
|
this.currentLineJoin = this.currentLineJoin == 2 ? 0 : this.currentLineJoin + 1;
|
2020-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, _(LineJoinNames[this.currentLineJoin]), -1);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
toggleLineCap: function() {
|
|
|
|
|
this.currentLineCap = this.currentLineCap == 2 ? 0 : this.currentLineCap + 1;
|
2020-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, _(LineCapNames[this.currentLineCap]), -1);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
toggleFontWeight: function() {
|
|
|
|
|
this.currentFontWeight = this.currentFontWeight == 1 ? 0 : this.currentFontWeight + 1;
|
|
|
|
|
if (this.currentElement) {
|
|
|
|
|
this.currentElement.font.weight = this.currentFontWeight;
|
|
|
|
|
this._redisplay();
|
|
|
|
|
}
|
2020-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, `<span font_weight="${FontWeightNames[this.currentFontWeight].toLowerCase()}">${_(FontWeightNames[this.currentFontWeight])}</span>`, -1);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
toggleFontStyle: function() {
|
|
|
|
|
this.currentFontStyle = this.currentFontStyle == 2 ? 0 : this.currentFontStyle + 1;
|
|
|
|
|
if (this.currentElement) {
|
|
|
|
|
this.currentElement.font.style = this.currentFontStyle;
|
|
|
|
|
this._redisplay();
|
|
|
|
|
}
|
2020-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, `<span font_style="${FontStyleNames[this.currentFontStyle].toLowerCase()}">${_(FontStyleNames[this.currentFontStyle])}</span>`, -1);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
toggleFontFamily: function() {
|
|
|
|
|
this.currentFontFamilyId = this.currentFontFamilyId == 5 ? 0 : this.currentFontFamilyId + 1;
|
|
|
|
|
let currentFontFamily = this.currentFontFamilyId == 0 ? this.fontFamily : FontFamilyNames[this.currentFontFamilyId];
|
|
|
|
|
if (this.currentElement) {
|
|
|
|
|
this.currentElement.font.family = currentFontFamily;
|
|
|
|
|
this._redisplay();
|
|
|
|
|
}
|
2020-01-01 20:56:40 -03:00
|
|
|
this.emit('show-osd', null, `<span font_family="${currentFontFamily}">${_(currentFontFamily)}</span>`, -1);
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
toggleHelp: function() {
|
|
|
|
|
if (this.helper.visible)
|
|
|
|
|
this.helper.hideHelp();
|
|
|
|
|
else
|
|
|
|
|
this.helper.showHelp();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
enterDrawingMode: function() {
|
|
|
|
|
this.keyPressedHandler = this.connect('key-press-event', this._onKeyPressed.bind(this));
|
|
|
|
|
this.buttonPressedHandler = this.connect('button-press-event', this._onButtonPressed.bind(this));
|
2019-03-27 10:23:34 -03:00
|
|
|
this._onKeyboardPopupMenuHandler = 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));
|
|
|
|
|
this.get_parent().set_background_color(this.hasBackground ? this.activeBackgroundColor : null);
|
2019-03-16 08:35:14 -03:00
|
|
|
this._updateStyle();
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-11 13:53:35 -03:00
|
|
|
leaveDrawingMode: function(save) {
|
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;
|
|
|
|
|
}
|
2019-03-27 10:23:34 -03:00
|
|
|
if (this._onKeyboardPopupMenuHandler) {
|
|
|
|
|
this.disconnect(this._onKeyboardPopupMenuHandler);
|
|
|
|
|
this._onKeyboardPopupMenuHandler = null;
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.helper.visible)
|
|
|
|
|
this.helper.hideHelp();
|
|
|
|
|
|
|
|
|
|
this.currentElement = null;
|
|
|
|
|
this._stopCursorTimeout();
|
2019-03-28 18:01:39 -03:00
|
|
|
this.currentShape = Shapes.NONE;
|
2019-03-05 08:36:59 -03:00
|
|
|
this.dashedLine = false;
|
2019-03-27 20:17:13 -03:00
|
|
|
this.fill = false;
|
2019-03-05 08:36:59 -03:00
|
|
|
this._redisplay();
|
2019-12-28 09:28:25 -03:00
|
|
|
if (this._menu)
|
|
|
|
|
this._menu.close();
|
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)
|
2019-03-11 20:29:14 -03:00
|
|
|
this.saveAsJson();
|
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
|
2019-03-27 22:00:57 -03:00
|
|
|
if (this.currentElement && this.currentElement.shape == Shapes.TEXT && this.currentElement.state == TextState.WRITING) {
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let content = `<svg viewBox="0 0 ${this.width} ${this.height}" xmlns="http://www.w3.org/2000/svg">`;
|
|
|
|
|
let backgroundColorString = this.hasBackground ? this.activeBackgroundColor.to_string() : 'transparent';
|
|
|
|
|
if (backgroundColorString != 'transparent') {
|
|
|
|
|
content += `\n <rect id="background" width="100%" height="100%" fill="${backgroundColorString}"/>`;
|
|
|
|
|
}
|
|
|
|
|
for (let i = 0; i < this.elements.length; i++) {
|
|
|
|
|
content += this.elements[i].buildSVG(backgroundColorString);
|
|
|
|
|
}
|
|
|
|
|
content += "\n</svg>";
|
|
|
|
|
|
2020-01-01 21:22:25 -03:00
|
|
|
let filename = `${DEFAULT_FILE_NAME} ${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
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-11 20:29:14 -03:00
|
|
|
saveAsJson: function() {
|
2020-01-01 21:22:25 -03:00
|
|
|
let filename = `${DEFAULT_FILE_NAME}.json`;
|
2019-03-11 13:53:35 -03:00
|
|
|
let dir = GLib.get_user_data_dir();
|
|
|
|
|
let path = GLib.build_filenamev([dir, filename]);
|
|
|
|
|
|
|
|
|
|
let oldContents;
|
|
|
|
|
if (GLib.file_test(path, GLib.FileTest.EXISTS)) {
|
|
|
|
|
oldContents = GLib.file_get_contents(path)[1];
|
|
|
|
|
if (oldContents instanceof Uint8Array)
|
|
|
|
|
oldContents = imports.byteArray.toString(oldContents);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 20:29:14 -03:00
|
|
|
// do not create a file to write just an empty array
|
|
|
|
|
if (!oldContents && this.elements.length == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-03-11 13:53:35 -03:00
|
|
|
// do not use "content = JSON.stringify(this.elements, null, 2);", neither "content = JSON.stringify(this.elements);"
|
|
|
|
|
// because of compromise between disk usage and human readability
|
|
|
|
|
let contents = `[\n ` + new Array(...this.elements.map(element => JSON.stringify(element))).join(`,\n\n `) + `\n]`;
|
|
|
|
|
|
|
|
|
|
if (contents != oldContents)
|
|
|
|
|
GLib.file_set_contents(path, contents);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_loadJson: function() {
|
2020-01-01 21:22:25 -03:00
|
|
|
let filename = `${DEFAULT_FILE_NAME}.json`;
|
2019-03-11 13:53:35 -03:00
|
|
|
let dir = GLib.get_user_data_dir();
|
|
|
|
|
let path = GLib.build_filenamev([dir, filename]);
|
|
|
|
|
|
|
|
|
|
if (!GLib.file_test(path, GLib.FileTest.EXISTS))
|
|
|
|
|
return;
|
|
|
|
|
let [success, contents] = GLib.file_get_contents(path);
|
|
|
|
|
if (!success)
|
|
|
|
|
return;
|
|
|
|
|
if (contents instanceof Uint8Array)
|
|
|
|
|
contents = imports.byteArray.toString(contents);
|
|
|
|
|
this.elements.push(...JSON.parse(contents).map(object => new DrawingElement(object)));
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
disable: function() {
|
|
|
|
|
this.erase();
|
2019-03-27 10:23:34 -03:00
|
|
|
this.menu.disable();
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// DrawingElement represents a "brushstroke".
|
|
|
|
|
// It can be converted into a cairo path as well as a svg element.
|
|
|
|
|
// See DrawingArea._startDrawing() to know its params.
|
|
|
|
|
var DrawingElement = new Lang.Class({
|
2019-03-08 07:37:59 -03:00
|
|
|
Name: 'DrawOnYourScreenDrawingElement',
|
2019-03-05 08:36:59 -03:00
|
|
|
|
|
|
|
|
_init: function(params) {
|
|
|
|
|
for (let key in params)
|
|
|
|
|
this[key] = params[key];
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-11 13:53:35 -03:00
|
|
|
// toJSON is called by JSON.stringify
|
|
|
|
|
toJSON: function() {
|
|
|
|
|
return {
|
|
|
|
|
shape: this.shape,
|
|
|
|
|
color: this.color,
|
|
|
|
|
line: this.line,
|
|
|
|
|
dash: this.dash,
|
|
|
|
|
fill: this.fill,
|
|
|
|
|
eraser: this.eraser,
|
|
|
|
|
transform: this.transform,
|
|
|
|
|
text: this.text,
|
|
|
|
|
font: this.font,
|
|
|
|
|
points: this.points.map((point) => [Math.round(point[0]*100)/100, Math.round(point[1]*100)/100])
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
buildCairo: function(cr, showTextCursor) {
|
|
|
|
|
cr.setLineCap(this.line.lineCap);
|
|
|
|
|
cr.setLineJoin(this.line.lineJoin);
|
|
|
|
|
cr.setLineWidth(this.line.lineWidth);
|
|
|
|
|
|
|
|
|
|
if (this.dash.array[0] > 0 && this.dash.array[1] > 0)
|
|
|
|
|
cr.setDash(this.dash.array, this.dash.offset);
|
|
|
|
|
else
|
|
|
|
|
cr.setDash([1000000], 0);
|
|
|
|
|
|
|
|
|
|
if (this.eraser)
|
|
|
|
|
cr.setOperator(Cairo.Operator.CLEAR);
|
|
|
|
|
else
|
|
|
|
|
cr.setOperator(Cairo.Operator.OVER);
|
|
|
|
|
|
2019-03-11 13:53:35 -03:00
|
|
|
let [success, color] = Clutter.Color.from_string(this.color);
|
|
|
|
|
if (success)
|
|
|
|
|
Clutter.cairo_set_source_color(cr, color);
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2019-03-07 10:28:35 -03:00
|
|
|
let [points, shape, trans] = [this.points, this.shape, this.transform];
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2019-03-07 10:28:35 -03:00
|
|
|
if (shape == Shapes.LINE && points.length == 3) {
|
|
|
|
|
cr.moveTo(points[0][0], points[0][1]);
|
|
|
|
|
cr.curveTo(points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1]);
|
|
|
|
|
} else if (shape == Shapes.NONE || shape == Shapes.LINE) {
|
2019-03-05 08:36:59 -03:00
|
|
|
cr.moveTo(points[0][0], points[0][1]);
|
|
|
|
|
for (let j = 1; j < points.length; j++) {
|
|
|
|
|
cr.lineTo(points[j][0], points[j][1]);
|
|
|
|
|
}
|
2019-03-07 10:28:35 -03:00
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
} else if (shape == Shapes.ELLIPSE && points.length == 2) {
|
2019-03-07 10:28:35 -03:00
|
|
|
this.rotate(cr, trans.angle + trans.startAngle, trans.center[0], trans.center[1]);
|
|
|
|
|
this.scale(cr, trans.ratio, trans.center[0], trans.center[1]);
|
2019-03-05 08:36:59 -03:00
|
|
|
let r = Math.hypot(points[1][0] - points[0][0], points[1][1] - points[0][1]);
|
|
|
|
|
cr.arc(points[0][0], points[0][1], r, 0, 2 * Math.PI);
|
2019-03-07 10:28:35 -03:00
|
|
|
this.scale(cr, 1 / trans.ratio, trans.center[0], trans.center[1]);
|
|
|
|
|
this.rotate(cr, - (trans.angle + trans.startAngle), trans.center[0], trans.center[1]);
|
2019-03-05 08:36:59 -03:00
|
|
|
|
|
|
|
|
} else if (shape == Shapes.RECTANGLE && points.length == 2) {
|
2019-03-07 10:28:35 -03:00
|
|
|
this.rotate(cr, trans.angle, trans.center[0], trans.center[1]);
|
2019-03-05 08:36:59 -03:00
|
|
|
cr.rectangle(points[0][0], points[0][1], points[1][0] - points[0][0], points[1][1] - points[0][1]);
|
2019-03-07 10:28:35 -03:00
|
|
|
this.rotate(cr, - trans.angle, trans.center[0], trans.center[1]);
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
} else if (shape == Shapes.TEXT && points.length == 2) {
|
2019-03-07 10:28:35 -03:00
|
|
|
this.rotate(cr, trans.angle, trans.center[0], trans.center[1]);
|
2019-03-27 22:00:57 -03:00
|
|
|
if (this.state == TextState.DRAWING)
|
|
|
|
|
cr.rectangle(points[0][0], points[0][1], points[1][0] - points[0][0], points[1][1] - points[0][1]);
|
2019-03-05 08:36:59 -03:00
|
|
|
cr.selectFontFace(this.font.family, this.font.style, this.font.weight);
|
|
|
|
|
cr.setFontSize(Math.abs(points[1][1] - points[0][1]));
|
|
|
|
|
cr.moveTo(Math.min(points[0][0], points[1][0]), Math.max(points[0][1], points[1][1]));
|
|
|
|
|
cr.showText((showTextCursor) ? (this.text + "_") : this.text);
|
2019-03-07 10:28:35 -03:00
|
|
|
this.rotate(cr, - trans.angle, trans.center[0], trans.center[1]);
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
buildSVG: function(bgColor) {
|
|
|
|
|
let row = "\n ";
|
|
|
|
|
let points = this.points.map((point) => [Math.round(point[0]*100)/100, Math.round(point[1]*100)/100]);
|
2019-03-11 13:53:35 -03:00
|
|
|
let color = this.eraser ? bgColor : this.color;
|
2019-03-07 10:28:35 -03:00
|
|
|
let isStraightLine = this.shape == Shapes.LINE && (points.length < 3 || points[2] == points[1] || points[2] == points[0]);
|
|
|
|
|
let fill = this.fill && !isStraightLine;
|
|
|
|
|
let attributes = `fill="${fill ? color : 'transparent'}" ` +
|
2019-03-29 18:31:13 -03:00
|
|
|
`stroke="${color}" ` +
|
|
|
|
|
`${fill ? '' : 'fill-opacity="0"'} ` +
|
2019-03-05 08:36:59 -03:00
|
|
|
`stroke-width="${this.line.lineWidth}" ` +
|
|
|
|
|
`stroke-linecap="${LineCapNames[this.line.lineCap].toLowerCase()}" ` +
|
|
|
|
|
`stroke-linejoin="${LineJoinNames[this.line.lineJoin].toLowerCase()}"`;
|
|
|
|
|
|
|
|
|
|
if (this.dash.array[0] > 0 && this.dash.array[1] > 0)
|
|
|
|
|
attributes += ` stroke-dasharray="${this.dash.array[0]} ${this.dash.array[1]}" stroke-dashoffset="${this.dash.offset}"`;
|
|
|
|
|
|
2019-03-07 10:28:35 -03:00
|
|
|
if (this.shape == Shapes.LINE && points.length == 3) {
|
2019-03-05 08:36:59 -03:00
|
|
|
row += `<path ${attributes} d="M${points[0][0]} ${points[0][1]}`;
|
2019-03-07 10:28:35 -03:00
|
|
|
row += ` C ${points[0][0]} ${points[0][1]}, ${points[1][0]} ${points[1][1]}, ${points[2][0]} ${points[2][1]}`;
|
|
|
|
|
row += `${fill ? 'z' : ''}"/>`;
|
2019-03-05 08:36:59 -03:00
|
|
|
|
2019-03-07 10:28:35 -03:00
|
|
|
} else if (this.shape == Shapes.NONE || this.shape == Shapes.LINE) {
|
|
|
|
|
row += `<path ${attributes} d="M${points[0][0]} ${points[0][1]}`;
|
2019-03-05 08:36:59 -03:00
|
|
|
for (let i = 1; i < points.length; i++) {
|
|
|
|
|
row += ` L ${points[i][0]} ${points[i][1]}`;
|
|
|
|
|
}
|
2019-03-07 10:28:35 -03:00
|
|
|
row += `${fill ? 'z' : ''}"/>`;
|
|
|
|
|
|
|
|
|
|
} else if (this.shape == Shapes.ELLIPSE && points.length == 2 && this.transform.ratio != 1) {
|
|
|
|
|
let ry = Math.hypot(points[1][0] - points[0][0], points[1][1] - points[0][1]);
|
|
|
|
|
let rx = ry * this.transform.ratio;
|
|
|
|
|
let angle = (this.transform.angle + this.transform.startAngle) * 180 / Math.PI;
|
|
|
|
|
row += `<ellipse ${attributes} cx="${points[0][0]}" cy="${points[0][1]}" rx="${rx}" ry="${ry}" transform="rotate(${angle}, ${points[0][0]}, ${points[0][1]})"/>`;
|
2019-03-05 08:36:59 -03:00
|
|
|
|
|
|
|
|
} else if (this.shape == Shapes.ELLIPSE && points.length == 2) {
|
|
|
|
|
let r = Math.hypot(points[1][0] - points[0][0], points[1][1] - points[0][1]);
|
|
|
|
|
row += `<circle ${attributes} cx="${points[0][0]}" cy="${points[0][1]}" r="${r}"/>`;
|
2019-03-07 10:28:35 -03:00
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
} else if (this.shape == Shapes.RECTANGLE && points.length == 2) {
|
2019-03-07 10:28:35 -03:00
|
|
|
let transAttribute = "";
|
|
|
|
|
if (this.transform.angle != 0) {
|
|
|
|
|
let angle = this.transform.angle * 180 / Math.PI;
|
|
|
|
|
transAttribute = ` transform="rotate(${angle}, ${this.transform.center[0]}, ${this.transform.center[1]})"`;
|
|
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
row += `<rect ${attributes} x="${Math.min(points[0][0], points[1][0])}" y="${Math.min(points[0][1], points[1][1])}" ` +
|
2019-03-07 10:28:35 -03:00
|
|
|
`width="${Math.abs(points[1][0] - points[0][0])}" height="${Math.abs(points[1][1] - points[0][1])}"${transAttribute}/>`;
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
} else if (this.shape == Shapes.TEXT && points.length == 2) {
|
2019-03-07 10:28:35 -03:00
|
|
|
let transAttribute = "";
|
|
|
|
|
if (this.transform.angle != 0) {
|
|
|
|
|
let angle = this.transform.angle * 180 / Math.PI;
|
|
|
|
|
transAttribute = ` transform="rotate(${angle}, ${this.transform.center[0]}, ${this.transform.center[1]})"`;
|
|
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
attributes = `fill="${color}" ` +
|
|
|
|
|
`stroke="transparent" ` +
|
2019-03-05 21:36:23 -03:00
|
|
|
`stroke-opacity="0" ` +
|
2019-03-05 08:36:59 -03:00
|
|
|
`font-family="${this.font.family}" ` +
|
|
|
|
|
`font-size="${Math.abs(points[1][1] - points[0][1])}" ` +
|
|
|
|
|
`font-weight="${FontWeightNames[this.font.weight].toLowerCase()}" ` +
|
|
|
|
|
`font-style="${FontStyleNames[this.font.style].toLowerCase()}"`;
|
|
|
|
|
|
2019-03-07 10:28:35 -03:00
|
|
|
row += `<text ${attributes}${transAttribute} x="${Math.min(points[0][0], points[1][0])}" y="${Math.max(points[0][1], points[1][1])}">${this.text}</text>`;
|
2019-03-05 08:36:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return row;
|
2019-03-05 17:08:43 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
addPoint: function(x, y, smoothedStroke) {
|
|
|
|
|
this.points.push([x, y]);
|
|
|
|
|
if (smoothedStroke)
|
|
|
|
|
this.smooth(this.points.length - 1);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
smooth: function(i) {
|
|
|
|
|
if (i < 2)
|
|
|
|
|
return;
|
|
|
|
|
this.points[i-1] = [(this.points[i-2][0] + this.points[i][0]) / 2, (this.points[i-2][1] + this.points[i][1]) / 2];
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
smoothAll: function() {
|
|
|
|
|
for (let i = 0; i < this.points.length; i++) {
|
|
|
|
|
this.smooth(i);
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-03-07 10:28:35 -03:00
|
|
|
|
|
|
|
|
rotate: function(cr, angle, x, y) {
|
|
|
|
|
if (angle == 0)
|
|
|
|
|
return;
|
|
|
|
|
cr.translate(x, y);
|
|
|
|
|
cr.rotate(angle);
|
|
|
|
|
cr.translate(-x, -y);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
scale: function(cr, ratio, x, y) {
|
|
|
|
|
if (ratio == 1)
|
|
|
|
|
return;
|
|
|
|
|
cr.translate(x, y);
|
|
|
|
|
cr.scale(ratio, 1);
|
|
|
|
|
cr.translate(-x, -y);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
transformRectangle: function(x, y) {
|
|
|
|
|
let points = this.points;
|
|
|
|
|
if (points.length < 2 || points[0][0] == points[1][0] || points[0][1] == points[1][1])
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.transform.center = [points[0][0] + (points[1][0] - points[0][0]) / 2, points[0][1] + (points[1][1] - points[0][1]) / 2];
|
|
|
|
|
|
|
|
|
|
this.transform.angle = getAngle(this.transform.center[0], this.transform.center[1], points[1][0], points[1][1], x, y);
|
|
|
|
|
this.transform.active = true;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
transformEllipse: function(x, y) {
|
|
|
|
|
let points = this.points;
|
|
|
|
|
if (points.length < 2 || points[0][0] == points[1][0] || points[0][1] == points[1][1])
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.transform.center = [points[0][0], points[0][1]];
|
|
|
|
|
|
|
|
|
|
let r1 = Math.hypot(points[1][0] - points[0][0], points[1][1] - points[0][1]);
|
|
|
|
|
let r2 = Math.hypot(x - points[0][0], y - points[0][1]);
|
|
|
|
|
this.transform.ratio = r2 / r1;
|
|
|
|
|
|
|
|
|
|
this.transform.angle = getAngle(this.transform.center[0], this.transform.center[1], points[1][0], points[1][1], x, y);
|
|
|
|
|
if (!this.transform.startAngle)
|
|
|
|
|
// that is the angle between the direction when starting ellipticalizing, and the x-axis
|
|
|
|
|
this.transform.startAngle = getAngle(points[0][0], points[0][1], points[0][0] + 1, points[0][1], points[1][0], points[1][1]);
|
|
|
|
|
this.transform.active = true;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
transformLine: function(x, y) {
|
|
|
|
|
if (this.points.length < 2)
|
|
|
|
|
return;
|
|
|
|
|
if (this.points.length == 2)
|
|
|
|
|
this.points[2] = this.points[1];
|
|
|
|
|
this.points[1] = [x, y];
|
|
|
|
|
this.transform.active = true;
|
|
|
|
|
},
|
2019-03-05 08:36:59 -03:00
|
|
|
});
|
|
|
|
|
|
2019-03-07 10:28:35 -03:00
|
|
|
function getAngle(xO, yO, xA, yA, xB, yB) {
|
|
|
|
|
// calculate angle of rotation in absolute value
|
|
|
|
|
// cos(AOB) = (OA.OB)/(||OA||*||OB||) where OA.OB = (xA-xO)*(xB-xO) + (yA-yO)*(yB-yO)
|
|
|
|
|
let angle = Math.acos( ((xA - xO)*(xB - xO) + (yA - yO)*(yB - yO)) / (Math.hypot(xA - xO, yA - yO) * Math.hypot(xB - xO, yB - yO)) );
|
|
|
|
|
|
|
|
|
|
// determine the sign of the angle
|
|
|
|
|
// equation of OA: y = ax + b
|
|
|
|
|
let a = (yA - yO) / (xA - xO);
|
|
|
|
|
let b = yA - a*xA;
|
|
|
|
|
if (yB < a*xB + b)
|
|
|
|
|
angle = - angle;
|
|
|
|
|
if (xA < xO)
|
|
|
|
|
angle = - angle;
|
|
|
|
|
return angle;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 08:36:59 -03:00
|
|
|
var HELPER_ANIMATION_TIME = 0.25;
|
2019-03-07 15:22:19 -03:00
|
|
|
var MEDIA_KEYS_SCHEMA = 'org.gnome.settings-daemon.plugins.media-keys';
|
|
|
|
|
var MEDIA_KEYS_KEYS = {
|
|
|
|
|
'screenshot': "Screenshot",
|
|
|
|
|
'screenshot-clip': "Screenshot to clipboard",
|
|
|
|
|
'area-screenshot': "Area screenshot",
|
|
|
|
|
'area-screenshot-clip': "Area screenshot to clipboard"
|
|
|
|
|
};
|
2019-03-05 08:36:59 -03:00
|
|
|
|
|
|
|
|
// DrawingHelper provides the "help osd" (Ctrl + F1)
|
|
|
|
|
// It uses the same texts as in prefs
|
|
|
|
|
var DrawingHelper = new Lang.Class({
|
2019-03-08 07:37:59 -03:00
|
|
|
Name: 'DrawOnYourScreenDrawingHelper',
|
2019-03-05 08:36:59 -03:00
|
|
|
Extends: St.ScrollView,
|
|
|
|
|
|
|
|
|
|
_init: function(params, monitor) {
|
|
|
|
|
this.parent(params);
|
|
|
|
|
this.monitor = monitor;
|
|
|
|
|
this.hide();
|
|
|
|
|
this.vbox = new St.BoxLayout({ style_class: 'osd-window draw-on-your-screen-helper', vertical: true });
|
|
|
|
|
this.add_actor(this.vbox);
|
|
|
|
|
this.vbox.add(new St.Label({ text: _("Global") }));
|
|
|
|
|
|
|
|
|
|
let settings = Convenience.getSettings();
|
|
|
|
|
|
|
|
|
|
for (let settingKey in Prefs.GLOBAL_KEYBINDINGS) {
|
|
|
|
|
let hbox = new St.BoxLayout({ vertical: false });
|
|
|
|
|
if (settingKey.indexOf('-separator-') != -1) {
|
|
|
|
|
this.vbox.add(hbox);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-10-11 04:22:37 -03:00
|
|
|
if (!settings.get_strv(settingKey)[0])
|
|
|
|
|
continue;
|
2019-03-05 08:36:59 -03:00
|
|
|
let [keyval, mods] = Gtk.accelerator_parse(settings.get_strv(settingKey)[0]);
|
|
|
|
|
hbox.add(new St.Label({ text: _(Prefs.GLOBAL_KEYBINDINGS[settingKey]) }));
|
|
|
|
|
hbox.add(new St.Label({ text: Gtk.accelerator_get_label(keyval, mods) }), { expand: true });
|
|
|
|
|
this.vbox.add(hbox);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.vbox.add(new St.Label({ text: _("Internal") }));
|
|
|
|
|
|
2019-03-05 18:00:14 -03:00
|
|
|
for (let i = 0; i < Prefs.OTHER_SHORTCUTS.length; i++) {
|
|
|
|
|
if (Prefs.OTHER_SHORTCUTS[i].desc.indexOf('-separator-') != -1) {
|
2019-12-29 11:26:20 -03:00
|
|
|
this.vbox.add(new St.BoxLayout({ vertical: false, style_class: 'draw-on-your-screen-helper-separator' }));
|
2019-03-05 08:36:59 -03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let hbox = new St.BoxLayout({ vertical: false });
|
2019-03-05 18:00:14 -03:00
|
|
|
hbox.add(new St.Label({ text: _(Prefs.OTHER_SHORTCUTS[i].desc) }));
|
|
|
|
|
hbox.add(new St.Label({ text: _(Prefs.OTHER_SHORTCUTS[i].shortcut) }), { expand: true });
|
2019-03-05 08:36:59 -03:00
|
|
|
this.vbox.add(hbox);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 11:26:20 -03:00
|
|
|
this.vbox.add(new St.BoxLayout({ vertical: false, style_class: 'draw-on-your-screen-helper-separator' }));
|
2019-03-05 08:36:59 -03:00
|
|
|
|
|
|
|
|
for (let settingKey in Prefs.INTERNAL_KEYBINDINGS) {
|
|
|
|
|
if (settingKey.indexOf('-separator-') != -1) {
|
2019-12-29 11:26:20 -03:00
|
|
|
this.vbox.add(new St.BoxLayout({ vertical: false, style_class: 'draw-on-your-screen-helper-separator' }));
|
2019-03-05 08:36:59 -03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let hbox = new St.BoxLayout({ vertical: false });
|
2019-10-11 04:22:37 -03:00
|
|
|
if (!settings.get_strv(settingKey)[0])
|
|
|
|
|
continue;
|
2019-03-05 08:36:59 -03:00
|
|
|
let [keyval, mods] = Gtk.accelerator_parse(settings.get_strv(settingKey)[0]);
|
|
|
|
|
hbox.add(new St.Label({ text: _(Prefs.INTERNAL_KEYBINDINGS[settingKey]) }));
|
|
|
|
|
hbox.add(new St.Label({ text: Gtk.accelerator_get_label(keyval, mods) }), { expand: true });
|
|
|
|
|
this.vbox.add(hbox);
|
|
|
|
|
}
|
2019-03-07 15:22:19 -03:00
|
|
|
|
|
|
|
|
let mediaKeysSettings;
|
|
|
|
|
try { mediaKeysSettings = Convenience.getSettings(MEDIA_KEYS_SCHEMA); } catch(e) { return; }
|
|
|
|
|
this.vbox.add(new St.Label({ text: _("System") }));
|
|
|
|
|
|
|
|
|
|
for (let settingKey in MEDIA_KEYS_KEYS) {
|
|
|
|
|
if (!mediaKeysSettings.settings_schema.has_key(settingKey))
|
2019-10-11 04:22:37 -03:00
|
|
|
continue;
|
|
|
|
|
let shortcut = GS_VERSION < '3.33.0' ? mediaKeysSettings.get_string(settingKey) : mediaKeysSettings.get_strv(settingKey)[0];
|
|
|
|
|
let [keyval, mods] = Gtk.accelerator_parse(shortcut);
|
2019-03-07 15:22:19 -03:00
|
|
|
let hbox = new St.BoxLayout({ vertical: false });
|
|
|
|
|
hbox.add(new St.Label({ text: _(MEDIA_KEYS_KEYS[settingKey]) }));
|
|
|
|
|
hbox.add(new St.Label({ text: Gtk.accelerator_get_label(keyval, mods) }), { expand: true });
|
|
|
|
|
this.vbox.add(hbox);
|
|
|
|
|
}
|
2019-03-05 08:36:59 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
showHelp: function() {
|
|
|
|
|
this.opacity = 0;
|
|
|
|
|
this.show();
|
|
|
|
|
|
|
|
|
|
let maxHeight = this.monitor.height*(3/4);
|
|
|
|
|
this.set_height(Math.min(this.height, maxHeight));
|
2019-03-10 11:33:38 -03:00
|
|
|
this.set_position(Math.floor(this.monitor.width / 2 - this.width / 2),
|
|
|
|
|
Math.floor(this.monitor.height / 2 - this.height / 2));
|
2019-03-11 14:24:10 -03:00
|
|
|
|
|
|
|
|
if (this.height == maxHeight)
|
|
|
|
|
this.vscrollbar_policy = Gtk.PolicyType.ALWAYS;
|
|
|
|
|
else
|
|
|
|
|
this.vscrollbar_policy = Gtk.PolicyType.NEVER;
|
2019-03-05 08:36:59 -03:00
|
|
|
|
|
|
|
|
Tweener.removeTweens(this);
|
|
|
|
|
Tweener.addTween(this, { opacity: 255,
|
|
|
|
|
time: HELPER_ANIMATION_TIME,
|
|
|
|
|
transition: 'easeOutQuad',
|
|
|
|
|
onComplete: null });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
hideHelp: function() {
|
|
|
|
|
Tweener.removeTweens(this);
|
|
|
|
|
Tweener.addTween(this, { opacity: 0,
|
|
|
|
|
time: HELPER_ANIMATION_TIME,
|
|
|
|
|
transition: 'easeOutQuad',
|
|
|
|
|
onComplete: this.hide.bind(this) });
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
});
|
2019-03-26 18:28:24 -03:00
|
|
|
|
|
|
|
|
var DrawingMenu = new Lang.Class({
|
|
|
|
|
Name: 'DrawOnYourScreenDrawingMenu',
|
|
|
|
|
|
2019-12-29 11:26:20 -03:00
|
|
|
_init: function(area, monitor) {
|
2019-03-26 18:28:24 -03:00
|
|
|
this.area = area;
|
|
|
|
|
let side = Clutter.get_default_text_direction() == Clutter.TextDirection.RTL ? St.Side.RIGHT : St.Side.LEFT;
|
|
|
|
|
this.menu = new PopupMenu.PopupMenu(Main.layoutManager.dummyCursor, 0.25, side);
|
2019-10-11 04:22:37 -03:00
|
|
|
this.menuManager = new PopupMenu.PopupMenuManager(GS_VERSION < '3.33.0' ? { actor: this.area } : this.area);
|
2019-03-26 18:28:24 -03:00
|
|
|
this.menuManager.addMenu(this.menu);
|
|
|
|
|
|
|
|
|
|
Main.layoutManager.uiGroup.add_actor(this.menu.actor);
|
|
|
|
|
this.menu.actor.add_style_class_name('background-menu draw-on-your-screen-menu');
|
2019-12-29 11:26:20 -03:00
|
|
|
this.menu.actor.set_style('max-height:' + monitor.height + 'px;');
|
2019-03-26 18:28:24 -03:00
|
|
|
this.menu.actor.hide();
|
|
|
|
|
|
|
|
|
|
// do not close the menu on item activated
|
|
|
|
|
this.menu.itemActivated = () => {};
|
|
|
|
|
this.menu.connect('open-state-changed', this._onMenuOpenStateChanged.bind(this));
|
2019-03-29 12:35:18 -03:00
|
|
|
|
|
|
|
|
this.strokeIcon = new Gio.FileIcon({ file: Gio.File.new_for_path(STROKE_ICON_PATH) });
|
|
|
|
|
this.fillIcon = new Gio.FileIcon({ file: Gio.File.new_for_path(FILL_ICON_PATH) });
|
|
|
|
|
this.linejoinIcon = new Gio.FileIcon({ file: Gio.File.new_for_path(LINEJOIN_ICON_PATH) });
|
|
|
|
|
this.linecapIcon = new Gio.FileIcon({ file: Gio.File.new_for_path(LINECAP_ICON_PATH) });
|
|
|
|
|
this.fullLineIcon = new Gio.FileIcon({ file: Gio.File.new_for_path(FULL_LINE_ICON_PATH) });
|
|
|
|
|
this.dashedLineIcon = new Gio.FileIcon({ file: Gio.File.new_for_path(DASHED_LINE_ICON_PATH) });
|
2019-03-26 18:28:24 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-27 10:23:34 -03:00
|
|
|
disable: function() {
|
|
|
|
|
this.menuManager.removeMenu(this.menu);
|
|
|
|
|
Main.layoutManager.uiGroup.remove_actor(this.menu.actor);
|
|
|
|
|
this.menu.actor.destroy();
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-26 18:28:24 -03:00
|
|
|
_onMenuOpenStateChanged: function(menu, open) {
|
2019-11-27 02:04:37 -03:00
|
|
|
if (open) {
|
|
|
|
|
this.area.setPointerCursor('DEFAULT');
|
|
|
|
|
} else {
|
|
|
|
|
this.area.updatePointerCursor();
|
2019-03-26 18:28:24 -03:00
|
|
|
// actionMode has changed, set previous actionMode in order to keep internal shortcuts working
|
|
|
|
|
Main.actionMode = ExtensionJs.DRAWING_ACTION_MODE | Shell.ActionMode.NORMAL;
|
2019-11-27 02:04:37 -03:00
|
|
|
}
|
2019-03-26 18:28:24 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-27 10:23:34 -03:00
|
|
|
popup: function() {
|
|
|
|
|
if (this.menu.isOpen) {
|
|
|
|
|
this.close();
|
|
|
|
|
} else {
|
|
|
|
|
this.open();
|
|
|
|
|
this.menu.actor.navigate_focus(null, Gtk.DirectionType.TAB_FORWARD, false);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-26 18:28:24 -03:00
|
|
|
open: function(x, y) {
|
2019-03-27 10:23:34 -03:00
|
|
|
if (this.menu.isOpen)
|
|
|
|
|
return;
|
|
|
|
|
if (x === undefined || y === undefined)
|
|
|
|
|
[x, y] = [this.area.monitor.x + this.area.monitor.width / 2, this.area.monitor.y + this.area.monitor.height / 2];
|
2019-03-26 18:28:24 -03:00
|
|
|
this._redisplay();
|
|
|
|
|
Main.layoutManager.setDummyCursorGeometry(x, y, 0, 0);
|
|
|
|
|
let monitor = this.area.monitor;
|
|
|
|
|
this.menu._arrowAlignment = (y - monitor.y) / monitor.height;
|
|
|
|
|
this.menu.open(BoxPointer.PopupAnimation.NONE);
|
|
|
|
|
this.menuManager.ignoreRelease();
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-27 10:23:34 -03:00
|
|
|
close: function() {
|
|
|
|
|
if (this.menu.isOpen)
|
|
|
|
|
this.menu.close();
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-26 18:28:24 -03:00
|
|
|
_redisplay: function() {
|
|
|
|
|
this.menu.removeAll();
|
|
|
|
|
|
|
|
|
|
this.menu.addAction(_("Undo"), this.area.undo.bind(this.area), 'edit-undo-symbolic');
|
|
|
|
|
this.menu.addAction(_("Redo"), this.area.redo.bind(this.area), 'edit-redo-symbolic');
|
|
|
|
|
this.menu.addAction(_("Erase"), this.area.deleteLastElement.bind(this.area), 'edit-clear-all-symbolic');
|
|
|
|
|
this.menu.addAction(_("Smooth"), this.area.smoothLastElement.bind(this.area), 'format-text-strikethrough-symbolic');
|
2019-03-27 18:11:51 -03:00
|
|
|
this._addSeparator(this.menu);
|
2019-03-26 18:28:24 -03:00
|
|
|
|
2019-03-27 18:11:51 -03:00
|
|
|
this._addSubMenuItem(this.menu, null, ShapeNames, this.area, 'currentShape', this.updateSectionVisibility.bind(this));
|
|
|
|
|
this._addColorSubMenuItem(this.menu);
|
2019-03-29 18:31:13 -03:00
|
|
|
this.fillItem = this._addSwitchItem(this.menu, _("Fill"), this.strokeIcon, this.fillIcon, this.area, 'fill');
|
2019-03-27 18:11:51 -03:00
|
|
|
this._addSeparator(this.menu);
|
2019-03-26 18:28:24 -03:00
|
|
|
|
2019-03-27 18:11:51 -03:00
|
|
|
let lineSection = new PopupMenu.PopupMenuSection();
|
|
|
|
|
this._addSliderItem(lineSection, this.area, 'currentLineWidth');
|
2019-03-29 12:35:18 -03:00
|
|
|
this._addSubMenuItem(lineSection, this.linejoinIcon, LineJoinNames, this.area, 'currentLineJoin');
|
|
|
|
|
this._addSubMenuItem(lineSection, this.linecapIcon, LineCapNames, this.area, 'currentLineCap');
|
|
|
|
|
this._addSwitchItem(lineSection, _("Dashed"), this.fullLineIcon, this.dashedLineIcon, this.area, 'dashedLine');
|
2019-03-27 18:11:51 -03:00
|
|
|
this._addSeparator(lineSection);
|
|
|
|
|
this.menu.addMenuItem(lineSection);
|
2019-03-28 11:26:10 -03:00
|
|
|
lineSection.itemActivated = () => {};
|
2019-03-27 18:11:51 -03:00
|
|
|
this.lineSection = lineSection;
|
2019-03-26 18:28:24 -03:00
|
|
|
|
2019-03-27 18:11:51 -03:00
|
|
|
let fontSection = new PopupMenu.PopupMenuSection();
|
2019-03-26 18:28:24 -03:00
|
|
|
let FontFamilyNamesCopy = Object.create(FontFamilyNames);
|
|
|
|
|
FontFamilyNamesCopy[0] = this.area.fontFamily;
|
2019-03-27 18:11:51 -03:00
|
|
|
this._addSubMenuItem(fontSection, 'font-x-generic-symbolic', FontFamilyNamesCopy, this.area, 'currentFontFamilyId');
|
|
|
|
|
this._addSubMenuItem(fontSection, 'format-text-bold-symbolic', FontWeightNames, this.area, 'currentFontWeight');
|
|
|
|
|
this._addSubMenuItem(fontSection, 'format-text-italic-symbolic', FontStyleNames, this.area, 'currentFontStyle');
|
|
|
|
|
this._addSeparator(fontSection);
|
|
|
|
|
this.menu.addMenuItem(fontSection);
|
|
|
|
|
this.fontSection = fontSection;
|
2019-03-26 18:28:24 -03:00
|
|
|
|
|
|
|
|
let manager = ExtensionJs.manager;
|
2019-03-27 18:11:51 -03:00
|
|
|
this._addSwitchItemWithCallback(this.menu, _("Hide panel and dock"), manager.hiddenList ? true : false, manager.togglePanelAndDockOpacity.bind(manager));
|
|
|
|
|
this._addSwitchItemWithCallback(this.menu, _("Add a drawing background"), this.area.hasBackground, this.area.toggleBackground.bind(this.area));
|
|
|
|
|
this._addSwitchItemWithCallback(this.menu, _("Square drawing area"), this.area.isSquareArea, this.area.toggleSquareArea.bind(this.area));
|
|
|
|
|
this._addSeparator(this.menu);
|
2019-03-26 18:28:24 -03:00
|
|
|
|
|
|
|
|
this.menu.addAction(_("Save drawing as a SVG file"), this.area.saveAsSvg.bind(this.area), 'document-save-symbolic');
|
|
|
|
|
this.menu.addAction(_("Open stylesheet.css"), manager.openStylesheetFile.bind(manager), 'document-open-symbolic');
|
|
|
|
|
this.menu.addAction(_("Show help"), this.area.toggleHelp.bind(this.area), 'preferences-desktop-keyboard-shortcuts-symbolic');
|
2019-03-27 18:11:51 -03:00
|
|
|
|
|
|
|
|
this.updateSectionVisibility();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updateSectionVisibility: function() {
|
2019-03-29 12:35:18 -03:00
|
|
|
if (this.area.currentShape != Shapes.TEXT) {
|
2019-03-29 18:31:13 -03:00
|
|
|
this.lineSection.actor.show();
|
2019-03-27 18:11:51 -03:00
|
|
|
this.fontSection.actor.hide();
|
2019-03-29 12:35:18 -03:00
|
|
|
this.fillItem.setSensitive(true);
|
|
|
|
|
} else {
|
2019-03-29 18:31:13 -03:00
|
|
|
this.lineSection.actor.hide();
|
2019-03-27 18:11:51 -03:00
|
|
|
this.fontSection.actor.show();
|
2019-03-29 12:35:18 -03:00
|
|
|
this.fillItem.setSensitive(false);
|
|
|
|
|
}
|
2019-03-26 18:28:24 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-29 18:31:13 -03:00
|
|
|
_addSwitchItem: function(menu, label, iconFalse, iconTrue, target, targetProperty) {
|
2019-03-26 18:28:24 -03:00
|
|
|
let item = new PopupMenu.PopupSwitchMenuItem(label, target[targetProperty]);
|
2019-10-11 04:22:37 -03:00
|
|
|
let itemActor = GS_VERSION < '3.33.0' ? item.actor : item;
|
2019-03-26 18:28:24 -03:00
|
|
|
|
2019-03-29 18:57:22 -03:00
|
|
|
item.icon = new St.Icon({ style_class: 'popup-menu-icon' });
|
2019-10-11 04:22:37 -03:00
|
|
|
itemActor.insert_child_at_index(item.icon, 1);
|
2019-03-29 12:35:18 -03:00
|
|
|
item.icon.set_gicon(target[targetProperty] ? iconTrue : iconFalse);
|
2019-03-26 18:28:24 -03:00
|
|
|
|
|
|
|
|
item.connect('toggled', (item, state) => {
|
|
|
|
|
target[targetProperty] = state;
|
2019-03-29 12:35:18 -03:00
|
|
|
item.icon.set_gicon(target[targetProperty] ? iconTrue : iconFalse);
|
2019-03-26 18:28:24 -03:00
|
|
|
});
|
2019-03-27 18:11:51 -03:00
|
|
|
menu.addMenuItem(item);
|
2019-03-29 12:35:18 -03:00
|
|
|
return item;
|
2019-03-26 18:28:24 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-27 18:11:51 -03:00
|
|
|
_addSwitchItemWithCallback: function(menu, label, active, onToggled) {
|
2019-03-26 18:28:24 -03:00
|
|
|
let item = new PopupMenu.PopupSwitchMenuItem(label, active);
|
|
|
|
|
item.connect('toggled', onToggled);
|
2019-03-27 18:11:51 -03:00
|
|
|
menu.addMenuItem(item);
|
2019-03-26 18:28:24 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-27 18:11:51 -03:00
|
|
|
_addSliderItem: function(menu, target, targetProperty) {
|
2019-03-26 18:28:24 -03:00
|
|
|
let item = new PopupMenu.PopupBaseMenuItem({ activate: false });
|
2019-10-11 04:22:37 -03:00
|
|
|
let itemActor = GS_VERSION < '3.33.0' ? item.actor : item;
|
2019-03-28 11:26:10 -03:00
|
|
|
let label = new St.Label({ text: target[targetProperty] + " " + _("px"), style_class: 'draw-on-your-screen-menu-slider-label' });
|
2019-03-26 18:28:24 -03:00
|
|
|
let slider = new Slider.Slider(target[targetProperty] / 50);
|
2019-10-11 04:22:37 -03:00
|
|
|
let sliderActor = GS_VERSION < '3.33.0' ? slider.actor : slider;
|
2019-03-26 18:28:24 -03:00
|
|
|
|
2019-10-11 04:22:37 -03:00
|
|
|
if (GS_VERSION < '3.33.0') {
|
|
|
|
|
slider.connect('value-changed', (slider, value, property) => {
|
2019-10-11 08:26:08 -03:00
|
|
|
target[targetProperty] = Math.max(Math.round(value * 50), 0);
|
2019-10-11 04:22:37 -03:00
|
|
|
label.set_text(target[targetProperty] + " px");
|
2019-10-11 08:26:08 -03:00
|
|
|
if (target[targetProperty] === 0)
|
|
|
|
|
label.add_style_class_name(ExtensionJs.WARNING_COLOR_STYLE_CLASS_NAME);
|
|
|
|
|
else
|
|
|
|
|
label.remove_style_class_name(ExtensionJs.WARNING_COLOR_STYLE_CLASS_NAME);
|
2019-10-11 04:22:37 -03:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
slider.connect('notify::value', () => {
|
2019-10-11 08:26:08 -03:00
|
|
|
target[targetProperty] = Math.max(Math.round(slider.value * 50), 0);
|
2019-10-11 04:22:37 -03:00
|
|
|
label.set_text(target[targetProperty] + " px");
|
2019-10-11 08:26:08 -03:00
|
|
|
if (target[targetProperty] === 0)
|
|
|
|
|
label.add_style_class_name(ExtensionJs.WARNING_COLOR_STYLE_CLASS_NAME);
|
|
|
|
|
else
|
|
|
|
|
label.remove_style_class_name(ExtensionJs.WARNING_COLOR_STYLE_CLASS_NAME);
|
2019-10-11 04:22:37 -03:00
|
|
|
});
|
|
|
|
|
}
|
2019-03-26 18:28:24 -03:00
|
|
|
|
2019-10-11 04:22:37 -03:00
|
|
|
itemActor.add(sliderActor, { expand: true });
|
|
|
|
|
itemActor.add(label);
|
|
|
|
|
itemActor.connect('key-press-event', slider.onKeyPressEvent.bind(slider));
|
2019-03-27 18:11:51 -03:00
|
|
|
menu.addMenuItem(item);
|
2019-03-26 18:28:24 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-27 18:11:51 -03:00
|
|
|
_addSubMenuItem: function(menu, icon, obj, target, targetProperty, callback) {
|
|
|
|
|
let item = new PopupMenu.PopupSubMenuMenuItem(_(obj[target[targetProperty]]), icon ? true : false);
|
2019-03-29 12:35:18 -03:00
|
|
|
if (icon && icon instanceof GObject.Object && GObject.type_is_a(icon, Gio.Icon))
|
2019-03-26 18:28:24 -03:00
|
|
|
item.icon.set_gicon(icon);
|
|
|
|
|
else if (icon)
|
|
|
|
|
item.icon.set_icon_name(icon);
|
|
|
|
|
|
|
|
|
|
item.menu.itemActivated = () => {
|
|
|
|
|
item.menu.close();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Mainloop.timeout_add(0, () => {
|
|
|
|
|
for (let i in obj) {
|
2019-03-29 18:31:13 -03:00
|
|
|
let text;
|
|
|
|
|
if (targetProperty == 'currentFontFamilyId')
|
|
|
|
|
text = `<span font_family="${obj[i]}">${_(obj[i])}</span>`;
|
|
|
|
|
else if (targetProperty == 'currentFontWeight')
|
|
|
|
|
text = `<span font_weight="${obj[i].toLowerCase()}">${_(obj[i])}</span>`;
|
|
|
|
|
else if (targetProperty == 'currentFontStyle')
|
|
|
|
|
text = `<span font_style="${obj[i].toLowerCase()}">${_(obj[i])}</span>`;
|
|
|
|
|
else
|
|
|
|
|
text = _(obj[i]);
|
|
|
|
|
|
2019-11-29 14:43:42 -03:00
|
|
|
let iCaptured = i;
|
2019-03-29 18:31:13 -03:00
|
|
|
let subItem = item.menu.addAction(text, () => {
|
2019-11-29 14:43:42 -03:00
|
|
|
item.label.set_text(_(obj[iCaptured]));
|
|
|
|
|
target[targetProperty] = iCaptured;
|
2019-03-27 18:11:51 -03:00
|
|
|
if (callback)
|
|
|
|
|
callback();
|
2019-03-26 18:28:24 -03:00
|
|
|
});
|
2019-03-29 18:31:13 -03:00
|
|
|
|
|
|
|
|
subItem.label.get_clutter_text().set_use_markup(true);
|
2019-03-26 18:28:24 -03:00
|
|
|
}
|
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
|
});
|
2019-03-27 18:11:51 -03:00
|
|
|
menu.addMenuItem(item);
|
2019-03-26 18:28:24 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-27 18:11:51 -03:00
|
|
|
_addColorSubMenuItem: function(menu) {
|
2019-03-26 18:28:24 -03:00
|
|
|
let item = new PopupMenu.PopupSubMenuMenuItem(_("Color"), true);
|
|
|
|
|
item.icon.set_icon_name('document-edit-symbolic');
|
|
|
|
|
item.icon.set_style(`color:${this.area.currentColor.to_string().slice(0, 7)};`);
|
|
|
|
|
|
|
|
|
|
item.menu.itemActivated = () => {
|
|
|
|
|
item.menu.close();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Mainloop.timeout_add(0, () => {
|
|
|
|
|
for (let i = 1; i < this.area.colors.length; i++) {
|
|
|
|
|
let text = `<span foreground="${this.area.colors[i].to_string()}">${this.area.colors[i].to_string()}</span>`;
|
2019-11-29 14:43:42 -03:00
|
|
|
let iCaptured = i;
|
2019-03-26 18:28:24 -03:00
|
|
|
let colorItem = item.menu.addAction(text, () => {
|
2019-11-29 14:43:42 -03:00
|
|
|
this.area.currentColor = this.area.colors[iCaptured];
|
2019-03-26 18:28:24 -03:00
|
|
|
item.icon.set_style(`color:${this.area.currentColor.to_string().slice(0, 7)};`);
|
|
|
|
|
});
|
|
|
|
|
colorItem.label.get_clutter_text().set_use_markup(true);
|
|
|
|
|
}
|
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
|
});
|
2019-03-27 18:11:51 -03:00
|
|
|
menu.addMenuItem(item);
|
2019-03-26 18:28:24 -03:00
|
|
|
},
|
|
|
|
|
|
2019-03-27 18:11:51 -03:00
|
|
|
_addSeparator: function(menu) {
|
2019-12-29 11:26:20 -03:00
|
|
|
let separatorItem = new PopupMenu.PopupSeparatorMenuItem(' ');
|
|
|
|
|
let separatorItemActor = GS_VERSION < '3.33.0' ? separatorItem.actor : separatorItem;
|
|
|
|
|
separatorItemActor.add_style_class_name('draw-on-your-screen-menu-separator-item');
|
|
|
|
|
menu.addMenuItem(separatorItem);
|
2019-03-26 18:28:24 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|