OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first. | 5 #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first. |
6 | 6 |
7 #include "ui/views/view.h" | 7 #include "ui/views/view.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
516 if (transform.IsIdentity()) { | 516 if (transform.IsIdentity()) { |
517 if (layer()) { | 517 if (layer()) { |
518 layer()->SetTransform(transform); | 518 layer()->SetTransform(transform); |
519 if (!paint_to_layer_) | 519 if (!paint_to_layer_) |
520 DestroyLayer(); | 520 DestroyLayer(); |
521 } else { | 521 } else { |
522 // Nothing. | 522 // Nothing. |
523 } | 523 } |
524 } else { | 524 } else { |
525 if (!layer()) | 525 if (!layer()) |
526 CreateLayer(); | 526 CreateLayer(ui::LAYER_TEXTURED); |
527 layer()->SetTransform(transform); | 527 layer()->SetTransform(transform); |
528 layer()->ScheduleDraw(); | 528 layer()->ScheduleDraw(); |
529 } | 529 } |
530 } | 530 } |
531 | 531 |
532 void View::SetPaintToLayer(bool paint_to_layer) { | 532 void View::SetPaintToLayer(ui::LayerType layer_type) { |
533 if (paint_to_layer_ == paint_to_layer) | 533 if (paint_to_layer_ && (layer()->type() == layer_type)) |
534 return; | 534 return; |
535 | 535 |
536 paint_to_layer_ = paint_to_layer; | 536 paint_to_layer_ = true; |
537 if (paint_to_layer_ && !layer()) { | 537 if (layer()) { |
sky
2017/01/23 21:21:05
Reduce the conditional to:
DestroyLayer(); (it ha
yiyix
2017/01/24 03:14:53
You are right. Sorry, i should thought about it.
| |
538 CreateLayer(); | |
539 } else if (!paint_to_layer_ && layer()) { | |
540 DestroyLayer(); | 538 DestroyLayer(); |
539 CreateLayer(layer_type); | |
540 } else { | |
541 CreateLayer(layer_type); | |
541 } | 542 } |
542 } | 543 } |
543 | 544 |
545 void View::DestroyLayer() { | |
546 if (!paint_to_layer_) | |
547 return; | |
548 | |
549 paint_to_layer_ = false; | |
550 if (!layer()) | |
551 return; | |
552 | |
553 ui::Layer* new_parent = layer()->parent(); | |
554 std::vector<ui::Layer*> children = layer()->children(); | |
555 for (size_t i = 0; i < children.size(); ++i) { | |
556 layer()->Remove(children[i]); | |
557 if (new_parent) | |
558 new_parent->Add(children[i]); | |
559 } | |
560 | |
561 LayerOwner::DestroyLayer(); | |
562 | |
563 if (new_parent) | |
564 ReorderLayers(); | |
565 | |
566 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL)); | |
567 | |
568 SchedulePaint(); | |
569 | |
570 Widget* widget = GetWidget(); | |
571 if (widget) | |
572 widget->UpdateRootLayers(); | |
573 } | |
574 | |
544 std::unique_ptr<ui::Layer> View::RecreateLayer() { | 575 std::unique_ptr<ui::Layer> View::RecreateLayer() { |
545 std::unique_ptr<ui::Layer> old_layer = LayerOwner::RecreateLayer(); | 576 std::unique_ptr<ui::Layer> old_layer = LayerOwner::RecreateLayer(); |
546 Widget* widget = GetWidget(); | 577 Widget* widget = GetWidget(); |
547 if (widget) | 578 if (widget) |
548 widget->UpdateRootLayers(); | 579 widget->UpdateRootLayers(); |
549 return old_layer; | 580 return old_layer; |
550 } | 581 } |
551 | 582 |
552 // RTL positioning ------------------------------------------------------------- | 583 // RTL positioning ------------------------------------------------------------- |
553 | 584 |
(...skipping 1623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2177 bool View::ConvertRectFromAncestor(const View* ancestor, | 2208 bool View::ConvertRectFromAncestor(const View* ancestor, |
2178 gfx::RectF* rect) const { | 2209 gfx::RectF* rect) const { |
2179 gfx::Transform trans; | 2210 gfx::Transform trans; |
2180 bool result = GetTransformRelativeTo(ancestor, &trans); | 2211 bool result = GetTransformRelativeTo(ancestor, &trans); |
2181 trans.TransformRectReverse(rect); | 2212 trans.TransformRectReverse(rect); |
2182 return result; | 2213 return result; |
2183 } | 2214 } |
2184 | 2215 |
2185 // Accelerated painting -------------------------------------------------------- | 2216 // Accelerated painting -------------------------------------------------------- |
2186 | 2217 |
2187 void View::CreateLayer() { | 2218 void View::CreateLayer(ui::LayerType layer_type) { |
2188 // A new layer is being created for the view. So all the layers of the | 2219 // A new layer is being created for the view. So all the layers of the |
2189 // sub-tree can inherit the visibility of the corresponding view. | 2220 // sub-tree can inherit the visibility of the corresponding view. |
2190 { | 2221 { |
2191 internal::ScopedChildrenLock lock(this); | 2222 internal::ScopedChildrenLock lock(this); |
2192 for (auto* child : children_) | 2223 for (auto* child : children_) |
2193 child->UpdateChildLayerVisibility(true); | 2224 child->UpdateChildLayerVisibility(true); |
2194 } | 2225 } |
2195 | 2226 |
2196 SetLayer(base::MakeUnique<ui::Layer>()); | 2227 SetLayer(base::MakeUnique<ui::Layer>(layer_type)); |
2197 layer()->set_delegate(this); | 2228 layer()->set_delegate(this); |
2198 layer()->set_name(GetClassName()); | 2229 layer()->set_name(GetClassName()); |
2199 | 2230 |
2200 UpdateParentLayers(); | 2231 UpdateParentLayers(); |
2201 UpdateLayerVisibility(); | 2232 UpdateLayerVisibility(); |
2202 | 2233 |
2203 // The new layer needs to be ordered in the layer tree according | 2234 // The new layer needs to be ordered in the layer tree according |
2204 // to the view tree. Children of this layer were added in order | 2235 // to the view tree. Children of this layer were added in order |
2205 // in UpdateParentLayers(). | 2236 // in UpdateParentLayers(). |
2206 if (parent()) | 2237 if (parent()) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2253 | 2284 |
2254 void View::ReparentLayer(const gfx::Vector2d& offset, ui::Layer* parent_layer) { | 2285 void View::ReparentLayer(const gfx::Vector2d& offset, ui::Layer* parent_layer) { |
2255 layer()->SetBounds(GetLocalBounds() + offset); | 2286 layer()->SetBounds(GetLocalBounds() + offset); |
2256 DCHECK_NE(layer(), parent_layer); | 2287 DCHECK_NE(layer(), parent_layer); |
2257 if (parent_layer) | 2288 if (parent_layer) |
2258 parent_layer->Add(layer()); | 2289 parent_layer->Add(layer()); |
2259 layer()->SchedulePaint(GetLocalBounds()); | 2290 layer()->SchedulePaint(GetLocalBounds()); |
2260 MoveLayerToParent(layer(), gfx::Point()); | 2291 MoveLayerToParent(layer(), gfx::Point()); |
2261 } | 2292 } |
2262 | 2293 |
2263 void View::DestroyLayer() { | |
2264 ui::Layer* new_parent = layer()->parent(); | |
2265 std::vector<ui::Layer*> children = layer()->children(); | |
2266 for (size_t i = 0; i < children.size(); ++i) { | |
2267 layer()->Remove(children[i]); | |
2268 if (new_parent) | |
2269 new_parent->Add(children[i]); | |
2270 } | |
2271 | |
2272 LayerOwner::DestroyLayer(); | |
2273 | |
2274 if (new_parent) | |
2275 ReorderLayers(); | |
2276 | |
2277 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL)); | |
2278 | |
2279 SchedulePaint(); | |
2280 | |
2281 Widget* widget = GetWidget(); | |
2282 if (widget) | |
2283 widget->UpdateRootLayers(); | |
2284 } | |
2285 | |
2286 // Input ----------------------------------------------------------------------- | 2294 // Input ----------------------------------------------------------------------- |
2287 | 2295 |
2288 bool View::ProcessMousePressed(const ui::MouseEvent& event) { | 2296 bool View::ProcessMousePressed(const ui::MouseEvent& event) { |
2289 int drag_operations = | 2297 int drag_operations = |
2290 (enabled_ && event.IsOnlyLeftMouseButton() && | 2298 (enabled_ && event.IsOnlyLeftMouseButton() && |
2291 HitTestPoint(event.location())) ? | 2299 HitTestPoint(event.location())) ? |
2292 GetDragOperations(event.location()) : 0; | 2300 GetDragOperations(event.location()) : 0; |
2293 ContextMenuController* context_menu_controller = event.IsRightMouseButton() ? | 2301 ContextMenuController* context_menu_controller = event.IsRightMouseButton() ? |
2294 context_menu_controller_ : 0; | 2302 context_menu_controller_ : 0; |
2295 View::DragInfo* drag_info = GetDragInfo(); | 2303 View::DragInfo* drag_info = GetDragInfo(); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2546 // Message the RootView to do the drag and drop. That way if we're removed | 2554 // Message the RootView to do the drag and drop. That way if we're removed |
2547 // the RootView can detect it and avoid calling us back. | 2555 // the RootView can detect it and avoid calling us back. |
2548 gfx::Point widget_location(event.location()); | 2556 gfx::Point widget_location(event.location()); |
2549 ConvertPointToWidget(this, &widget_location); | 2557 ConvertPointToWidget(this, &widget_location); |
2550 widget->RunShellDrag(this, data, widget_location, drag_operations, source); | 2558 widget->RunShellDrag(this, data, widget_location, drag_operations, source); |
2551 // WARNING: we may have been deleted. | 2559 // WARNING: we may have been deleted. |
2552 return true; | 2560 return true; |
2553 } | 2561 } |
2554 | 2562 |
2555 } // namespace views | 2563 } // namespace views |
OLD | NEW |