Class: Cura::Application

Inherits:
Object
  • Object
show all
Includes:
Cura::Attributes::HasAttributes, Cura::Attributes::HasEvents, Cura::Attributes::HasInitialize, Cura::Attributes::HasWindows
Defined in:
lib/cura/application.rb

Overview

An application.

Instance Attribute Summary collapse

Attributes included from Cura::Attributes::HasEvents

#event_handler

Attributes included from Cura::Attributes::HasWindows

#windows

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cura::Attributes::HasEvents

included, #on_event

Methods included from Cura::Attributes::HasWindows

#delete_window, #delete_window_at, #delete_windows

Methods included from Cura::Attributes::HasAttributes

included, #update_attributes

Constructor Details

#initialize(attributes = {}) ⇒ Application

Returns a new instance of Application.



52
53
54
55
56
57
58
59
60
61
# File 'lib/cura/application.rb', line 52

def initialize(attributes={})
  super
  
  @running = false
  @cursor = Cursor.new(application: self)
  @pencil = Pencil.new
  
  setup_adapter
  setup_dispatcher
end

Instance Attribute Details

#cursorCursor (readonly)

Get the text cursor.

Returns:



80
81
82
# File 'lib/cura/application.rb', line 80

def cursor
  @cursor
end

#dispatcherEvent::Dispatcher (readonly)

Get the event dispatcher.

Returns:



90
91
92
# File 'lib/cura/application.rb', line 90

def dispatcher
  @dispatcher
end

#pencilPencil (readonly)

Get the pencil used for drawing.

Returns:



85
86
87
# File 'lib/cura/application.rb', line 85

def pencil
  @pencil
end

Class Method Details

.run(attributes = {}) ⇒ Object



41
42
43
# File 'lib/cura/application.rb', line 41

def run(attributes={})
  new(attributes).run
end

Instance Method Details

#adapterAdapter

Get the adapter used for running this application.

Returns:



# File 'lib/cura/application.rb', line 63

#adapter=(adapter) ⇒ Adapter

Set the adapter used for running this application. This cannot be set after #run is used.

Parameters:

  • value (Adapter)

    The new adapter.

Returns:



75
# File 'lib/cura/application.rb', line 75

attribute(:adapter) { |adapter| validate_adapter(adapter) }

#add_window(window) ⇒ Window

Add a window to this application.

Parameters:

Returns:



167
168
169
170
171
172
173
# File 'lib/cura/application.rb', line 167

def add_window(window)
  super
  
  window.application = self
  
  window
end

#dispatch_event(event, options = {}) ⇒ Event::Base

Dispatch an event.

Parameters:

  • event (#to_sym)

    The name of the event class to create an instance of or an event instance.

  • options (#to_hash, #to_h) (defaults to: {})

Options Hash (options):

  • :target (#to_i)

    The optional target of the event.

Returns:



159
160
161
# File 'lib/cura/application.rb', line 159

def dispatch_event(event, options={})
  @dispatcher.dispatch_event(event, options)
end

#drawApplication

Draw all windows.

Returns:



188
189
190
191
192
# File 'lib/cura/application.rb', line 188

def draw
  draw_windows
  
  self
end

#focus(component) ⇒ Component::Base

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.

Parameters:

Returns:

Raises:

  • (TypeError)


143
144
145
146
147
148
149
150
151
# File 'lib/cura/application.rb', line 143

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

#focusedComponent::Base

Get the currently focused component.

Returns:



132
133
134
# File 'lib/cura/application.rb', line 132

def focused
  @dispatcher.target
end

#inspectString

Instance inspection.

Returns:

  • (String)


197
198
199
# File 'lib/cura/application.rb', line 197

def inspect
  "#<#{self.class}>"
end

#runApplication

Run this application.

Returns:



95
96
97
98
99
100
101
# File 'lib/cura/application.rb', line 95

def run
  run_event_loop
  
  self
ensure
  @adapter.cleanup
end

#running?Boolean

Check if this application is running.

Returns:

  • (Boolean)


125
126
127
# File 'lib/cura/application.rb', line 125

def running?
  @running
end

#stopApplication

Stop the application after the current run cycle.

Returns:



106
107
108
109
110
# File 'lib/cura/application.rb', line 106

def stop
  @running = false
  
  self
end

#stop!Application

Stop the application immediently.

Returns:



115
116
117
118
119
120
# File 'lib/cura/application.rb', line 115

def stop!
  stop
  @adapter.cleanup
  
  self
end

#updateApplication

Update all windows.

Returns:



178
179
180
181
182
183
# File 'lib/cura/application.rb', line 178

def update
  update_windows
  cursor.update
  
  self
end