Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "ash/common/system/tray/three_view_layout.h" | |
| 6 | |
| 7 #include "ash/common/system/tray/size_range_layout.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "ui/views/border.h" | |
| 10 #include "ui/views/layout/box_layout.h" | |
| 11 #include "ui/views/layout/fill_layout.h" | |
| 12 | |
| 13 namespace ash { | |
| 14 namespace { | |
| 15 | |
| 16 // Converts ThreeViewLayout::Orientation values to | |
| 17 // views::BoxLayout::Orientation values. | |
| 18 views::BoxLayout::Orientation GetOrientation( | |
| 19 ThreeViewLayout::Orientation orientation) { | |
| 20 switch (orientation) { | |
| 21 case ThreeViewLayout::Orientation::HORIZONTAL: | |
| 22 return views::BoxLayout::kHorizontal; | |
| 23 case ThreeViewLayout::Orientation::VERTICAL: | |
| 24 return views::BoxLayout::kVertical; | |
| 25 } | |
| 26 // Required for some compilers. | |
| 27 NOTREACHED(); | |
| 28 return views::BoxLayout::kHorizontal; | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 ThreeViewLayout::ThreeViewLayout() : ThreeViewLayout(0) {} | |
| 34 | |
| 35 ThreeViewLayout::ThreeViewLayout(int padding_between_containers) | |
| 36 : ThreeViewLayout(Orientation::HORIZONTAL, padding_between_containers) {} | |
| 37 | |
| 38 ThreeViewLayout::ThreeViewLayout(Orientation orientation) | |
| 39 : ThreeViewLayout(orientation, 0) {} | |
| 40 | |
| 41 ThreeViewLayout::ThreeViewLayout(Orientation orientation, | |
| 42 int padding_between_containers) | |
| 43 : box_layout_(new views::BoxLayout(GetOrientation(orientation), | |
| 44 0, | |
| 45 0, | |
| 46 padding_between_containers)), | |
| 47 start_container_(new views::View), | |
|
sky
2016/10/21 16:35:11
MakeUnique where applicable now.
| |
| 48 start_container_layout_manager_(new SizeRangeLayout), | |
| 49 center_container_(new views::View), | |
| 50 center_container_layout_manager_(new SizeRangeLayout), | |
| 51 end_container_(new views::View), | |
| 52 end_container_layout_manager_(new SizeRangeLayout) { | |
| 53 start_container_->set_owned_by_client(); | |
| 54 center_container_->set_owned_by_client(); | |
| 55 end_container_->set_owned_by_client(); | |
| 56 | |
| 57 start_container_->SetLayoutManager(GetLayoutManager(Container::START)); | |
| 58 center_container_->SetLayoutManager(GetLayoutManager(Container::CENTER)); | |
| 59 end_container_->SetLayoutManager(GetLayoutManager(Container::END)); | |
| 60 | |
| 61 GetLayoutManager(Container::START) | |
| 62 ->SetLayoutManager(CreateDefaultLayoutManager(orientation)); | |
| 63 GetLayoutManager(Container::CENTER) | |
| 64 ->SetLayoutManager(CreateDefaultLayoutManager(orientation)); | |
| 65 GetLayoutManager(Container::END) | |
| 66 ->SetLayoutManager(CreateDefaultLayoutManager(orientation)); | |
| 67 } | |
| 68 | |
| 69 ThreeViewLayout::~ThreeViewLayout() {} | |
| 70 | |
| 71 void ThreeViewLayout::SetMinCrossAxisSize(int min_size) { | |
| 72 box_layout_->set_minimum_cross_axis_size(min_size); | |
| 73 } | |
| 74 | |
| 75 void ThreeViewLayout::SetMinSize(Container container, const gfx::Size& size) { | |
| 76 GetLayoutManager(container)->SetMinSize(size); | |
| 77 } | |
| 78 | |
| 79 void ThreeViewLayout::SetMaxSize(Container container, const gfx::Size& size) { | |
| 80 GetLayoutManager(container)->SetMaxSize(size); | |
| 81 } | |
| 82 | |
| 83 void ThreeViewLayout::AddView(Container container, views::View* view) { | |
| 84 GetContainer(container)->AddChildView(view); | |
| 85 } | |
| 86 | |
| 87 void ThreeViewLayout::SetView(Container container, views::View* view) { | |
| 88 views::View* container_view = GetContainer(container); | |
| 89 container_view->RemoveAllChildViews(true); | |
| 90 container_view->AddChildView(view); | |
| 91 } | |
| 92 | |
| 93 void ThreeViewLayout::SetInsets(const gfx::Insets& insets) { | |
| 94 box_layout_->set_inside_border_insets(insets); | |
| 95 } | |
| 96 | |
| 97 void ThreeViewLayout::SetBorder(Container container, | |
| 98 std::unique_ptr<views::Border> border) { | |
| 99 GetContainer(container)->SetBorder(std::move(border)); | |
| 100 } | |
| 101 | |
| 102 void ThreeViewLayout::SetContainerVisible(Container container, bool visible) { | |
| 103 GetContainer(container)->SetVisible(visible); | |
| 104 } | |
| 105 | |
| 106 void ThreeViewLayout::SetFlexForContainer(Container container, int flex) { | |
| 107 box_layout_->SetFlexForView(GetContainer(container), flex); | |
| 108 } | |
| 109 | |
| 110 void ThreeViewLayout::SetLayoutManager( | |
| 111 Container container, | |
| 112 std::unique_ptr<LayoutManager> layout_manager) { | |
| 113 GetLayoutManager(container)->SetLayoutManager(std::move(layout_manager)); | |
| 114 if (GetLayoutManager(container)) | |
| 115 GetLayoutManager(container)->Installed(GetContainer(container)); | |
| 116 } | |
| 117 | |
| 118 void ThreeViewLayout::Installed(views::View* host) { | |
| 119 DCHECK(!host_); | |
| 120 host_ = host; | |
| 121 if (host_) { | |
| 122 host_->AddChildView(GetContainer(Container::START)); | |
| 123 host_->AddChildView(GetContainer(Container::CENTER)); | |
| 124 host_->AddChildView(GetContainer(Container::END)); | |
| 125 } | |
| 126 box_layout_->Installed(host_); | |
| 127 } | |
| 128 | |
| 129 void ThreeViewLayout::Layout(views::View* host) { | |
| 130 box_layout_->Layout(host); | |
| 131 } | |
| 132 | |
| 133 gfx::Size ThreeViewLayout::GetPreferredSize(const views::View* host) const { | |
| 134 return box_layout_->GetPreferredSize(host); | |
| 135 } | |
| 136 | |
| 137 int ThreeViewLayout::GetPreferredHeightForWidth(const views::View* host, | |
| 138 int width) const { | |
| 139 return box_layout_->GetPreferredHeightForWidth(host, width); | |
| 140 } | |
| 141 | |
| 142 void ThreeViewLayout::ViewAdded(views::View* host, views::View* view) { | |
| 143 box_layout_->ViewAdded(host, view); | |
| 144 } | |
| 145 | |
| 146 void ThreeViewLayout::ViewRemoved(views::View* host, views::View* view) { | |
| 147 box_layout_->ViewRemoved(host, view); | |
| 148 } | |
| 149 | |
| 150 std::unique_ptr<views::LayoutManager> | |
| 151 ThreeViewLayout::CreateDefaultLayoutManager(Orientation orientation) const { | |
| 152 views::BoxLayout* box_layout = | |
| 153 new views::BoxLayout(GetOrientation(orientation), 0, 0, 0); | |
| 154 box_layout->set_main_axis_alignment( | |
| 155 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | |
| 156 box_layout->set_cross_axis_alignment( | |
| 157 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); | |
| 158 return std::unique_ptr<views::LayoutManager>(box_layout); | |
| 159 } | |
| 160 | |
| 161 views::View* ThreeViewLayout::GetContainer(Container container) const { | |
| 162 switch (container) { | |
| 163 case Container::START: | |
| 164 return start_container_.get(); | |
| 165 case Container::CENTER: | |
| 166 return center_container_.get(); | |
| 167 case Container::END: | |
| 168 return end_container_.get(); | |
| 169 } | |
| 170 // Required for some compilers. | |
| 171 NOTREACHED(); | |
| 172 return nullptr; | |
| 173 } | |
| 174 | |
| 175 SizeRangeLayout* ThreeViewLayout::GetLayoutManager(Container container) const { | |
| 176 switch (container) { | |
| 177 case Container::START: | |
| 178 return start_container_layout_manager_; | |
| 179 case Container::CENTER: | |
| 180 return center_container_layout_manager_; | |
| 181 case Container::END: | |
| 182 return end_container_layout_manager_; | |
| 183 } | |
| 184 // Required for some compilers. | |
| 185 NOTREACHED(); | |
| 186 return nullptr; | |
| 187 } | |
| 188 | |
| 189 } // namespace ash | |
| OLD | NEW |