14 - creating a mac os x application with rubycocoa.rb

来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 48 行

RB
48
字号
require 'osx/cocoa'include OSXZeroDate = NSDate.dateWithString('2000-01-01 00:00:00 +0000')class Timer < NSObject    ib_outlets :clock, :timeField, :button        def initialize        @timer = \	NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats\	(1.0, self, :tick, nil, true)    end    #---    def start(sender)        @running = true       @start = NSDate.date        @accumulated = 0 unless @accumulated        @elapsed = 0.0        @button.setTitle('Stop')        @button.setAction(:stop)    end    #---    def stop(sender)        @running = false        @accumulated += @elapsed        @button.setTitle('Start')        @button.setAction(:start)    end        def reset(sender)        stop(nil)        @accumulated, @elapsed = 0.0, 0.0        @clock.setDateValue(ZeroDate)        @timeField.setObjectValue(ZeroDate)    end        def tick()     if @running            @elapsed = NSDate.date.timeIntervalSinceDate(@start)            d = ZeroDate.addTimeInterval(@elapsed + @accumulated)            @clock.setDateValue(d)             @timeField.setObjectValue(d)        end    endend#---

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?