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



48
49
50
51
52
53
54
55
56
57
58
59
# 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

Instance Attribute Details

#cursorCursor (readonly)

Get the text cursor.



78
79
80
# File 'lib/cura/application.rb', line 78

def cursor
  @cursor
end

#dispatcherEvent::Dispatcher (readonly)

Get the event dispatcher.



88
89
90
# File 'lib/cura/application.rb', line 88

def dispatcher
  @dispatcher
end

#pencilPencil (readonly)

Get the pencil used for drawing.



83
84
85
# File 'lib/cura/application.rb', line 83

def pencil
  @pencil
end

Class Method Details

.run(attributes = {}) ⇒ Object



38
39
40
# File 'lib/cura/application.rb', line 38

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

Instance Method Details

#adapterAdapter

Get the adapter used for running this application.



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

#adapter=(adapter) ⇒ Adapter

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



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

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

#add_window(window) ⇒ Window

Add a window to this application.



170
171
172
173
174
175
176
# File 'lib/cura/application.rb', line 170

def add_window(window)
  super

  window.application = self

  window
end

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

Dispatch an event.

Options Hash (options):

  • :target (#to_i)

    The optional target of the event.



162
163
164
# File 'lib/cura/application.rb', line 162

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

#drawApplication

Draw all windows.



191
192
193
194
195
# File 'lib/cura/application.rb', line 191

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.

Raises:

  • (TypeError)


146
147
148
149
150
151
152
153
154
# 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

#focusedComponent::Base

Get the currently focused component.



135
136
137
# File 'lib/cura/application.rb', line 135

def focused
  @dispatcher.target
end

#inspectString

Instance inspection.



200
201
202
# File 'lib/cura/application.rb', line 200

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

#runApplication

Run this application.



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

def run
  run_event_loop

  self
ensure
  unless @cleaned
    @adapter.cleanup
    @cleaned = true
  end
end

#running?Boolean

Check if this application is running.



128
129
130
# File 'lib/cura/application.rb', line 128

def running?
  @running
end

#stopApplication

Stop the application after the current run cycle.



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

def stop
  @running = false

  self
end

#stop!Application

Stop the application immediently.



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

def stop!
  @running = false

  @adapter.cleanup
  @cleaned = true

  self
end

#updateApplication

Update all windows.



181
182
183
184
185
186
# File 'lib/cura/application.rb', line 181

def update
  update_windows
  cursor.update

  self
end