Commit 6f8c23bd authored by thanhph's avatar thanhph Committed by Commit bot

Ash devtools: Add support for changing visibility.

Adding visibility property will enable a window/widget/view to be shown
or hidden by simply changing the value 1 (visible) to 0 (hide) in the
element CSS style.

BUG=697175

Review-Url: https://codereview.chromium.org/2739523002
Cr-Commit-Position: refs/heads/master@{#457745}
parent 5d465e52
...@@ -18,6 +18,7 @@ const char kHeight[] = "height"; ...@@ -18,6 +18,7 @@ const char kHeight[] = "height";
const char kWidth[] = "width"; const char kWidth[] = "width";
const char kX[] = "x"; const char kX[] = "x";
const char kY[] = "y"; const char kY[] = "y";
const char kVisibility[] = "visibility";
std::unique_ptr<CSS::SourceRange> BuildDefaultSourceRange() { std::unique_ptr<CSS::SourceRange> BuildDefaultSourceRange() {
// These tell the frontend where in the stylesheet a certain style // These tell the frontend where in the stylesheet a certain style
...@@ -41,27 +42,37 @@ std::unique_ptr<CSS::CSSProperty> BuildCSSProperty(const std::string& name, ...@@ -41,27 +42,37 @@ std::unique_ptr<CSS::CSSProperty> BuildCSSProperty(const std::string& name,
.build(); .build();
} }
std::unique_ptr<Array<CSS::CSSProperty>> BuildBoundsCSSPropertyArray( std::unique_ptr<Array<CSS::CSSProperty>> BuildCSSPropertyArray(
const gfx::Rect& bounds) { const gfx::Rect& bounds,
const bool visible) {
auto cssProperties = Array<CSS::CSSProperty>::create(); auto cssProperties = Array<CSS::CSSProperty>::create();
cssProperties->addItem(BuildCSSProperty(kHeight, bounds.height())); cssProperties->addItem(BuildCSSProperty(kHeight, bounds.height()));
cssProperties->addItem(BuildCSSProperty(kWidth, bounds.width())); cssProperties->addItem(BuildCSSProperty(kWidth, bounds.width()));
cssProperties->addItem(BuildCSSProperty(kX, bounds.x())); cssProperties->addItem(BuildCSSProperty(kX, bounds.x()));
cssProperties->addItem(BuildCSSProperty(kY, bounds.y())); cssProperties->addItem(BuildCSSProperty(kY, bounds.y()));
cssProperties->addItem(BuildCSSProperty(kVisibility, visible));
return cssProperties; return cssProperties;
} }
std::unique_ptr<CSS::CSSStyle> BuildCSSStyle(int node_id, std::unique_ptr<CSS::CSSStyle> BuildCSSStyle(int node_id,
const gfx::Rect& bounds) { const gfx::Rect& bounds,
bool visible) {
return CSS::CSSStyle::create() return CSS::CSSStyle::create()
.setRange(BuildDefaultSourceRange()) .setRange(BuildDefaultSourceRange())
.setStyleSheetId(base::IntToString(node_id)) .setStyleSheetId(base::IntToString(node_id))
.setCssProperties(BuildBoundsCSSPropertyArray(bounds)) .setCssProperties(BuildCSSPropertyArray(bounds, visible))
.setShorthandEntries(Array<std::string>::create()) .setShorthandEntries(Array<std::string>::create())
.build(); .build();
} }
Response ParseBounds(const std::string& style_text, gfx::Rect& bounds) { ui::devtools::protocol::Response NodeNotFoundError(int node_id) {
return ui::devtools::protocol::Response::Error(
"Node with id=" + std::to_string(node_id) + " not found");
}
Response ParseProperties(const std::string& style_text,
gfx::Rect* bounds,
bool* visible) {
std::vector<std::string> tokens = base::SplitString( std::vector<std::string> tokens = base::SplitString(
style_text, ":;", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); style_text, ":;", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
for (size_t i = 0; i < tokens.size() - 1; i += 2) { for (size_t i = 0; i < tokens.size() - 1; i += 2) {
...@@ -71,13 +82,15 @@ Response ParseBounds(const std::string& style_text, gfx::Rect& bounds) { ...@@ -71,13 +82,15 @@ Response ParseBounds(const std::string& style_text, gfx::Rect& bounds) {
return Response::Error("Unable to parse value for property=" + property); return Response::Error("Unable to parse value for property=" + property);
if (property == kHeight) if (property == kHeight)
bounds.set_height(std::max(0, value)); bounds->set_height(std::max(0, value));
else if (property == kWidth) else if (property == kWidth)
bounds.set_width(std::max(0, value)); bounds->set_width(std::max(0, value));
else if (property == kX) else if (property == kX)
bounds.set_x(value); bounds->set_x(value);
else if (property == kY) else if (property == kY)
bounds.set_y(value); bounds->set_y(value);
else if (property == kVisibility)
*visible = std::max(0, value) == 1;
else else
return Response::Error("Unsupported property=" + property); return Response::Error("Unsupported property=" + property);
} }
...@@ -110,10 +123,8 @@ ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode( ...@@ -110,10 +123,8 @@ ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode(
ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>* ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>*
inline_style) { inline_style) {
*inline_style = GetStylesForNode(node_id); *inline_style = GetStylesForNode(node_id);
if (!inline_style) { if (!inline_style)
return ui::devtools::protocol::Response::Error( return NodeNotFoundError(node_id);
"Node with that id not found");
}
return ui::devtools::protocol::Response::OK(); return ui::devtools::protocol::Response::OK();
} }
...@@ -134,23 +145,20 @@ ui::devtools::protocol::Response AshDevToolsCSSAgent::setStyleTexts( ...@@ -134,23 +145,20 @@ ui::devtools::protocol::Response AshDevToolsCSSAgent::setStyleTexts(
return ui::devtools::protocol::Response::Error("Invalid node id"); return ui::devtools::protocol::Response::Error("Invalid node id");
gfx::Rect updated_bounds; gfx::Rect updated_bounds;
if (!GetBoundsForNodeId(node_id, &updated_bounds)) { bool visible = false;
return ui::devtools::protocol::Response::Error( if (!GetPropertiesForNodeId(node_id, &updated_bounds, &visible))
"No node found with that id"); return NodeNotFoundError(node_id);
}
ui::devtools::protocol::Response response( ui::devtools::protocol::Response response(
ParseBounds(edit->getText(), updated_bounds)); ParseProperties(edit->getText(), &updated_bounds, &visible));
if (!response.isSuccess()) if (!response.isSuccess())
return response; return response;
updated_styles->addItem(BuildCSSStyle(node_id, updated_bounds)); updated_styles->addItem(BuildCSSStyle(node_id, updated_bounds, visible));
if (!UpdateBounds(node_id, updated_bounds)) {
return ui::devtools::protocol::Response::Error(
"No node found with that id");
}
}
if (!SetPropertiesForNodeId(node_id, updated_bounds, visible))
return NodeNotFoundError(node_id);
}
*result = std::move(updated_styles); *result = std::move(updated_styles);
return ui::devtools::protocol::Response::OK(); return ui::devtools::protocol::Response::OK();
} }
...@@ -170,8 +178,10 @@ void AshDevToolsCSSAgent::OnViewBoundsChanged(views::View* view) { ...@@ -170,8 +178,10 @@ void AshDevToolsCSSAgent::OnViewBoundsChanged(views::View* view) {
std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle> std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle>
AshDevToolsCSSAgent::GetStylesForNode(int node_id) { AshDevToolsCSSAgent::GetStylesForNode(int node_id) {
gfx::Rect bounds; gfx::Rect bounds;
return GetBoundsForNodeId(node_id, &bounds) ? BuildCSSStyle(node_id, bounds) bool visible = false;
: nullptr; return GetPropertiesForNodeId(node_id, &bounds, &visible)
? BuildCSSStyle(node_id, bounds, visible)
: nullptr;
} }
void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) { void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) {
...@@ -179,47 +189,62 @@ void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) { ...@@ -179,47 +189,62 @@ void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) {
frontend()->styleSheetChanged(base::IntToString(node_id)); frontend()->styleSheetChanged(base::IntToString(node_id));
} }
bool AshDevToolsCSSAgent::GetBoundsForNodeId(int node_id, gfx::Rect* bounds) { bool AshDevToolsCSSAgent::GetPropertiesForNodeId(int node_id,
gfx::Rect* bounds,
bool* visible) {
WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id);
if (window) { if (window) {
*bounds = window->GetBounds(); *bounds = window->GetBounds();
*visible = window->IsVisible();
return true; return true;
} }
views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id);
if (widget) { if (widget) {
*bounds = widget->GetRestoredBounds(); *bounds = widget->GetRestoredBounds();
*visible = widget->IsVisible();
return true; return true;
} }
views::View* view = dom_agent_->GetViewFromNodeId(node_id); views::View* view = dom_agent_->GetViewFromNodeId(node_id);
if (view) { if (view) {
*bounds = view->bounds(); *bounds = view->bounds();
*visible = view->visible();
return true; return true;
} }
return false; return false;
} }
bool AshDevToolsCSSAgent::UpdateBounds(int node_id, const gfx::Rect& bounds) { bool AshDevToolsCSSAgent::SetPropertiesForNodeId(int node_id,
const gfx::Rect& bounds,
bool visible) {
WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id);
if (window) { if (window) {
window->SetBounds(bounds); window->SetBounds(bounds);
if (visible != window->IsVisible()) {
if (visible)
window->Show();
else
window->Hide();
}
return true; return true;
} }
views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id);
if (widget) { if (widget) {
widget->SetBounds(bounds); widget->SetBounds(bounds);
if (visible != widget->IsVisible()) {
if (visible)
widget->Show();
else
widget->Hide();
}
return true; return true;
} }
views::View* view = dom_agent_->GetViewFromNodeId(node_id); views::View* view = dom_agent_->GetViewFromNodeId(node_id);
if (view) { if (view) {
view->SetBoundsRect(bounds); view->SetBoundsRect(bounds);
if (visible != view->visible())
view->SetVisible(visible);
return true; return true;
} }
return false; return false;
} }
......
...@@ -43,9 +43,10 @@ class ASH_EXPORT AshDevToolsCSSAgent ...@@ -43,9 +43,10 @@ class ASH_EXPORT AshDevToolsCSSAgent
std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle> GetStylesForNode( std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle> GetStylesForNode(
int node_id); int node_id);
void InvalidateStyleSheet(int node_id); void InvalidateStyleSheet(int node_id);
bool GetBoundsForNodeId(int node_id, gfx::Rect* bounds); bool GetPropertiesForNodeId(int node_id, gfx::Rect* bounds, bool* visible);
bool UpdateBounds(int node_id, const gfx::Rect& bounds); bool SetPropertiesForNodeId(int node_id,
const gfx::Rect& bounds,
bool visible);
AshDevToolsDOMAgent* dom_agent_; AshDevToolsDOMAgent* dom_agent_;
DISALLOW_COPY_AND_ASSIGN(AshDevToolsCSSAgent); DISALLOW_COPY_AND_ASSIGN(AshDevToolsCSSAgent);
......
...@@ -717,14 +717,17 @@ TEST_F(AshDevToolsTest, WindowWidgetViewSetStyleText) { ...@@ -717,14 +717,17 @@ TEST_F(AshDevToolsTest, WindowWidgetViewSetStyleText) {
// Test different combinations on window node // Test different combinations on window node
DOM::Node* window_node = parent_children->get(1); DOM::Node* window_node = parent_children->get(1);
SetStyleTexts(window_node, "x: 25; y:35; width: 5; height: 20;", true); SetStyleTexts(window_node,
"x: 25; y:35; width: 5; height: 20; visibility: 1;", true);
EXPECT_EQ(gfx::Rect(25, 35, 5, 20), window->GetBounds()); EXPECT_EQ(gfx::Rect(25, 35, 5, 20), window->GetBounds());
EXPECT_TRUE(window->IsVisible());
SetStyleTexts(window_node, "test_nothing_happens:1;", false); SetStyleTexts(window_node, "test_nothing_happens:1;", false);
EXPECT_EQ(gfx::Rect(25, 35, 5, 20), window->GetBounds()); // Not changed EXPECT_EQ(gfx::Rect(25, 35, 5, 20), window->GetBounds()); // Not changed
SetStyleTexts(window_node, "\nheight: 10;\n ", true); SetStyleTexts(window_node, "\nheight: 10;\nvisibility: 0;\n", true);
EXPECT_EQ(gfx::Rect(25, 35, 5, 10), window->GetBounds()); EXPECT_EQ(gfx::Rect(25, 35, 5, 10), window->GetBounds());
EXPECT_FALSE(window->IsVisible());
SetStyleTexts(window_node, "\nx: 10; y: 23; width: 52;\n ", true); SetStyleTexts(window_node, "\nx: 10; y: 23; width: 52;\n ", true);
EXPECT_EQ(gfx::Rect(10, 23, 52, 10), window->GetBounds()); EXPECT_EQ(gfx::Rect(10, 23, 52, 10), window->GetBounds());
...@@ -732,15 +735,18 @@ TEST_F(AshDevToolsTest, WindowWidgetViewSetStyleText) { ...@@ -732,15 +735,18 @@ TEST_F(AshDevToolsTest, WindowWidgetViewSetStyleText) {
// Test different combinations on widget node // Test different combinations on widget node
DOM::Node* widget_node = parent_children->get(0); DOM::Node* widget_node = parent_children->get(0);
SetStyleTexts(widget_node, "x: 25; y:35; width: 53; height: 64;", true); SetStyleTexts(widget_node,
"x: 25; y:35; width: 53; height: 64; visibility: 0;", true);
EXPECT_EQ(gfx::Rect(25, 35, 53, 64), widget->GetRestoredBounds()); EXPECT_EQ(gfx::Rect(25, 35, 53, 64), widget->GetRestoredBounds());
EXPECT_FALSE(widget->IsVisible());
SetStyleTexts(widget_node, "test_nothing_happens:1;", false); SetStyleTexts(widget_node, "test_nothing_happens:1;", false);
EXPECT_EQ(gfx::Rect(25, 35, 53, 64), EXPECT_EQ(gfx::Rect(25, 35, 53, 64),
widget->GetRestoredBounds()); // Not changed widget->GetRestoredBounds()); // Not changed
SetStyleTexts(widget_node, "\nheight: 123;\n ", true); SetStyleTexts(widget_node, "\nheight: 123;\nvisibility: 1;\n", true);
EXPECT_EQ(gfx::Rect(25, 35, 53, 123), widget->GetRestoredBounds()); EXPECT_EQ(gfx::Rect(25, 35, 53, 123), widget->GetRestoredBounds());
EXPECT_TRUE(widget->IsVisible());
SetStyleTexts(widget_node, "\nx: 10; y: 23; width: 98;\n ", true); SetStyleTexts(widget_node, "\nx: 10; y: 23; width: 98;\n ", true);
EXPECT_EQ(gfx::Rect(10, 23, 98, 123), widget->GetRestoredBounds()); EXPECT_EQ(gfx::Rect(10, 23, 98, 123), widget->GetRestoredBounds());
...@@ -748,8 +754,10 @@ TEST_F(AshDevToolsTest, WindowWidgetViewSetStyleText) { ...@@ -748,8 +754,10 @@ TEST_F(AshDevToolsTest, WindowWidgetViewSetStyleText) {
// Test different combinations on view node // Test different combinations on view node
DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0);
SetStyleTexts(root_view_node, "x: 25; y:35; width: 45; height: 20;", true); SetStyleTexts(root_view_node,
"x: 25; y:35; width: 45; height: 20; visibility: 0;", true);
EXPECT_EQ(gfx::Rect(25, 35, 45, 20), root_view->bounds()); EXPECT_EQ(gfx::Rect(25, 35, 45, 20), root_view->bounds());
EXPECT_FALSE(root_view->visible());
SetStyleTexts(root_view_node, "test_nothing_happens:1;", false); SetStyleTexts(root_view_node, "test_nothing_happens:1;", false);
EXPECT_EQ(gfx::Rect(25, 35, 45, 20), root_view->bounds()); // Not changed EXPECT_EQ(gfx::Rect(25, 35, 45, 20), root_view->bounds()); // Not changed
...@@ -757,8 +765,10 @@ TEST_F(AshDevToolsTest, WindowWidgetViewSetStyleText) { ...@@ -757,8 +765,10 @@ TEST_F(AshDevToolsTest, WindowWidgetViewSetStyleText) {
SetStyleTexts(root_view_node, "\nheight: 73;\n ", true); SetStyleTexts(root_view_node, "\nheight: 73;\n ", true);
EXPECT_EQ(gfx::Rect(25, 35, 45, 73), root_view->bounds()); EXPECT_EQ(gfx::Rect(25, 35, 45, 73), root_view->bounds());
SetStyleTexts(root_view_node, "\nx: 10; y: 23; width: 52;\n ", true); SetStyleTexts(root_view_node, "\nx: 10; y: 23; width: 52;\nvisibility: 1;\n",
true);
EXPECT_EQ(gfx::Rect(10, 23, 52, 73), root_view->bounds()); EXPECT_EQ(gfx::Rect(10, 23, 52, 73), root_view->bounds());
EXPECT_TRUE(root_view->visible());
} }
} // namespace ash } // namespace ash
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment