class Chamomile::Progress
Animated progress bar with spring-based animation and optional gradient.
Constants
- DAMPING
- DEFAULT_WIDTH
- EMPTY_CHAR
- EPSILON
- FPS
- FREQUENCY
-
Spring constants
- FULL_CHAR
Attributes
Public Class Methods
Source
# File lib/chamomile/components/progress.rb, line 38 def initialize(width: DEFAULT_WIDTH, show_percentage: true, percentage_format: " %3.0f%%", full_char: FULL_CHAR, empty_char: EMPTY_CHAR, full_color: nil, empty_color: nil, gradient: nil, frequency: FREQUENCY, damping: DAMPING) @id = self.class.next_id @width = width @full_char = full_char @empty_char = empty_char @show_percentage = show_percentage @percentage_format = percentage_format @full_color = full_color @empty_color = empty_color @gradient = gradient @frequency = frequency.to_f @damping = damping.to_f @percent = 0.0 @target = 0.0 @velocity = 0.0 @tag = 0 @animating = false end
Source
# File lib/chamomile/components/progress.rb, line 22 def self.next_id @id_mutex.synchronize do if Process.pid != @id_pid @id_pid = Process.pid @next_id = 0 @id_mutex = Mutex.new end @next_id += 1 "#{@id_pid}-pg-#{@next_id}" end end
Public Instance Methods
Source
# File lib/chamomile/components/progress.rb, line 78 def animating? @animating end
Source
# File lib/chamomile/components/progress.rb, line 69 def decr_percent(v) set_percent(@target - v) end
Source
# File lib/chamomile/components/progress.rb, line 82 def handle(msg) return unless msg.is_a?(ProgressFrameMsg) return unless msg.id == @id && msg.tag == @tag dt = 1.0 / FPS force = (@target - @percent) * @frequency @velocity = (@velocity + (force * dt)) * (1.0 / (1.0 + (@damping * dt))) @percent += @velocity * dt @percent = @percent.clamp(0.0, 1.0) if (@percent - @target).abs < EPSILON && @velocity.abs < EPSILON @percent = @target @velocity = 0.0 @animating = false @tag += 1 nil else @tag += 1 frame_cmd end end
Also aliased as: update
Source
# File lib/chamomile/components/progress.rb, line 65 def incr_percent(v) set_percent(@target + v) end
Source
# File lib/chamomile/components/progress.rb, line 60 def set_percent(p) @target = p.to_f.clamp(0.0, 1.0) start_animation end
Source
# File lib/chamomile/components/progress.rb, line 73 def set_spring_options(frequency, damping) @frequency = frequency.to_f @damping = damping.to_f end
Source
# File lib/chamomile/components/progress.rb, line 106 def view render_bar(@percent) end
Source
# File lib/chamomile/components/progress.rb, line 110 def view_as(percent) render_bar(percent.to_f.clamp(0.0, 1.0)) end