From a8633f1fbc18993bc2af0709bcbe07661ec0ec4b Mon Sep 17 00:00:00 2001 From: abakkk Date: Mon, 14 Sep 2020 01:32:37 +0200 Subject: [PATCH] add gicons to menu openDrawingSubMenu items --- area.js | 25 ++++++++++++++++++++++--- files.js | 7 ++++++- menu.js | 11 ++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/area.js b/area.js index 4bdb9a5..63c16e9 100644 --- a/area.js +++ b/area.js @@ -1102,6 +1102,27 @@ var DrawingArea = new Lang.Class({ this.savePersistent(); }, + // Used by the menu. + getSvgContentForJson(json) { + let elements = []; + + elements.push(...JSON.parse(json.contents).map(object => { + if (object.image) + object.image = new Files.Image(object.image); + return new Elements.DrawingElement(object); + })); + + let size = Math.min(this.monitor.width, this.monitor.height); + let [x, y] = [(this.monitor.width - size) / 2, (this.monitor.height - size) / 2]; + + let prefixes = 'xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"'; + let content = ``; + elements.forEach(element => content += element.buildSVG('transparent')); + content += "\n"; + + return content; + }, + saveAsSvg: function() { // stop drawing or writing if (this.currentElement && this.currentElement.shape == Shapes.TEXT && this.isWriting) { @@ -1124,9 +1145,7 @@ var DrawingArea = new Lang.Class({ content += `\n `; content += `\n `; } - for (let i = 0; i < this.elements.length; i++) { - content += this.elements[i].buildSVG(backgroundColorString); - } + this.elements.forEach(element => content += element.buildSVG(backgroundColorString)); content += "\n"; if (Files.saveSvg(content)) { diff --git a/files.js b/files.js index a443c8b..3339604 100644 --- a/files.js +++ b/files.js @@ -388,6 +388,11 @@ var Json = new Lang.Class({ } this._contents = contents; + }, + + createGicon: function(content) { + let bytes = new GLib.Bytes(content); + this.gicon = Gio.BytesIcon.new(bytes); } }); @@ -521,7 +526,7 @@ var saveSvg = function(content) { return false; try { - return file.replace_contents(content, null, false, Gio.FileCreateFlags.NONE, null); + return file.replace_contents(content, null, false, Gio.FileCreateFlags.NONE, null)[0]; } catch(e) { return false; } diff --git a/menu.js b/menu.js index c460073..ff66d71 100644 --- a/menu.js +++ b/menu.js @@ -591,13 +591,18 @@ var DrawingMenu = new Lang.Class({ _populateOpenDrawingSubMenu: function() { this.openDrawingSubMenu.removeAll(); - let jsons = Files.Jsons.getSorted(); - jsons.forEach(json => { + Files.Jsons.getSorted().forEach(json => { + if (!json.gicon) { + let svgContent = this.area.getSvgContentForJson(json); + json.createGicon(svgContent); + } + let subItem = this.openDrawingSubMenu.addAction(`${String(json)}`, () => { this.area.loadJson(json); this._updateDrawingNameMenuItem(); this._updateSaveDrawingSubMenuItemSensitivity(); - }); + }, json.gicon); + subItem.label.get_clutter_text().set_use_markup(true); getActor(subItem).connect('key-focus-in', updateSubMenuAdjustment);