Sha256: 2065386fd0870a45ec3e00c7a28d30bd55674f9cb629c98fda46bd822c1a21d8

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Tk
  module Event
    module Handler
      data = Data::PROPERTIES.transpose[0].join(' ').gsub(/%/, '%%')
      @callback = %(bind %s %s { ::RubyFFI::event %d %s #{data} })
      @store = []
      @bound = {}
      @mutex = Mutex.new

      module_function

      def invoke(id, event)
        return unless found = @store.at(id)
        found.call(event)
      end

      def register_block(block)
        id = nil

        @mutex.synchronize{
          @store << block
          id = @store.size - 1
        }

        return id
      end

      def register(tag, sequence, &block)
        id = register_block(block)
        Tk.interp.eval(@callback % [tag, sequence, id, sequence])
        @bound[[tag, sequence]] = block
        id
      end

      def register_custom(block)
        id = register_block(block)
        yield id
        id
      end

      def unregister(tag, sequence)
        key = [tag, sequence]

        if block = @bound[key]
          Tk.execute(:bind, tag, sequence, nil)
          id = @store.index(block)
          @store[id] = nil
          @bound.delete(key)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ffi-tk-2010.03 lib/ffi-tk/event/handler.rb
ffi-tk-2010.02 lib/ffi-tk/event/handler.rb
ffi-tk-2010.01 lib/ffi-tk/event/handler.rb