Question

I'm creating an application for fun to learn ruby and shoes, but it seems that I cannot accomplish what I'm trying to do...

I have a stack with some text inside and, under certain circumstances, I want to change the background of that stack, but for some reason all the text inside the stack is deleted. How can I avoid that? I just want to change the background color.

Sample code to show the problem:

Shoes.app do

    @first_stack = stack do
        background orange
        @title = title "my title"
        @subtitle = subtitle "my subtitle"
    end 

    button ("change background") {
        @first_stack.background gold
    }

end
Was it helpful?

Solution

Seems background creates a fill which means your text is still there just nested under the fill. Here is a work around

Shoes.app do 
  def change_color(back)
    @first_stack.clear
    @first_stack.background back
    @first_stack.title @title
    @first_stack.subtitle @subtitle
  end
  @first_stack = stack do
    background orange
    @title = title "my title"
    @subtitle = subtitle "my subtitle"
  end 

  button ("change background") do 
    change_color(gold)
  end
end

This just clears the first stack and recreates it with a new color. Still looking into a more eloquent method.

EDIT

Found a solution:

Shoes.app do
  @first_stack = stack do
      @fs_background = background orange
      @title = title "my title"
      @subtitle = subtitle "my subtitle"
  end 
  button ("change background") do
     @fs_background.remove
     @first_stack.before(@title) {@fs_background = background gold}
  end
end

This will work the way you want as it places a background layer on top of the original background layer but before @title.

OTHER TIPS

Assuming you make the background the first element:

Shoes.app do
  @f = stack do
    background blue
    title "my title"
    subtitle "my subtitle"
  end 
  button ("change background") do
     @f.children.first.remove
     @f.before(@f.children.first) {
       background gold
     }
  end
end

in Shoes3.3.1 you can do it that way

Shoes.app do
    @first_stack = stack do
        background orange
        @title = title "my title"
        @subtitle = subtitle "my subtitle"
    end 

    button ("change background") {
        @first_stack.contents[0].fill = gold
        @first_stack.refresh_slot
    }
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top