OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "athena/home/public/home_card.h" | 5 #include "athena/home/public/home_card.h" |
6 | 6 |
7 #include "athena/home/app_list_view_delegate.h" | 7 #include "athena/home/app_list_view_delegate.h" |
| 8 #include "athena/home/minimized_home.h" |
8 #include "athena/home/public/app_model_builder.h" | 9 #include "athena/home/public/app_model_builder.h" |
9 #include "athena/input/public/accelerator_manager.h" | 10 #include "athena/input/public/accelerator_manager.h" |
10 #include "athena/screen/public/screen_manager.h" | 11 #include "athena/screen/public/screen_manager.h" |
| 12 #include "athena/wm/public/window_manager.h" |
| 13 #include "athena/wm/public/window_manager_observer.h" |
| 14 #include "base/bind.h" |
11 #include "ui/app_list/search_provider.h" | 15 #include "ui/app_list/search_provider.h" |
12 #include "ui/app_list/views/app_list_view.h" | 16 #include "ui/app_list/views/app_list_view.h" |
13 #include "ui/aura/layout_manager.h" | 17 #include "ui/aura/layout_manager.h" |
14 #include "ui/aura/window.h" | 18 #include "ui/aura/window.h" |
| 19 #include "ui/views/layout/box_layout.h" |
15 #include "ui/wm/core/visibility_controller.h" | 20 #include "ui/wm/core/visibility_controller.h" |
16 #include "ui/wm/core/window_animations.h" | 21 #include "ui/wm/core/window_animations.h" |
17 | 22 |
18 namespace athena { | 23 namespace athena { |
19 namespace { | 24 namespace { |
20 | 25 |
21 HomeCard* instance = NULL; | 26 HomeCard* instance = NULL; |
22 | 27 |
| 28 // Makes sure the homecard is center-aligned horizontally and bottom-aligned |
| 29 // vertically. |
23 class HomeCardLayoutManager : public aura::LayoutManager { | 30 class HomeCardLayoutManager : public aura::LayoutManager { |
24 public: | 31 public: |
25 explicit HomeCardLayoutManager(aura::Window* container) | 32 class Delegate { |
26 : container_(container) {} | 33 public: |
| 34 virtual ~Delegate() {} |
| 35 |
| 36 virtual int GetHomeCardHeight() const = 0; |
| 37 |
| 38 virtual int GetHorizontalMargin() const = 0; |
| 39 |
| 40 // TODO(mukai): Remove this when bubble is no longer used for |
| 41 // VISIBLE_CENTERED or VISIBLE_BOTTOM states. |
| 42 virtual bool HasShadow() const = 0; |
| 43 |
| 44 virtual aura::Window* GetNativeWindow() = 0; |
| 45 }; |
| 46 |
| 47 explicit HomeCardLayoutManager(Delegate* delegate) |
| 48 : delegate_(delegate) {} |
27 virtual ~HomeCardLayoutManager() {} | 49 virtual ~HomeCardLayoutManager() {} |
28 | 50 |
29 void UpdateVirtualKeyboardBounds(const gfx::Rect& bounds) { | 51 void UpdateVirtualKeyboardBounds(const gfx::Rect& bounds) { |
30 virtual_keyboard_bounds_ = bounds; | 52 virtual_keyboard_bounds_ = bounds; |
31 Layout(); | 53 Layout(); |
32 } | 54 } |
33 | 55 |
34 private: | 56 private: |
35 // aura::LayoutManager: | 57 // aura::LayoutManager: |
36 virtual void OnWindowResized() OVERRIDE { Layout(); } | 58 virtual void OnWindowResized() OVERRIDE { Layout(); } |
37 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { Layout(); } | 59 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { Layout(); } |
38 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} | 60 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} |
39 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE { | 61 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE { |
40 Layout(); | 62 Layout(); |
41 } | 63 } |
42 virtual void OnChildWindowVisibilityChanged(aura::Window* child, | 64 virtual void OnChildWindowVisibilityChanged(aura::Window* child, |
43 bool visible) OVERRIDE { | 65 bool visible) OVERRIDE { |
44 Layout(); | 66 Layout(); |
45 } | 67 } |
46 virtual void SetChildBounds(aura::Window* child, | 68 virtual void SetChildBounds(aura::Window* child, |
47 const gfx::Rect& requested_bounds) OVERRIDE { | 69 const gfx::Rect& requested_bounds) OVERRIDE { |
48 SetChildBoundsDirect(child, gfx::Rect(requested_bounds.size())); | 70 SetChildBoundsDirect(child, gfx::Rect(requested_bounds.size())); |
49 } | 71 } |
50 | 72 |
51 void Layout() { | 73 void Layout() { |
52 const int kHomeCardHeight = 150; | 74 int height = delegate_->GetHomeCardHeight(); |
53 const int kHomeCardHorizontalMargin = 50; | 75 int horiz_margin = delegate_->GetHorizontalMargin(); |
54 // Currently the home card is provided as a bubble and the bounds has to be | 76 aura::Window* home_card = delegate_->GetNativeWindow(); |
55 // increased to cancel the shadow. | 77 // |home_card| could be detached from the root window (e.g. when it is being |
56 // TODO(mukai): stops using the bubble and remove this. | 78 // destroyed). |
57 const int kHomeCardShadowWidth = 30; | 79 if (!home_card || !home_card->GetRootWindow()) |
58 if (container_->children().size() < 1) | |
59 return; | |
60 aura::Window* home_card = container_->children()[0]; | |
61 if (!home_card->IsVisible()) | |
62 return; | 80 return; |
63 | 81 |
64 gfx::Rect screen_bounds = home_card->GetRootWindow()->bounds(); | 82 gfx::Rect screen_bounds = home_card->GetRootWindow()->bounds(); |
65 if (!virtual_keyboard_bounds_.IsEmpty()) | 83 if (!virtual_keyboard_bounds_.IsEmpty()) |
66 screen_bounds.set_height(virtual_keyboard_bounds_.y()); | 84 screen_bounds.set_height(virtual_keyboard_bounds_.y()); |
67 gfx::Rect card_bounds = screen_bounds; | 85 gfx::Rect card_bounds = screen_bounds; |
68 card_bounds.Inset(kHomeCardHorizontalMargin, | 86 card_bounds.Inset(horiz_margin, screen_bounds.height() - height, |
69 screen_bounds.height() - kHomeCardHeight, | 87 horiz_margin, 0); |
70 kHomeCardHorizontalMargin, | 88 |
71 0); | 89 if (delegate_->HasShadow()) { |
72 card_bounds.Inset(-kHomeCardShadowWidth, -kHomeCardShadowWidth); | 90 // Currently the home card is provided as a bubble and the bounds has to |
| 91 // be increased to cancel the shadow. |
| 92 // TODO(mukai): stops using the bubble and remove this. |
| 93 const int kHomeCardShadowWidth = 30; |
| 94 card_bounds.Inset(-kHomeCardShadowWidth, -kHomeCardShadowWidth); |
| 95 } |
73 SetChildBoundsDirect(home_card, card_bounds); | 96 SetChildBoundsDirect(home_card, card_bounds); |
74 } | 97 } |
75 | 98 |
76 aura::Window* container_; | 99 Delegate* delegate_; |
77 gfx::Rect virtual_keyboard_bounds_; | 100 gfx::Rect virtual_keyboard_bounds_; |
78 | 101 |
79 DISALLOW_COPY_AND_ASSIGN(HomeCardLayoutManager); | 102 DISALLOW_COPY_AND_ASSIGN(HomeCardLayoutManager); |
80 }; | 103 }; |
81 | 104 |
82 class HomeCardImpl : public HomeCard, public AcceleratorHandler { | 105 class HomeCardImpl : public HomeCard, |
| 106 public AcceleratorHandler, |
| 107 public HomeCardLayoutManager::Delegate, |
| 108 public MinimizedHomeDragDelegate, |
| 109 public WindowManagerObserver { |
83 public: | 110 public: |
84 explicit HomeCardImpl(AppModelBuilder* model_builder); | 111 explicit HomeCardImpl(AppModelBuilder* model_builder); |
85 virtual ~HomeCardImpl(); | 112 virtual ~HomeCardImpl(); |
86 | 113 |
87 void Init(); | 114 void Init(); |
88 | 115 |
89 private: | 116 private: |
90 enum Command { | 117 enum Command { |
91 COMMAND_SHOW_HOME_CARD, | 118 COMMAND_SHOW_HOME_CARD, |
92 }; | 119 }; |
(...skipping 11 matching lines...) Expand all Loading... |
104 virtual bool OnAcceleratorFired(int command_id, | 131 virtual bool OnAcceleratorFired(int command_id, |
105 const ui::Accelerator& accelerator) OVERRIDE { | 132 const ui::Accelerator& accelerator) OVERRIDE { |
106 DCHECK_EQ(COMMAND_SHOW_HOME_CARD, command_id); | 133 DCHECK_EQ(COMMAND_SHOW_HOME_CARD, command_id); |
107 if (state_ == HIDDEN) | 134 if (state_ == HIDDEN) |
108 SetState(VISIBLE_CENTERED); | 135 SetState(VISIBLE_CENTERED); |
109 else | 136 else |
110 SetState(HIDDEN); | 137 SetState(HIDDEN); |
111 return true; | 138 return true; |
112 } | 139 } |
113 | 140 |
| 141 // HomeCardLayoutManager::Delegate: |
| 142 virtual int GetHomeCardHeight() const OVERRIDE { |
| 143 const int kHomeCardHeight = 150; |
| 144 const int kHomeCardMinimizedHeight = 8; |
| 145 CHECK_NE(HIDDEN, state_); |
| 146 return state_ == VISIBLE_MINIMIZED ? kHomeCardMinimizedHeight : |
| 147 kHomeCardHeight; |
| 148 } |
| 149 |
| 150 virtual int GetHorizontalMargin() const OVERRIDE { |
| 151 CHECK_NE(HIDDEN, state_); |
| 152 const int kHomeCardHorizontalMargin = 50; |
| 153 return state_ == VISIBLE_MINIMIZED ? 0 : kHomeCardHorizontalMargin; |
| 154 } |
| 155 |
| 156 virtual bool HasShadow() const OVERRIDE { |
| 157 CHECK_NE(HIDDEN, state_); |
| 158 return state_ != VISIBLE_MINIMIZED; |
| 159 } |
| 160 |
| 161 virtual aura::Window* GetNativeWindow() OVERRIDE { |
| 162 switch (state_) { |
| 163 case HIDDEN: |
| 164 return NULL; |
| 165 case VISIBLE_MINIMIZED: |
| 166 return minimized_widget_ ? minimized_widget_->GetNativeWindow() : NULL; |
| 167 case VISIBLE_CENTERED: |
| 168 case VISIBLE_BOTTOM: |
| 169 return home_card_widget_ ? home_card_widget_->GetNativeWindow() : NULL; |
| 170 } |
| 171 return NULL; |
| 172 } |
| 173 |
| 174 // MinimizedHomeDragDelegate: |
| 175 virtual void OnDragUpCompleted() OVERRIDE { |
| 176 WindowManager::GetInstance()->ToggleOverview(); |
| 177 } |
| 178 |
| 179 // WindowManagerObserver: |
| 180 virtual void OnOverviewModeEnter() OVERRIDE { |
| 181 SetState(VISIBLE_BOTTOM); |
| 182 } |
| 183 |
| 184 virtual void OnOverviewModeExit() OVERRIDE { |
| 185 SetState(VISIBLE_MINIMIZED); |
| 186 } |
| 187 |
114 scoped_ptr<AppModelBuilder> model_builder_; | 188 scoped_ptr<AppModelBuilder> model_builder_; |
115 | 189 |
116 HomeCard::State state_; | 190 HomeCard::State state_; |
117 | 191 |
118 views::Widget* home_card_widget_; | 192 views::Widget* home_card_widget_; |
| 193 views::Widget* minimized_widget_; |
119 AppListViewDelegate* view_delegate_; | 194 AppListViewDelegate* view_delegate_; |
120 HomeCardLayoutManager* layout_manager_; | 195 HomeCardLayoutManager* layout_manager_; |
121 | 196 |
122 // Right now HomeCard allows only one search provider. | 197 // Right now HomeCard allows only one search provider. |
123 // TODO(mukai): port app-list's SearchController and Mixer. | 198 // TODO(mukai): port app-list's SearchController and Mixer. |
124 scoped_ptr<app_list::SearchProvider> search_provider_; | 199 scoped_ptr<app_list::SearchProvider> search_provider_; |
125 | 200 |
126 DISALLOW_COPY_AND_ASSIGN(HomeCardImpl); | 201 DISALLOW_COPY_AND_ASSIGN(HomeCardImpl); |
127 }; | 202 }; |
128 | 203 |
129 HomeCardImpl::HomeCardImpl(AppModelBuilder* model_builder) | 204 HomeCardImpl::HomeCardImpl(AppModelBuilder* model_builder) |
130 : model_builder_(model_builder), | 205 : model_builder_(model_builder), |
131 state_(HIDDEN), | 206 state_(VISIBLE_MINIMIZED), |
132 home_card_widget_(NULL), | 207 home_card_widget_(NULL), |
| 208 minimized_widget_(NULL), |
133 layout_manager_(NULL) { | 209 layout_manager_(NULL) { |
134 DCHECK(!instance); | 210 DCHECK(!instance); |
135 instance = this; | 211 instance = this; |
| 212 WindowManager::GetInstance()->AddObserver(this); |
136 } | 213 } |
137 | 214 |
138 HomeCardImpl::~HomeCardImpl() { | 215 HomeCardImpl::~HomeCardImpl() { |
139 DCHECK(instance); | 216 DCHECK(instance); |
| 217 WindowManager::GetInstance()->RemoveObserver(this); |
140 home_card_widget_->CloseNow(); | 218 home_card_widget_->CloseNow(); |
| 219 minimized_widget_->CloseNow(); |
141 view_delegate_ = NULL; | 220 view_delegate_ = NULL; |
142 instance = NULL; | 221 instance = NULL; |
143 } | 222 } |
144 | 223 |
145 void HomeCardImpl::SetState(HomeCard::State state) { | 224 void HomeCardImpl::SetState(HomeCard::State state) { |
146 if (state == HIDDEN) | 225 // Update |state_| before changing the visibility of the widgets, so that |
147 home_card_widget_->Hide(); | 226 // LayoutManager callbacks get the correct state. |
148 else | |
149 home_card_widget_->Show(); | |
150 state_ = state; | 227 state_ = state; |
| 228 switch (state_) { |
| 229 case VISIBLE_MINIMIZED: |
| 230 home_card_widget_->Hide(); |
| 231 minimized_widget_->Show(); |
| 232 break; |
| 233 case HIDDEN: |
| 234 home_card_widget_->Hide(); |
| 235 minimized_widget_->Hide(); |
| 236 break; |
| 237 case VISIBLE_BOTTOM: |
| 238 case VISIBLE_CENTERED: |
| 239 home_card_widget_->Show(); |
| 240 minimized_widget_->Hide(); |
| 241 break; |
| 242 } |
151 } | 243 } |
152 | 244 |
153 void HomeCardImpl::RegisterSearchProvider( | 245 void HomeCardImpl::RegisterSearchProvider( |
154 app_list::SearchProvider* search_provider) { | 246 app_list::SearchProvider* search_provider) { |
155 DCHECK(!search_provider_); | 247 DCHECK(!search_provider_); |
156 search_provider_.reset(search_provider); | 248 search_provider_.reset(search_provider); |
157 view_delegate_->RegisterSearchProvider(search_provider_.get()); | 249 view_delegate_->RegisterSearchProvider(search_provider_.get()); |
158 } | 250 } |
159 | 251 |
160 void HomeCardImpl::UpdateVirtualKeyboardBounds( | 252 void HomeCardImpl::UpdateVirtualKeyboardBounds( |
161 const gfx::Rect& bounds) { | 253 const gfx::Rect& bounds) { |
| 254 if (state_ == VISIBLE_MINIMIZED) { |
| 255 if (bounds.IsEmpty()) |
| 256 minimized_widget_->Show(); |
| 257 else |
| 258 minimized_widget_->Hide(); |
| 259 } |
162 layout_manager_->UpdateVirtualKeyboardBounds(bounds); | 260 layout_manager_->UpdateVirtualKeyboardBounds(bounds); |
163 } | 261 } |
164 | 262 |
165 void HomeCardImpl::Init() { | 263 void HomeCardImpl::Init() { |
166 InstallAccelerators(); | 264 InstallAccelerators(); |
167 | 265 |
168 aura::Window* container = | 266 aura::Window* container = |
169 ScreenManager::Get()->CreateContainer("HomeCardContainer"); | 267 ScreenManager::Get()->CreateContainer("HomeCardContainer"); |
170 layout_manager_ = new HomeCardLayoutManager(container); | 268 layout_manager_ = new HomeCardLayoutManager(this); |
171 container->SetLayoutManager(layout_manager_); | 269 container->SetLayoutManager(layout_manager_); |
172 wm::SetChildWindowVisibilityChangesAnimated(container); | 270 wm::SetChildWindowVisibilityChangesAnimated(container); |
173 | 271 |
174 view_delegate_ = new AppListViewDelegate(model_builder_.get()); | 272 view_delegate_ = new AppListViewDelegate(model_builder_.get()); |
175 if (search_provider_) | 273 if (search_provider_) |
176 view_delegate_->RegisterSearchProvider(search_provider_.get()); | 274 view_delegate_->RegisterSearchProvider(search_provider_.get()); |
177 app_list::AppListView* view = new app_list::AppListView(view_delegate_); | 275 app_list::AppListView* view = new app_list::AppListView(view_delegate_); |
178 view->InitAsBubbleAtFixedLocation( | 276 view->InitAsBubbleAtFixedLocation( |
179 container, | 277 container, |
180 0 /* initial_apps_page */, | 278 0 /* initial_apps_page */, |
181 gfx::Point(), | 279 gfx::Point(), |
182 views::BubbleBorder::FLOAT, | 280 views::BubbleBorder::FLOAT, |
183 true /* border_accepts_events */); | 281 true /* border_accepts_events */); |
184 home_card_widget_ = view->GetWidget(); | 282 home_card_widget_ = view->GetWidget(); |
185 // TODO: the initial value might not be visible. | 283 |
186 state_ = VISIBLE_CENTERED; | 284 // Start off in the minimized state. |
187 view->ShowWhenReady(); | 285 minimized_widget_ = CreateMinimizedHome(container, this); |
| 286 SetState(VISIBLE_MINIMIZED); |
188 } | 287 } |
189 | 288 |
190 void HomeCardImpl::InstallAccelerators() { | 289 void HomeCardImpl::InstallAccelerators() { |
191 const AcceleratorData accelerator_data[] = { | 290 const AcceleratorData accelerator_data[] = { |
192 {TRIGGER_ON_PRESS, ui::VKEY_L, ui::EF_CONTROL_DOWN, | 291 {TRIGGER_ON_PRESS, ui::VKEY_L, ui::EF_CONTROL_DOWN, |
193 COMMAND_SHOW_HOME_CARD, AF_NONE}, | 292 COMMAND_SHOW_HOME_CARD, AF_NONE}, |
194 }; | 293 }; |
195 AcceleratorManager::Get()->RegisterAccelerators( | 294 AcceleratorManager::Get()->RegisterAccelerators( |
196 accelerator_data, arraysize(accelerator_data), this); | 295 accelerator_data, arraysize(accelerator_data), this); |
197 } | 296 } |
(...skipping 14 matching lines...) Expand all Loading... |
212 instance = NULL; | 311 instance = NULL; |
213 } | 312 } |
214 | 313 |
215 // static | 314 // static |
216 HomeCard* HomeCard::Get() { | 315 HomeCard* HomeCard::Get() { |
217 DCHECK(instance); | 316 DCHECK(instance); |
218 return instance; | 317 return instance; |
219 } | 318 } |
220 | 319 |
221 } // namespace athena | 320 } // namespace athena |
OLD | NEW |