Archived
1
0
Fork 0

Organize code

This commit is contained in:
Braiden Vasco 2017-07-25 14:48:37 +00:00
parent e924f093de
commit d891c2026e
10 changed files with 38 additions and 19 deletions

View file

@ -2,18 +2,11 @@
module Widgets
class Base
attr_reader :parent
attr_reader :props
def initialize(parent)
@parent = parent
@props = {}.freeze
end
def window
@window ||= parent&.subwin(props[:x], props[:y], props[:width], props[:height]) || Curses.stdscr
end
def props=(value)
raise TypeError, "expected props to be a #{Hash}" unless value.is_a? Hash
raise ArgumentError, 'expected props to be frozen' unless value.frozen?
@ -29,6 +22,14 @@ module Widgets
window.refresh
end
private
attr_reader :parent, :props
def window
@window ||= parent&.subwin(props[:x], props[:y], props[:width], props[:height]) || Curses.stdscr
end
def draw
raise NotImplementedError, "#{self.class}#draw"
end

View file

@ -17,6 +17,8 @@ module Widgets
@message.props = props[:new_message]
end
private
def focus
case props[:focus]
when :info then @info

View file

@ -3,6 +3,8 @@
module Widgets
class Chat < VPanel
class History < Base
private
def draw
offset = 0

View file

@ -3,6 +3,8 @@
module Widgets
class Chat < VPanel
class Info < Base
private
def draw
setpos 0, 0
Style.default.online_mark window do

View file

@ -6,6 +6,8 @@ module Widgets
window.subwin height, width, y, x
end
private
def draw
children.each(&:render)
end

View file

@ -16,6 +16,8 @@ module Widgets
WIDTH = LOGO.first.length
HEIGHT = LOGO.length
private
def draw
Style.default.logo window do
LOGO.each_with_index do |s, index|

View file

@ -9,6 +9,14 @@ module Widgets
@chat = Widgets::Chat.new self
end
def props=(_value)
super
@sidebar.props = props[:sidebar]
@chat.props = props[:chat]
end
private
def focus
case props[:focus]
when :sidebar then @sidebar
@ -16,12 +24,6 @@ module Widgets
end
end
def props=(_value)
super
@sidebar.props = props[:sidebar]
@chat.props = props[:chat]
end
def children
[@sidebar, @chat]
end

View file

@ -2,6 +2,8 @@
module Widgets
class Menu < Base
private
def draw
props[:items][props[:top]...(props[:top] + props[:height])].each_with_index.each do |item, offset|
index = props[:top] + offset

View file

@ -9,6 +9,14 @@ module Widgets
@menu = Menu.new self
end
def props=(_value)
super
@logo.props = props[:logo]
@menu.props = props[:menu]
end
private
def focus
case props[:focus]
when :menu
@ -16,12 +24,6 @@ module Widgets
end
end
def props=(_value)
super
@logo.props = props[:logo]
@menu.props = props[:menu]
end
def children
[@logo, @menu]
end

View file

@ -2,6 +2,8 @@
module Widgets
class Text < Base
private
def draw
total = props[:width] - 1
start = [0, props[:cursor_pos] - total].max