<commit_before># encoding: utf-8

require_relative 'element'

module Fidgit
  class Label < Element
    DEFAULT_BACKGROUND_COLOR = Gosu::Color.rgba(0, 0, 0, 0)
    DEFAULT_BORDER_COLOR = Gosu::Color.rgba(0, 0, 0, 0)
    DEFAULT_COLOR = Gosu::Color.rgb(255, 255, 255)

    attr_reader :color, :background_color, :border_color, :text, :icon

    def text=(value)
      @text = value
      recalc
      nil
    end

    # @param (see Element#initialize)
    #
    # @option (see Element#initialize)
    # @option options [Gui::Icon, Gosu::Image, nil] :icon (nil)
    # @option options [String] :text ('')
    def initialize(parent, options = {}, &block)
      options = {
        text: '',
        color: DEFAULT_COLOR,
        background_color: DEFAULT_BACKGROUND_COLOR,
        border_color: DEFAULT_BORDER_COLOR
      }.merge! options

      @text = options[:text].dup
      @icon = options[:icon]
      @color = options[:color].dup

      super(parent, options)
    end

    def draw_foreground
      current_x = x + padding_x
      if @icon
        @icon.draw(current_x, y + padding_y, z)
        current_x += @icon.width + padding_x
      end

      unless @text.empty?
        font.draw(@text, current_x, y + padding_y, z, 1, 1, @color)
      end

      nil
    end

    protected
    def layout
      if @icon
        if @text.empty?
          rect.width = [@icon.width + padding_x * 2, width].max
          rect.height = [@icon.height + padding_y * 2, height].max
        else
          rect.width = [@icon.width + font.text_width(@text) + padding_x * 3, width].max
          rect.height = [[@icon.height, font_size].max + padding_y * 2, height].max
        end
      else
        rect.width = [font.text_width(@text) + padding_x * 2, width].max
        rect.height = [font_size + padding_y * 2, height].max
      end

      nil
    end
  end
end<commit_msg>Allow label icon to be set by the user.<commit_after># encoding: utf-8

require_relative 'element'

module Fidgit
  class Label < Element
    DEFAULT_BACKGROUND_COLOR = Gosu::Color.rgba(0, 0, 0, 0)
    DEFAULT_BORDER_COLOR = Gosu::Color.rgba(0, 0, 0, 0)
    DEFAULT_COLOR = Gosu::Color.rgb(255, 255, 255)

    attr_reader :color, :background_color, :border_color, :text, :icon

    def text=(value)
      @text = value
      recalc
      nil
    end

    def icon=(value)
      @icon = value
      recalc
      nil
    end

    # @param (see Element#initialize)
    #
    # @option (see Element#initialize)
    # @option options [Gui::Icon, Gosu::Image, nil] :icon (nil)
    # @option options [String] :text ('')
    def initialize(parent, options = {}, &block)
      options = {
        text: '',
        color: DEFAULT_COLOR,
        background_color: DEFAULT_BACKGROUND_COLOR,
        border_color: DEFAULT_BORDER_COLOR
      }.merge! options

      @text = options[:text].dup
      @icon = options[:icon]
      @color = options[:color].dup

      super(parent, options)
    end

    def draw_foreground
      current_x = x + padding_x
      if @icon
        @icon.draw(current_x, y + padding_y, z)
        current_x += @icon.width + padding_x
      end

      unless @text.empty?
        font.draw(@text, current_x, y + padding_y, z, 1, 1, @color)
      end

      nil
    end

    protected
    def layout
      if @icon
        if @text.empty?
          rect.width = [@icon.width + padding_x * 2, width].max
          rect.height = [@icon.height + padding_y * 2, height].max
        else
          rect.width = [@icon.width + font.text_width(@text) + padding_x * 3, width].max
          rect.height = [[@icon.height, font_size].max + padding_y * 2, height].max
        end
      else
        rect.width = [font.text_width(@text) + padding_x * 2, width].max
        rect.height = [font_size + padding_y * 2, height].max
      end

      nil
    end
  end
end
{'fraction_non_alphanumeric': 0.12019858897308597, 'fraction_numerical': 0.01358766657956624, 'mean_word_length': 2.2634271099744248, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6675830', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1229, 'n_words': 318}