Sha256: 09d979b3d537d84ef1f2a18e232e15cb8ae014ef52f7dd1b6512a0e7f29e4ad7

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

module GirFFIGtk
  # Override init to automatically use ARGV as its argument.
  module AutoArgv
    def self.included(base)
      base.extend ClassMethods
      class << base
        alias_method :init_without_auto_argv, :init
        alias_method :init, :init_with_auto_argv
      end
    end

    # Implementation of class methods for AutoArgv
    module ClassMethods
      def init_with_auto_argv
        remaining = init_without_auto_argv([$PROGRAM_NAME, *ARGV]).to_a
        remaining.shift
        ARGV.replace remaining
      end
    end
  end

  # Override main to start an idle thread to allow Ruby threads to run during
  # the main loop, and to handle exceptions
  module MainLoopOverride
    class DummyLoop
      def quit
        Gtk.main_quit
      end
    end

    def self.included(base)
      base.extend ClassMethods
      class << base
        alias_method :main_without_override, :main
        alias_method :main, :main_with_override
      end
    end

    # Implementation of class methods for MainLoopOverride
    module ClassMethods
      def main_with_override
        case RUBY_ENGINE
        when 'jruby'
        when 'rbx'
        else # 'ruby' most likely
          GLib::MainLoop::ThreadEnabler.instance.setup_idle_handler
        end
        GLib::MainLoop::RUNNING_LOOPS << DummyLoop.new
        result = main_without_override
        ex = GLib::MainLoop::EXCEPTIONS.shift
        GLib::MainLoop::RUNNING_LOOPS.pop
        raise ex if ex
        result
      end
    end
  end
end

# Overrides for Gtk module functions
module Gtk
  setup_method 'init'
  setup_method 'main'

  include GirFFIGtk::AutoArgv
  include GirFFIGtk::MainLoopOverride
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gir_ffi-gtk-0.11.0 lib/gir_ffi-gtk/base.rb
gir_ffi-gtk-0.10.0 lib/gir_ffi-gtk/base.rb
gir_ffi-gtk-0.9.0 lib/gir_ffi-gtk/base.rb