use std::panic::Location;
use crate::style::StyleProfile;
use crate::tokens;
use crate::tree::*;
use crate::widgets::text::text;
#[track_caller]
pub fn code_block(s: impl Into<String>) -> El {
let loc = Location::caller();
let body = text(s)
.at_loc(loc)
.mono()
.font_size(tokens::TEXT_SM.size)
.nowrap_text()
.width(Size::Hug)
.height(Size::Hug);
code_block_chrome_at(body, loc)
}
#[track_caller]
pub fn code_block_chrome(body: El) -> El {
code_block_chrome_at(body, Location::caller())
}
fn code_block_chrome_at(body: El, loc: &'static Location<'static>) -> El {
column([body])
.at_loc(loc)
.style_profile(StyleProfile::Surface)
.surface_role(SurfaceRole::Sunken)
.fill(tokens::MUTED)
.stroke(tokens::BORDER)
.default_radius(tokens::RADIUS_MD)
.default_padding(Sides::all(tokens::SPACE_3))
.width(Size::Fill(1.0))
.height(Size::Hug)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn code_block_wraps_mono_body_in_sunken_surface() {
let block = code_block("fn main() {\n println!(\"hi\");\n}");
assert_eq!(block.kind, Kind::Group);
assert_eq!(block.axis, Axis::Column);
assert_eq!(block.style_profile, StyleProfile::Surface);
assert_eq!(block.surface_role, SurfaceRole::Sunken);
assert_eq!(block.fill, Some(tokens::MUTED));
assert_eq!(block.stroke, Some(tokens::BORDER));
assert_eq!(block.padding, Sides::all(tokens::SPACE_3));
assert_eq!(block.width, Size::Fill(1.0));
assert_eq!(block.children.len(), 1);
let body = &block.children[0];
assert_eq!(body.kind, Kind::Text);
assert!(body.font_mono);
assert_eq!(body.font_size, tokens::TEXT_SM.size);
assert_eq!(body.text_wrap, TextWrap::NoWrap);
assert_eq!(
body.text.as_deref(),
Some("fn main() {\n println!(\"hi\");\n}")
);
}
}