/GTK3 - ProgressBar


gem install gtk3

# frozen_string_literal: true

require 'gtk3'

class ProgressBarWindow < Gtk::Window
  attr_accessor :progressbar, :activity_mode
  def initialize
    super
    self.title = 'ProgressBar Demo'
    self.border_width = 10

    vbox = Gtk::Box.new(:vertical, 6)
    add vbox

    @progressbar = Gtk::ProgressBar.new
    vbox.pack_start progressbar

    button = Gtk::CheckButton.new('Show text')
    button.signal_connect('toggled') { |b| on_show_text_toggled b }
    vbox.pack_start button

    button = Gtk::CheckButton.new('Activity mode')
    button.signal_connect('toggled') { |b| on_activity_mode_toggled b }
    vbox.pack_start button

    button = Gtk::CheckButton.new('Right to Left')
    button.signal_connect('toggled') { |b| on_right_to_left_toggled b }
    vbox.pack_start button

    timeout_id = GLib::Timeout.add(50) { on_timeout }
  end

  def on_show_text_toggled(button)
    show_text = button.active?
    text = show_text ? 'some text' : ''
    progressbar.text = text
    progressbar.show_text = show_text
  end

  def on_activity_mode_toggled(button)
    @activity_mode = button.active?
    if activity_mode
      progressbar.pulse
    else
      progressbar.set_fraction 0
    end
  end

  def on_right_to_left_toggled(button)
    value = button.active?
    progressbar.set_inverted value
  end

  def on_timeout
    if activity_mode
      progressbar.pulse
    else
      new_value = progressbar.fraction + 0.01
      new_value = 0 if new_value > 1
      progressbar.fraction = new_value
    end
  end
end

win = ProgressBarWindow.new
win.signal_connect('destroy') { Gtk.main_quit }
win.show_all
Gtk.main

Python version