started decoupling widgets and ui

This commit is contained in:
Thomas Eppers 2021-10-20 15:05:56 +02:00
parent 05a9669fec
commit a7f509b165
4 changed files with 32 additions and 42 deletions

View File

@ -90,10 +90,10 @@ impl Ui {
) )
.split(rect.size()); .split(rect.size());
let (list, state) = ui.services.render(&ui.state); let (list, state) = ui.services.render(ui.state == State::SelectService);
rect.render_stateful_widget(list, chunks[0], state); rect.render_stateful_widget(list, chunks[0], state);
rect.render_widget(ui.repo.render(&ui.state), chunks[1]); rect.render_widget(ui.repo.render(ui.state == State::EditRepo), chunks[1]);
let (list, state) = ui.tags.render(&ui.state); let (list, state) = ui.tags.render(ui.state == State::SelectTag);
rect.render_stateful_widget(list, chunks[2], state); rect.render_stateful_widget(list, chunks[2], state);
rect.render_widget(ui.info.render(), chunks[3]); rect.render_widget(ui.info.render(), chunks[3]);
}) })
@ -145,22 +145,24 @@ impl Ui {
} }
_ => (), _ => (),
}, },
Ok(Key::Char(key)) => { Ok(Key::Char(key)) => match ui.state {
if ui.state == State::EditRepo { State::SelectService => (),
State::EditRepo => {
ui.info.set_info("Editing Repository"); ui.info.set_info("Editing Repository");
ui.repo.handle_input(Key::Char(key));
} }
ui.repo.handle_input(&ui.state, Key::Char(key)); State::SelectTag => (),
ui.tags.handle_input(&ui.state, Key::Char(key)); },
} Ok(Key::Backspace) => match ui.state {
Ok(Key::Backspace) => { State::SelectService => (),
if ui.state == State::EditRepo { State::EditRepo => {
ui.info.set_info("Editing Repository"); ui.info.set_info("Editing Repository");
ui.repo.handle_input(Key::Backspace);
} }
ui.repo.handle_input(&ui.state, Key::Backspace); State::SelectTag => (),
ui.tags.handle_input(&ui.state, Key::Backspace); },
} Ok(Key::Up) => match ui.state {
Ok(Key::Up) => { State::SelectService if ui.services.find_previous_match() => {
if ui.state == State::SelectService && ui.services.find_previous_match() {
match ui.services.extract_repo() { match ui.services.extract_repo() {
Err(e) => ui.info.set_info(&format!("{}", e)), Err(e) => ui.info.set_info(&format!("{}", e)),
Ok(s) => { Ok(s) => {
@ -176,9 +178,10 @@ impl Ui {
} }
} }
} }
ui.tags.handle_input(&ui.state, Key::Up); State::SelectService => (),
ui.repo.handle_input(&ui.state, Key::Up); State::EditRepo => (),
} State::SelectTag => ui.tags.handle_input(Key::Up),
},
Ok(Key::Down) => match ui.state { Ok(Key::Down) => match ui.state {
State::SelectService if ui.services.find_next_match() => { State::SelectService if ui.services.find_next_match() => {
match ui.services.extract_repo() { match ui.services.extract_repo() {
@ -196,15 +199,10 @@ impl Ui {
} }
} }
} }
_ => { State::SelectService => (),
ui.tags.handle_input(&ui.state, Key::Down); State::EditRepo => (),
ui.repo.handle_input(&ui.state, Key::Down); State::SelectTag => ui.tags.handle_input(Key::Down),
}
}, },
Ok(key) => {
ui.repo.handle_input(&ui.state, Key::Down);
ui.tags.handle_input(&ui.state, key);
}
_ => (), _ => (),
} }

View File

@ -29,13 +29,13 @@ impl RepoEntry {
self.old_text = entry; self.old_text = entry;
} }
pub fn render(&self, state: &crate::ui::State) -> Paragraph { pub fn render(&self, colored: bool) -> Paragraph {
let title = match self.changed { let title = match self.changed {
true => "Repository*", true => "Repository*",
false => "Repository", false => "Repository",
}; };
let border_style = if state == &crate::ui::State::EditRepo { let border_style = if colored {
Style::default().fg(Color::Green) Style::default().fg(Color::Green)
} else { } else {
Style::default().fg(Color::Gray) Style::default().fg(Color::Gray)
@ -52,11 +52,7 @@ impl RepoEntry {
.alignment(Alignment::Left) .alignment(Alignment::Left)
} }
pub fn handle_input(&mut self, state: &State, key: termion::event::Key) { pub fn handle_input(&mut self, key: termion::event::Key) {
if state != &State::EditRepo {
return;
}
match key { match key {
// Key::Char('\n') => self.confirm(), //handled in Ui // Key::Char('\n') => self.confirm(), //handled in Ui
Key::Char(c) => { Key::Char(c) => {

View File

@ -72,8 +72,8 @@ impl ServiceSwitcher {
} }
} }
pub fn render(&mut self, state: &State) -> (List, &mut ListState) { pub fn render(&mut self, colored: bool) -> (List, &mut ListState) {
let border_style = if state == &State::SelectService { let border_style = if colored {
Style::default().fg(Color::Green) Style::default().fg(Color::Green)
} else { } else {
Style::default().fg(Color::Gray) Style::default().fg(Color::Gray)

View File

@ -109,8 +109,8 @@ impl TagList {
} }
} }
pub fn render(&mut self, state: &State) -> (List, &mut ListState) { pub fn render(&mut self, colored: bool) -> (List, &mut ListState) {
let border_style = if state == &State::SelectTag { let border_style = if colored {
Style::default().fg(Color::Green) Style::default().fg(Color::Green)
} else { } else {
Style::default().fg(Color::Gray) Style::default().fg(Color::Gray)
@ -144,11 +144,7 @@ impl TagList {
(items, &mut self.state) (items, &mut self.state)
} }
pub fn handle_input(&mut self, state: &State, key: termion::event::Key) { pub fn handle_input(&mut self, key: termion::event::Key) {
if state != &State::SelectTag {
return;
}
match key { match key {
Key::Down => self.next(), Key::Down => self.next(),
Key::Up => self.previous(), Key::Up => self.previous(),