Refactor ZenGlanceManager and ZenThemesImporter classes

This commit is contained in:
mr. M 2024-10-28 14:44:32 +01:00
parent e782d724cd
commit 01905fa4e4
No known key found for this signature in database
GPG key ID: CBD57A2AEDBDA1FB
5 changed files with 174 additions and 0 deletions

15
src/ZenActorsManager.mjs Normal file
View file

@ -0,0 +1,15 @@
// Utility to register JSWindowActors
var gZenActorsManager = {
_actors: new Set(),
addJSWindowActor(...args) {
if (this._actors.has(args[0])) {
// Actor already registered, nothing to do
return;
}
ChromeUtils.registerWindowActor(...args);
this._actors.add(args[0]);
},
}

View file

@ -0,0 +1,19 @@
{
function registerWindowActors() {
// TODO: Only if the pref is enabled
gZenActorsManager.addJSWindowActor("ZenGlance", {
parent: {
esModuleURI: "chrome://browser/content/zen-components/actors/ZenGlanceParent.sys.mjs",
},
child: {
esModuleURI: "chrome://browser/content/zen-components/actors/ZenGlanceChild.sys.mjs",
events: {
DOMContentLoaded: {},
},
},
});
}
registerWindowActors();
}

View file

@ -304,3 +304,17 @@ var gZenThemesImporter = new (class {
await gZenStylesheetManager.writeStylesheet(this.styleSheetPath, themes);
}
})();
gZenActorsManager.addJSWindowActor("ZenThemeMarketplace", {
parent: {
esModuleURI: "chrome://browser/content/zen-components/actors/ZenThemeMarketplaceParent.sys.mjs",
},
child: {
esModuleURI: "chrome://browser/content/zen-components/actors/ZenThemeMarketplaceChild.sys.mjs",
events: {
DOMContentLoaded: {},
},
},
matches: ["https://*.zen-browser.app/*", "about:preferences"],
allFrames: true,
});

View file

@ -0,0 +1,102 @@
export class ZenGlanceChild extends JSWindowActorChild {
constructor() {
super();
this.mouseUpListener = this.handleMouseUp.bind(this);
this.mouseDownListener = this.handleMouseDown.bind(this);
this.clickListener = this.handleClick.bind(this);
}
async handleEvent(event) {
switch (event.type) {
case 'DOMContentLoaded':
await this.initiateGlance();
break;
default:
}
}
async getActivationMethod() {
if (this._activationMethod === undefined) {
this._activationMethod = await this.sendQuery('ZenGlance:GetActivationMethod');
}
return this._activationMethod;
}
async getHoverActivationDelay() {
if (this._hoverActivationDelay === undefined) {
this._hoverActivationDelay = await this.sendQuery('ZenGlance:GetHoverActivationDelay');
}
return this._hoverActivationDelay;
}
async receiveMessage(message) {
switch (message.name) {
}
}
async initiateGlance() {
this.mouseIsDown = false;
const activationMethod = await this.getActivationMethod();
if (activationMethod === 'hover') {
this.contentWindow.addEventListener('mousedown', this.mouseDownListener);
this.contentWindow.addEventListener('mouseup', this.mouseUpListener);
this.contentWindow.document.removeEventListener('click', this.clickListener);
} else if (activationMethod === 'ctrl' || activationMethod === 'alt' || activationMethod === 'shift') {
this.contentWindow.document.addEventListener('click', this.clickListener);
this.contentWindow.removeEventListener('mousedown', this.mouseDownListener);
this.contentWindow.removeEventListener('mouseup', this.mouseUpListener);
}
}
ensureOnlyKeyModifiers(event) {
return !(event.ctrlKey ^ event.altKey ^ event.shiftKey);
}
openGlance(url) {
this.sendAsyncMessage('ZenGlance:OpenGlance', { url });
}
handleMouseUp(event) {
this.mouseIsDown = false;
}
async handleMouseDown(event) {
if (event.target.tagName !== 'A') {
return;
}
this.mouseIsDown = true;
const hoverActivationDelay = await this.getHoverActivationDelay();
setTimeout(() => {
if (this.mouseIsDown) {
this.openGlance(event.target.href);
}
}, hoverActivationDelay);
}
handleClick(event) {
if (this.ensureOnlyKeyModifiers(event)) {
return;
}
const activationMethod = this._activationMethod;
if (activationMethod === 'ctrl' && !event.ctrlKey) {
return;
} else if (activationMethod === 'alt' && !event.altKey) {
return;
} else if (activationMethod === 'shift' && !event.shiftKey) {
return;
} else if (activationMethod === 'hover' || typeof activationMethod === 'undefined') {
return;
}
// get closest A element
const target = event.target.closest('A');
if (target) {
event.preventDefault();
event.stopPropagation();
this.openGlance(target.href);
}
}
}

View file

@ -0,0 +1,24 @@
export class ZenGlanceParent extends JSWindowActorParent {
constructor() {
super();
}
async receiveMessage(message) {
switch (message.name) {
case 'ZenGlance:GetActivationMethod': {
return Services.prefs.getStringPref('zen.glance.activation-method', 'ctrl');
}
case 'ZenGlance:GetHoverActivationDelay': {
return Services.prefs.getIntPref('zen.glance.hold-duration', 500);
}
case 'ZenGlance:OpenGlance': {
this.openGlance();
break;
}
}
}
openGlance() {
console.log('Opening glance');
}
}