From 29e9f0e351cdbae4c09a5bdfe5f2b32fadae99e2 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Tue, 25 Jul 2017 16:36:19 +0000 Subject: [PATCH] Handle new message putc --- lib/main.rb | 18 +++++++++++++++++- lib/widgets/chat.rb | 10 +++++++++- lib/widgets/main.rb | 6 ++++-- lib/widgets/text.rb | 7 +++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 78fc00d..6189e91 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -69,6 +69,8 @@ private on_menu_up: method(:on_menu_up), on_menu_down: method(:on_menu_down), + on_new_message_putc: method(:on_new_message_putc), + sidebar: { x: 0, y: 0, @@ -175,7 +177,7 @@ private def on_window_right @state = state.merge( - focus: :sidebar, + focus: :chat, sidebar: state[:sidebar].merge( focused: true, @@ -245,4 +247,18 @@ private ).freeze, ).freeze end + + def on_new_message_putc(char) + text = state[:chat][:new_message][:text] + cursor_pos = state[:chat][:new_message][:cursor_pos] + + @state = state.merge( + chat: state[:chat].merge( + new_message: state[:chat][:new_message].merge( + text: "#{text[0...cursor_pos]}#{char}#{text[cursor_pos..-1]}", + cursor_pos: cursor_pos + 1, + ).freeze, + ).freeze, + ).freeze + end end diff --git a/lib/widgets/chat.rb b/lib/widgets/chat.rb index 43f6ccd..1a74745 100644 --- a/lib/widgets/chat.rb +++ b/lib/widgets/chat.rb @@ -12,9 +12,17 @@ module Widgets def props=(_value) super + @info.props = props[:info] @history.props = props[:history] - @message.props = props[:new_message] + + @message.props = props[:new_message].merge( + on_putc: props[:on_new_message_putc], + ).freeze + end + + def trigger(event) + focus&.trigger event end private diff --git a/lib/widgets/main.rb b/lib/widgets/main.rb index a64e301..219f205 100644 --- a/lib/widgets/main.rb +++ b/lib/widgets/main.rb @@ -17,7 +17,9 @@ module Widgets on_menu_down: props[:on_menu_down], ).freeze - @chat.props = props[:chat] + @chat.props = props[:chat].merge( + on_new_message_putc: props[:on_new_message_putc], + ).freeze end def trigger(event) @@ -27,7 +29,7 @@ module Widgets when Events::Window::Right props[:on_window_right].call else - focus.trigger event + focus&.trigger event end end diff --git a/lib/widgets/text.rb b/lib/widgets/text.rb index 30deffb..802fbeb 100644 --- a/lib/widgets/text.rb +++ b/lib/widgets/text.rb @@ -2,6 +2,13 @@ module Widgets class Text < Base + def trigger(event) + case event + when Events::Text::Putc + props[:on_putc].call event.char + end + end + private def draw