Beispiel #1
0
def _create_data_layout(cmd: lib.Command,
                        widget: Gtk.Widget) -> tuple[Pango.Layout, int]:
    icon_width = 1
    layout = None
    if cmd.data and cmd.definition.show_data:
        layout = widget.create_pango_layout(cmd.data)
        layout.set_font_description(icon.FONT_SMALL)
        data_layout_width = layout.get_pixel_size()[0]
        icon_width = max(icon_width, math.ceil(data_layout_width / icon.WIDTH))
    return (layout, icon_width)
Beispiel #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