1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
use super::theme::*; use seed::{prelude::*, *}; use seed_hooks::use_state; use std::collections::HashMap; #[track_caller] pub fn conditionally_skip_rendering< 'a, T: 'static + BreakpointTheme, Ms: 'static, Q: Orders<Ms>, F: 'a + Fn() -> Theme, >( theme_func: F, orders: &mut Q, ) { let current_breakpoint = use_state::<Option<T>, _>(|| None); let bp_hm = use_state(|| { theme_func() .anymap .remove::<HashMap<T, (u32, Option<u32>)>>() .unwrap() }); bp_hm.get_with(|bp_hm| { current_breakpoint.update(|current_breakpoint| { let mut need_to_set_bp = false; if let Some(bp_key) = current_breakpoint { let bp_pair = bp_hm.get(bp_key).cloned().unwrap(); let is_the_same_bp = match bp_pair { (lower, Some(higher)) => window() .match_media(&format!( "(min-width: {}px) and (max-width: {}px", lower, higher - 1 )) .unwrap() .unwrap() .matches(), (lower, _) => window() .match_media(&format!("(min-width: {}px)", lower)) .unwrap() .unwrap() .matches(), }; if is_the_same_bp { orders.skip(); } else { need_to_set_bp = true; } } else { need_to_set_bp = true } if need_to_set_bp { let opt_bp = bp_hm.iter().find_map(|(bp_key, bp_pair)| { let is_this_bp = match bp_pair { (lower, Some(higher)) => window() .match_media(&format!( "(min-width: {}px) and (max-width: {}px)", lower, higher - 1 )) .unwrap() .unwrap() .matches(), (lower, _) => window() .match_media(&format!("(min-width: {}px)", lower)) .unwrap() .unwrap() .matches(), }; if is_this_bp { Some(bp_key.clone()) } else { None } }); *current_breakpoint = opt_bp.clone(); } }); }); }