Skip to content

Commit

Permalink
make context menu display either Lock or Unlock
Browse files Browse the repository at this point in the history
depending on the selection
  • Loading branch information
ollimeier committed May 16, 2024
1 parent 3343666 commit 5bcd23e
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions src/fontra/views/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ export class EditorController {
});
}

initContextMenuItems() {
async initContextMenuItems() {
this.basicContextMenuItems = [];
for (const isRedo of [false, true]) {
this.basicContextMenuItems.push({
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -2140,7 +2170,7 @@ export class EditorController {
if (!guidelineGlyph) {
continue;
}
guidelineGlyph.locked = !event.altKey;
guidelineGlyph.locked = locking;
}
}
return `${identifier} Guideline(s)`;
Expand Down

0 comments on commit 5bcd23e

Please sign in to comment.