refactor: reduce cognitive complexity in five functions (S3776)

This commit is contained in:
Dylan M. Taylor
2026-10-26 16:01:53 -04:00
committed by GitHub
parent aaf6c1c38a
commit 4bb73d5d34
6 changed files with 301 additions and 236 deletions
+86 -65
View File
@@ -147,6 +147,86 @@ pub fn look(ctx: &Ctx, t: &Table, r: usize, c: usize) -> CellLook {
look_with_def(ctx, resolve_style(ctx, t), t, r, c)
}
/// Which parts of the table a cell sits in, for the preset styles.
#[derive(Clone, Copy)]
struct PresetPart {
header: bool,
total: bool,
first_col: bool,
last_col: bool,
banded: bool,
}
impl PresetPart {
/// In a header, total, first or last column: the parts a preset style makes strong.
fn strong(self) -> bool {
self.header || self.total || self.first_col || self.last_col
}
}
/// Tint `k` of accent `a`.
fn tint(a: Rgba, k: i32) -> Rgba {
deckcraft_color::apply(a, &[ColorTransform::Tint(k)])
}
fn light1_look(a: Rgba, p: PresetPart, on: impl Fn(Rgba) -> Rgba) -> CellLook {
let fill = p.banded.then(|| tint(a, 20000));
CellLook { fill, text: fill.map(on), bold: p.strong(), border: if p.header { Some((a, 1.0)) } else { None }, sides: [None; 4], paint: None }
}
fn light2_look(a: Rgba, p: PresetPart) -> CellLook {
CellLook {
fill: if p.header { Some(a) } else { None },
text: if p.header { Some(Rgba::WHITE) } else { None },
bold: p.header || p.total || p.first_col,
border: Some((a, 1.0)),
sides: [None; 4],
paint: None,
}
}
fn dark1_look(a: Rgba, p: PresetPart) -> CellLook {
CellLook {
fill: Some(if p.header {
Rgba::BLACK
} else if p.banded {
deckcraft_color::apply(a, &[ColorTransform::Shade(60000)])
} else {
deckcraft_color::apply(a, &[ColorTransform::Shade(75000)])
}),
text: Some(Rgba::WHITE),
bold: p.header || p.total || p.first_col,
border: Some((Rgba::WHITE, 1.0)),
sides: [None; 4],
paint: None,
}
}
fn medium4_look(a: Rgba, p: PresetPart, on: impl Fn(Rgba) -> Rgba) -> CellLook {
let fill = if p.header || p.banded { tint(a, 40000) } else { tint(a, 20000) };
CellLook { fill: Some(fill), text: Some(on(fill)), bold: p.strong(), border: Some((a, 0.75)), sides: [None; 4], paint: None }
}
/// medium2: strong header, banded light tints, white grid.
fn medium2_look(a: Rgba, p: PresetPart, on: impl Fn(Rgba) -> Rgba) -> CellLook {
let strong = p.strong();
let fill = if strong {
a
} else if p.banded {
tint(a, 40000)
} else {
tint(a, 20000)
};
CellLook {
fill: Some(fill),
text: Some(if strong { Rgba::WHITE } else { on(fill) }),
bold: strong,
border: Some((Rgba::WHITE, 1.0)),
sides: [None; 4],
paint: None,
}
}
pub fn look_with_def(ctx: &Ctx, def: Option<&TableStyleDef>, t: &Table, r: usize, c: usize) -> CellLook {
if let Some(def) = def {
return look_def(ctx, def, t, r, c);
@@ -160,8 +240,7 @@ pub fn look_with_def(ctx: &Ctx, def: Option<&TableStyleDef>, t: &Table, r: usize
let body_r = if t.first_row { r.wrapping_sub(1) } else { r };
let band = t.band_row && !header && !total && body_r % 2 == 0;
let band_c = t.band_col && c.is_multiple_of(2);
let tint = |k: i32| deckcraft_color::apply(a, &[ColorTransform::Tint(k)]);
let white = Rgba::WHITE;
let p = PresetPart { header, total, first_col, last_col, banded: band || band_c };
let text_color = ctx.color(&ColorRef::scheme(SchemeSlot::Tx1), None);
// Text on a tinted cell never inherits the text colour, which a dark theme makes light and
// unreadable on the light tint: PowerPoint's medium styles set it to the theme's dark colour
@@ -173,69 +252,11 @@ pub fn look_with_def(ctx: &Ctx, def: Option<&TableStyleDef>, t: &Table, r: usize
match kind {
"none" => CellLook { fill: None, text: None, bold: false, border: None, sides: [None; 4], paint: None },
"grid" => CellLook { fill: None, text: None, bold: false, border: Some((text_color, 0.75)), sides: [None; 4], paint: None },
"light1" => {
let fill = (band || band_c).then(|| tint(20000));
CellLook {
fill,
text: fill.map(on),
bold: header || total || first_col || last_col,
border: if header { Some((a, 1.0)) } else { None },
sides: [None; 4],
paint: None,
}
}
"light2" => CellLook {
fill: if header { Some(a) } else { None },
text: if header { Some(white) } else { None },
bold: header || total || first_col,
border: Some((a, 1.0)),
sides: [None; 4],
paint: None,
},
"dark1" => CellLook {
fill: Some(if header {
Rgba::BLACK
} else if band || band_c {
deckcraft_color::apply(a, &[ColorTransform::Shade(60000)])
} else {
deckcraft_color::apply(a, &[ColorTransform::Shade(75000)])
}),
text: Some(white),
bold: header || total || first_col,
border: Some((white, 1.0)),
sides: [None; 4],
paint: None,
},
"medium4" => {
let fill = if header || band || band_c { tint(40000) } else { tint(20000) };
CellLook {
fill: Some(fill),
text: Some(on(fill)),
bold: header || total || first_col || last_col,
border: Some((a, 0.75)),
sides: [None; 4],
paint: None,
}
}
_ => {
// medium2: strong header, banded light tints, white grid.
let strong = header || total || first_col || last_col;
let fill = if strong {
a
} else if band || band_c {
tint(40000)
} else {
tint(20000)
};
CellLook {
fill: Some(fill),
text: Some(if strong { white } else { on(fill) }),
bold: strong,
border: Some((white, 1.0)),
sides: [None; 4],
paint: None,
}
}
"light1" => light1_look(a, p, on),
"light2" => light2_look(a, p),
"dark1" => dark1_look(a, p),
"medium4" => medium4_look(a, p, on),
_ => medium2_look(a, p, on),
}
}
+37
View File
@@ -1439,6 +1439,43 @@ fn table_body_text_is_dark_on_the_tinted_cells_of_a_dark_theme() {
}
}
#[test]
fn table_light_and_dark_presets_give_their_own_header_and_body_looks() {
// light1, light2 and dark1 each style the header and banded body cells their own way.
use deckcraft_color::{ColorTransform, Rgba, SchemeSlot};
use deckcraft_model::style::ColorRef;
let p = Presentation::default();
let ctx = Ctx::for_slide(&p, &p.slides[0]).unwrap();
let a = ctx.color(&ColorRef::scheme(SchemeSlot::Accent1), None);
let look = |style: &str, r: usize| {
let mut t = Table::new(3, 1, 100.0, 90.0);
t.style = style.to_string();
t.first_row = true;
t.band_row = true;
crate::table::look_with_def(&ctx, None, &t, r, 0)
};
// light1: header has no fill, bold text and an accent border; banded body rows get a light tint.
let h = look("light1", 0);
assert_eq!((h.fill, h.bold, h.border), (None, true, Some((a, 1.0))));
let b = look("light1", 1);
assert_eq!(b.fill, Some(deckcraft_color::apply(a, &[ColorTransform::Tint(20000)])));
assert!(b.text.is_some() && !b.bold);
// light2: accent-filled header with white bold text; body has no fill.
let h = look("light2", 0);
assert_eq!((h.fill, h.text, h.bold, h.border), (Some(a), Some(Rgba::WHITE), true, Some((a, 1.0))));
let b = look("light2", 1);
assert_eq!((b.fill, b.text, b.bold), (None, None, false));
// dark1: black header, shaded accent body (darker when banded), white text and white borders.
let h = look("dark1", 0);
assert_eq!((h.fill, h.text, h.bold, h.border), (Some(Rgba::BLACK), Some(Rgba::WHITE), true, Some((Rgba::WHITE, 1.0))));
let b = look("dark1", 1);
assert_eq!(b.fill, Some(deckcraft_color::apply(a, &[ColorTransform::Shade(60000)])));
assert_eq!(b.text, Some(Rgba::WHITE));
}
/// A white 280x200 pt rectangle at (20, 20) with `effects`, rendered at 1 px/pt.
fn effect_box(effects: deckcraft_model::Effects) -> Image {
use deckcraft_model::style::{Fill, Line};