From ba5ea3c6275885733bfbe6fdc016e7e32f8a7359 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Wed, 26 Feb 2025 20:00:46 -0500 Subject: [PATCH] wip: styling: toggle bold and italic --- src/ui/mod.rs | 60 ++++++++++++++++++++++++++------------- src/ui/render/viewport.rs | 27 +++++++++++++----- src/ui/test.rs | 10 +++++++ 3 files changed, 70 insertions(+), 27 deletions(-) diff --git a/src/ui/mod.rs b/src/ui/mod.rs index f334de8..8ee1ae4 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -670,28 +670,10 @@ impl<'ws> Workspace<'ws> { self.state.char_queue.clear(); } KeyCode::Char('b') if key.modifiers.contains(KeyModifiers::CONTROL) => { - let address = self.book.location.clone(); - self.book.set_cell_style( - &[("font.b", "true")], - &Area { - sheet: self.book.current_sheet, - row: address.row as i32, - column: address.col as i32, - width: 1, - height: 1, - })?; + self.toggle_bold()?; } KeyCode::Char('i') if key.modifiers.contains(KeyModifiers::CONTROL) => { - let address = self.book.location.clone(); - self.book.set_cell_style( - &[("font.i", "true")], - &Area { - sheet: self.book.current_sheet, - row: address.row as i32, - column: address.col as i32, - width: 1, - height: 1, - })?; + self.toggle_italic()?; } KeyCode::Char(d) if d.is_ascii_digit() => { self.handle_numeric_prefix(d); @@ -864,6 +846,44 @@ impl<'ws> Workspace<'ws> { return Ok(None); } + fn toggle_italic(&mut self) -> Result<(), anyhow::Error> { + let address = self.book.location.clone(); + let value = if let Some(style) = self.book.get_cell_style(self.book.current_sheet, &address) { + if style.font.i { "false" } else { "true" } + } else { + "true" + }; + self.book.set_cell_style( + &[("font.i", value)], + &Area { + sheet: self.book.current_sheet, + row: address.row as i32, + column: address.col as i32, + width: 1, + height: 1, + })?; + Ok(()) + } + + fn toggle_bold(&mut self) -> Result<(), anyhow::Error> { + let address = self.book.location.clone(); + let value = if let Some(style) = self.book.get_cell_style(self.book.current_sheet, &address) { + if style.font.b { "false" } else { "true" } + } else { + "true" + }; + self.book.set_cell_style( + &[("font.b", value)], + &Area { + sheet: self.book.current_sheet, + row: address.row as i32, + column: address.col as i32, + width: 1, + height: 1, + })?; + Ok(()) + } + fn paste_range(&mut self) -> Result<(), anyhow::Error> { match &self.state.clipboard { Some(ClipboardContents::Cell(contents)) => { diff --git a/src/ui/render/viewport.rs b/src/ui/render/viewport.rs index e3bf60e..847951a 100644 --- a/src/ui/render/viewport.rs +++ b/src/ui/render/viewport.rs @@ -193,15 +193,28 @@ impl<'ws> Viewport<'ws> { ci: usize, mut cell: Cell<'widget>, ) -> Cell<'widget> { - let style = self + // TODO(zaphar): Should probably create somekind of formatter abstraction. + if let Some(style) = self .book - .get_cell_style(self.book.current_sheet, &Address { row: ri, col: ci }); + .get_cell_style(self.book.current_sheet, &Address { row: ri, col: ci }) { + cell = self.compute_cell_colors(&style, ri, ci, cell); + cell = if style.font.b { + cell.bold() + } else { cell }; + cell = if style.font.i { + cell.italic() + } else { cell }; + } + cell + } + + fn compute_cell_colors<'widget>(&self, style: &ironcalc::base::types::Style, ri: usize, ci: usize, mut cell: Cell<'widget>) -> Cell<'widget> { let bg_color = map_color( - style.as_ref().map(|s| s.fill.bg_color.as_ref()).flatten(), + style.fill.bg_color.as_ref(), Color::Rgb(35, 33, 54), ); let fg_color = map_color( - style.as_ref().map(|s| s.fill.fg_color.as_ref()).flatten(), + style.fill.fg_color.as_ref(), Color::White, ); if let Some((start, end)) = &self.range_selection.map_or(None, |r| r.get_range()) { @@ -212,12 +225,12 @@ impl<'ws> Viewport<'ws> { } else { cell = cell.bg(bg_color).fg(fg_color); } - match (self.book.location.row == ri, self.book.location.col == ci) { + cell = match (self.book.location.row == ri, self.book.location.col == ci) { (true, true) => cell.fg(Color::White).bg(Color::Rgb(57, 61, 71)), // TODO(zaphar): Support ironcalc style options _ => cell, - } - .bold() + }; + cell } } diff --git a/src/ui/test.rs b/src/ui/test.rs index 21cc249..f9568e7 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -1299,6 +1299,11 @@ fn test_bold_text() { .get_cell_style(0, &Address { row: 1, col: 1 }) .expect("Failed to get style"); assert!(style.font.b); + script() + .ctrl('b') + .run(&mut ws) + .expect("Unable to run script"); + assert!(!before_style.font.b); } #[test] @@ -1318,6 +1323,11 @@ fn test_italic_text() { .get_cell_style(0, &Address { row: 1, col: 1 }) .expect("Failed to get style"); assert!(style.font.i); + script() + .ctrl('i') + .run(&mut ws) + .expect("Unable to run script"); + assert!(!before_style.font.i); } fn new_workspace<'a>() -> Workspace<'a> {