Skip to content

Commit

Permalink
tables: handle define window style of 0 without panic
Browse files Browse the repository at this point in the history
The 0th style has different meanings depending on if this is the first
define window or a subsequent define window.  Use the first define
window by default.
  • Loading branch information
ystreet committed Apr 10, 2024
1 parent b706442 commit b714f41
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,17 +534,17 @@ impl DefineWindowArgs {

/// Retrieve the default window attributes for this [`DefineWindowArgs`]
pub fn window_attributes(&self) -> SetWindowAttributesArgs {
PREDEFINED_WINDOW_STYLES[self.window_style_id as usize - 1]
PREDEFINED_WINDOW_STYLES[self.window_style_id.max(1) as usize - 1]
}

/// Retrieve the default pen attributes for this [`DefineWindowArgs`]
pub fn pen_attributes(&self) -> SetPenAttributesArgs {
PREDEFINED_PEN_STYLES_ATTRIBUTES[self.pen_style_id as usize - 1]
PREDEFINED_PEN_STYLES_ATTRIBUTES[self.pen_style_id.max(1) as usize - 1]
}

/// Retrieve the default pen color for this [`DefineWindowArgs`]
pub fn pen_color(&self) -> SetPenColorArgs {
PREDEFINED_PEN_STYLES_COLOR[self.pen_style_id as usize - 1]
PREDEFINED_PEN_STYLES_COLOR[self.pen_style_id.max(1) as usize - 1]
}
}

Expand All @@ -563,7 +563,7 @@ static PREDEFINED_WINDOW_STYLES: [SetWindowAttributesArgs; 7] = [
border_type: BorderType::None,
border_color: Color::BLACK,
},
// style w
// style 2
SetWindowAttributesArgs {
justify: Justify::Left,
print_direction: Direction::LeftToRight,
Expand Down Expand Up @@ -2178,4 +2178,31 @@ mod test {
}
}
}

#[test]
fn define_zero_style_id() {
test_init_log();
let define = DefineWindowArgs::new(
0,
0,
Anchor::BottomMiddle,
true,
100,
50,
11,
31,
true,
true,
true,
0,
0,
);
let win_attrs = define.window_attributes();
assert_eq!(win_attrs.fill_opacity, Opacity::Solid);
assert_eq!(win_attrs.fill_color, Color::BLACK);
let pen_attrs = define.pen_attributes();
assert_eq!(pen_attrs.font_style, FontStyle::Default);
let pen_color = define.pen_color();
assert_eq!(pen_color, PREDEFINED_PEN_STYLES_COLOR[0]);
}
}

0 comments on commit b714f41

Please sign in to comment.