Archived
1
0
Fork 0

Render Curses::Window with props

This commit is contained in:
Braiden Vasco 2017-07-25 14:29:01 +00:00
parent 9eb51dbc23
commit 71776fcafb
7 changed files with 54 additions and 48 deletions

View file

@ -71,10 +71,17 @@ private
focus: :menu, focus: :menu,
focused: true, focused: true,
menu: { logo: {
x: 0, x: 0,
y: 0, y: 0,
width: Widgets::Logo::WIDTH, width: Widgets::Logo::WIDTH,
height: Widgets::Logo::HEIGHT,
}.freeze,
menu: {
x: 0,
y: Widgets::Logo::HEIGHT,
width: Widgets::Logo::WIDTH,
height: Curses.stdscr.maxy - Widgets::Logo::HEIGHT, height: Curses.stdscr.maxy - Widgets::Logo::HEIGHT,
focused: true, focused: true,
@ -90,7 +97,7 @@ private
}.freeze, }.freeze,
chat: { chat: {
x: 0, x: Widgets::Logo::WIDTH,
y: 0, y: 0,
width: Curses.stdscr.maxx - Widgets::Logo::WIDTH, width: Curses.stdscr.maxx - Widgets::Logo::WIDTH,
height: Curses.stdscr.maxy, height: Curses.stdscr.maxy,
@ -98,7 +105,7 @@ private
focused: false, focused: false,
info: { info: {
x: 0, x: Widgets::Logo::WIDTH,
y: 0, y: 0,
width: Curses.stdscr.maxx - Widgets::Logo::WIDTH, width: Curses.stdscr.maxx - Widgets::Logo::WIDTH,
height: 2, height: 2,
@ -109,10 +116,10 @@ private
}.freeze, }.freeze,
new_message: { new_message: {
x: 0, x: Widgets::Logo::WIDTH,
y: 0, y: Curses.stdscr.maxy - 1,
width: Curses.stdscr.maxx - Widgets::Logo::WIDTH, width: Curses.stdscr.maxx - Widgets::Logo::WIDTH,
height: Curses.stdscr.maxy - 3, height: 1,
focused: false, focused: false,
text: '', text: '',
@ -120,10 +127,10 @@ private
}.freeze, }.freeze,
history: { history: {
x: 0, x: Widgets::Logo::WIDTH,
y: 0, y: 2,
width: Curses.stdscr.maxx - Widgets::Logo::WIDTH, width: Curses.stdscr.maxx - Widgets::Logo::WIDTH,
height: 1, height: Curses.stdscr.maxy - 3,
focused: true, focused: true,
messages: 1.upto(100).map do messages: 1.upto(100).map do

View file

@ -35,13 +35,7 @@ class Screen
Curses.timeout = 0 # non-blocking input Curses.timeout = 0 # non-blocking input
Curses.stdscr.keypad = true Curses.stdscr.keypad = true
@window ||= Widgets::Main.new( @window ||= Widgets::Main.new nil
nil,
0,
0,
Curses.stdscr.maxx,
Curses.stdscr.maxy,
)
end end
def close def close

View file

@ -3,29 +3,45 @@
module Widgets module Widgets
class Base class Base
attr_reader :parent attr_reader :parent
attr_reader :window
attr_reader :width, :height
attr_reader :props attr_reader :props
def initialize(parent, x, y, width, height) def initialize(parent)
@parent = parent @parent = parent
@window = parent ? parent.window.subwin(height, width, y, x) : Curses.stdscr
@width = width
@height = height
@props = {}.freeze @props = {}.freeze
end end
def window
@window ||= parent ? parent.window.subwin(height, width, y, x) : Curses.stdscr
end
def trigger(event); end def trigger(event); end
def props=(value) def props=(value)
raise TypeError, "expected props to be a #{Hash}" unless value.is_a? Hash raise TypeError, "expected props to be a #{Hash}" unless value.is_a? Hash
raise ArgumentError, 'expected props to be frozen' unless value.frozen? raise ArgumentError, 'expected props to be frozen' unless value.frozen?
@window = nil if props[:x] != value[:x] || props[:y] != value[:y] ||
props[:width] != value[:width] || props[:height] != value[:height]
@props = value @props = value
end end
def x
props[:x]
end
def y
props[:y]
end
def width
props[:width]
end
def height
props[:height]
end
def focused def focused
props[:focused] props[:focused]
end end

View file

@ -2,20 +2,12 @@
module Widgets module Widgets
class Chat < VPanel class Chat < VPanel
def initialize(parent, x, y, width, height) def initialize(_parent)
super super
info_height = 2 @info = Info.new self
message_height = 1 @history = History.new self
history_height = height - info_height - message_height @message = NewMessage.new self
info_top = 0
history_top = info_height
message_top = info_height + history_height
@info = Info.new self, x, y + info_top, width, info_height
@history = History.new self, x, y + history_top, width, history_height
@message = NewMessage.new self, x, y + message_top, width, message_height
end end
def props=(_value) def props=(_value)

View file

@ -16,10 +16,6 @@ module Widgets
WIDTH = LOGO.first.length WIDTH = LOGO.first.length
HEIGHT = LOGO.length HEIGHT = LOGO.length
def initialize(parent, x, y, _width, _height)
super parent, x, y, WIDTH, HEIGHT
end
def draw def draw
Style.default.logo window do Style.default.logo window do
LOGO.each_with_index do |s, index| LOGO.each_with_index do |s, index|

View file

@ -2,11 +2,11 @@
module Widgets module Widgets
class Main < Container class Main < Container
def initialize(parent, x, y, width, height) def initialize(_parent)
super super
@sidebar = Widgets::Sidebar.new self, x, y, nil, height @sidebar = Widgets::Sidebar.new self
@chat = Widgets::Chat.new self, x + @sidebar.width, y, width - @sidebar.width, height @chat = Widgets::Chat.new self
end end
def focus def focus

View file

@ -2,11 +2,11 @@
module Widgets module Widgets
class Sidebar < Container class Sidebar < Container
def initialize(parent, x, y, _width, height) def initialize(_parent)
super parent, x, y, Logo::WIDTH, height super
@logo = Logo.new self, x, y, nil, nil @logo = Logo.new self
@menu = Menu.new self, x, @logo.height, @logo.width, height - @logo.height @menu = Menu.new self
end end
def focus def focus
@ -18,6 +18,7 @@ module Widgets
def props=(_value) def props=(_value)
super super
@logo.props = props[:logo]
@menu.props = props[:menu] @menu.props = props[:menu]
end end