How to insert Paintable from icon name to TextBuffer

I want to insert some icon from asset into the text buffer, by using insert_paintable.

It requires paintable implementation, but this code fails as paintable return NULL (None)

buffer.insert_paintable(
    &mut buffer.end_iter(),
    &Image::from_icon_name("user-available-symbolic")
        .paintable()
        .unwrap(), // fails
);

Can I use icon name with buffer, parse it or convert into the bytes array, or something else?

The paintable accessor is only going to return you a Paintable implementation if you set one; it will not turn any random data into a Paintable.

If you want to load an icon and get a Paintable back, use Gtk.IconTheme.lookup_icon().

1 Like

Thank you, this implementation works, but icon for some reasons not resolved:

buffer.insert_paintable(
    &mut buffer.end_iter(),
    &IconTheme::lookup_icon(
        &IconTheme::default(),
        "user-available-symbolic",
        &[],
        16,
        16,
        TextDirection::Ltr,
        IconLookupFlags::FORCE_SYMBOLIC,
    ),
);

it looks like this:
image

The “user-available-symbolic” icon available in my system, suppose I forgot about something in lookup request…

Well, found the answer: I should define current Display just:

let display = Display::default();
let icon_theme = IconTheme::for_display(&display.unwrap()); // handle

buffer.insert_paintable(
    &mut buffer.end_iter(),
    &icon_theme.lookup_icon(
        "user-available-symbolic",
        &[],
        16,
        16,
        TextDirection::None,
        IconLookupFlags::FORCE_SYMBOLIC,
    ),
);

You cannot call GTK API without a display connection, which comes from calling gtk_init(), either directly from an explicit call, or indirectly through GtkApplication; if you’re not doing that, then you’re doing something appallingly wrong to begin with.

1 Like

Yes, sure - just forgot about that!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.