module Glimmer::LibUI::CustomControl

Attributes

args[R]
body_root[R]
content[R]
keyword[R]
libui[R]
options[R]
parent[R]
parent_proxy[R]

Public Class Methods

add_custom_control_namespaces_for(klass) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 92
def add_custom_control_namespaces_for(klass)
  Glimmer::LibUI::CustomControl.namespaces_for_class(klass).drop(1).each do |namespace|
    Glimmer::LibUI::CustomControl.custom_control_namespaces << namespace
  end
end
after_body(&block) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 160
def after_body(&block)
  @after_body_block = block
end
before_body(&block) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 152
def before_body(&block)
  @before_body_block = block
end
body(&block) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 156
def body(&block)
  @body_block = block
end
custom_control_namespaces() click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 106
def custom_control_namespaces
  @custom_control_namespaces ||= reset_custom_control_namespaces
end
def_option_attr_accessors(new_options) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 138
        def def_option_attr_accessors(new_options)
          new_options.each do |option, default|
            class_eval <<-end_eval, __FILE__, __LINE__
              def #{option}
                options[:#{option}]
              end
              
              def #{option}=(option_value)
                self.options[:#{option}] = option_value
              end
            end_eval
          end
        end
flyweight_custom_control_classes() click to toggle source

Flyweight Design Pattern memoization cache. Can be cleared if memory is needed.

# File lib/glimmer/libui/custom_control.rb, line 78
def flyweight_custom_control_classes
  @flyweight_custom_control_classes ||= {}
end
for(keyword) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 44
def for(keyword)
  unless flyweight_custom_control_classes.keys.include?(keyword)
    begin
      extracted_namespaces = keyword.
        to_s.
        split(/__/).map do |namespace|
          namespace.camelcase(:upper)
        end
      custom_control_namespaces.each do |base|
        extracted_namespaces.reduce(base) do |result, namespace|
          if !result.constants.include?(namespace)
            namespace = result.constants.detect {|c| c.to_s.upcase == namespace.to_s.upcase } || namespace
          end
          begin
            flyweight_custom_control_classes[keyword] = constant = result.const_get(namespace)
            return constant if constant.ancestors.include?(Glimmer::LibUI::CustomControl)
            flyweight_custom_control_classes[keyword] = constant
          rescue => e
            # Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
            flyweight_custom_control_classes[keyword] = result
          end
        end
      end
      raise "#{keyword} has no custom control class!"
    rescue => e
      Glimmer::Config.logger.debug {e.message}
      Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
      flyweight_custom_control_classes[keyword] = nil
    end
  end
  flyweight_custom_control_classes[keyword]
end
keyword() click to toggle source

Returns keyword to use for this custom control

# File lib/glimmer/libui/custom_control.rb, line 83
def keyword
  self.name.underscore.gsub('::', '__')
end
namespaces_for_class(m) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 98
def namespaces_for_class(m)
  return [m] if m.name.nil?
  namespace_constants = m.name.split(/::/).map(&:to_sym)
  namespace_constants.reduce([Object]) do |output, namespace_constant|
    output += [output.last.const_get(namespace_constant)]
  end[1..-1].uniq.reverse
end
new(keyword, parent, args, options, &content) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 167
def initialize(keyword, parent, args, options, &content)
  @parent_proxy = @parent = parent
  options ||= {}
  @options = self.class.options.merge(options)
  @content = ProcTracker.new(content) if content
  execute_hook('before_body')
  body_block = self.class.instance_variable_get("@body_block")
  raise Glimmer::Error, 'Invalid custom control for having no body! Please define body block!' if body_block.nil?
  @body_root = instance_exec(&body_block)
  raise Glimmer::Error, 'Invalid custom control for having an empty body! Please fill body block!' if @body_root.nil?
  @libui = @body_root.libui
  execute_hook('after_body')
  # TODO deregister all observer_registrations on destroy of the control once that listener is supported
  # (on_destroy) unless it is the last window closing, in which case exit faster
  post_add_content if content.nil?
end
option(new_option, default: nil) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 131
def option(new_option, default: nil)
  new_option = new_option.to_s.to_sym
  new_options = {new_option => default}
  @options = options.merge(new_options)
  def_option_attr_accessors(new_options)
end
options(*new_options) click to toggle source

Allows defining convenience option accessors for an array of option names Example: ‘options :color1, :color2` defines `#color1` and `#color2` where they return the instance values `options` and `options` respectively. Can be called multiple times to set more options additively. When passed no arguments, it returns list of all option names captured so far

# File lib/glimmer/libui/custom_control.rb, line 120
def options(*new_options)
  new_options = new_options.compact.map(&:to_s).map(&:to_sym)
  if new_options.empty?
    @options ||= {} # maps options to defaults
  else
    new_options = new_options.reduce({}) {|new_options_hash, new_option| new_options_hash.merge(new_option => nil)}
    @options = options.merge(new_options)
    def_option_attr_accessors(new_options)
  end
end
reset_custom_control_namespaces() click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 110
def reset_custom_control_namespaces
  @custom_control_namespaces = Set[Object, Glimmer::LibUI]
end
shortcut_keyword() click to toggle source

Returns shortcut keyword to use for this custom control (keyword minus namespace)

# File lib/glimmer/libui/custom_control.rb, line 88
def shortcut_keyword
  self.name.underscore.gsub('::', '__').split('__').last
end

Public Instance Methods

can_handle_listener?(listener) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 197
def can_handle_listener?(listener)
  body_root&.can_handle_listener?(listener.to_s)
end
handle_listener(listener, &block) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 201
def handle_listener(listener, &block)
  body_root.handle_listener(listener.to_s, &block)
end
has_instance_method?(method_name) click to toggle source

This method ensures it has an instance method not coming from Glimmer DSL

# File lib/glimmer/libui/custom_control.rb, line 206
def has_instance_method?(method_name)
  respond_to?(method_name) and
    !@body_root.respond_to_libui?(method_name) and
    (method(method_name) rescue nil) and
    !method(method_name)&.source_location&.first&.include?('glimmer/dsl/engine.rb') and
    !method(method_name)&.source_location&.first&.include?('glimmer/libui/control_proxy.rb')
end
method_missing(method_name, *args, &block) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 224
def method_missing(method_name, *args, &block)
  # TODO Consider supporting a glimmer error silencing option for methods defined here
  # but fail the glimmer DSL for the right reason to avoid seeing noise in the log output
  if block && can_handle_listener?(method_name)
    handle_listener(method_name, &block)
  else
    @body_root.send(method_name, *args, &block)
  end
end
observer_registrations() click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 193
def observer_registrations
  @observer_registrations ||= []
end
post_add_content() click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 189
def post_add_content
  # No Op by default
end
post_initialize_child(child) click to toggle source

Subclasses may override to perform post initialization work on an added child

# File lib/glimmer/libui/custom_control.rb, line 185
def post_initialize_child(child)
  # No Op by default
end
respond_to?(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/glimmer/libui/custom_control.rb, line 234
def respond_to?(method_name, *args, &block)
  result = false
  result ||= super
  result ||= can_handle_listener?(method_name)
  result ||= @body_root.respond_to?(method_name, *args, &block)
end

Private Instance Methods

execute_hook(hook_name) click to toggle source
# File lib/glimmer/libui/custom_control.rb, line 243
def execute_hook(hook_name)
  hook_block = self.class.instance_variable_get("@#{hook_name}_block")
  return if hook_block.nil?
  temp_method_name = "#{hook_name}_block_#{hook_block.hash.abs}_#{(Time.now.to_f * 1_000_000).to_i}"
  singleton_class.define_method(temp_method_name, &hook_block)
  send(temp_method_name)
  singleton_class.send(:remove_method, temp_method_name)
end