class Timer

Constants

HOUR_MAX
MINUTE_MAX
SECOND_MAX

Attributes

hour[RW]
min[RW]
played[RW]
sec[RW]
started[RW]

Public Class Methods

new() click to toggle source
# File examples/timer.rb, line 12
def initialize
  @pid = nil
  @alarm_file = File.expand_path('../sounds/AlanWalker-Faded.mid', __dir__)
  @hour = @min = @sec = 0
  at_exit { stop_alarm }
  setup_timer
  create_gui
end

Public Instance Methods

create_gui() click to toggle source
# File examples/timer.rb, line 78
def create_gui
  window('Timer') {
    margined true
    
    group('Countdown') {
      vertical_box {
        horizontal_box {
          spinbox(0, HOUR_MAX) {
            stretchy false
            value <=> [self, :hour]
          }
          label(':') {
            stretchy false
          }
          spinbox(0, MINUTE_MAX) {
            stretchy false
            value <=> [self, :min]
          }
          label(':') {
            stretchy false
          }
          spinbox(0, SECOND_MAX) {
            stretchy false
            value <=> [self, :sec]
          }
        }
        horizontal_box {
          button('Start') {
            enabled <= [self, :started, on_read: :!]
            
            on_clicked do
              self.started = true
              self.played = false
            end
          }
          
          button('Stop') {
            enabled <= [self, :started]
            
            on_clicked do
              self.started = false
            end
          }
        }
      }
    }
  }.show
end
play_alarm() click to toggle source
# File examples/timer.rb, line 28
def play_alarm
  stop_alarm
  if @pid.nil?
    begin
      @pid = spawn "timidity -G 0.0-10.0 #{@alarm_file}"
      @th = Process.detach @pid
    rescue Errno::ENOENT
      warn 'Timidty++ not found. Please install Timidity++.'
      warn 'https://sourceforge.net/projects/timidity/'
    end
  end
end
setup_timer() click to toggle source
# File examples/timer.rb, line 41
def setup_timer
  unless @setup_timer
    Glimmer::LibUI.timer(1) do
      if @started
        seconds = @sec
        minutes = @min
        hours = @hour
        if seconds > 0
          self.sec = seconds -= 1
        end
        if seconds == 0
          if minutes > 0
            self.min = minutes -= 1
            self.sec = seconds = SECOND_MAX
          end
          if minutes == 0
            if hours > 0
              self.hour = hours -= 1
              self.min = minutes = MINUTE_MAX
              self.sec = seconds = SECOND_MAX
            end
            if hours == 0 && minutes == 0 && seconds == 0
              self.started = false
              unless @played
                play_alarm
                msg_box('Alarm', 'Countdown Is Finished!')
                self.played = true
              end
            end
          end
        end
      end
    end
    @setup_timer = true
  end
end
stop_alarm() click to toggle source
# File examples/timer.rb, line 21
def stop_alarm
  if @pid
    Process.kill(:SIGKILL, @pid) if @th.alive?
    @pid = nil
  end
end