add gicons to menu openDrawingSubMenu items

This commit is contained in:
abakkk 2020-09-14 01:32:37 +02:00
parent 60091a94b6
commit a8633f1fbc
3 changed files with 36 additions and 7 deletions

25
area.js
View File

@ -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 = `<svg viewBox="${x} ${y} ${size} ${size}" ${prefixes}>`;
elements.forEach(element => content += element.buildSVG('transparent'));
content += "\n</svg>";
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 <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"/>`;
}
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</svg>";
if (Files.saveSvg(content)) {

View File

@ -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;
}

11
menu.js
View File

@ -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(`<i>${String(json)}</i>`, () => {
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);