Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions crates/engine/src/cmd/sheet/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,13 @@ pub(super) fn move_sheet(s: &mut Session, p: &Json) -> Result<Json> {

pub(super) fn tab_color(s: &mut Session, p: &Json) -> Result<Json> {
let i = target_sheet(s, p)?;
let c = crate::cmd::format::color_param(p.get("color"));
// The Tab Color keytip omits color to clear it.
let c = match p.get("color") {
None => gridcraft_model::Color::Auto,
Some(value) => crate::cmd::format::color_param(Some(value)).ok_or_else(|| bad("sheet.tabColor", "invalid `color`"))?,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
};
edit(s, |cx| {
cx.sheet_mut(i)?.tab_color = c.filter(|c| *c != gridcraft_model::Color::Auto);
cx.sheet_mut(i)?.tab_color = (c != gridcraft_model::Color::Auto).then_some(c);
Ok(Json::Null)
})
}
Expand Down Expand Up @@ -294,3 +298,67 @@ pub(super) fn step_sheet(s: &mut Session, dir: isize) -> Result<Json> {
// No visible sheet in this direction: keep the current sheet and its selection.
ok()
}

#[cfg(test)]
mod tests {
use super::*;
use gridcraft_model::Color;

#[test]
fn invalid_tab_colors_preserve_the_existing_color_and_history() {
let mut s = Session::new();
s.new_workbook();
s.execute("sheet.tabColor", json!({"color": "#FF0000"})).unwrap();
s.execute("cell.set", json!({"cell": "C1", "input": "redo marker"})).unwrap();
s.execute("edit.undo", json!({})).unwrap();
let (before, revision, undo, redo) = {
let doc = s.doc().unwrap();
assert_eq!(doc.wb.active().unwrap().tab_color, Some(Color::Rgb(0xFF0000)));
assert!(!doc.undo.is_empty() && !doc.redo.is_empty());
(doc.wb.clone(), doc.revision, doc.undo.len(), doc.redo.len())
};
for params in [
json!({"color": "#GG0000"}),
json!({"color": "#GGFF0000"}),
json!({"color": ""}),
json!({"color": null}),
json!({"color": false}),
json!({"color": 7}),
json!({"color": []}),
json!({"color": {}}),
json!({"color": {"theme": "bad"}}),
] {
let error = s.execute("sheet.tabColor", params.clone()).unwrap_err().to_string();
assert!(error.contains("invalid `color`"), "{params}: {error}");
let doc = s.doc().unwrap();
assert_eq!(*doc.wb, *before, "{params}");
assert_eq!((doc.revision, doc.undo.len(), doc.redo.len()), (revision, undo, redo), "{params}");
}
s.execute("edit.redo", json!({})).unwrap();
assert_eq!(s.doc().unwrap().wb.active().unwrap().value(CellRef::new(0, 2)), gridcraft_core::Value::from("redo marker"));
}

#[test]
fn tab_colors_still_accept_rgb_theme_and_clearing() {
let mut s = Session::new();
s.new_workbook();
for (color, expected) in [
(json!("#00FF00"), Color::Rgb(0x00FF00)),
(json!("#80FF0000"), Color::Rgb(0xFF0000)),
(json!({"theme": 3, "tint": 0.4}), Color::Theme(3, 400)),
] {
s.execute("sheet.tabColor", json!({"color": color})).unwrap();
assert_eq!(s.doc().unwrap().wb.active().unwrap().tab_color, Some(expected));
}
for clear in ["none", "auto", "automatic", "NoNe"] {
s.execute("sheet.tabColor", json!({"color": "#FF0000"})).unwrap();
s.execute("sheet.tabColor", json!({"color": clear})).unwrap();
assert_eq!(s.doc().unwrap().wb.active().unwrap().tab_color, None, "{clear}");
}
s.execute("sheet.tabColor", json!({"color": "#FF0000"})).unwrap();
s.journal.clear();
s.execute("sheet.tabColor", json!({})).unwrap();
assert_eq!(s.doc().unwrap().wb.active().unwrap().tab_color, None, "omitted color still clears the tab");
assert!(s.journal.iter().any(|(id, _)| id == "sheet.tabColor"), "menu-style invocation is still journaled");
}
}
14 changes: 11 additions & 3 deletions crates/model/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ impl Color {
Color::Rgb(((r as u32) << 16) | ((g as u32) << 8) | b as u32)
}
pub fn from_hex(s: &str) -> Option<Color> {
let h = s.trim().trim_start_matches('#');
let h = if h.len() == 8 { h.get(2..)? } else { h };
if h.len() != 6 {
let h = s.trim();
let h = h.strip_prefix('#').unwrap_or(h);
// Alpha is ignored for RGB colors, but its digits must still be valid hexadecimal.
if !matches!(h.len(), 6 | 8) || !h.bytes().all(|b| b.is_ascii_hexdigit()) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return None;
}
let h = if h.len() == 8 { h.get(2..)? } else { h };
u32::from_str_radix(h, 16).ok().map(Color::Rgb)
}
/// Resolved RGB, `None` for automatic.
Expand Down Expand Up @@ -543,6 +545,12 @@ mod tests {
let th = Theme::default();
assert_eq!(Color::from_hex("#FF0000").ok_or("none")?.resolve(&th), Some([255, 0, 0]));
assert_eq!(Color::from_hex("FF00FF00").ok_or("none")?.resolve(&th), Some([0, 255, 0]));
assert_eq!(Color::from_hex("#80FF0000"), Some(Color::Rgb(0xFF0000)));
assert_eq!(Color::from_hex(" ff0000 "), Some(Color::Rgb(0xFF0000)));
assert_eq!(Color::from_hex(" #80ff0000 "), Some(Color::Rgb(0xFF0000)));
for invalid in ["#GGFF0000", "#0GFF0000", "#éFF0000", "#+F0000", "##FF0000", "##80FF0000", "###FF0000"] {
assert_eq!(Color::from_hex(invalid), None, "{invalid}");
}
assert_eq!(Color::Theme(0, -500).resolve(&th), Some([128, 128, 128]));
assert_eq!(Color::Theme(1, 500).resolve(&th), Some([128, 128, 128]));
assert_eq!(Color::Auto.resolve(&th), None);
Expand Down
Loading