refine modal show_image alignment

This commit is contained in:
nworbnhoj 2022-06-28 07:32:55 +10:00
parent 29925a5caa
commit 6d1d514fe5

View file

@ -197,30 +197,41 @@ impl Modals {
pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> { pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> {
self.lock(); self.lock();
// resize and/or rotate // resize and/or rotate
let (modal_width, modal_height) = const BORDER: u32 = 3;
(gam::IMG_MODAL_WIDTH as f32, gam::IMG_MODAL_HEIGHT as f32);
let (modal_width, modal_height) = (
gam::IMG_MODAL_WIDTH - 2 * BORDER,
gam::IMG_MODAL_HEIGHT - 2 * BORDER,
);
let (w, h, _) = img.size(); let (w, h, _) = img.size();
let (img_width, img_height) = (w as f32, h as f32); let (img_width, img_height) = (w as f32, h as f32);
let portrait_scale = (modal_width / img_width).min(modal_height / img_height); let portrait_scale = (modal_width as f32 / img_width).min(modal_height as f32 / img_height);
let landscape_scale = (modal_width / img_height).min(modal_height / img_width); let landscape_scale =
(modal_width as f32 / img_height).min(modal_height as f32 / img_width);
let mut bm = if portrait_scale >= 1.0 { let mut bm = if portrait_scale >= 1.0 {
log::info!("show image as is");
Bitmap::from(img) Bitmap::from(img)
} else if landscape_scale >= 1.0 { } else if landscape_scale >= 1.0 {
log::info!("rotate image");
Bitmap::from(img).rotate90() Bitmap::from(img).rotate90()
} else if portrait_scale >= landscape_scale { } else if portrait_scale >= landscape_scale {
log::info!("scale image {}", portrait_scale);
Bitmap::new_resize(img, (portrait_scale * img_width) as usize) Bitmap::new_resize(img, (portrait_scale * img_width) as usize)
} else { } else {
log::info!("scale image {} and rotate", landscape_scale);
Bitmap::new_resize(img, (landscape_scale * img_width) as usize).rotate90() Bitmap::new_resize(img, (landscape_scale * img_width) as usize).rotate90()
}; };
let (bm_width, bm_height) = bm.size(); let (bm_width, bm_height) = bm.size();
let (bm_width, bm_height) = (bm_width as u32, bm_height as u32); let (bm_width, bm_height) = (bm_width as u32, bm_height as u32);
// center image in modal // center image in modal
let center = Point::new( let center = Point::new(
((gam::IMG_MODAL_WIDTH - bm_width) / 2).try_into().unwrap(), (BORDER + (gam::IMG_MODAL_WIDTH - 2 * BORDER - bm_width) / 2)
((gam::IMG_MODAL_HEIGHT - bm_height) / 2) .try_into()
.unwrap(),
(BORDER + (gam::IMG_MODAL_HEIGHT - 2 * BORDER - bm_height) / 2)
.try_into() .try_into()
.unwrap(), .unwrap(),
); );