Background button: Accept all supported image types

This commit is contained in:
Romain Vigier
2022-06-17 08:43:44 +02:00
parent 66d910fed4
commit befc82ec65
2 changed files with 32 additions and 31 deletions
-11
View File
@@ -31,16 +31,5 @@ SPDX-License-Identifier: GPL-3.0-or-later
<property name="title" translatable="yes">Select your background image</property> <property name="title" translatable="yes">Select your background image</property>
<property name="select-multiple">False</property> <property name="select-multiple">False</property>
<signal name="response" handler="onFileChooserResponse" swapped="no"/> <signal name="response" handler="onFileChooserResponse" swapped="no"/>
<property name="filter">
<object class="GtkFileFilter">
<mime-types>
<mime-type>image/jpeg</mime-type>
<mime-type>image/png</mime-type>
<mime-type>image/svg+xml</mime-type>
<mime-type>image/tiff</mime-type>
<mime-type>application/xml</mime-type>
</mime-types>
</object>
</property>
</object> </object>
</interface> </interface>
+32 -20
View File
@@ -42,6 +42,7 @@ var BackgroundButton = GObject.registerClass({
super(props); super(props);
this.#setupSize(); this.#setupSize();
this.#setupDropTarget(); this.#setupDropTarget();
this.#setupFileChooserFilter();
} }
#setupSize() { #setupSize() {
@@ -59,13 +60,7 @@ var BackgroundButton = GObject.registerClass({
const dropTarget = Gtk.DropTarget.new(Gio.File.$gtype, Gdk.DragAction.COPY); const dropTarget = Gtk.DropTarget.new(Gio.File.$gtype, Gdk.DragAction.COPY);
dropTarget.connect('drop', (_target, file, _x, _y) => { dropTarget.connect('drop', (_target, file, _x, _y) => {
const contentType = Gio.content_type_guess(file.get_basename(), null)[0]; const contentType = Gio.content_type_guess(file.get_basename(), null)[0];
if ( if (this.#isContentTypeSupported(contentType)) {
Gio.content_type_equals(contentType, 'image/jpeg') ||
Gio.content_type_equals(contentType, 'image/png') ||
Gio.content_type_equals(contentType, 'image/svg+xml') ||
Gio.content_type_equals(contentType, 'image/tiff') ||
Gio.content_type_equals(contentType, 'application/xml')
) {
this.uri = file.get_uri(); this.uri = file.get_uri();
return true; return true;
} else { } else {
@@ -81,6 +76,24 @@ var BackgroundButton = GObject.registerClass({
this.add_controller(dropTarget); this.add_controller(dropTarget);
} }
#setupFileChooserFilter() {
this._filechooser.filter = new Gtk.FileFilter();
this._filechooser.filter.add_pixbuf_formats();
this._filechooser.filter.add_mime_type('application/xml');
}
#getSupportedContentTypes() {
return GdkPixbuf.Pixbuf.get_formats().flatMap(format => format.get_mime_types()).concat('application/xml');
}
#isContentTypeSupported(contentType) {
for (const supportedContentType of this.#getSupportedContentTypes()) {
if (Gio.content_type_equals(contentType, supportedContentType))
return true;
}
return false;
}
vfunc_mnemonic_activate() { vfunc_mnemonic_activate() {
this.openFileChooser(); this.openFileChooser();
} }
@@ -104,26 +117,25 @@ var BackgroundButton = GObject.registerClass({
if (!uri) if (!uri)
return null; return null;
let path;
const file = Gio.File.new_for_uri(uri); const file = Gio.File.new_for_uri(uri);
const contentType = Gio.content_type_guess(file.get_basename(), null)[0]; const contentType = Gio.content_type_guess(file.get_basename(), null)[0];
if (
Gio.content_type_equals(contentType, 'image/jpeg') || if (!this.#isContentTypeSupported(contentType))
Gio.content_type_equals(contentType, 'image/png') || return null;
Gio.content_type_equals(contentType, 'image/svg+xml') ||
Gio.content_type_equals(contentType, 'image/tiff') let path;
) { if (Gio.content_type_equals(contentType, 'application/xml')) {
path = file.get_path();
} else if (Gio.content_type_equals(contentType, 'application/xml')) {
const decoder = new TextDecoder('utf-8'); const decoder = new TextDecoder('utf-8');
const contents = decoder.decode(file.load_contents(null)[1]); const contents = decoder.decode(file.load_contents(null)[1]);
try { try {
path = contents.match(/<file>(.+)<\/file>/m)[1]; path = contents.match(/<file>(.+)<\/file>/m)[1];
} catch (_e) {} } catch (e) {
} console.error(e);
if (!path)
return null; return null;
}
} else {
path = file.get_path();
}
const pixbuf = GdkPixbuf.Pixbuf.new_from_file(path); const pixbuf = GdkPixbuf.Pixbuf.new_from_file(path);
const scale = pixbuf.width / pixbuf.height > width / height ? height / pixbuf.height : width / pixbuf.width; const scale = pixbuf.width / pixbuf.height > width / height ? height / pixbuf.height : width / pixbuf.width;