diff --git a/src/preferences/BackgroundButton.js b/src/preferences/BackgroundButton.js index f5dbc6e..ccf51c5 100644 --- a/src/preferences/BackgroundButton.js +++ b/src/preferences/BackgroundButton.js @@ -1,7 +1,7 @@ -// SPDX-FileCopyrightText: 2021 Romain Vigier +// SPDX-FileCopyrightText: 2021, 2022 Romain Vigier // SPDX-License-Identifier: GPL-3.0-or-later -const { Gdk, GLib, GObject, Gtk } = imports.gi; +const { Gdk, GdkPixbuf, Gio, GLib, GObject, Gtk } = imports.gi; const { extensionUtils } = imports.misc; const Me = extensionUtils.getCurrentExtension(); @@ -10,7 +10,7 @@ const Me = extensionUtils.getCurrentExtension(); var BackgroundButton = GObject.registerClass({ GTypeName: 'BackgroundButton', Template: `file://${GLib.build_filenamev([Me.path, 'preferences', 'ui', 'BackgroundButton.ui'])}`, - InternalChildren: ['choose_button', 'change_button', 'clear_button', 'filechooser'], + InternalChildren: ['filechooser'], Properties: { path: GObject.ParamSpec.string( 'path', @@ -19,19 +19,24 @@ var BackgroundButton = GObject.registerClass({ GObject.ParamFlags.READWRITE, null ), + width: GObject.ParamSpec.int( + 'width', + 'Width', + 'Thumbnail width', + GObject.ParamFlags.READWRITE, + 0, 600, + 180 + ), + height: GObject.ParamSpec.int( + 'height', + 'Height', + 'Thumbnail height', + GObject.ParamFlags.READWRITE, + 0, 600, + 120 + ), }, -}, class BackgroundButton extends Gtk.Stack { - vfunc_mnemonic_activate() { - this.activate(); - } - - activate() { - if (this.path) - return this._change_button.activate(); - else - return this._choose_button.activate(); - } - +}, class BackgroundButton extends Gtk.Button { openFileChooser() { this._filechooser.transient_for = this.get_root(); this._filechooser.show(); @@ -43,19 +48,24 @@ var BackgroundButton = GObject.registerClass({ this.path = fileChooser.get_file().get_uri(); } - onPathChanged(button) { - button.visible_child_name = button.path ? 'edit' : 'choose'; - } - - onChooseButtonClicked(_button) { + onClicked(_button) { this.openFileChooser(); } - onChangeButtonClicked(_button) { - this.openFileChooser(); - } - - onClearButtonClicked(_button) { - this.path = ''; + getThumbnail(_widget, path) { + if (!path) + 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 thumbPixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.GDK_COLORSPACE_RGB, false, pixbuf.bits_per_sample, this.width, this.height); + pixbuf.scale( + thumbPixbuf, + 0, 0, + this.width, this.height, + -(pixbuf.width * scale - this.width) / 2, -(pixbuf.height * scale - this.height) / 2, + scale, scale, + GdkPixbuf.InterpType.GDK_INTERP_BILINEAR + ); + return Gdk.Texture.new_for_pixbuf(thumbPixbuf); } }); diff --git a/src/preferences/ui/BackgroundButton.ui b/src/preferences/ui/BackgroundButton.ui index be21d3e..071de77 100644 --- a/src/preferences/ui/BackgroundButton.ui +++ b/src/preferences/ui/BackgroundButton.ui @@ -4,54 +4,20 @@ SPDX-FileCopyrightText: 2021 Romain Vigier SPDX-License-Identifier: GPL-3.0-or-later --> -