Handle XML thumbnails and improve appearance

This commit is contained in:
Romain Vigier
2022-02-18 15:44:27 +01:00
parent a3425bed51
commit d40a739680
2 changed files with 34 additions and 12 deletions
+30 -12
View File
@@ -19,18 +19,18 @@ var BackgroundButton = GObject.registerClass({
GObject.ParamFlags.READWRITE, GObject.ParamFlags.READWRITE,
null null
), ),
width: GObject.ParamSpec.int( thumb_width: GObject.ParamSpec.int(
'width', 'thumb-width',
'Width',
'Thumbnail width', 'Thumbnail width',
'Width of the displayed thumbnail',
GObject.ParamFlags.READWRITE, GObject.ParamFlags.READWRITE,
0, 600, 0, 600,
180 180
), ),
height: GObject.ParamSpec.int( thumb_height: GObject.ParamSpec.int(
'height', 'thumb-height',
'Height',
'Thumbnail height', 'Thumbnail height',
'Height of the displayed thumbnail',
GObject.ParamFlags.READWRITE, GObject.ParamFlags.READWRITE,
0, 600, 0, 600,
120 120
@@ -52,20 +52,38 @@ var BackgroundButton = GObject.registerClass({
this.openFileChooser(); this.openFileChooser();
} }
getThumbnail(_widget, path) { getThumbnail(_widget, uri, width, height) {
if (!uri)
return null;
let path;
const file = Gio.File.new_for_uri(uri);
const contentType = Gio.content_type_guess(file.get_path(), null)[0];
if (Gio.content_type_equals(contentType, 'image/jpeg') || Gio.content_type_equals(contentType, 'image/png') || Gio.content_type_equals(contentType, 'image/tiff')) {
path = file.get_path();
} else if (Gio.content_type_equals(contentType, 'application/xml')) {
const decoder = new TextDecoder('utf-8');
const contents = decoder.decode(file.load_contents(null)[1]);
try {
path = contents.match(/<file>(.+)<\/file>/m)[1];
} catch (_e) {}
}
if (!path) if (!path)
return null; return null;
const pixbuf = GdkPixbuf.Pixbuf.new_from_file(Gio.File.new_for_uri(path).get_path());
const scale = this.width / (pixbuf.width > pixbuf.height ? pixbuf.height : pixbuf.width); const pixbuf = GdkPixbuf.Pixbuf.new_from_file(path);
const thumbPixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.GDK_COLORSPACE_RGB, false, pixbuf.bits_per_sample, this.width, this.height); const scale = (width > height ? width : height) / (pixbuf.width > pixbuf.height ? pixbuf.height : pixbuf.width);
const thumbPixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.GDK_COLORSPACE_RGB, pixbuf.has_alpha, pixbuf.bits_per_sample, width, height);
pixbuf.scale( pixbuf.scale(
thumbPixbuf, thumbPixbuf,
0, 0, 0, 0,
this.width, this.height, width, height,
-(pixbuf.width * scale - this.width) / 2, -(pixbuf.height * scale - this.height) / 2, -(pixbuf.width * scale - width) / 2, -(pixbuf.height * scale - height) / 2,
scale, scale, scale, scale,
GdkPixbuf.InterpType.GDK_INTERP_BILINEAR GdkPixbuf.InterpType.GDK_INTERP_BILINEAR
); );
return Gdk.Texture.new_for_pixbuf(thumbPixbuf); return Gdk.Texture.new_for_pixbuf(thumbPixbuf);
} }
}); });
+4
View File
@@ -15,9 +15,13 @@ SPDX-License-Identifier: GPL-3.0-or-later
<binding name="paintable"> <binding name="paintable">
<closure function="getThumbnail" type="GdkPaintable"> <closure function="getThumbnail" type="GdkPaintable">
<lookup name="path">BackgroundButton</lookup> <lookup name="path">BackgroundButton</lookup>
<lookup name="thumb-width">BackgroundButton</lookup>
<lookup name="thumb-height">BackgroundButton</lookup>
</closure> </closure>
</binding> </binding>
<property name="can-shrink">False</property> <property name="can-shrink">False</property>
<property name="width-request" bind-source="BackgroundButton" bind-property="thumb-width" bind-flags="sync-create"/>
<property name="height-request" bind-source="BackgroundButton" bind-property="thumb-height" bind-flags="sync-create"/>
</object> </object>
</child> </child>
</template> </template>