Njutik
Njutik

Reputation: 15

Ruby: how to renew the path of an image after image has been changed?

I am trying to build a GUI with ruby Shoes to select a background image for my desktop. As I am struggling with a particular part of my idea I am going to describe just the problem part, the rest works fine.

Here's the code of my ruby Shoes app:

Shoes.app :title => "Images!" do 
    stack do
        @img1 = image "desktop_pic", :width => 200, :height => 100
        button "Change image", :margin => 25 do
            @img1.path = "/home/njutik/preview_desktop_pic"
        end
    end
end

Here's what the result looks like when I start my Shoes app:

result_at_beginning

In the background there is a different ruby script running which generates a new background-image and stores it as preview_desktop_pic.

So when I click the Change image-button, the path of @img1 gets adjusted and I see the new image:

result_after_clicking_change_button

That's fine so far. My problem is, that nothing happens when I click the Change image-button again. Of course in the meantime there is already a new image preview_desktop_pic so the code line

@img1.path = "/home/njutik/preview_destkop_pic"

which is executed each time I press the Change image-button should show me a new picture but nothing happens. Even when I delete the preview_desktop_pic from the folder and then press the Change image-button there is still no change at all and all I see is the same picture shown after clicking the Change image-button for the first time.

So my question is: what am I doing wrong and how can I make the Shoes app show the current preview_desktop_pic every time I press the Change image-button?

Any hints would be really great!

Update: After the comment of 7stud I tried to define a singleton method for the button. Like this:

@change_image = button "Change image"

def @change_image.click 
  @img1.path = "/home/njutik/preview_login_background"
end

@change_image.click

But that did not help - nothing changed. Then I tried this:

def @img1.reload
  @img1.path = "/home/njutik/preview_login_background"
end

button "Change image" do 
  @img1.reload
end

But this also did not help. I thought that by defining singleton methods I would delete the cache memory.

Any further hints would be really helpful.

Upvotes: 1

Views: 227

Answers (2)

Ciarambola
Ciarambola

Reputation: 21

To change the image, the new image must have a different name than the previous one. I do not know why but it works for me then.

Upvotes: 0

7stud
7stud

Reputation: 48599

I don't think you are doing anything wrong. I think that what you are seeing has to do with image caching, which Shoes uses for efficiency. If ten windows all use the same image, Shoes does not fetch the file from disk (or download the file from the web) for each window. Instead, Shoes caches the image used in the first window, and when the other windows use the same path for the Image, Shoes retrieves the image from the cache. As a result, if the path for your Image doesn't change, it looks like Shoes uses the cached image. It would be nice if an Image in Shoes had a reload() method, which would force Shoes to ignore the cache and go get the image again.

In your app, the first button click works because the Image's path changes from "/desktop_pic" to "/home/njutik/preview_desktop_pic". Subsequent clicks do not change the Image's path.

Upvotes: 0

Related Questions