diff --git a/package.json b/package.json index 18f004605..a2f262475 100644 --- a/package.json +++ b/package.json @@ -1579,6 +1579,12 @@ "default": 0, "markdownDescription": "The default trim percentage of the PDF viewer. This should be an integer between 0 and 99, indicating the portion of PDF to be trimmed. For example, setting the value to `10` means that 10% of the height and width of PDF pages are trimmed off." }, + "latex-workshop.view.pdf.toolbar.hide.timeout": { + "scope": "window", + "type": "number", + "default": 1, + "markdownDescription": "The timeout delay in second to hide the toolbar. Setting this value to zero will disable toolbar hiding." + }, "latex-workshop.view.pdf.scrollMode": { "scope": "window", "type": "number", diff --git a/src/preview/viewer.ts b/src/preview/viewer.ts index 753bf6232..c75ef8277 100644 --- a/src/preview/viewer.ts +++ b/src/preview/viewer.ts @@ -33,7 +33,7 @@ lw.watcher.pdf.onChange(pdfUri => { refresh(pdfUri) } }) -lw.onConfigChange(['view.pdf.invert', 'view.pdf.invertMode', 'view.pdf.color', 'view.pdf.internal'], () => { +lw.onConfigChange(['view.pdf.toolbar.hide.timeout', 'view.pdf.invert', 'view.pdf.invertMode', 'view.pdf.color', 'view.pdf.internal'], () => { reload() }) @@ -314,6 +314,7 @@ function getParams(): PdfViewerParams { invertType === 'always' || (invertType === 'compat' && (configuration.get('view.pdf.invert') as number) > 0) const pack: PdfViewerParams = { + toolbar: configuration.get('view.pdf.toolbar.hide.timeout') as number, scale: configuration.get('view.pdf.zoom') as string, trim: configuration.get('view.pdf.trim') as number, scrollMode: configuration.get('view.pdf.scrollMode') as number, diff --git a/test/units/10_viewer_pdf_server.test.ts b/test/units/10_viewer_pdf_server.test.ts index 2de275cd7..4ae38a677 100644 --- a/test/units/10_viewer_pdf_server.test.ts +++ b/test/units/10_viewer_pdf_server.test.ts @@ -524,6 +524,12 @@ describe(path.basename(__filename).split('.')[0] + ':', () => { manager.getClients(pdfUri)?.clear() }) + it('should reload all viewers if config `view.pdf.toolbar.hide.timeout` is changed', async () => { + const promise = waitMsg(JSON.stringify({ type: 'reload' }).repeat(2)) + await set.codeConfig('view.pdf.toolbar.hide.timeout', 0) + await promise + }) + it('should reload all viewers if config `view.pdf.invert` is changed', async () => { const promise = waitMsg(JSON.stringify({ type: 'reload' }).repeat(2)) await set.codeConfig('view.pdf.invert', 1) diff --git a/types/latex-workshop-protocol-types/index.d.ts b/types/latex-workshop-protocol-types/index.d.ts index a90844ec6..4f589f842 100644 --- a/types/latex-workshop-protocol-types/index.d.ts +++ b/types/latex-workshop-protocol-types/index.d.ts @@ -23,6 +23,7 @@ export type ServerResponse = { } export type PdfViewerParams = { + toolbar: number, scale: string, trim: number, scrollMode: number, diff --git a/viewer/components/gui.ts b/viewer/components/gui.ts index 50be5e8ec..f9b6ed9db 100644 --- a/viewer/components/gui.ts +++ b/viewer/components/gui.ts @@ -9,14 +9,19 @@ import type { PDFViewerApplicationType } from './interface.js' declare const PDFViewerApplication: PDFViewerApplicationType let hideToolbar: number | undefined -export function patchViewerUI() { +export async function patchViewerUI() { if (utils.isEmbedded()) { // Cannot simply remove this element, as pdf.js indeed require it to // bind listeners. document.getElementById('printButton')!.style.display = 'none' } + const params = await utils.getParams() + document.getElementById('outerContainer')!.onmousemove = (e) => { + if (params.toolbar === 0) { + return + } if (e.clientY <= 64) { if (hideToolbar) { clearTimeout(hideToolbar) @@ -24,6 +29,9 @@ export function patchViewerUI() { } showToolbar() } else { + if (typeof PDFViewerApplication === 'undefined') { + return + } if (hideToolbar === undefined && !PDFViewerApplication.findBar.opened && !PDFViewerApplication.pdfSidebar.isOpen && !PDFViewerApplication.secondaryToolbar.isOpen) { hideToolbar = setTimeout(() => { const toolbarDom = document.getElementsByClassName('toolbar')[0] @@ -31,15 +39,17 @@ export function patchViewerUI() { const containerDom = document.getElementById('viewerContainer')! containerDom.classList.add('topped') hideToolbar = undefined - }, 1000) + }, params.toolbar * 1000) } } } document.getElementById('sidebarResizer')?.classList.add('hidden') - document.getElementsByClassName('toolbar')[0]?.classList.add('hide') - document.getElementById('viewerContainer')!.classList.add('topped') document.getElementById('firstPage')?.previousElementSibling?.classList.add('visibleLargeView') + if (params.toolbar !== 0) { + document.getElementsByClassName('toolbar')[0]?.classList.add('hide') + document.getElementById('viewerContainer')!.classList.add('topped') + } const template = document.createElement('template') template.innerHTML = @@ -233,11 +243,4 @@ function showToolbar() { toolbarDom.classList.remove('hide') const containerDom = document.getElementById('viewerContainer')! containerDom.classList.remove('topped') - - // hideToolbarInterval = setInterval(() => { - // if(!PDFViewerApplication.findBar.opened && !PDFViewerApplication.pdfSidebar.isOpen && !PDFViewerApplication.secondaryToolbar.isOpen) { - // d.className = d.className.replace(' notransition', '') + ' hide' - // clearInterval(hideToolbarInterval) - // } - // }, 3000) } diff --git a/viewer/components/refresh.ts b/viewer/components/refresh.ts index 4cc459899..639e4d1c9 100644 --- a/viewer/components/refresh.ts +++ b/viewer/components/refresh.ts @@ -3,7 +3,6 @@ import { getTrimValue, setTrimValue } from './trimming.js' import { sendLog } from './connection.js' import { viewerState, viewerStatePromise } from './state.js' import { type PDFViewerApplicationType, type PDFViewerApplicationOptionsType, RenderingStates } from './interface.js' -import type { PdfViewerParams } from '../../types/latex-workshop-protocol-types/index.js' declare const pdfjsLib: any declare const PDFViewerApplication: PDFViewerApplicationType @@ -137,7 +136,7 @@ export async function restoreState() { } async function restoreDefault() { - const params = await (await fetch('config.json')).json() as PdfViewerParams + const params = await utils.getParams() if (params.trim !== undefined) { setTrimValue(params.trim) diff --git a/viewer/components/state.ts b/viewer/components/state.ts index 01914c208..5ffa9bca3 100644 --- a/viewer/components/state.ts +++ b/viewer/components/state.ts @@ -1,6 +1,6 @@ import * as utils from './utils.js' import type { PDFViewerApplicationType } from './interface' -import type { PanelManagerResponse, PdfViewerParams, PdfViewerState } from '../../types/latex-workshop-protocol-types/index.js' +import type { PanelManagerResponse, PdfViewerState } from '../../types/latex-workshop-protocol-types/index.js' import { getTrimValue } from './trimming.js' import { isSyncTeXEnabled, registerSyncTeX, setSyncTeXKey } from './synctex.js' import { IsAutoRefreshEnabled } from './refresh.js' @@ -53,7 +53,7 @@ export function uploadState() { } export async function setParams() { - const params = await (await fetch('config.json')).json() as PdfViewerParams + const params = await utils.getParams() const htmlElement = document.querySelector('html') as HTMLHtmlElement const viewerContainer = document.querySelector('#viewerContainer') as HTMLHtmlElement diff --git a/viewer/components/utils.ts b/viewer/components/utils.ts index 577b99d85..c3dc0b868 100644 --- a/viewer/components/utils.ts +++ b/viewer/components/utils.ts @@ -1,5 +1,17 @@ +import type { PdfViewerParams } from '../../types/latex-workshop-protocol-types/index' + export const pdfFilePrefix = 'pdf..' +export async function getParams(): Promise { + const storedParams = (globalThis as any).lwParams as PdfViewerParams | undefined + if (storedParams) { + return storedParams as PdfViewerParams + } + const params = await (await fetch('config.json')).json() as PdfViewerParams + ;(globalThis as any).lwParams = params + return params +} + export async function sleep(timeout: number) { await new Promise((resolve) => setTimeout(resolve, timeout)) } diff --git a/viewer/latexworkshop.ts b/viewer/latexworkshop.ts index 81dd70178..7b18deac2 100644 --- a/viewer/latexworkshop.ts +++ b/viewer/latexworkshop.ts @@ -2,7 +2,6 @@ import { patchViewerUI, registerKeyBind, repositionAnnotation } from './componen import * as utils from './components/utils.js' import type { PdfjsEventName, PDFViewerApplicationType, PDFViewerApplicationOptionsType } from './components/interface.js' -import type { PdfViewerParams } from '../types/latex-workshop-protocol-types/index' import { initTrim, setTrimCSS } from './components/trimming.js' import { doneRefresh, restoreState } from './components/refresh.js' import { initUploadState, setParams, uploadState } from './components/state.js' @@ -47,8 +46,8 @@ function onPDFViewerEvent(event: PdfjsEventName, cb: (evt?: any) => unknown, opt async function initialization() { document.title = utils.parseURL().docTitle + const params = await utils.getParams() const worker = new Worker('build/pdf.worker.mjs', { type: 'module' }) - const params = await (await fetch('config.json')).json() as PdfViewerParams document.addEventListener('webviewerloaded', () => { const color = utils.isPrefersColorSchemeDark(params.codeColorTheme) ? params.color.dark : params.color.light const options = { @@ -67,7 +66,7 @@ async function initialization() { }) initConnect() - patchViewerUI() + await patchViewerUI() registerKeyBind() }