Ejemplo n.º 1
0
def get_surface_for_pixbuf(widget: Gtk.Widget, pixbuf: Optional[GdkPixbuf.Pixbuf])\
        -> Optional[cairo.Surface]:
    """:returns: a cairo surface, if possible"""
    if not pixbuf:
        return None
    scale_factor = widget.get_scale_factor()
    return Gdk.cairo_surface_create_from_pixbuf(pixbuf, scale_factor,
                                                widget.get_window())
Ejemplo n.º 2
0
def append_to_snapshot(cmd: lib.Command,
                       snapshot: Gtk.Snapshot, x, y,
                       widget: Gtk.Widget, colors: lib.icon.CommandColorScheme,
                       code: lib.CodeBuffer = None) -> int:
    defin: lib.CommandDefinition = cmd.definition
    bg, fg = colors[defin.color]
    # Extend the width of the icon for icons with data
    data_layout, icon_width = _create_data_layout(cmd, widget)

    area = Graphene.Rect.init(
        Graphene.Rect(), x, y, icon_width * icon.WIDTH, icon.HEIGHT)

    # Background
    snapshot.append_color(bg, area)
    # Foreground icon
    if not (defin.data_only and cmd.data):
        if icon_width > 1:
            icon_area = Graphene.Rect.init(
                Graphene.Rect(), x + (icon_width - 1) * icon.WIDTH / 2, y,
                icon.WIDTH, icon.HEIGHT)
        else:
            icon_area = area
        defin.icon.snapshot(snapshot, icon_area, fg, widget.get_scale_factor())
    # Data
    if cmd.data and defin.show_data:
        if defin.id == 'img' and code is not None:
            texture = code.get_command_data_preview(cmd)
            if texture is not None:
                t_scale = max(
                    1,
                    texture.get_width() / area.get_width(),
                    texture.get_height() / area.get_height())
                ta_width = texture.get_width() / t_scale
                ta_height = texture.get_height() / t_scale
                t_area = Graphene.Rect.init(
                    Graphene.Rect(),
                    area.get_x() + area.get_width() / 2 - ta_width / 2,
                    area.get_y() + area.get_height() / 2 - ta_height / 2,
                    ta_width, ta_height)
                snapshot.append_texture(texture, t_area)
        if defin.id == 'color':
            data_color = utils.rgba(f'rgb({cmd.data})')
            layout = widget.create_pango_layout('⬤')
            _append_layout(
                snapshot, layout, icon.FONT_NORMAL, data_color, area)
        if data_layout:
            _append_layout(
                snapshot, data_layout, None, fg, area,
                0 if defin.data_only else 5)
    return icon_width