chore: fix styles

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-05-09 12:07:23 +02:00
parent ed5a5db7c5
commit 7cead58ed3
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
4 changed files with 38 additions and 11 deletions

View File

@ -59,7 +59,7 @@ impl<'a> App<'a> {
self.command = Some(CommandBarState::default()); self.command = Some(CommandBarState::default());
self.mode = Mode::Command self.mode = Mode::Command
} }
Msg::SubmitCommand => { Msg::SubmitCommand { command } => {
tracing::info!("submitting command"); tracing::info!("submitting command");
self.command = None; self.command = None;

View File

@ -22,16 +22,16 @@ impl Default for CommandBarState {
} }
} }
#[derive(Default)]
pub struct CommandBar {}
impl CommandBarState { impl CommandBarState {
pub fn update(&mut self, msg: &Msg) -> anyhow::Result<impl IntoCommand> { pub fn update(&mut self, msg: &Msg) -> anyhow::Result<impl IntoCommand> {
if let Msg::Edit(e) = msg { if let Msg::Edit(e) = msg {
self.contents.update(e)?; self.contents.update(e)?;
if let EditMsg::InsertNewLine = 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 { impl StatefulWidget for CommandBar {
type State = CommandBarState; type State = CommandBarState;

View File

@ -145,11 +145,16 @@ impl Default for InputBuffer {
pub struct InputField<'a> { pub struct InputField<'a> {
title: &'a str, title: &'a str,
focused: bool,
} }
impl<'a> InputField<'a> { impl<'a> InputField<'a> {
pub fn new(title: &'a str) -> Self { 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; type State = InputBuffer;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { 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 { match &state.state {
BufferState::Focused { content, position } => { BufferState::Focused { content, position } => {
@ -165,7 +174,7 @@ impl<'a> StatefulWidget for InputField<'a> {
.block(block) .block(block)
.render(area, buf); .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)); .set_style(Style::new().bg(Color::Magenta).fg(Color::Black));
} }
BufferState::Static { content, .. } => { 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 { enum CreateItemFocused {
Title, Title,
Description, Description,
@ -239,9 +256,16 @@ impl StatefulWidget for &mut CreateItem {
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let chunks = let chunks =
Layout::vertical(vec![Constraint::Length(3), Constraint::Length(3)]).split(area); 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); match state.focused {
InputField::new("description").render(chunks[1], buf, &mut state.description); 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); // let title = Paragraph::new("something"); //.block(block);

View File

@ -12,7 +12,7 @@ pub enum Msg {
EnterViewMode, EnterViewMode,
EnterCommandMode, EnterCommandMode,
SubmitCommand, SubmitCommand { command: String },
Edit(EditMsg), Edit(EditMsg),
} }