Skip to content

Commit

Permalink
Use new GTK dialog API
Browse files Browse the repository at this point in the history
Fixes #1007
  • Loading branch information
johnfactotum committed Apr 23, 2023
1 parent 1c31610 commit 61358a9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Read books in style.
### Run Time Dependencies

- `gjs` (>= 1.72)
- `gtk4`
- `gtk4` (>= 4.10)
- `libadwaita` (`gir1.2-adw-1` in Debian-based distros)
- `webkitgtk-6.0` (`gir1.2-webkit-6.0` in Debian-based distros)

Expand Down
44 changes: 20 additions & 24 deletions src/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,18 +400,16 @@ GObject.registerClass({
const selected = this.selected
const item = this.selected_item
if (item.type === 'choose') {
const chooser = new Gtk.ColorChooserDialog({
modal: true,
transient_for: this.root,
})
chooser.show()
chooser.connect('response', (_, res) => {
if (res === Gtk.ResponseType.OK) {
const color = chooser.get_rgba().to_string()
new Gtk.ColorDialog().choose_rgba(this.root, null, null, (self, res) => {
try {
const color = self.choose_rgba_finish(res).to_string()
this.selectColor(color)
this.emit('color-changed', color)
} else this.selected = this.#prevSelected
chooser.close()
} catch (e) {
if (e instanceof Gtk.DialogError) console.debug(e)
else console.error(e)
this.selected = this.#prevSelected
}
})
} else {
this.emit('color-changed', item.value)
Expand Down Expand Up @@ -521,23 +519,21 @@ export const exportAnnotations = (window, data) => {
builder.get_object('ok-button').connect('clicked', () => {
const { selected } = builder.get_object('format-combo')
const format = ['json', 'html', 'md'][selected]
const chooser = new Gtk.FileChooserNative({
title: _('Save File'),
action: Gtk.FileChooserAction.SAVE,
transient_for: window,
modal: true,
})
const { metadata = {} } = data
const title = vprintf(_('Annotations for “%s”'), [metadata.title])
const total = vprintf(ngettext('%d Annotation', '%d Annotations', n), [n])
chooser.set_current_name(title + '.' + format)
chooser.show()
chooser.connect('response', (_, res) => {
if (res !== Gtk.ResponseType.ACCEPT) return
const contents = exportFunctions[format](data, title, total)
chooser.get_file().replace_contents(contents, null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION, null)
})
new Gtk.FileDialog({ initial_name: title + '.' + format })
.save(window, null, (self, res) => {
try {
const file = self.save_finish(res)
const contents = exportFunctions[format](data, title, total)
file.replace_contents(contents, null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION, null)
} catch (e) {
if (e instanceof Gtk.DialogError) console.debug(e)
else console.error(e)
}
})
dialog.close()
})
builder.get_object('cancel-button').connect('clicked', () => dialog.close())
Expand Down
56 changes: 27 additions & 29 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,34 @@ const ApplicationWindow = GObject.registerClass({
this.#bookViewer.open(file)
}
open() {
const chooser = new Gtk.FileChooserNative({
title: _('Open File'),
action: Gtk.FileChooserAction.OPEN,
transient_for: this,
modal: true,
const dialog = new Gtk.FileDialog()
const ebooks = new Gtk.FileFilter({
name: _('E-Book Files'),
mime_types: [
'application/epub+zip',
'application/x-mobipocket-ebook',
'application/vnd.amazon.mobi8-ebook',
'application/x-mobi8-ebook',
'application/x-fictionbook+xml',
'application/x-zip-compressed-fb2',
'application/vnd.comicbook+zip',
],
})

const all = new Gtk.FileFilter()
all.set_name(_('All Files'))
all.add_pattern('*')

const ebook = new Gtk.FileFilter()
ebook.set_name(_('E-book Files'))
for (const type of [
'application/epub+zip',
'application/x-mobipocket-ebook',
'application/vnd.amazon.mobi8-ebook',
'application/x-mobi8-ebook',
'application/x-fictionbook+xml',
'application/x-zip-compressed-fb2',
'application/vnd.comicbook+zip',
]) ebook.add_mime_type(type)

chooser.add_filter(all)
chooser.add_filter(ebook)
chooser.set_filter(ebook)

chooser.show()
chooser.connect('response', (_, res) => {
if (res === Gtk.ResponseType.ACCEPT) this.openFile(chooser.get_file())
dialog.filters = new Gio.ListStore()
dialog.filters.append(new Gtk.FileFilter({
name: _('All Files'),
patterns: ['*'],
}))
dialog.filters.append(ebooks)
dialog.default_filter = ebooks
dialog.open(this, null, (_, res) => {
try {
const file = dialog.open_finish(res)
this.openFile(file)
} catch (e) {
if (e instanceof Gtk.DialogError) console.debug(e)
else console.error(e)
}
})
}
showLibrary() {
Expand Down
24 changes: 11 additions & 13 deletions src/book-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,20 +814,18 @@ export const BookViewer = GObject.registerClass({
new Adw.Toast({ title: _('Copied to clipboard') }))
},
'save-as': () => {
const chooser = new Gtk.FileChooserNative({
title: _('Save File'),
action: Gtk.FileChooserAction.SAVE,
transient_for: win,
modal: true,
})
const ext = /\/([^+]*)/.exec(mimetype)?.[1]
chooser.set_current_name(win.title + (ext ? `.${ext}` : ''))
chooser.connect('response', (_, res) => {
if (res === Gtk.ResponseType.ACCEPT) chooser.get_file()
.replace_contents(bytes, null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION, null)
})
chooser.show()
new Gtk.FileDialog({ initial_name: win.title + (ext ? `.${ext}` : '') })
.save(win, null, (self, res) => {
try {
const file = self.save_finish(res)
file.replace_contents(bytes, null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION, null)
} catch (e) {
if (e instanceof Gtk.DialogError) console.debug(e)
else console.error(e)
}
})
},
})
win.add_controller(utils.addShortcuts({ '<ctrl>w': () => win.close() }))
Expand Down

0 comments on commit 61358a9

Please sign in to comment.