Skip to content

Commit

Permalink
Save cover thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfactotum committed Jul 29, 2023
1 parent efc2dd4 commit 4d97e89
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
6 changes: 6 additions & 0 deletions data/com.github.johnfactotum.Foliate.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<key name="view-mode" type="s">
<default>'grid'</default>
</key>
<key name="show-covers" type="b">
<default>true</default>
</key>
<key name="cover-size" type="i">
<default>256</default>
</key>
</schema>

<schema id="com.github.johnfactotum.Foliate.viewer"
Expand Down
23 changes: 18 additions & 5 deletions src/book-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import GObject from 'gi://GObject'
import WebKit from 'gi://WebKit'
import Gdk from 'gi://Gdk'
import Pango from 'gi://Pango'
import GdkPixbuf from 'gi://GdkPixbuf'
import { gettext as _ } from 'gettext'

import * as utils from './utils.js'
Expand Down Expand Up @@ -179,6 +180,18 @@ class BookData {
#saveBookmarks() {
this.storage.set('bookmarks', this.bookmarks.export())
}
saveCover(cover) {
const settings = utils.settings('library')
if (!(settings?.get_boolean('show-covers') ?? true)) return
const path = pkg.cachepath(this.key + '.png')
if (Gio.File.new_for_path(path).query_exists(null)) return
const width = settings?.get_int('cover-size') ?? 256
const ratio = width / cover.get_width()
const scaled = ratio >= 1 ? cover
: cover.scale_simple(width, Math.round(cover.get_height() * ratio),
GdkPixbuf.InterpType.BILINEAR)
scaled.savev(path, 'png', [], [])
}
}

class BookDataStore {
Expand Down Expand Up @@ -816,11 +829,10 @@ export const BookViewer = GObject.registerClass({
this._navbar.loadPageList(book.pageList, reader.pageTotal)
this._navbar.loadLandmarks(book.landmarks)

this._view.getCover().then(cover => {
this.#cover = cover
if (cover) this._book_cover.set_from_pixbuf(cover)
else this._book_cover.icon_name = 'image-missing-symbolic'
})
const cover = await this._view.getCover()
this.#cover = cover
if (cover) this._book_cover.set_from_pixbuf(cover)
else this._book_cover.icon_name = 'image-missing-symbolic'

book.metadata.identifier ||= makeIdentifier(this.#file)
const { identifier } = book.metadata
Expand All @@ -842,6 +854,7 @@ export const BookViewer = GObject.registerClass({
updateBookmarks()
this.#data.storage.set('metadata', book.metadata)
getURIStore().set(identifier, this.#file.get_uri())
if (cover) this.#data.saveCover(cover)
}
else await this._view.next()
}
Expand Down
6 changes: 4 additions & 2 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import * as format from './format.js'
import { exportAnnotations } from './annotations.js'
import { formatAuthors, makeBookInfoWindow } from './book-info.js'

const showCovers = utils.settings('library')?.get_boolean('show-covers') ?? true

const listDir = function* (path) {
const dir = Gio.File.new_for_path(path)
if (!GLib.file_test(path, GLib.FileTest.IS_DIR)) return null
Expand Down Expand Up @@ -288,7 +290,7 @@ GObject.registerClass({
'setup': (_, item) => item.child =
utils.connect(new BookItem(), this.#itemConnections),
'bind': (_, { child, item }) => {
const { cover, data } = this.#getData(item, true)
const { cover, data } = this.#getData(item, showCovers)
child.update(item, data, cover)
if (cover?.then) cover
.then(cover => child.update(item, data, cover))
Expand All @@ -308,7 +310,7 @@ GObject.registerClass({
'setup': (_, item) => item.child = utils.connect(
new BookRow(), this.#itemConnections),
'bind': (_, { child, item }) => {
const { data } = this.#getData(item, true)
const { data } = this.#getData(item, false)
child.update(item, data)
},
}),
Expand Down

0 comments on commit 4d97e89

Please sign in to comment.