From a63e67b8d9f818804ff6ad777511cf00542fc116 Mon Sep 17 00:00:00 2001 From: tommy Date: Fri, 12 Jul 2019 19:20:38 -0400 Subject: [PATCH] Basic chat window history functionality --- voxygen/src/hud/chat.rs | 66 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/voxygen/src/hud/chat.rs b/voxygen/src/hud/chat.rs index fcdcbb470a..4c376d4bf1 100644 --- a/voxygen/src/hud/chat.rs +++ b/voxygen/src/hud/chat.rs @@ -82,7 +82,9 @@ impl<'a> Chat<'a> { pub struct State { messages: VecDeque, input: String, - + history: VecDeque, + history_pos: usize, + history_max: usize, ids: Ids, } @@ -100,6 +102,9 @@ impl<'a> Widget for Chat<'a> { State { input: "".to_owned(), messages: VecDeque::new(), + history: VecDeque::new(), + history_pos: 0, + history_max: 32, ids: Ids::new(id_gen), } } @@ -266,8 +271,65 @@ impl<'a> Widget for Chat<'a> { }) { let msg = state.input.clone(); - state.update(|s| s.input.clear()); + state.update(|s| { + s.input.clear(); + // update the history + s.history.push_front(msg.clone()); + s.history_pos = 0; + while s.history.len() > s.history_max { + s.history.pop_back(); + } + }); Some(Event::SendMessage(msg)) + } + // If up is pressed, use history + else if ui + .widget_input(state.ids.input) + .presses() + .key() + .any(|key_press| match key_press.key { + Key::Up => true, + _ => false, + }) + { + state.update(|s| { + if !s.history.is_empty() { + if s.history_pos < s.history.len() { + s.history_pos += 1; + if let Some(string) = s.history.get(s.history_pos - 1) { + s.input.clear(); + s.input.push_str(string); + } + } + } + }); + None + } + // If down is pressed, use history + else if ui + .widget_input(state.ids.input) + .presses() + .key() + .any(|key_press| match key_press.key { + Key::Down => true, + _ => false, + }) + { + state.update(|s| { + if !s.history.is_empty() { + if s.history_pos > 1 { + s.history_pos -= 1; + s.input.clear(); + if let Some(string) = s.history.get(s.history_pos - 1) { + s.input.push_str(string); + } + } else { + s.history_pos = 0; + s.input.clear(); + } + } + }); + None } else { None }