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
use chromiumoxide_cdp::cdp::browser_protocol::emulation::{ ScreenOrientation, ScreenOrientationType, SetDeviceMetricsOverrideParams, SetTouchEmulationEnabledParams, }; use chromiumoxide_types::Method; use crate::cmd::CommandChain; use crate::handler::viewport::Viewport; #[derive(Debug, Default)] pub struct EmulationManager { pub emulating_mobile: bool, pub has_touch: bool, pub needs_reload: bool, } impl EmulationManager { pub fn init_commands(&mut self, viewport: &Viewport) -> CommandChain { let orientation = if viewport.is_landscape { ScreenOrientation::new(ScreenOrientationType::LandscapePrimary, 90) } else { ScreenOrientation::new(ScreenOrientationType::PortraitPrimary, 0) }; let set_device = SetDeviceMetricsOverrideParams::builder() .mobile(viewport.emulating_mobile) .width(viewport.width) .height(viewport.height) .device_scale_factor(viewport.device_scale_factor.unwrap_or(1.)) .screen_orientation(orientation) .build() .unwrap(); let set_touch = SetTouchEmulationEnabledParams::new(true); let chain = CommandChain::new(vec![ ( set_device.identifier(), serde_json::to_value(set_device).unwrap(), ), ( set_touch.identifier(), serde_json::to_value(set_touch).unwrap(), ), ]); self.needs_reload = self.emulating_mobile != viewport.emulating_mobile || self.has_touch != viewport.has_touch; chain } }