Sha256: a222f88ec78b1a572f50e518d99e56ddb6595986ef9f6410e0eaeb92361409b0

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

module UI
  class Button < Control
    include Eventable

    def initialize
      super
      calculate_measure(true)
    end

    def measure(width, height)
      self.proxy.sizeToFit

      [
        width.nan? ? self.proxy.frame.size.width : width,
        height.nan? ? self.proxy.frame.size.height : height
      ]
    end

    def color=(color)
      case color
      when Hash
        color.map do |state, color|
          proxy.setTitleColor(UI::Color(color).proxy, forState: CONTROL_STATES[state])
        end
      else
        proxy.setTitleColor(UI::Color(color).proxy, forState: CONTROL_STATES[:normal])
      end
    end

    def color(state = :normal)
      UI::Color(proxy.titleColorForState(CONTROL_STATES[state]))
    end

    def title=(title)
      case title
      when Hash
        title.map do |state, title|
          proxy.setTitle(title, forState: CONTROL_STATES[state])
        end
      when String
        proxy.setTitle(title, forState: CONTROL_STATES[:normal])
      end
    end

    def title(state = :normal)
      proxy.titleForState(CONTROL_STATES[state])
    end

    def image=(image)
      proxy.setImage(UIImage.imageNamed(image), forState: CONTROL_STATES[:normal])
    end

    def font
      UI::Font._wrap(proxy.titleLabel.font)
    end

    def font=(font)
      proxy.titleLabel.font = UI::Font(font).proxy
    end

    def on_tap
      trigger(:tap)
    end

    def proxy
      @proxy ||= begin
        ui_button = UIButton.buttonWithType(UIButtonTypeCustom)
        ui_button.translatesAutoresizingMaskIntoConstraints = false
        ui_button.addTarget(self, action: :on_tap, forControlEvents: UIControlEventTouchUpInside)
        ui_button
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
motion-flow-0.1.8 flow/ui/cocoa/button.rb
motion-flow-0.1.7 flow/ui/cocoa/button.rb
motion-flow-0.1.6 flow/ui/cocoa/button.rb
motion-flow-0.1.5 flow/ui/cocoa/button.rb