import PySimpleGUI as sg

"""
    Demo_Animated_GIFs.py
    
    Shows how to:
    * Use PopupAnimated
    * Use animated GIF in a custom window layout
    * Store your GIFs in base64 format inside this source file (copy and paste into your source file)
    
    The first image that uses PopupAnimated will stop after a few seconds on its own.
    The remaining images are shown 1 at a time. To move on to the next image, click the current image.
    If you want to exit before reaching the final image, right click the image and choose 'exit'
"""

# ---------------------------- Base 64 GIFs ----------------------------

line_boxes = b'[encoded data, 15548 characters]'


line_bubbles = b'[encoded data, 10796 characters]'

ring_black_dots = b'[encoded data, 8044 characters]'

ring_gray_segments = b'[encoded data, 10440 characters]'


ring_lines = b'[encoded data, 14972 characters]'


red_dots_ring = b'[encoded data, 23600 characters]'

blue_dots = b'[encoded data, 37536 characters]'

ring_blue = b'[encoded data, 64308 characters]'



bar_striped = b'[encoded data, 4720 characters]'

# list of all of the base64 GIFs
gifs = [ring_blue, red_dots_ring, ring_black_dots, ring_gray_segments, ring_lines, blue_dots, red_dots_ring, bar_striped, line_boxes, line_bubbles]

# first show how to use PopupAnimated using built-in GIF image
for i in range(100000):
    sg.PopupAnimated(sg.DEFAULT_BASE64_LOADING_GIF, background_color='white', time_between_frames=100)
sg.PopupAnimated(None)      # close all Animated Popups

# Next demo is to show how to create custom windows with animations
layout = [[sg.Image(data=gifs[0], enable_events=True, background_color='white', key='_IMAGE_', right_click_menu=['UNUSED', 'Exit'])],]

window = sg.Window('My new window', no_titlebar=True, grab_anywhere=True, keep_on_top=True, background_color='white', alpha_channel=.8, margins=(0,0)).Layout(layout)

offset = 0
gif = gifs[0]
while True:                                     # Event Loop
    event, values = window.Read(timeout=10)     # loop every 10 ms to show that the 100 ms value below is used for animation
    if event in (None, 'Exit', 'Cancel'):
        break

    elif event == '_IMAGE_':                    # if clicked on the image
        offset += (offset < len(gifs)-1)        # add 1 until the last one
        gif = gifs[offset]                      # get a new gif image
    # update the animation in the window
    window.Element('_IMAGE_').UpdateAnimation(gif,  time_between_frames=100)
