Archived
1
0
Fork 0

Fix state for new message input

This commit is contained in:
Braiden Vasco 2017-07-26 06:32:00 +00:00
parent d9e3c66218
commit fe0b90746c
2 changed files with 146 additions and 70 deletions

View file

@ -72,6 +72,10 @@ private
status: friend.status, status: friend.status,
status_message: friend.status_message.freeze, status_message: friend.status_message.freeze,
history: [].freeze, history: [].freeze,
new_message: {
text: '',
cursor_pos: 0,
}.freeze,
] ]
end.to_h.freeze, end.to_h.freeze,
).freeze ).freeze
@ -90,6 +94,10 @@ private
status: friend.status, status: friend.status,
status_message: friend.status_message.freeze, status_message: friend.status_message.freeze,
history: [].freeze, history: [].freeze,
new_message: {
text: '',
cursor_pos: 0,
}.freeze,
}.freeze, }.freeze,
).freeze, ).freeze,
).freeze ).freeze
@ -245,121 +253,189 @@ private
end end
def on_new_message_putc(char) def on_new_message_putc(char)
text = state[:chat][:new_message][:text] return if state[:active_friend_index].nil?
cursor_pos = state[:chat][:new_message][:cursor_pos]
friend_number = state[:friends].keys[state[:active_friend_index]]
return if friend_number.nil?
friend = state[:friends][friend_number]
new_message = friend[:new_message]
text = new_message[:text]
cursor_pos = new_message[:cursor_pos]
text = "#{text[0...cursor_pos]}#{char}#{text[cursor_pos..-1]}"
cursor_pos += 1
@state = state.merge( @state = state.merge(
chat: state[:chat].merge( friends: state[:friends].merge(
new_message: self.class.update_new_message( friend_number => friend.merge(
state[:chat][:new_message], new_message: new_message.merge(
text: "#{text[0...cursor_pos]}#{char}#{text[cursor_pos..-1]}", text: text,
cursor_pos: cursor_pos + 1, cursor_pos: cursor_pos,
), ).freeze,
).freeze,
).freeze, ).freeze,
).freeze ).freeze
end end
def on_new_message_left def on_new_message_left
return if state[:active_friend_index].nil?
friend_number = state[:friends].keys[state[:active_friend_index]]
return if friend_number.nil?
friend = state[:friends][friend_number]
new_message = friend[:new_message]
cursor_pos = new_message[:cursor_pos]
cursor_pos -= 1
cursor_pos = 0 if cursor_pos.negative?
@state = state.merge( @state = state.merge(
chat: state[:chat].merge( friends: state[:friends].merge(
new_message: self.class.update_new_message( friend_number => friend.merge(
state[:chat][:new_message], new_message: new_message.merge(
text: state[:chat][:new_message][:text], cursor_pos: cursor_pos,
cursor_pos: state[:chat][:new_message][:cursor_pos] - 1, ).freeze,
), ).freeze,
).freeze, ).freeze,
).freeze ).freeze
end end
def on_new_message_right def on_new_message_right
return if state[:active_friend_index].nil?
friend_number = state[:friends].keys[state[:active_friend_index]]
return if friend_number.nil?
friend = state[:friends][friend_number]
new_message = friend[:new_message]
text = new_message[:text]
cursor_pos = new_message[:cursor_pos]
cursor_pos += 1
cursor_pos = text.length if cursor_pos > text.length
@state = state.merge( @state = state.merge(
chat: state[:chat].merge( friends: state[:friends].merge(
new_message: self.class.update_new_message( friend_number => friend.merge(
state[:chat][:new_message], new_message: new_message.merge(
text: state[:chat][:new_message][:text], cursor_pos: cursor_pos,
cursor_pos: state[:chat][:new_message][:cursor_pos] + 1, ).freeze,
), ).freeze,
).freeze, ).freeze,
).freeze ).freeze
end end
def on_new_message_home def on_new_message_home
return if state[:active_friend_index].nil?
friend_number = state[:friends].keys[state[:active_friend_index]]
return if friend_number.nil?
friend = state[:friends][friend_number]
new_message = friend[:new_message]
@state = state.merge( @state = state.merge(
chat: state[:chat].merge( friends: state[:friends].merge(
new_message: self.class.update_new_message( friend_number => friend.merge(
state[:chat][:new_message], new_message: new_message.merge(
text: state[:chat][:new_message][:text], cursor_pos: 0,
cursor_pos: 0, ).freeze,
), ).freeze,
).freeze, ).freeze,
).freeze ).freeze
end end
def on_new_message_end def on_new_message_end
return if state[:active_friend_index].nil?
friend_number = state[:friends].keys[state[:active_friend_index]]
return if friend_number.nil?
friend = state[:friends][friend_number]
new_message = friend[:new_message]
@state = state.merge( @state = state.merge(
chat: state[:chat].merge( friends: state[:friends].merge(
new_message: self.class.update_new_message( friend_number => friend.merge(
state[:chat][:new_message], new_message: new_message.merge(
text: state[:chat][:new_message][:text], cursor_pos: new_message[:text].length,
cursor_pos: state[:chat][:new_message][:text].length, ).freeze,
), ).freeze,
).freeze, ).freeze,
).freeze ).freeze
end end
def on_new_message_backspace def on_new_message_backspace
text = state[:chat][:new_message][:text] return if state[:active_friend_index].nil?
cursor_pos = state[:chat][:new_message][:cursor_pos]
friend_number = state[:friends].keys[state[:active_friend_index]]
return if friend_number.nil?
friend = state[:friends][friend_number]
new_message = friend[:new_message]
text = new_message[:text]
cursor_pos = new_message[:cursor_pos]
return unless cursor_pos.positive? return unless cursor_pos.positive?
text = "#{text[0...(cursor_pos - 1)]}#{text[cursor_pos..-1]}"
cursor_pos -= 1
@state = state.merge( @state = state.merge(
chat: state[:chat].merge( friends: state[:friends].merge(
new_message: self.class.update_new_message( friend_number => friend.merge(
state[:chat][:new_message], new_message: new_message.merge(
text: "#{text[0...(cursor_pos - 1)]}#{text[cursor_pos..-1]}", text: text,
cursor_pos: cursor_pos - 1, cursor_pos: cursor_pos,
), ).freeze,
).freeze,
).freeze, ).freeze,
).freeze ).freeze
end end
def on_new_message_delete def on_new_message_delete
text = state[:chat][:new_message][:text] return if state[:active_friend_index].nil?
cursor_pos = state[:chat][:new_message][:cursor_pos]
friend_number = state[:friends].keys[state[:active_friend_index]]
return if friend_number.nil?
friend = state[:friends][friend_number]
new_message = friend[:new_message]
text = new_message[:text]
cursor_pos = new_message[:cursor_pos]
return if cursor_pos > text.length return if cursor_pos > text.length
text = "#{text[0...cursor_pos]}#{text[(cursor_pos + 1)..-1]}"
@state = state.merge( @state = state.merge(
chat: state[:chat].merge( friends: state[:friends].merge(
new_message: self.class.update_new_message( friend_number => friend.merge(
state[:chat][:new_message], new_message: new_message.merge(
text: "#{text[0...cursor_pos]}#{text[(cursor_pos + 1)..-1]}", text: text,
cursor_pos: cursor_pos, cursor_pos: cursor_pos,
), ).freeze,
).freeze,
).freeze, ).freeze,
).freeze ).freeze
end end
####################
# Helper functions #
####################
class << self
def update_new_message(state, text:, cursor_pos:)
if cursor_pos.negative?
cursor_pos = 0
elsif cursor_pos > text.length
cursor_pos = text.length
end
state.merge(
text: text,
cursor_pos: cursor_pos,
).freeze
end
end
######### #########
# State # # State #
######### #########
@ -442,9 +518,6 @@ private
width: Curses.stdscr.maxx - Widgets::Logo::WIDTH, width: Curses.stdscr.maxx - Widgets::Logo::WIDTH,
height: 1, height: 1,
focused: false, focused: false,
text: '',
cursor_pos: 0,
}.freeze, }.freeze,
history: { history: {

View file

@ -34,6 +34,9 @@ module Widgets
on_backspace: props[:on_new_message_backspace], on_backspace: props[:on_new_message_backspace],
on_delete: props[:on_new_message_delete], 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 ).freeze
end end