From 7cead58ed35727e452da35f659334aa8f6dd89f8 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Thu, 9 May 2024 12:07:23 +0200 Subject: [PATCH] chore: fix styles Signed-off-by: kjuulh --- crates/hyperlog-tui/src/app.rs | 2 +- crates/hyperlog-tui/src/app/command_bar.rs | 11 ++++--- crates/hyperlog-tui/src/app/dialog.rs | 34 ++++++++++++++++++---- crates/hyperlog-tui/src/models.rs | 2 +- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/crates/hyperlog-tui/src/app.rs b/crates/hyperlog-tui/src/app.rs index 1d5f0ef..3380dcd 100644 --- a/crates/hyperlog-tui/src/app.rs +++ b/crates/hyperlog-tui/src/app.rs @@ -59,7 +59,7 @@ impl<'a> App<'a> { self.command = Some(CommandBarState::default()); self.mode = Mode::Command } - Msg::SubmitCommand => { + Msg::SubmitCommand { command } => { tracing::info!("submitting command"); self.command = None; diff --git a/crates/hyperlog-tui/src/app/command_bar.rs b/crates/hyperlog-tui/src/app/command_bar.rs index 24dc88b..eccb3c6 100644 --- a/crates/hyperlog-tui/src/app/command_bar.rs +++ b/crates/hyperlog-tui/src/app/command_bar.rs @@ -22,16 +22,16 @@ impl Default for CommandBarState { } } -#[derive(Default)] -pub struct CommandBar {} - impl CommandBarState { pub fn update(&mut self, msg: &Msg) -> anyhow::Result { if let Msg::Edit(e) = msg { self.contents.update(e)?; if let EditMsg::InsertNewLine = e { - return Ok(Msg::SubmitCommand.into_command()); + return Ok(Msg::SubmitCommand { + command: self.contents.string(), + } + .into_command()); } } @@ -39,6 +39,9 @@ impl CommandBarState { } } +#[derive(Default)] +pub struct CommandBar {} + impl StatefulWidget for CommandBar { type State = CommandBarState; diff --git a/crates/hyperlog-tui/src/app/dialog.rs b/crates/hyperlog-tui/src/app/dialog.rs index 298a5ce..8441f87 100644 --- a/crates/hyperlog-tui/src/app/dialog.rs +++ b/crates/hyperlog-tui/src/app/dialog.rs @@ -145,11 +145,16 @@ impl Default for InputBuffer { pub struct InputField<'a> { title: &'a str, + + focused: bool, } impl<'a> InputField<'a> { pub fn new(title: &'a str) -> Self { - Self { title } + Self { + title, + focused: false, + } } } @@ -157,7 +162,11 @@ impl<'a> StatefulWidget for InputField<'a> { type State = InputBuffer; fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { - let block = Block::bordered().title(self.title); + let mut block = Block::bordered().title(self.title); + + if !self.focused { + block = block.dark_gray(); + } match &state.state { BufferState::Focused { content, position } => { @@ -165,7 +174,7 @@ impl<'a> StatefulWidget for InputField<'a> { .block(block) .render(area, buf); - buf.get_mut(area.x + 1 + *position as u16, area.y + 1) + buf.get_mut(clamp_x(&area, area.x + 1 + *position as u16), area.y + 1) .set_style(Style::new().bg(Color::Magenta).fg(Color::Black)); } BufferState::Static { content, .. } => { @@ -177,6 +186,14 @@ impl<'a> StatefulWidget for InputField<'a> { } } +fn clamp_x(area: &Rect, x: u16) -> u16 { + if x >= area.width { + area.width - 1 + } else { + x + } +} + enum CreateItemFocused { Title, Description, @@ -239,9 +256,16 @@ impl StatefulWidget for &mut CreateItem { fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { let chunks = Layout::vertical(vec![Constraint::Length(3), Constraint::Length(3)]).split(area); + let mut title_input = InputField::new("title"); + let mut description_input = InputField::new("description"); - InputField::new("title").render(chunks[0], buf, &mut state.title); - InputField::new("description").render(chunks[1], buf, &mut state.description); + match state.focused { + CreateItemFocused::Title => title_input.focused = true, + CreateItemFocused::Description => description_input.focused = true, + } + + title_input.render(chunks[0], buf, &mut state.title); + description_input.render(chunks[1], buf, &mut state.description); // let title = Paragraph::new("something"); //.block(block); diff --git a/crates/hyperlog-tui/src/models.rs b/crates/hyperlog-tui/src/models.rs index 56316dd..f3012c4 100644 --- a/crates/hyperlog-tui/src/models.rs +++ b/crates/hyperlog-tui/src/models.rs @@ -12,7 +12,7 @@ pub enum Msg { EnterViewMode, EnterCommandMode, - SubmitCommand, + SubmitCommand { command: String }, Edit(EditMsg), }