Skip to content

Commit

Permalink
Add a config view.pdf.toolbar.hide.timeout to control toolbar hidin…
Browse files Browse the repository at this point in the history
…g timeout
  • Loading branch information
James-Yu committed Oct 13, 2024
1 parent bd34e12 commit 101808f
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 19 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/preview/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions test/units/10_viewer_pdf_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions types/latex-workshop-protocol-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type ServerResponse = {
}

export type PdfViewerParams = {
toolbar: number,
scale: string,
trim: number,
scrollMode: number,
Expand Down
25 changes: 14 additions & 11 deletions viewer/components/gui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,47 @@ 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)
hideToolbar = undefined
}
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]
toolbarDom.classList.add('hide')
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 =
Expand Down Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions viewer/components/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions viewer/components/state.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions viewer/components/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import type { PdfViewerParams } from '../../types/latex-workshop-protocol-types/index'

export const pdfFilePrefix = 'pdf..'

export async function getParams(): Promise<PdfViewerParams> {
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))
}
Expand Down
5 changes: 2 additions & 3 deletions viewer/latexworkshop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 = {
Expand All @@ -67,7 +66,7 @@ async function initialization() {
})

initConnect()
patchViewerUI()
await patchViewerUI()
registerKeyBind()
}

Expand Down

0 comments on commit 101808f

Please sign in to comment.