Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Image preview should not zoom on first click if unfocused #82074

Merged
merged 15 commits into from Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 40 additions & 1 deletion extensions/image-preview/media/main.js
Expand Up @@ -70,6 +70,7 @@
let ctrlPressed = false;
let altPressed = false;
let hasLoadedImage = false;
let consumeClick = false;

// Elements
const container = document.body;
Expand Down Expand Up @@ -116,6 +117,18 @@
});
}

function changeActive(value) {
if (value) {
container.classList.add('zoom-in');
consumeClick = true;
} else {
ctrlPressed = false;
altPressed = false;
container.classList.remove('zoom-out');
container.classList.remove('zoom-in');
}
}

function firstZoom() {
if (!image || !hasLoadedImage) {
return;
Expand Down Expand Up @@ -152,6 +165,18 @@
}
});

container.addEventListener('mousedown', (/** @type {MouseEvent} */ e) => {
MartinBrathen marked this conversation as resolved.
Show resolved Hide resolved
if (!image || !hasLoadedImage) {
return;
}

if (e.button !== 0) {
return;
}

consumeClick = false;
});

container.addEventListener('click', (/** @type {MouseEvent} */ e) => {
if (!image || !hasLoadedImage) {
return;
Expand All @@ -161,6 +186,18 @@
return;
}

ctrlPressed = e.ctrlKey;
altPressed = e.altKey;

if (isMac ? altPressed : ctrlPressed) {
container.classList.remove('zoom-in');
container.classList.add('zoom-out');
}

if (consumeClick) {
consumeClick = false;
return;
}
// left click
if (scale === 'fit') {
firstZoom();
Expand Down Expand Up @@ -218,7 +255,6 @@
});

container.classList.add('image');
container.classList.add('zoom-in');

image.classList.add('scale-to-fit');

Expand Down Expand Up @@ -254,6 +290,9 @@
case 'setScale':
updateScale(e.data.scale);
break;
case 'setActive':
changeActive(e.data.value);
break;
}
});
}());
1 change: 1 addition & 0 deletions extensions/image-preview/src/preview.ts
Expand Up @@ -120,6 +120,7 @@ export class Preview extends Disposable {
}
this._previewState = PreviewState.Visible;
}
this.webviewEditor.webview.postMessage({ type: 'setActive', value: this.webviewEditor.active });
MartinBrathen marked this conversation as resolved.
Show resolved Hide resolved
}

private getWebiewContents(): string {
Expand Down