Rewrite Widgets::Main with elements
This commit is contained in:
parent
aa5503d140
commit
ca855f4ceb
3 changed files with 38 additions and 45 deletions
|
@ -6,10 +6,6 @@ module Widgets
|
||||||
focus&.trigger event
|
focus&.trigger event
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
|
||||||
node.draw
|
|
||||||
end
|
|
||||||
|
|
||||||
def node
|
def node
|
||||||
elem = render
|
elem = render
|
||||||
React::Curses::Nodes.klass_for(elem).new nil, elem
|
React::Curses::Nodes.klass_for(elem).new nil, elem
|
||||||
|
|
|
@ -2,17 +2,42 @@
|
||||||
|
|
||||||
module Widgets
|
module Widgets
|
||||||
class Main < Container
|
class Main < Container
|
||||||
def initialize(_parent)
|
def trigger(event)
|
||||||
super
|
case event
|
||||||
|
when Events::Window::Left
|
||||||
@sidebar = Widgets::Sidebar.new self
|
props[:on_window_left].call
|
||||||
@chat = Widgets::Chat.new self
|
when Events::Window::Right
|
||||||
|
props[:on_window_right].call
|
||||||
|
else
|
||||||
|
focus&.trigger event
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def props=(_value)
|
def draw
|
||||||
super
|
node.draw
|
||||||
|
end
|
||||||
|
|
||||||
@sidebar.props = props[:sidebar].merge(
|
def node
|
||||||
|
elem = render
|
||||||
|
React::Curses::Nodes.klass_for(elem).new nil, elem
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
create_element :window, x: props[:x], y: props[:y], width: props[:width], height: props[:height] do
|
||||||
|
create_element :wrapper do
|
||||||
|
render_sidebar
|
||||||
|
render_chat
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# (0...props[:height]).each do |y|
|
||||||
|
# setpos props[:sidebar][:width], y
|
||||||
|
# addstr "\u2502"
|
||||||
|
# end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_sidebar
|
||||||
|
create_element Sidebar, props[:sidebar].merge(
|
||||||
on_menu_up: props[:on_menu_up],
|
on_menu_up: props[:on_menu_up],
|
||||||
on_menu_down: props[:on_menu_down],
|
on_menu_down: props[:on_menu_down],
|
||||||
|
|
||||||
|
@ -20,8 +45,10 @@ module Widgets
|
||||||
|
|
||||||
friends: props[:data][:friends],
|
friends: props[:data][:friends],
|
||||||
).freeze
|
).freeze
|
||||||
|
end
|
||||||
|
|
||||||
@chat.props = props[:chat].merge(
|
def render_chat
|
||||||
|
create_element Chat, props[:chat].merge(
|
||||||
on_new_message_enter: props[:on_new_message_enter],
|
on_new_message_enter: props[:on_new_message_enter],
|
||||||
|
|
||||||
on_new_message_putc: props[:on_new_message_putc],
|
on_new_message_putc: props[:on_new_message_putc],
|
||||||
|
@ -41,37 +68,11 @@ module Widgets
|
||||||
).freeze
|
).freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
def trigger(event)
|
|
||||||
case event
|
|
||||||
when Events::Window::Left
|
|
||||||
props[:on_window_left].call
|
|
||||||
when Events::Window::Right
|
|
||||||
props[:on_window_right].call
|
|
||||||
else
|
|
||||||
focus&.trigger event
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def render
|
|
||||||
super
|
|
||||||
|
|
||||||
(0...props[:height]).each do |y|
|
|
||||||
setpos props[:sidebar][:width], y
|
|
||||||
addstr "\u2502"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def focus
|
def focus
|
||||||
case props[:focus]
|
case props[:focus]
|
||||||
when :sidebar then @sidebar
|
when :sidebar then node.children[0].children[0].instance
|
||||||
when :chat then @chat
|
when :chat then node.children[0].children[1].instance
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def children
|
|
||||||
[@sidebar, @chat]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,10 +6,6 @@ module Widgets
|
||||||
focus&.trigger event
|
focus&.trigger event
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
|
||||||
node.draw
|
|
||||||
end
|
|
||||||
|
|
||||||
def node
|
def node
|
||||||
elem = render
|
elem = render
|
||||||
React::Curses::Nodes.klass_for(elem).new nil, elem
|
React::Curses::Nodes.klass_for(elem).new nil, elem
|
||||||
|
|
Reference in a new issue