blob: 5d830c6bf4941b2fb8456adad3ed6957e5baafd3 [file] [log] [blame]
Sean Gilhuly5e4e3fd2019-01-29 20:50:471// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/ui_devtools/viz/viz_element.h"
6
7#include <algorithm>
8
9#include "components/ui_devtools/ui_element_delegate.h"
10#include "components/ui_devtools/viz/frame_sink_element.h"
11#include "components/ui_devtools/viz/surface_element.h"
12
13namespace ui_devtools {
14
15namespace {
16
17bool IsVizElement(const UIElement* element) {
18 return element->type() == UIElementType::FRAMESINK ||
19 element->type() == UIElementType::SURFACE;
20}
21
22bool CompareVizElements(const UIElement* a, const UIElement* b) {
23 if (a->type() == UIElementType::FRAMESINK) {
24 if (b->type() == UIElementType::SURFACE)
25 return true;
26 // From() checks that |b| is a FrameSinkElement.
27 return FrameSinkElement::From(a) < FrameSinkElement::From(b);
28 } else {
29 if (b->type() == UIElementType::FRAMESINK)
30 return false;
31 // From() checks that |a| and |b| are SurfaceElements.
32 return SurfaceElement::From(a) < SurfaceElement::From(b);
33 }
34}
35
36} // namespace
37
38VizElement::VizElement(const UIElementType type,
39 UIElementDelegate* delegate,
40 UIElement* parent)
41 : UIElement(type, delegate, parent) {}
42
43VizElement::~VizElement() {}
44
45void VizElement::AddToParentSorted(UIElement* parent, bool notify_delegate) {
46 parent->AddOrderedChild(this, &CompareVizElements, notify_delegate);
47}
48
49void VizElement::Reparent(UIElement* new_parent) {
50 if (parent() == new_parent)
51 return;
52 // Become a child of |new_parent| without calling OnUIElementRemoved or
53 // OnUIElementAdded on |delegate_|. Instead, call OnUIElementReordered, which
54 // destroys and rebuilds the DOM node in the new location.
55 parent()->RemoveChild(this, false);
56 AddToParentSorted(new_parent, false);
57 delegate()->OnUIElementReordered(new_parent, this);
58 set_parent(new_parent);
59}
60
61VizElement* VizElement::AsVizElement(UIElement* element) {
62 DCHECK(IsVizElement(element));
63 return static_cast<VizElement*>(element);
64}
65
66} // namespace ui_devtools