Gtk4 demos using shard hugopl/gtk4.cr

I am working on some Gtk4 demos using the shard hugopl/gtk4.cr.
Most things work but there are some small things not working like update the CSS for a label
in lesson09, see gummybears/gtk4_with_crystal.git

In lesson09 a menu is constructed with some stateful menu items and their callbacks. The menu has
a submenu Color where the user can change the background color of a label. This part is not working, see

def color_activated(action : Gio::SimpleAction, parameter : GLib::Variant)

    new_color = parameter.to_s
    #
    # remove single quotes
    #
    new_color = new_color.gsub(/\'/,"")
    new_css   = sprintf("label.lb { background-color: %s; }",new_color)

    # need to update the css for the label
    # but unsure how to do that
    #
    # the C code uses  gtk_css_provider_load_from_data (provider, new_css, -1)
    # and looking at class Gtk::Provider there is a method load_from_data
    # which accepts an Enumerable(UInt8) as data
    #
    # not sure how to do that
    #
    # Next line does not compile
    # 
    @provider.load_from_data(new_css)

    #
    # set menu item state
    #
    action.state = parameter
  end
4 Likes

Excellent idea. Gtk 4 for Crystal looks good, but I have no idea on how to use it, except for trivial examples.

It should be

@provider.load_from_data(new_css.to_slice)

docs: String#to_slice and Bytes

On GTK 4.12, that API has been deprecated and replaced by:
Gtk::CssProvider#load_from_bytes
and
Gtk::CssProvider#load_from_string

Changed the code and it works.

Thanks

Forgot to give the github link

That’s the main problem with GTK, lack of guides, tutorials, etc… they are trying but the lack of man power doesn’t help.

A lot of the time I need to dig the C sources of GTK to find how things works. Another thing that helps is to follow https://blog.gtk.org/, blog posts there are usually way better than any documentation.

About the bindings… it still have some gaps, it still easy to find bugs when trying to do more complex stuff, but the basic works nicely.

I’m doing it on my free time, so it gets some months without any updates then some weeks with a lot of commits and releases, I would love to be paid to work full time on it but this is unrealistic :cry:

Anyway, any crash, etc… file a bug report and at some point will fix it :slight_smile:

7 Likes