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 malloc_size_of_derive::MallocSizeOf;
9use serde::Serialize;
10use serde_json::{Map, Value};
11
12use crate::actor::{Actor, ActorEncode, ActorError, ActorRegistry};
13use crate::protocol::ClientRequest;
14use crate::{ActorMsg, StreamId};
15
16#[derive(MallocSizeOf)]
17pub(crate) struct LayoutInspectorActor {
18    name: String,
19}
20
21#[derive(Serialize)]
22pub(crate) struct GetGridsReply {
23    from: String,
24    grids: Vec<String>,
25}
26
27#[derive(Serialize)]
28pub(crate) struct GetCurrentFlexboxReply {
29    from: String,
30    flexbox: Option<()>,
31}
32
33impl Actor for LayoutInspectorActor {
34    fn name(&self) -> String {
35        self.name.clone()
36    }
37
38    /// The layout inspector actor can handle the following messages:
39    ///
40    /// - `getGrids`: Returns a list of CSS grids, non functional at the moment
41    ///
42    /// - `getCurrentFlexbox`: Returns the active flexbox, non functional at the moment
43    fn handle_message(
44        &self,
45        request: ClientRequest,
46        _registry: &ActorRegistry,
47        msg_type: &str,
48        _msg: &Map<String, Value>,
49        _id: StreamId,
50    ) -> Result<(), ActorError> {
51        match msg_type {
52            "getGrids" => {
53                let msg = GetGridsReply {
54                    from: self.name(),
55                    // TODO: Actually create a list of grids
56                    grids: vec![],
57                };
58                request.reply_final(&msg)?
59            },
60            "getCurrentFlexbox" => {
61                let msg = GetCurrentFlexboxReply {
62                    from: self.name(),
63                    // TODO: Create and return the current flexbox object
64                    flexbox: None,
65                };
66                request.reply_final(&msg)?
67            },
68            _ => return Err(ActorError::UnrecognizedPacketType),
69        };
70        Ok(())
71    }
72
73    fn cleanup(&self, _id: StreamId) {}
74}
75
76impl LayoutInspectorActor {
77    pub fn new(name: String) -> Self {
78        Self { name }
79    }
80}
81
82impl ActorEncode<ActorMsg> for LayoutInspectorActor {
83    fn encode(&self, _: &ActorRegistry) -> ActorMsg {
84        ActorMsg { actor: self.name() }
85    }
86}