devtools/actors/inspector/
layout.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! The layout actor informs the DevTools client of the layout properties of the document, such as
6//! grids or flexboxes. It acts as a placeholder for now.
7
8use serde::Serialize;
9use serde_json::{Map, Value};
10
11use crate::StreamId;
12use crate::actor::{Actor, ActorError, ActorRegistry};
13use crate::protocol::ClientRequest;
14
15#[derive(Serialize)]
16pub struct LayoutInspectorActorMsg {
17    actor: String,
18}
19
20pub struct LayoutInspectorActor {
21    name: String,
22}
23
24#[derive(Serialize)]
25pub struct GetGridsReply {
26    from: String,
27    grids: Vec<String>,
28}
29
30#[derive(Serialize)]
31pub struct GetCurrentFlexboxReply {
32    from: String,
33    flexbox: Option<()>,
34}
35
36impl Actor for LayoutInspectorActor {
37    fn name(&self) -> String {
38        self.name.clone()
39    }
40
41    /// The layout inspector actor can handle the following messages:
42    ///
43    /// - `getGrids`: Returns a list of CSS grids, non functional at the moment
44    ///
45    /// - `getCurrentFlexbox`: Returns the active flexbox, non functional at the moment
46    fn handle_message(
47        &self,
48        request: ClientRequest,
49        _registry: &ActorRegistry,
50        msg_type: &str,
51        _msg: &Map<String, Value>,
52        _id: StreamId,
53    ) -> Result<(), ActorError> {
54        match msg_type {
55            "getGrids" => {
56                let msg = GetGridsReply {
57                    from: self.name(),
58                    // TODO: Actually create a list of grids
59                    grids: vec![],
60                };
61                request.reply_final(&msg)?
62            },
63            "getCurrentFlexbox" => {
64                let msg = GetCurrentFlexboxReply {
65                    from: self.name(),
66                    // TODO: Create and return the current flexbox object
67                    flexbox: None,
68                };
69                request.reply_final(&msg)?
70            },
71            _ => return Err(ActorError::UnrecognizedPacketType),
72        };
73        Ok(())
74    }
75
76    fn cleanup(&self, _id: StreamId) {}
77}
78
79impl LayoutInspectorActor {
80    pub fn new(name: String) -> Self {
81        Self { name }
82    }
83
84    pub fn encodable(&self) -> LayoutInspectorActorMsg {
85        LayoutInspectorActorMsg { actor: self.name() }
86    }
87}