Format components devtools and devtools_traits #21373

This commit is contained in:
kingdido999 2018-09-02 20:29:47 +08:00
parent b211e45bb0
commit ad822b74c7
17 changed files with 669 additions and 461 deletions

View file

@ -61,27 +61,25 @@ impl Actor for HighlighterActor {
self.name.clone()
}
fn handle_message(&self,
_registry: &ActorRegistry,
msg_type: &str,
_msg: &Map<String, Value>,
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
fn handle_message(
&self,
_registry: &ActorRegistry,
msg_type: &str,
_msg: &Map<String, Value>,
stream: &mut TcpStream,
) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"showBoxModel" => {
let msg = ShowBoxModelReply {
from: self.name(),
};
let msg = ShowBoxModelReply { from: self.name() };
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
"hideBoxModel" => {
let msg = HideBoxModelReply {
from: self.name(),
};
let msg = HideBoxModelReply { from: self.name() };
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
_ => ActorMessageStatus::Ignored,
})
@ -98,29 +96,33 @@ impl Actor for NodeActor {
self.name.clone()
}
fn handle_message(&self,
registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
fn handle_message(
&self,
registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
stream: &mut TcpStream,
) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"modifyAttributes" => {
let target = msg.get("to").unwrap().as_str().unwrap();
let mods = msg.get("modifications").unwrap().as_array().unwrap();
let modifications = mods.iter().map(|json_mod| {
serde_json::from_str(&serde_json::to_string(json_mod).unwrap()).unwrap()
}).collect();
let modifications = mods
.iter()
.map(|json_mod| {
serde_json::from_str(&serde_json::to_string(json_mod).unwrap()).unwrap()
}).collect();
self.script_chan.send(ModifyAttribute(self.pipeline,
registry.actor_to_script(target.to_owned()),
modifications))
.unwrap();
let reply = ModifyAttributeReply {
from: self.name(),
};
self.script_chan
.send(ModifyAttribute(
self.pipeline,
registry.actor_to_script(target.to_owned()),
modifications,
)).unwrap();
let reply = ModifyAttributeReply { from: self.name() };
stream.write_json_packet(&reply);
ActorMessageStatus::Processed
}
},
_ => ActorMessageStatus::Ignored,
})
@ -175,19 +177,23 @@ struct NodeActorMsg {
}
trait NodeInfoToProtocol {
fn encode(self,
actors: &ActorRegistry,
display: bool,
script_chan: IpcSender<DevtoolScriptControlMsg>,
pipeline: PipelineId) -> NodeActorMsg;
fn encode(
self,
actors: &ActorRegistry,
display: bool,
script_chan: IpcSender<DevtoolScriptControlMsg>,
pipeline: PipelineId,
) -> NodeActorMsg;
}
impl NodeInfoToProtocol for NodeInfo {
fn encode(self,
actors: &ActorRegistry,
display: bool,
script_chan: IpcSender<DevtoolScriptControlMsg>,
pipeline: PipelineId) -> NodeActorMsg {
fn encode(
self,
actors: &ActorRegistry,
display: bool,
script_chan: IpcSender<DevtoolScriptControlMsg>,
pipeline: PipelineId,
) -> NodeActorMsg {
let actor_name = if !actors.script_actor_registered(self.uniqueId.clone()) {
let name = actors.new_name("node");
let node_actor = NodeActor {
@ -215,15 +221,16 @@ impl NodeInfoToProtocol for NodeInfo {
publicId: self.publicId,
systemId: self.systemId,
attrs: self.attrs.into_iter().map(|attr| {
AttrMsg {
attrs: self
.attrs
.into_iter()
.map(|attr| AttrMsg {
namespace: attr.namespace,
name: attr.name,
value: attr.value,
}
}).collect(),
}).collect(),
pseudoClassLocks: vec!(), //TODO get this data from script
pseudoClassLocks: vec![], //TODO get this data from script
isDisplayed: display,
@ -272,25 +279,28 @@ impl Actor for WalkerActor {
self.name.clone()
}
fn handle_message(&self,
registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
fn handle_message(
&self,
registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
stream: &mut TcpStream,
) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"querySelector" => {
let msg = QuerySelectorReply {
from: self.name(),
};
let msg = QuerySelectorReply { from: self.name() };
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
"documentElement" => {
let (tx, rx) = ipc::channel().unwrap();
self.script_chan.send(GetDocumentElement(self.pipeline, tx)).unwrap();
self.script_chan
.send(GetDocumentElement(self.pipeline, tx))
.unwrap();
let doc_elem_info = rx.recv().unwrap().ok_or(())?;
let node = doc_elem_info.encode(registry, true, self.script_chan.clone(), self.pipeline);
let node =
doc_elem_info.encode(registry, true, self.script_chan.clone(), self.pipeline);
let msg = DocumentElementReply {
from: self.name(),
@ -298,36 +308,38 @@ impl Actor for WalkerActor {
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
"clearPseudoClassLocks" => {
let msg = ClearPseudoclassesReply {
from: self.name(),
};
let msg = ClearPseudoclassesReply { from: self.name() };
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
"children" => {
let target = msg.get("node").unwrap().as_str().unwrap();
let (tx, rx) = ipc::channel().unwrap();
self.script_chan.send(GetChildren(self.pipeline,
registry.actor_to_script(target.to_owned()),
tx))
.unwrap();
self.script_chan
.send(GetChildren(
self.pipeline,
registry.actor_to_script(target.to_owned()),
tx,
)).unwrap();
let children = rx.recv().unwrap().ok_or(())?;
let msg = ChildrenReply {
hasFirst: true,
hasLast: true,
nodes: children.into_iter().map(|child| {
child.encode(registry, true, self.script_chan.clone(), self.pipeline)
}).collect(),
nodes: children
.into_iter()
.map(|child| {
child.encode(registry, true, self.script_chan.clone(), self.pipeline)
}).collect(),
from: self.name(),
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
_ => ActorMessageStatus::Ignored,
})
@ -447,52 +459,72 @@ impl Actor for PageStyleActor {
self.name.clone()
}
fn handle_message(&self,
registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
fn handle_message(
&self,
registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
stream: &mut TcpStream,
) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"getApplied" => {
//TODO: query script for relevant applied styles to node (msg.node)
let msg = GetAppliedReply {
entries: vec!(),
rules: vec!(),
sheets: vec!(),
entries: vec![],
rules: vec![],
sheets: vec![],
from: self.name(),
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
"getComputed" => {
//TODO: query script for relevant computed styles on node (msg.node)
let msg = GetComputedReply {
computed: vec!(),
computed: vec![],
from: self.name(),
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
//TODO: query script for box layout properties of node (msg.node)
"getLayout" => {
let target = msg.get("node").unwrap().as_str().unwrap();
let (tx, rx) = ipc::channel().unwrap();
self.script_chan.send(GetLayout(self.pipeline,
registry.actor_to_script(target.to_owned()),
tx))
.unwrap();
self.script_chan
.send(GetLayout(
self.pipeline,
registry.actor_to_script(target.to_owned()),
tx,
)).unwrap();
let ComputedNodeLayout {
display, position, zIndex, boxSizing,
autoMargins, marginTop, marginRight, marginBottom, marginLeft,
borderTopWidth, borderRightWidth, borderBottomWidth, borderLeftWidth,
paddingTop, paddingRight, paddingBottom, paddingLeft,
width, height,
display,
position,
zIndex,
boxSizing,
autoMargins,
marginTop,
marginRight,
marginBottom,
marginLeft,
borderTopWidth,
borderRightWidth,
borderBottomWidth,
borderLeftWidth,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft,
width,
height,
} = rx.recv().unwrap().ok_or(())?;
let auto_margins = msg.get("autoMargins")
.and_then(&Value::as_bool).unwrap_or(false);
let auto_margins = msg
.get("autoMargins")
.and_then(&Value::as_bool)
.unwrap_or(false);
// http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/actors/styles.js
let msg = GetLayoutReply {
@ -504,10 +536,18 @@ impl Actor for PageStyleActor {
autoMargins: if auto_margins {
let mut m = Map::new();
let auto = serde_json::value::Value::String("auto".to_owned());
if autoMargins.top { m.insert("top".to_owned(), auto.clone()); }
if autoMargins.right { m.insert("right".to_owned(), auto.clone()); }
if autoMargins.bottom { m.insert("bottom".to_owned(), auto.clone()); }
if autoMargins.left { m.insert("left".to_owned(), auto.clone()); }
if autoMargins.top {
m.insert("top".to_owned(), auto.clone());
}
if autoMargins.right {
m.insert("right".to_owned(), auto.clone());
}
if autoMargins.bottom {
m.insert("bottom".to_owned(), auto.clone());
}
if autoMargins.left {
m.insert("left".to_owned(), auto.clone());
}
serde_json::value::Value::Object(m)
} else {
serde_json::value::Value::Null
@ -531,7 +571,7 @@ impl Actor for PageStyleActor {
let msg = serde_json::from_str::<Value>(&msg).unwrap();
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
_ => ActorMessageStatus::Ignored,
})
@ -543,11 +583,13 @@ impl Actor for InspectorActor {
self.name.clone()
}
fn handle_message(&self,
registry: &ActorRegistry,
msg_type: &str,
_msg: &Map<String, Value>,
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
fn handle_message(
&self,
registry: &ActorRegistry,
msg_type: &str,
_msg: &Map<String, Value>,
stream: &mut TcpStream,
) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"getWalker" => {
if self.walker.borrow().is_none() {
@ -562,21 +604,24 @@ impl Actor for InspectorActor {
}
let (tx, rx) = ipc::channel().unwrap();
self.script_chan.send(GetRootNode(self.pipeline, tx)).unwrap();
self.script_chan
.send(GetRootNode(self.pipeline, tx))
.unwrap();
let root_info = rx.recv().unwrap().ok_or(())?;
let node = root_info.encode(registry, false, self.script_chan.clone(), self.pipeline);
let node =
root_info.encode(registry, false, self.script_chan.clone(), self.pipeline);
let msg = GetWalkerReply {
from: self.name(),
walker: WalkerMsg {
actor: self.walker.borrow().clone().unwrap(),
root: node,
}
},
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
"getPageStyle" => {
if self.pageStyle.borrow().is_none() {
@ -598,7 +643,7 @@ impl Actor for InspectorActor {
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
//TODO: this is an old message; try adding highlightable to the root traits instead
// and support getHighlighter instead
@ -621,7 +666,7 @@ impl Actor for InspectorActor {
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},
_ => ActorMessageStatus::Ignored,
})