Skip to content

Instantly share code, notes, and snippets.

@xpaulnim
Created June 5, 2018 18:19
Show Gist options
  • Save xpaulnim/8ecc3fddc585fde07945129784d4afea to your computer and use it in GitHub Desktop.
Save xpaulnim/8ecc3fddc585fde07945129784d4afea to your computer and use it in GitHub Desktop.
Load Images Asynchronously Gtk Vala
// Compile with valac --pkg gtk+-3.0 --pkg=gio-2.0 Application.vala
public class Application : Gtk.Application {
private const int COVER_SIZE = 170;
public Application () {
Object(application_id: "testing.my.application",
flags: ApplicationFlags.FLAGS_NONE);
}
public static File? open_file(string path) {
if(path.index_of("file:") == 0 || path.index_of("resource:") == 0 || path.index_of("http:") == 0 || path.index_of("https:") == 0) {
return GLib.File.new_for_uri(path);
} else {
return GLib.File.new_for_path(path);
}
}
public static void create_cover_image (Gtk.Image image, Gdk.Pixbuf pixbuf, int image_size) throws Error {
Gdk.Pixbuf cover_image = pixbuf;
if (cover_image.height == cover_image.width)
cover_image = cover_image.scale_simple (image_size, image_size, Gdk.InterpType.BILINEAR);
if (cover_image.height > cover_image.width) {
int new_height = image_size * cover_image.height / cover_image.width;
int new_width = image_size;
int offset = (new_height - new_width) / 2;
cover_image = new Gdk.Pixbuf.subpixbuf(cover_image.scale_simple (new_width, new_height, Gdk.InterpType.BILINEAR), 0, offset, image_size, image_size);
} else if (cover_image.height < cover_image.width) {
int new_height = image_size;
int new_width = image_size * cover_image.width / cover_image.height;
int offset = (new_width - new_height) / 2;
cover_image = new Gdk.Pixbuf.subpixbuf(cover_image.scale_simple (new_width, new_height, Gdk.InterpType.BILINEAR), offset, 0, image_size, COVER_SIZE);
}
if(image != null) {
image.clear();
image.set_from_pixbuf (cover_image);
}
}
public async void set_image(Gtk.Image image, string path) throws Error {
GLib.File file = open_file(path);
GLib.InputStream stream = yield file.read_async ();
Gdk.Pixbuf cover_image = yield new Gdk.Pixbuf.from_stream_async (stream);
create_cover_image(image, cover_image, COVER_SIZE);
// image.set_from_pixbuf (cover_image);
}
protected override void activate () {
var window = new Gtk.ApplicationWindow (this);
window.set_default_size (400, 400);
window.title = "My Gtk.Application";
var button = new Gtk.Button.with_label("Click me");
button.clicked.connect(() => {
print("Clicked!");
});
var path = "https://upload.wikimedia.org/wikipedia/commons/8/8e/Velazquez-The_Surrender_of_Breda.jpg";
var image = new Gtk.Image();
image.pixel_size = COVER_SIZE;
set_image.begin (image, path, (obj, res) => {
window.show_all();
});
var image2 = new Gtk.Image();
set_image.begin (image2, path, (obj, res) => {
window.show_all();
});
var image3 = new Gtk.Image();
set_image.begin (image3, path, (obj, res) => {
window.show_all();
});
var image4 = new Gtk.Image();
set_image.begin (image4, path, (obj, res) => {
window.show_all();
});
var image5 = new Gtk.Image();
set_image.begin (image5, path, (obj, res) => {
window.show_all();
});
var container = new Gtk.Box(Gtk.Orientation.VERTICAL, 5);
container.add(button);
container.add(image);
container.add(image2);
container.add(image3);
container.add(image4);
container.add(image5);
// var label = new Gtk.Label("<b>Success With God & Psych</b>".replace("&", "&amp;"));
// label.wrap = true;
// label.use_markup = true;
window.add(container);
window.show_all ();
}
public static int main (string[] args) {
Application app = new Application ();
return app.run (args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment