Sha256: 72baa7a582c741ca3ae7af16d9c0bac158b1b88c12039537152f46d0a250b4ac

Contents?: true

Size: 1.79 KB

Versions: 16

Compression:

Stored size: 1.79 KB

Contents

# encoding: utf-8

module Fidgit
  # A button that toggles its value from false<->true when clicked.
  class ToggleButton < Button
    event :changed

    attr_reader :value
    def value=(value); @value = value; update_status; end

    # @param (see Button#initialize)
    #
    # @option (see Button#initialize)
    def initialize(text, options = {}, &block)
      options = {
        value: false
      }.merge! options

      @value = options[:value]

      super(text, options)

      @text_on = (options[:text_on] || text).dup
      @icon_on = options[:icon_on] || icon
      @tip_on = (options[:tip_on] || tip).dup
      @border_color_on = (options[:border_color_on] || options[:border_color] || default(:toggled, :border_color)).dup

      @text_off = (options[:text_off] || text).dup
      @icon_off = options[:icon_off] || icon
      @tip_off = (options[:tip_off] || tip).dup
      @border_color_off = (options[:border_color_off] || options[:border_color] || default(:border_color)).dup

      update_status

      subscribe :clicked_left_mouse_button do |sender, x, y|
        @value = (not @value)
        update_status
        publish :changed, @value
      end
    end

    protected
    # The block for a toggle-button is connected to :changed event.
    def post_init_block(&block)
      subscribe :changed, &block
    end

    protected
    def update_status
      if @value
        self.text = @text_on.dup
        @icon = @icon_on ? @icon_on.dup : nil
        @tip = @tip_on.dup
        @border_color = @border_color_on.dup
      else
        self.text = @text_off.dup
        @icon = @icon_off ? @icon_off.dup : nil
        @tip = @tip_off.dup
        @border_color = @border_color_off.dup
      end

      recalc

      nil
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
fidgit-0.2.4 lib/fidgit/elements/toggle_button.rb
fidgit-0.2.3 lib/fidgit/elements/toggle_button.rb
fidgit-0.2.2 lib/fidgit/elements/toggle_button.rb
fidgit-0.2.1 lib/fidgit/elements/toggle_button.rb
fidgit-0.2.0 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.10 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.9 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.8 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.7 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.6 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.5 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.4 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.3 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.2 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.1 lib/fidgit/elements/toggle_button.rb
fidgit-0.1.0 lib/fidgit/elements/toggle_button.rb