diamondburned/gotk4

Need access to gdk_pixbuf_get_type (ListStore for IconView / TreeView)

Closed this issue · 9 comments

sqp commented

I know it's possible to do this in the builder (I used to do it before gotk3 had the function) but that seem like driving a heavy truck to move a letter. And I'd prefer not to show that in the gallery example.
IconView is not really usable without that.
Here's the gotk3 code:

// PixbufGetType is a wrapper around gdk_pixbuf_get_type().
func PixbufGetType() glib.Type {
	return glib.Type(C.gdk_pixbuf_get_type())
}

This was in gdk/pixbuf.go, I'm not sure if it belongs in the gdkpixbuf package like : gdkpixbuf.GetType() or just gdkpixbuf.Type()

I'm not sure how viable generating a type getter for every single class is. The
generated documentation for packages are already really big, but this will make
it a lot bigger.


Does this code work?

var pixbufType = glib.TypeFromName("GdkPixbuf")

The code used to get the GdkPixbuf string is below:

package main

import (
	"log"

	"github.com/diamondburned/gotk4/pkg/core/glib"
	"github.com/diamondburned/gotk4/pkg/gdkpixbuf/v2"
)

func main() {
	pixbuf := gdkpixbuf.NewPixbuf(
		gdkpixbuf.ColorspaceRGB,
		false, 8, 1, 1,
	)

	log.Println(pixbuf.TypeFromInstance())
	log.Println(pixbuf.TypeFromInstance().Name())
	log.Println(glib.TypeFromName("GdkPixbuf"))

	// Output:
	// GdkPixbuf
	// GdkPixbuf
	// GdkPixbuf
}
sqp commented

AFAIK there was no other specific need like this, but the IconView is not usable without it.
Your answer seem perfect: efficient and readable.
It would be nice to add an information in the ListStore doc, as it's one of its hidden options.

For the gallery I wanted to get pixbuf from icon name, which seem pretty hard now that everything is wrapped in Paintable or other, so for now it will be from download.

Side question now that my gallery is almost ready, is there a way to make a screenshot of the window (or part of) :

mainW := app.Win.Child()
snap := gtk.NewSnapshot()
mainW.SnapshotChild(mainW.FirstChild(), snap)
renderNode := snap.ToNode()
renderNode.WriteToFile("screenshot.png")

Panics in the ToNode() method.

panic: interface conversion: *glib.Object is not gsk.RenderNoder: missing method Bounds
pkg/gtk/v4/gtksnapshot.go:895

Example found on the gnome forums

Is there a log print that says "missing marshaler for type"?

This will only work on commit 5622f54 and later.

I should probably change the code generator to generate an assertion that vomits
out a more helpful message when the type mismatches.

Edit: commit 2625db3 implements this.

sqp commented

your new error message:

gotk4: marshaler error for : invalid type

Need to make more changes in pkg/core/glib/glib.go to print the real type:

-  log.Printf("gotk4: missing marshaler for type %s (i.e. %s)", v.Type(), fundamental)
+  log.Printf("gotk4: missing marshaler for type %s (%T): %s", v.Type(), v, fundamental)

-  log.Printf("gotk4: marshaler error for %s: %v", v.Type(), err)
+  log.Printf("gotk4: marshaler error for type %s (%T): %v", v.Type(), v, err)

Which now crash my screenshot attempt like this:


Gtk-WARNING: Trying to snapshot GtkViewport 0x2dfc7e0 without a current allocation
GLib-GObject-WARNING: invalid (NULL) pointer instance
GLib-GObject-CRITICAL: g_value_init_from_instance: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

With my new error message and its magical %T
gotk4: missing marshaler for type (*glib.Value): invalid type

That's unusual. Can you make a working piece of code that reproduces the
problem?

Wait... I'm wondering if this is because the returned renderNode is nil...

I think the most appropriate fix in this case would be to fix the bug that's
causing ToNode to return nil.

For now, commit 1988d98 now panics with a message indicating that an object that
wasn't supposed to be nil is now nil, which should crash the application with a
more helpful and less confusing error.

I'm not sure how viable generating a type getter for every single class is. The
generated documentation for packages are already really big, but this will make
it a lot bigger.

Latest commit now generates a gdkpixbuf.GTypePixbuf value. TypeFromName is no longer needed.