Search code examples
rubyshoes

Changing background of a Stack in Shoes


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

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.