Archived
1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
cli-old/lib/widgets/chat.rb
Braiden Vasco 896aae87cb Rename Curses::React::Component#draw to #render, #render to #draw
To make components similar to React components
2017-07-28 00:02:59 +00:00

73 lines
1.8 KiB
Ruby

# frozen_string_literal: true
module Widgets
class Chat < VPanel
def initialize(_parent)
super
@info = Info.new self
@history = History.new self
@message = NewMessage.new self
end
def props=(_value)
super
@info.props = props[:info].merge(
public_key: props[:friend] ? props[:friend][:public_key] : nil,
name: props[:friend] ? props[:friend][:name] : nil,
status: props[:friend] ? props[:friend][:status] : nil,
status_message: props[:friend] ? props[:friend][:status_message] : nil,
).freeze
@history.props = props[:history].merge(
messages: props[:friend] ? props[:friend][:history] : [].freeze,
).freeze
@message.props = props[:new_message].merge(
on_enter: props[:on_new_message_enter],
on_putc: props[:on_new_message_putc],
on_left: props[:on_new_message_left],
on_right: props[:on_new_message_right],
on_home: props[:on_new_message_home],
on_end: props[:on_new_message_end],
on_backspace: props[:on_new_message_backspace],
on_delete: props[:on_new_message_delete],
text: props[:friend] ? props[:friend][:new_message][:text] : '',
cursor_pos: props[:friend] ? props[:friend][:new_message][:cursor_pos] : 0,
).freeze
end
def trigger(event)
focus&.trigger event
end
private
def render
super
setpos 0, 2
addstr '-' * props[:width]
setpos 0, props[:height] - 2
addstr '-' * props[:width]
end
def focus
case props[:focus]
when :info then @info
when :history then @history
when :new_message then @message
end
end
def children
[@info, @history, @message]
end
end
end