shoes / shoes3

a tiny graphical app kit for ruby
http://walkabout.mvmanila.com
Other
181 stars 19 forks source link

Image is disappearing upon setting its path back to the original path #452

Closed coyote963 closed 5 years ago

coyote963 commented 5 years ago

I am trying to make a simple app that displays an image (say on.png) and upon clicking it replaces it with a different image (off.png) and clicking toggles this image.

The problem I am running into is that the image turns off correctly. When I click an off image, the image disappears entirely even though I have used the same path for the original image and the set image.

Shoes.app do
    @highlighted = false
    @card = image "/path/to/image/on.png"
    @card.click do
        if !@highlighted
            @card.path =  "/path/to/image/off.png"
        else
            @card.path = "/path/to/image/on.png"
        end
        @highlighted = !@highlighted
    end
end

It is more mysterious that when I set the on state to something else entirely: suppose foo.png the toggling behavior works as intended

Shoes.app do
    @highlighted = false
    @card = image "/path/to/image/on.png"
    @card.click do
        if !@highlighted
            @card.path =  "/path/to/image/off.png"
        else
            @card.path = "/path/to/image/foo.png"
        end
        @highlighted = !@highlighted
    end
end

Can someone please explain what is the difference and how I can fix this bug? I really appreciate it.

ccoupe commented 5 years ago

The obvious answer, probably wrong is if foo.png works and on.png doesn't then perhaps 'on.png' is not a good png or the path is wrong. There might be a console error message - press Alt-/ (or apple-/) which could help.

coyote963 commented 5 years ago

I have tried looking at the console, and there is no error message logged

on.png is displayed when the app is first run, and off.png is displayed when on.png is clicked the first time. The trouble is as soon as off.png is clicked again, the image disappears. So I believe the file paths should be correct

For my purposes, I had to do a workaround by making a duplicate of on.png - on (Copy).png and use that in place of the toggle image back to on.png.

I believe the error lies in the image "remembering" its initialized image path and it being cached somehow, but looking at the source code, I can't seem to understand it well, as well as the fact that there is c code (which I am unfamiliar with).

ccoupe commented 5 years ago

You may be running into the cache "feature". Images are cached so they aren't downloaded/reloaded. You can turn off caching with app.cache = false at the top of your Shoes.app block.

coyote963 commented 5 years ago

Wow that fixed the issue! Thanks a lot!