can-lehmann / owlkettle

A declarative user interface framework based on GTK 4
https://can-lehmann.github.io/owlkettle/README
MIT License
375 stars 13 forks source link

Image widget missing? #33

Closed zedeus closed 1 year ago

zedeus commented 1 year ago

There doesn't seem to be a widget for displaying images. Useful if not necessary for things like image viewers and music players that show covers. GTK has a few different ways of doing it, it would probably be a good idea to support a couple of variants with different tradeoffs. A way to support async image loading would be very nice as well, maybe using GdkPixbuf: https://docs.gtk.org/gdk-pixbuf/type_func.Pixbuf.new_from_stream_async.html

can-lehmann commented 1 year ago

You are correct. There is currently no widget for displaying images that are not icons. I will look into wrapping GdkPixbuf and creating a GtkImage based Image widget. In the meantime, you should be able to display images inside a DrawingArea using cairo.

You can load an image using loadImageSurface and then display it using CairoContext.draw to display the ImageSurface. I think that cairo only supports pngs though.

zedeus commented 1 year ago

That sounds really nice, thank you. My two requests for such a widget would be the ability to supply a size, using the scaled variants of GTK's loading functions, and the ability to swap the image with a different one.

can-lehmann commented 1 year ago

I added a Picture widget based on GtkPicture in https://github.com/can-lehmann/owlkettle/commit/a60893d267e5ac7923eee1a6a15a2f2f4a24ad83 (GtkImage seems to be only for use with icons). Here is an example of a simple image viewer that uses it: https://github.com/can-lehmann/owlkettle/blob/main/examples/widgets/picture.nim You can use this overload of the loadPixbuf proc to load a scaled image.

zedeus commented 1 year ago

Sweet, that was fast. How about loading several images at once asynchronously? Many image viewers support a gallery view where you can see many at once. This would require dynamically loading and unloading a lot of images on the fly, to avoid slowing down everything to a crawl and using gigabytes of memory to keep e.g. 10k pixbufs in memory.

can-lehmann commented 1 year ago

I added loadPixbufAsync in https://github.com/can-lehmann/owlkettle/commit/86bd2f813e5875c0686417730acbdd79f69eaebf. Note that even though it might look like it integrates with Nim's asyncdispatch module, I would not expect it to work well. Proper async support is a goal for owlkettle 3.0. For the time being, you can use addCallback like this: .https://github.com/can-lehmann/owlkettle/blob/main/examples/widgets/picture.nim#L74

zedeus commented 1 year ago

You're a beast, great work!