0

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
DocKuro
  • 445
  • 4
  • 13

3 Answers3

3

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.

engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • Ok, so the background is not a 'back' but just a layer and I should place it before the first element of my stack to make it in the right z-index. – DocKuro Mar 08 '14 at 12:11
  • by the way your code works even removing the 'background:gold' clause from the @first_stack declaration. I'm looking into removing the old background too, as I understand it will still be there if I do it this way. Thank you for the suggestion! – DocKuro Mar 08 '14 at 12:13
  • You could try @first_stack.background.remove. The Z-index is set based on call ordering so no matter where you place the background you will have to call before or it will fill over top of other elements. – engineersmnky Mar 08 '14 at 13:22
  • Already tried the remove, but it doesn't work... About the background, I think the name is deceiving, they should have called that just something like 'fill_layer' because it's not on the back (even if it's written in a note on the docs) – DocKuro Mar 08 '14 at 15:25
  • 1
    remove works if you make the background an `instance_variable` see edited version. If you comment out the before portion you can see the layer is gone. – engineersmnky Mar 10 '14 at 13:23
0

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
dgo.a
  • 2,634
  • 23
  • 35
0

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
guest
  • 81
  • 1
  • 2