mirror of
https://github.com/zaphar/sheetsui.git
synced 2025-07-22 04:39:48 -04:00
wip: styling: toggle bold and italic
This commit is contained in:
parent
dae3d71c54
commit
ba5ea3c627
@ -670,28 +670,10 @@ impl<'ws> Workspace<'ws> {
|
|||||||
self.state.char_queue.clear();
|
self.state.char_queue.clear();
|
||||||
}
|
}
|
||||||
KeyCode::Char('b') if key.modifiers.contains(KeyModifiers::CONTROL) => {
|
KeyCode::Char('b') if key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||||
let address = self.book.location.clone();
|
self.toggle_bold()?;
|
||||||
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,
|
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
KeyCode::Char('i') if key.modifiers.contains(KeyModifiers::CONTROL) => {
|
KeyCode::Char('i') if key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||||
let address = self.book.location.clone();
|
self.toggle_italic()?;
|
||||||
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,
|
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
KeyCode::Char(d) if d.is_ascii_digit() => {
|
KeyCode::Char(d) if d.is_ascii_digit() => {
|
||||||
self.handle_numeric_prefix(d);
|
self.handle_numeric_prefix(d);
|
||||||
@ -864,6 +846,44 @@ impl<'ws> Workspace<'ws> {
|
|||||||
return Ok(None);
|
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> {
|
fn paste_range(&mut self) -> Result<(), anyhow::Error> {
|
||||||
match &self.state.clipboard {
|
match &self.state.clipboard {
|
||||||
Some(ClipboardContents::Cell(contents)) => {
|
Some(ClipboardContents::Cell(contents)) => {
|
||||||
|
@ -193,15 +193,28 @@ impl<'ws> Viewport<'ws> {
|
|||||||
ci: usize,
|
ci: usize,
|
||||||
mut cell: Cell<'widget>,
|
mut cell: Cell<'widget>,
|
||||||
) -> Cell<'widget> {
|
) -> Cell<'widget> {
|
||||||
let style = self
|
// TODO(zaphar): Should probably create somekind of formatter abstraction.
|
||||||
|
if let Some(style) = self
|
||||||
.book
|
.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(
|
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),
|
Color::Rgb(35, 33, 54),
|
||||||
);
|
);
|
||||||
let fg_color = map_color(
|
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,
|
Color::White,
|
||||||
);
|
);
|
||||||
if let Some((start, end)) = &self.range_selection.map_or(None, |r| r.get_range()) {
|
if let Some((start, end)) = &self.range_selection.map_or(None, |r| r.get_range()) {
|
||||||
@ -212,12 +225,12 @@ impl<'ws> Viewport<'ws> {
|
|||||||
} else {
|
} else {
|
||||||
cell = cell.bg(bg_color).fg(fg_color);
|
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)),
|
(true, true) => cell.fg(Color::White).bg(Color::Rgb(57, 61, 71)),
|
||||||
// TODO(zaphar): Support ironcalc style options
|
// TODO(zaphar): Support ironcalc style options
|
||||||
_ => cell,
|
_ => cell,
|
||||||
}
|
};
|
||||||
.bold()
|
cell
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1299,6 +1299,11 @@ fn test_bold_text() {
|
|||||||
.get_cell_style(0, &Address { row: 1, col: 1 })
|
.get_cell_style(0, &Address { row: 1, col: 1 })
|
||||||
.expect("Failed to get style");
|
.expect("Failed to get style");
|
||||||
assert!(style.font.b);
|
assert!(style.font.b);
|
||||||
|
script()
|
||||||
|
.ctrl('b')
|
||||||
|
.run(&mut ws)
|
||||||
|
.expect("Unable to run script");
|
||||||
|
assert!(!before_style.font.b);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1318,6 +1323,11 @@ fn test_italic_text() {
|
|||||||
.get_cell_style(0, &Address { row: 1, col: 1 })
|
.get_cell_style(0, &Address { row: 1, col: 1 })
|
||||||
.expect("Failed to get style");
|
.expect("Failed to get style");
|
||||||
assert!(style.font.i);
|
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> {
|
fn new_workspace<'a>() -> Workspace<'a> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user