Code Coverage
 
Lines
Covered
61.54% covered (warning)
61.54%
40 / 65
1
module TheFox
1
module TermKit
1
class UIApp < App
1
attr_accessor :app_controller
1
attr_accessor :active_controller
1
def initialize
13
super()
#puts 'UIApp initialize'
13
@render_count = 0
13
@app_controller = nil
13
@active_controller = nil
13
ui_init
end
##
# See App `run_cycle()` method.
1
def run_cycle
0
super()
#puts 'UIApp->run_cycle'
0
render
end
1
def set_app_controller(app_controller)
3
if !app_controller.is_a?(AppController)
2
raise ArgumentError, "Argument is not a AppController -- #{app_controller.class} given"
end
1
@app_controller = app_controller
end
1
def set_active_controller(active_controller)
3
if !active_controller.is_a?(ViewController)
1
raise ArgumentError, "Argument is not a ViewController -- #{active_controller.class} given"
end
2
if !@active_controller.nil?
1
@active_controller.inactive
end
2
@active_controller = active_controller
2
@active_controller.active
end
##
# Handles the actual rendering and drawing of the UI layer. Calls `draw_point()` for all points of `@active_controller`.
1
def render
#sleep 1 # @TODO: remove this line
0
area = nil # @TODO: use current terminal size as area
0
@render_count += 1
# @logger.debug("--- RENDER: #{@render_count} ---")
0
if !@active_controller.nil?
# @logger.debug("RENDER active_controller OK: #{@active_controller.inspect}")
# @logger.debug("RENDER active_controller view grid_cache: #{@active_controller.view.grid_cache.inspect}")
0
@active_controller.render(area).each do |y_pos, row|
0
row.each do |x_pos, content|
#sleep 0.1 # @TODO: remove this line
# @logger.debug("RENDER #{x_pos}:#{y_pos} '#{content}'")
0
draw_point(Point.new(x_pos, y_pos), content)
#ui_refresh # @TODO: remove this line
end
end
end
0
ui_refresh
end
1
def draw_line(point, row)
0
x_pos = point.x
0
y_pos = point.y
0
row.length.times do |n|
0
draw_point(Point.new(x_pos, y_pos), ViewContent.new(row[n]))
0
x_pos += 1
end
end
##
# Needs to be implemented by the sub-class.
#
# For example, CursesApp is a sub-class of UIApp. CursesApp uses `Curses.setpos` and `Curses.addstr` in `draw_point()` to draw the points.
1
def draw_point(point, content)
1
raise NotImplementedError
end
1
def ui_refresh
1
raise NotImplementedError
end
1
def ui_max_x
1
-1
end
1
def ui_max_y
1
-1
end
1
protected
1
def app_will_terminate
#puts 'UIApp app_will_terminate'
1
ui_close
end
1
def ui_init
# raise NotImplementedError
end
1
def ui_close
# raise NotImplementedError
end
1
def key_down(key)
1
if !key.nil? && !@active_controller.nil?
0
event = KeyEvent.new
0
event.key = key
0
begin
0
@active_controller.handle_event(event)
0
rescue Exception::UnhandledKeyEventException => e
0
@logger.warn("#{self.class} UnhandledKeyEventException: #{e}")
0
if @app_controller.nil?
0
@logger.warn("#{self.class} UnhandledKeyEventException: no app controller set, raise")
0
raise e
end
0
@app_controller.handle_event(e.event)
rescue Exception::UnhandledEventException => e
0
@logger.warn("#{self.class} UnhandledEventException: #{e}")
end
end
end
end
end
end