Made state a private field to reduce possible side effects

This commit is contained in:
Xithrius 2022-12-19 21:37:55 -08:00
parent 3d6d8b9701
commit dd09ce2d4e
No known key found for this signature in database
GPG Key ID: A867F27CC80B28C1
5 changed files with 31 additions and 25 deletions

View File

@ -107,7 +107,7 @@ pub struct App {
/// Messages to be filtered out
pub filters: Filters,
/// Which window the terminal is currently focused on
pub state: State,
state: State,
/// What the user currently has inputted
pub input_buffer: LineBuffer,
/// The current suggestion, if any
@ -145,6 +145,14 @@ impl App {
self.scrolling.jump_to(0);
}
pub fn get_state(&self) -> State {
self.state.clone()
}
pub fn set_state(&mut self, other: State) {
self.state = other;
}
#[allow(dead_code)]
pub fn rotate_theme(&mut self) {
todo!("Rotate through different themes")

View File

@ -145,7 +145,7 @@ impl Default for FrontendConfig {
title_shown: true,
margin: 0,
badges: false,
theme: Theme::Dark,
theme: Theme::default(),
username_highlight: true,
state_tabs: false,
cursor_shape: CursorType::default(),

View File

@ -48,7 +48,7 @@ async fn handle_insert_enter_key(action: &mut UserActionAttributes<'_, '_>) {
tx,
} = action;
match app.state {
match app.get_state() {
State::Insert => {
let input_message = &mut app.input_buffer;
@ -105,7 +105,7 @@ async fn handle_insert_enter_key(action: &mut UserActionAttributes<'_, '_>) {
input_message.update("", 0);
app.state = State::Normal;
app.set_state(State::Normal);
}
_ => {}
}
@ -123,8 +123,8 @@ async fn handle_insert_type_movements(action: &mut UserActionAttributes<'_, '_>)
match key {
Key::Up => {
if app.state == State::Insert {
app.state = State::Normal;
if app.get_state() == State::Insert {
app.set_state(State::Normal);
}
}
Key::Ctrl('f') | Key::Right => {
@ -180,14 +180,14 @@ async fn handle_insert_type_movements(action: &mut UserActionAttributes<'_, '_>)
}
Key::Esc => {
input_buffer.update("", 0);
app.state = State::Normal;
app.set_state(State::Normal);
}
_ => {}
}
}
fn handle_user_scroll(app: &mut App, key: Key) {
match app.state {
match app.get_state() {
State::Insert | State::MessageSearch | State::Normal => {
let limit = app.scrolling.get_offset() < app.messages.len();
@ -224,7 +224,7 @@ pub async fn handle_stateful_user_input(
if let Some(Event::Input(key)) = events.next().await {
handle_user_scroll(app, key);
match app.state {
match app.get_state() {
State::Insert | State::ChannelSwitch | State::MessageSearch => {
let mut action = UserActionAttributes::new(app, config, tx, key);
@ -232,13 +232,13 @@ pub async fn handle_stateful_user_input(
}
_ => match key {
Key::Char('c') => {
app.state = State::Normal;
app.set_state(State::Normal);
}
Key::Char('s') => {
app.state = State::ChannelSwitch;
app.set_state(State::ChannelSwitch);
}
Key::Ctrl('f') => {
app.state = State::MessageSearch;
app.set_state(State::MessageSearch);
}
Key::Ctrl('t') => {
app.filters.toggle();
@ -247,23 +247,23 @@ pub async fn handle_stateful_user_input(
app.filters.reverse();
}
Key::Char('i') | Key::Insert => {
app.state = State::Insert;
app.set_state(State::Insert);
}
Key::Char('@' | '/') => {
app.state = State::Insert;
app.set_state(State::Insert);
app.input_buffer.update(&key.to_string(), 1);
}
Key::Ctrl('p') => {
panic!("Manual panic triggered by user.");
}
Key::Char('?') => app.state = State::Help,
Key::Char('?') => app.set_state(State::Help),
Key::Char('q') => {
return Some(TerminalAction::Quitting);
}
Key::Esc => {
app.scrolling.jump_to(0);
app.state = State::Normal;
app.set_state(State::Normal);
}
_ => {}
},

View File

@ -94,7 +94,7 @@ pub fn render_insert_box<T: Backend>(
((cursor_pos + 3) as u16).saturating_sub(input_rect.width),
));
if matches!(app.state, State::ChannelSwitch) {
if matches!(app.get_state(), State::ChannelSwitch) {
frame.render_widget(Clear, input_rect);
}

View File

@ -102,7 +102,7 @@ pub fn draw_ui<T: Backend>(frame: &mut Frame<T>, app: &mut App, config: &Complet
// Constraints for different states of the application.
// Modify this in order to create new layouts.
let mut v_constraints = match app.state {
let mut v_constraints = match app.get_state() {
State::Insert | State::MessageSearch => vec![Constraint::Min(1), Constraint::Length(3)],
_ => vec![Constraint::Min(1)],
};
@ -154,15 +154,13 @@ pub fn draw_ui<T: Backend>(frame: &mut Frame<T>, app: &mut App, config: &Complet
continue;
}
let buffer = &mut app.input_buffer;
let username_highlight = if config.frontend.username_highlight {
Some(config.twitch.username.clone())
} else {
None
};
let (rows, num_results) = if buffer.is_empty() {
let (rows, num_results) = if app.input_buffer.is_empty() {
data.to_row_and_num_search_results(
&config.frontend,
message_chunk_width,
@ -174,8 +172,8 @@ pub fn draw_ui<T: Backend>(frame: &mut Frame<T>, app: &mut App, config: &Complet
data.to_row_and_num_search_results(
&config.frontend,
message_chunk_width,
match app.state {
State::MessageSearch => Some(buffer.to_string()),
match &app.get_state() {
State::MessageSearch => Some(app.input_buffer.to_string()),
_ => None,
},
username_highlight,
@ -248,12 +246,12 @@ pub fn draw_ui<T: Backend>(frame: &mut Frame<T>, app: &mut App, config: &Complet
frame.render_widget(table, layout.first_chunk());
if config.frontend.state_tabs {
components::render_state_tabs(frame, &layout, &app.state);
components::render_state_tabs(frame, &layout, &app.get_state());
}
let window = WindowAttributes::new(frame, app, layout, config.frontend.state_tabs);
match window.app.state {
match window.app.get_state() {
// States of the application that require a chunk of the main window
State::Insert => components::render_chat_box(window, config.storage.mentions),
State::MessageSearch => {