import { Disposable } from "../../../../base/common/lifecycle.js"; import { MenuId } from "../../../../platform/actions/common/actions.js"; import { IClipboardService } from "../../../../platform/clipboard/common/clipboardService.service.js"; import { IContextKeyService } from "../../../../platform/contextkey/common/contextkey.service.js"; import { IContextMenuService } from "../../../../platform/contextview/browser/contextView.service.js"; import { IWebWorkerService } from "../../../../platform/webWorker/browser/webWorkerService.service.js"; import { IEditorPane } from "../../../common/editor.js"; /** * Either a numeric zoom factor (`1` is the natural size) or `'fit'` to scale the image to fit * into the viewer. */ export type ImageViewerScale = number | "fit"; export interface IImageViewerViewState { readonly scale: ImageViewerScale; readonly scrollLeft: number; readonly scrollTop: number; } export interface IImageViewerOptions { /** * The element that holds keyboard focus for the viewer. Defaults to the viewer element. * * It is used to decide whether a click should zoom (clicks that only move focus into the * viewer don't) and whether copying applies to the viewer. It also receives the native `copy` * event, modifier key changes and context menu requests from the keyboard. */ readonly focusTarget?: HTMLElement; } export interface IImageViewerSourceOptions { /** * Alternative text for the image. */ readonly alt: string; /** * View state to restore once the image is shown. When omitted, the image is scaled to fit. */ readonly viewState?: IImageViewerViewState; /** * The encoded image. PNG images are copied as is instead of being encoded again. */ readonly data?: IImageViewerSourceData; } export interface IImageViewerSourceData { readonly mimeType: string; read(): Promise; } /** * An editor pane that renders its content with an {@link ImageViewer}. */ export interface IImageViewerEditorPane extends IEditorPane { /** * The viewer that shows the current image, if any. */ readonly imageViewer: ImageViewer | undefined; } export declare function isImageViewerEditorPane(pane: IEditorPane | undefined): pane is IImageViewerEditorPane; /** * Context menu shown when right clicking an image in an {@link ImageViewer}. Commands contributed * to it are invoked with the viewer as their first argument. */ export declare const ImageViewerContextMenuId: MenuId; /** * Returns the next predefined zoom level when zooming in from `scale`. */ export declare function getNextZoomLevel(scale: number): number; /** * Returns the previous predefined zoom level when zooming out from `scale`. */ export declare function getPreviousZoomLevel(scale: number): number; /** * Displays a single image with support for zooming (click, modifier + scroll wheel and pinch), * scrolling around zoomed images, and copying the image. Shared by the image preview editor and * the image carousel. */ export declare class ImageViewer extends Disposable { private readonly _contextMenuService; private readonly _contextKeyService; private readonly _clipboardService; private readonly _webWorkerService; private static readonly _instances; /** * Gets the viewer that currently has keyboard focus, if any. */ static getFocusedViewer(): ImageViewer | undefined; readonly element: HTMLElement; private readonly _image; private readonly _focusTarget; private _scale; private _hasImage; private _sourceRequestId; private _sourceData; /** * Whether the image needs an explicit size to be zoomed. Images without an intrinsic width, * such as SVGs with only a viewBox, otherwise stretch to fill the viewer, which cancels out * the zoom. */ private _needsExplicitSize; /** * Size of the image when it was last scaled to fit. */ private _lastFitSize; private readonly _onDidChangeScale; readonly onDidChangeScale: import("../../../../base/common/event.js").Event; constructor(options: IImageViewerOptions, _contextMenuService: IContextMenuService, _contextKeyService: IContextKeyService, _clipboardService: IClipboardService, _webWorkerService: IWebWorkerService); get scale(): ImageViewerScale; get hasImage(): boolean; /** * Whether keyboard focus is in the viewer or its focus target. */ hasFocus(): boolean; /** * The natural size of the current image, if it has loaded. */ get naturalSize(): { readonly width: number; readonly height: number; } | undefined; /** * Shows the image at `src`. The image is decoded before it replaces the current one, so * switching between images doesn't flash. * * @returns `true` if the image was decoded and shown, `false` if it failed to decode or a * newer call superseded this one. */ setSource(src: string, options: IImageViewerSourceOptions): Promise; /** * Discards an image that is still loading, so it doesn't replace the current one. */ cancelPendingSource(): void; /** * Removes the current image. */ clear(): void; getViewState(): IImageViewerViewState; setScale(scale: ImageViewerScale): void; zoomIn(): void; zoomOut(): void; /** * Copies the current image to the clipboard as a PNG. */ copyImage(): Promise; private _registerListeners; /** * Whether `e` presses the context menu key or Shift+F10 while the viewer, or the focus target * itself, has focus. Other elements in the focus target, such as buttons, keep their own * context menus. */ private _isContextMenuKey; /** * Anchors a context menu opened from the keyboard at the center of the viewer. The image is * centered in the viewer, or fills it when zoomed in. */ private _getKeyboardContextMenuAnchor; private _showContextMenu; /** * Returns the current scale as a number, computing the effective scale when the image is * scaled to fit. */ private _getNumericScale; /** * Applies fit-to-viewer or a numeric zoom while preserving the visible center of the image. */ private _applyScale; /** * Gets the size of the image at a zoom level of 1. * * Only the larger dimension is returned as the other one follows from the aspect ratio. Natural * sizes are rounded, so the smaller dimension of a very thin SVG may otherwise be reported as 0. */ private _getBaseSize; /** * Checks if the image keeps its natural width in a container that is much wider than it. */ private _hasIntrinsicWidth; /** * Reads the source PNG so it can be copied without encoding it again, which is both faster and * lossless. Falls back to encoding when the data turns out not to be a PNG. */ private _readPng; } /** * Encodes the displayed frame at its natural size, sharing the image viewer's worker encoder. */ export declare function encodeImageAsPng(image: HTMLImageElement, webWorkerService: IWebWorkerService): Promise;