class Cura::Application
An application.
Attributes
Get the text cursor.
@return [Cursor]
Get the event dispatcher.
@return [Event::Dispatcher]
Get the pencil used for drawing.
@return [Pencil]
Public Class Methods
Cura::Attributes::HasEvents::new
# File lib/cura/application.rb, line 48 def initialize(attributes={}) super # setup_adapter @running = false @cursor = Cursor.new(application: self) @pencil = Pencil.new setup_adapter setup_dispatcher end
# File lib/cura/application.rb, line 38 def run(attributes={}) new(attributes).run end
Public Instance Methods
Add a window to this application.
@param [Window] window @return [Window]
Cura::Attributes::HasWindows#add_window
# File lib/cura/application.rb, line 170 def add_window(window) super window.application = self window end
Dispatch an event.
@param [#to_sym] event The name of the event class to create an instance of or an event instance. @param [#to_hash, to_h] options @option options [#to_i] :target The optional target of the event. @return [Event::Base] The dispatched event.
# File lib/cura/application.rb, line 162 def dispatch_event(event, options={}) @dispatcher.dispatch_event(event, options) end
Draw all windows.
@return [Application]
# File lib/cura/application.rb, line 191 def draw draw_windows self end
Set focus to a component.
There can only be one component focused at a time within an application, if any. All dispatched events are sent to the currently focused component, or the application if no component is focused.
@param [nil, Component::Base] component @return [Component::Base]
# File lib/cura/application.rb, line 146 def focus(component) raise TypeError, "component must be nil or be a Cura::Component::Base" unless component.nil? || component.is_a?(Cura::Component::Base) dispatch_event(:unfocus) @dispatcher.target = component dispatch_event(:focus) component end
Get the currently focused component.
@return [Component::Base]
# File lib/cura/application.rb, line 135 def focused @dispatcher.target end
Instance inspection.
@return [String]
# File lib/cura/application.rb, line 200 def inspect "#<#{self.class}>" end
Run this application.
@return [Application] This application.
# File lib/cura/application.rb, line 93 def run run_event_loop self ensure unless @cleaned @adapter.cleanup @cleaned = true end end
Check if this application is running.
@return [Boolean]
# File lib/cura/application.rb, line 128 def running? @running end
Stop the application after the current run cycle.
@return [Application] This application.
# File lib/cura/application.rb, line 107 def stop @running = false self end
Stop the application immediently.
@return [Application] This application.
# File lib/cura/application.rb, line 116 def stop! @running = false @adapter.cleanup @cleaned = true self end
Update all windows.
@return [Application]
# File lib/cura/application.rb, line 181 def update update_windows cursor.update self end
Protected Instance Methods
# File lib/cura/application.rb, line 239 def run_event_loop @running = true while @running update draw dispatcher.run end end
# File lib/cura/application.rb, line 206 def setup_adapter if @adapter.nil? adapter_class ||= Adapter.all.first raise Error::InvalidAdapter if adapter_class.nil? @adapter = adapter_class.new end # TODO: If a class is given, run .new on it first # TODO: Adapter.name(*arguments) which defaults to an underscore'd version of the class constant # TODO: Adapter.find_by_name # TODO: Use Adapter.find_by_name here to allow passing of a Symbol to #adapter= @adapter.setup end
# File lib/cura/application.rb, line 222 def setup_dispatcher @dispatcher = Event::Dispatcher.new(application: self) @dispatcher.middleware << Event::Middleware::Aimer::MouseFocus.new @dispatcher.middleware << Event::Middleware::Aimer::TargetOption.new @dispatcher.middleware << Event::Middleware::Aimer::DispatcherTarget.new @dispatcher.middleware << Event::Middleware::Dispatch.new @dispatcher.middleware << Event::Middleware::Translator::MouseClick.new end
# File lib/cura/application.rb, line 232 def validate_adapter(adapter) # TODO: Raise error if ever set more than once raise Error::InvalidAdapter unless adapter.is_a?(Cura::Adapter) adapter end