wip: styling: toggle bold and italic

This commit is contained in:
Jeremy Wall 2025-02-26 20:00:46 -05:00
parent dae3d71c54
commit ba5ea3c627
3 changed files with 70 additions and 27 deletions

View File

@ -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)) => {

View File

@ -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
}
}

View File

@ -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> {