chromiumoxide/handler/
emulation.rs1use chromiumoxide_cdp::cdp::browser_protocol::emulation::{
2 ScreenOrientation, ScreenOrientationType, SetDeviceMetricsOverrideParams,
3 SetTouchEmulationEnabledParams,
4};
5use chromiumoxide_types::Method;
6
7use crate::cmd::CommandChain;
8use crate::handler::viewport::Viewport;
9use std::time::Duration;
10
11#[derive(Default, Debug, Clone, Copy, PartialEq)]
12pub struct EmulationManager {
13 pub emulating_mobile: bool,
15 pub has_touch: bool,
17 pub needs_reload: bool,
19 pub request_timeout: Duration,
21}
22
23impl EmulationManager {
24 pub fn new(request_timeout: Duration) -> Self {
26 Self {
27 emulating_mobile: false,
28 has_touch: false,
29 needs_reload: false,
30 request_timeout,
31 }
32 }
33 pub fn init_commands(&mut self, viewport: &Viewport) -> CommandChain {
38 let mut chains = Vec::with_capacity(2);
39 let set_touch = SetTouchEmulationEnabledParams::new(viewport.emulating_mobile);
40 let orientation = if viewport.is_landscape {
41 ScreenOrientation::new(ScreenOrientationType::LandscapePrimary, 90)
42 } else {
43 ScreenOrientation::new(ScreenOrientationType::PortraitPrimary, 0)
44 };
45
46 if let Ok(set_device) = SetDeviceMetricsOverrideParams::builder()
47 .mobile(viewport.emulating_mobile)
48 .width(viewport.width)
49 .height(viewport.height)
50 .device_scale_factor(viewport.device_scale_factor.unwrap_or(1.))
51 .screen_orientation(orientation)
52 .build()
53 {
54 if let Ok(set_device_value) = serde_json::to_value(&set_device) {
55 chains.push((set_device.identifier(), set_device_value));
56 }
57 }
58
59 if let Ok(set_touch_value) = serde_json::to_value(&set_touch) {
60 chains.push((set_touch.identifier(), set_touch_value));
61 }
62
63 let chain = CommandChain::new(chains, self.request_timeout);
64
65 self.needs_reload = self.emulating_mobile != viewport.emulating_mobile
66 || self.has_touch != viewport.has_touch;
67 chain
68 }
69}