libbhoo 0.3.1

Minimal UI foundations for SpectreOS
Documentation
1
2
3
4
5
6
7
use bhoo::*;
const B:Rect=Rect{x:0.,y:0.,width:240.,height:50.};
fn render(ui:&mut Ui,value:&mut bool,input:Input,icons:SwitchIconMode,enabled:bool)->Vec<DrawCommand>{ui.frame(input,B,0.,|f|{f.switch_with_icons(1,"Switch",value,enabled,icons);})}
fn thumb(commands:&[DrawCommand])->Rect{commands.iter().find_map(|c|match c{DrawCommand::RoundedRect{bounds,radius,..} if *radius>6.0&&*radius<=14.0&&(bounds.width-bounds.height).abs()<0.01=>Some(*bounds),_=>None}).unwrap()}
#[test] fn default_thumb_and_optional_icon_sizes(){for (value,mode,expected) in [(false,SwitchIconMode::None,16.0),(true,SwitchIconMode::None,24.0),(false,SwitchIconMode::Selected,16.0),(false,SwitchIconMode::Both,24.0)]{let mut ui=Ui::default();let mut value=value;let commands=render(&mut ui,&mut value,Input::default(),mode,true);assert_eq!(thumb(&commands).width,expected);let lines=commands.iter().filter(|c|matches!(c,DrawCommand::Line{..})).count();assert_eq!(lines,if mode==SwitchIconMode::Both||mode==SwitchIconMode::Selected&&value{2}else{0});}}
#[test] fn press_expands_thumb_and_disabled_does_not(){let mut ui=Ui::default();let mut v=false;render(&mut ui,&mut v,Input::default(),SwitchIconMode::None,true);render(&mut ui,&mut v,Input{pointer:Some([16.,18.]),pressed:true,time:1.,..Input::default()},SwitchIconMode::None,true);let cmd=render(&mut ui,&mut v,Input{pointer:Some([16.,18.]),time:1.11,..Input::default()},SwitchIconMode::None,true);assert_eq!(thumb(&cmd).width,28.);let mut ui=Ui::default();let cmd=render(&mut ui,&mut v,Input{pointer:Some([16.,18.]),pressed:true,time:1.,..Input::default()},SwitchIconMode::None,false);assert_eq!(thumb(&cmd).width,16.);}
#[test] fn unselected_track_uses_surface_and_reduced_motion_snaps_pressed_thumb(){let mut ui=Ui::default();ui.motion_preference=MotionPreference::Reduced;let mut v=false;let color=ui.theme.surface_highest;let commands=render(&mut ui,&mut v,Input::default(),SwitchIconMode::None,true);assert!(commands.iter().any(|c|matches!(c,DrawCommand::RoundedRect{bounds,color:fill,..} if bounds.width==48.0&&*fill==color)));let commands=render(&mut ui,&mut v,Input{pointer:Some([16.,18.]),pressed:true,time:1.,..Input::default()},SwitchIconMode::None,true);assert_eq!(thumb(&commands).width,28.);}