diff --git a/src/fontra/views/editor/editor.js b/src/fontra/views/editor/editor.js index 273fafeaf..398b1b9b0 100644 --- a/src/fontra/views/editor/editor.js +++ b/src/fontra/views/editor/editor.js @@ -1047,7 +1047,7 @@ export class EditorController { }); } - initContextMenuItems() { + async initContextMenuItems() { this.basicContextMenuItems = []; for (const isRedo of [false, true]) { this.basicContextMenuItems.push({ @@ -1138,15 +1138,11 @@ export class EditorController { shortCut: undefined, }); + const hasLockedGuidelines = this.selectionHasLockedGuidelines(); this.glyphEditContextMenuItems.push({ - title: (event) => { - return event - ? `${event.altKey ? "Unlock" : "Lock"} Guideline(s)` - : "Lock Guideline(s)"; - }, + title: () => this.getLockGuidelineLabel(hasLockedGuidelines), enabled: () => this.canLockGuideline(), - callback: (event) => this.doLockGuideline(event), - altKey: true, + callback: (event) => this.doLockGuideline(!hasLockedGuidelines), }); this.glyphEditContextMenuItems.push(...this.sceneController.getContextMenuItems()); @@ -2109,6 +2105,40 @@ export class EditorController { return { contentElement, warningElement }; } + async selectionHasLockedGuidelines() { + const { + guidelineGlyph: guidelineGlyphSelection, + guidelineFont: guidelineFontSelection, + } = parseSelection(this.sceneController.selection); + + const instance = await this.sceneModel.getSelectedPositionedGlyph()?.glyph.instance; + for (const guidelineIndex of guidelineGlyphSelection || []) { + const guidelineGlyph = instance.guidelines[guidelineIndex]; + if (guidelineGlyph.locked) { + return true; + } + } + + //TODO: Guidelines Font + // check if any of the selected guidelines are locked + + return false; + } + + async getLockGuidelineLabel(hasLockedGuidelines) { + const { + guidelineGlyph: guidelineGlyphSelection, + guidelineFont: guidelineFontSelection, + } = parseSelection(this.sceneController.selection); + const guidelinSelection = new Array().concat( + guidelineGlyphSelection || [], + guidelineFontSelection || [] + ); + + const s = guidelinSelection.length > 1 ? "s" : ""; + return `${hasLockedGuidelines ? "Unlock" : "Lock"} Guideline${s}`; + } + canLockGuideline() { if (this.fontController.readOnly || this.sceneModel.isSelectedGlyphLocked()) { return false; @@ -2124,12 +2154,12 @@ export class EditorController { return guidelinSelection.length; } - async doLockGuideline(event) { + async doLockGuideline(locking = false) { const { guidelineGlyph: guidelineGlyphSelection, guidelineFont: guidelineFontSelection, } = parseSelection(this.sceneController.selection); - const identifier = event.altKey ? "Unlock" : "Lock"; + const identifier = locking ? "Unlock" : "Lock"; // Lock glyph guidelines if (guidelineGlyphSelection) { @@ -2140,7 +2170,7 @@ export class EditorController { if (!guidelineGlyph) { continue; } - guidelineGlyph.locked = !event.altKey; + guidelineGlyph.locked = locking; } } return `${identifier} Guideline(s)`;