tags:

views:

110

answers:

1

In Shoes, I'd like to create a button that is initially hidden. I've tried passing :hidden => true as part of the button style, as well as calling @button.hide() after creating it, but it remains obstinately visible.

I've been able to work around this by not creating the button until I want it shown, but that requires checking to see if it already exists, rather than just using it.

+1  A: 

Not at present. Buttons are still fairly unreliable in Shoes, especially on Windows. You can work around the issue by putting the button in a slot and hiding or showing the slot, but you may discover that the button won't hide again once it has been clicked once:

Shoes.app do
  para 'This is some text.'

  @flow = flow :width => 50, :hidden => true do
    button 'a' do |btn|
      alert 'Hello, World!'
    end
  end

  button 'toggle' do
    @flow.toggle
  end
  para 'Blah blah blah'

end

Luckily, there is a way out: slots. Slots can be given a click event, which makes them behave much as a button does. In fact, you can make fairly decent buttons out of slots.

Here's something I cobbled together. It lets you use the pesto_button method to generate buttons built on flows. You can modify it to fit your needs, including such things as using an image as the background, modifiable text (with auto-expanding width?), ability to change styles on the fly, etc:

class PestoButton < Widget
  def initialize (text, opts = {})
    @border_color = opts[:border_color] || gray
    @border_width = opts[:border_width] || 3
    @color = opts[:up_color] || gainsboro
    @click_color = opts[:down_color] || @border_color
    @width = opts[:width] || 80
    @click = block_given? ? Proc.new { yield } : nil
    @text = text
    @visible = true
    @flow = flow :width => @width do
      background @color
      border @border_color, :strokewidth => @border_width
      para @text, :align => 'center'
    end

    @flow.click do
      @flow.clear
      @flow.append do
        background @click_color
        border @border_color, :strokewidth => @border_width
        para @text, :align => 'center'
      end
    end

    @flow.release do
      @flow.clear
      @flow.append do
        background @color
        border @border_color, :strokewidth => @border_width
        para @text, :align => 'center'
        @click.call if @click
      end
    end
  end

  def click
    @click = block_given? ? Proc.new { yield } : nil
  end

  def show
    @flow.show
  end

  def toggle
    @flow.toggle
  end

  def hide
    @flow.hide
  end
end

Shoes.app do
  para 'This is some text.'
  @btn = pesto_button 'Click me!' do
    alert 'Hello, World!'
  end

  button 'toggle' do
    @btn.toggle
  end

  button 'new block' do
    @btn.click do
      alert 'Goodbye, World!'
    end
  end

  button 'no block' do
    @btn.click  #Clears the click method
  end

  para 'Blah blah blah'
end
Pesto