Skip to content

Commit

Permalink
View: add ability to automatically hide cursor
Browse files Browse the repository at this point in the history
Controlled by the boolean attribute `autohide-cursor`
  • Loading branch information
johnfactotum committed Sep 27, 2024
1 parent f3afcab commit ba40330
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions view.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@ import { textWalker } from './text-walker.js'

const SEARCH_PREFIX = 'foliate-search:'

class CursorAutohider {
#timeout
#el
#check
#state
constructor(el, check, state = {}) {
this.#el = el
this.#check = check
this.#state = state
if (this.#state.hidden) this.hide()
this.#el.addEventListener('mousemove', ({ screenX, screenY }) => {
// check if it actually moved
if (screenX === this.#state.x && screenY === this.#state.y) return
this.#state.x = screenX, this.#state.y = screenY
this.show()
if (this.#timeout) clearTimeout(this.#timeout)
if (check()) this.#timeout = setTimeout(this.hide.bind(this), 1000)
}, false)
}
cloneFor(el) {
return new CursorAutohider(el, this.#check, this.#state)
}
hide() {
this.#el.style.cursor = 'none'
this.#state.hidden = true
}
show() {
this.#el.style.removeProperty('cursor')
this.#state.hidden = false
}
}

class History extends EventTarget {
#arr = []
#index = -1
Expand Down Expand Up @@ -67,6 +99,8 @@ export class View extends HTMLElement {
#tocProgress
#pageProgress
#searchResults = new Map()
#cursorAutohider = new CursorAutohider(this, () =>
this.hasAttribute('autohide-cursor'))
isFixedLayout = false
lastLocation
history = new History()
Expand Down Expand Up @@ -187,6 +221,7 @@ export class View extends HTMLElement {
doc.documentElement.dir ||= this.language.direction ?? ''

this.#handleLinks(doc, index)
this.#cursorAutohider.cloneFor(doc.documentElement)

this.#emit('load', { doc, index })
}
Expand Down

0 comments on commit ba40330

Please sign in to comment.