Skip to content

Commit

Permalink
Scroll annotation view when location changes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfactotum committed Oct 13, 2023
1 parent d76a0ec commit e7979ce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,13 @@ GObject.registerClass({
},
}, class extends Gtk.ListView {
#filter
#location
// don't scroll when activating, as that would mean it's already in view
#shouldScroll = true
constructor(params) {
super(params)
this.connect('activate', (_, pos) => {
this.#shouldScroll = false
const annotation = this.model.model.get_item(pos).item ?? {}
if (annotation) this.emit('go-to-annotation', annotation)
})
Expand Down Expand Up @@ -306,6 +310,7 @@ GObject.registerClass({
.new(model, false, true, item => item.subitems ?? null)
this.#filter = new Gtk.FilterListModel({ model: tree })
this.model = new Gtk.NoSelection({ model: this.#filter })
if (this.#location) this.scrollToCFI(this.#location.cfi)
}
filter(query) {
query = query?.trim()?.toLowerCase()
Expand All @@ -317,6 +322,29 @@ GObject.registerClass({
} : null)
this.#filter.filter = filter
}
#scrollToIndex(i) {
return this.scroll_to(i, Gtk.ListScrollFlags.NONE, null)
}
scrollToCFI(cfi) {
for (const [i, item] of utils.gliter(this.#filter))
if (item.item.value && CFI.compare(cfi, item.item.value) <= 0)
return this.#scrollToIndex(i)
return this.#scrollToIndex(this.#filter.get_n_items() - 1)
}
update(location) {
if (!this.#filter) {
this.#location = location
return
}
if (!this.#shouldScroll) {
this.#shouldScroll = true
this.#location = location
return
}
if (this.#location.cfi === location.cfi) return
this.scrollToCFI(location.cfi)
this.#location = location
}
})

const AnnotationColor = utils.makeDataClass('FoliateAnnotationColor', {
Expand Down
2 changes: 2 additions & 0 deletions src/book-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ export const BookViewer = GObject.registerClass({
this._search_view.index = section.current
this._navbar.update(payload)
this._bookmark_view.update(payload)
this._annotation_view.update(payload)
if (this.#data) {
this.#data.storage.set('progress', [location.current, location.total])
this.#data.storage.set('lastLocation', cfi)
Expand All @@ -914,6 +915,7 @@ export const BookViewer = GObject.registerClass({
}
#showSelection({ type, value, text, lang, pos: { point, dir } }) {
if (type === 'annotation') return new Promise(resolve => {
this._annotation_view.scrollToCFI(value)
const annotation = this.#data.annotations.get(value)
const popover = utils.connect(new AnnotationPopover({ annotation }), {
'delete-annotation': () => this.#deleteAnnotation(annotation),
Expand Down

0 comments on commit e7979ce

Please sign in to comment.