Skip to content
Snippets Groups Projects
animate.py 969 B
Newer Older
  • Learn to ignore specific revisions
  • Benoit Favre's avatar
    Benoit Favre committed
    from gi.repository import GObject
    
    timer = None
    def cancel():
        global timer
        if timer:
            GObject.source_remove(timer)
    
    Benoit Favre's avatar
    Benoit Favre committed
            timer = None
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    LINEAR=1
    DECELERATE=2
    
    
    def animate_value(callback, current, target, policy=DECELERATE, speed=32):
    
    Benoit Favre's avatar
    Benoit Favre committed
        global timer
    
    Benoit Favre's avatar
    Benoit Favre committed
        timer = None
    
    Benoit Favre's avatar
    Benoit Favre committed
        if current != target:
            if policy == DECELERATE:
                delta = abs(target - current) / 2
            else:
    
                delta = speed
    
    Benoit Favre's avatar
    Benoit Favre committed
            if current > target:
                current -= delta
            else:
                current += delta
            if abs(current - target) < 2:
                current = target
            callback(current)
            timer = GObject.timeout_add(1000 / 25, lambda: animate_value(callback, current, target))
    
    def scroll_to(scrollview, widget):
        result = widget.translate_coordinates(scrollview.get_child().get_child(), 0, 0)
        if result:
            adj = scrollview.get_vadjustment()
    
    Benoit Favre's avatar
    Benoit Favre committed
            animate_value(adj.set_value, adj.get_value(), result[1] - 150)