Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(297)

Side by Side Diff: ui/views/view.cc

Issue 2500623002: Add ViewObserver to View for view updates (Closed)
Patch Set: Remove Rect forward declaration and view.h include Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/view.h ('k') | ui/views/view_observer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 #include "ui/gfx/skia_util.h" 44 #include "ui/gfx/skia_util.h"
45 #include "ui/gfx/transform.h" 45 #include "ui/gfx/transform.h"
46 #include "ui/native_theme/native_theme.h" 46 #include "ui/native_theme/native_theme.h"
47 #include "ui/views/accessibility/native_view_accessibility.h" 47 #include "ui/views/accessibility/native_view_accessibility.h"
48 #include "ui/views/background.h" 48 #include "ui/views/background.h"
49 #include "ui/views/border.h" 49 #include "ui/views/border.h"
50 #include "ui/views/context_menu_controller.h" 50 #include "ui/views/context_menu_controller.h"
51 #include "ui/views/drag_controller.h" 51 #include "ui/views/drag_controller.h"
52 #include "ui/views/focus/view_storage.h" 52 #include "ui/views/focus/view_storage.h"
53 #include "ui/views/layout/layout_manager.h" 53 #include "ui/views/layout/layout_manager.h"
54 #include "ui/views/view_observer.h"
54 #include "ui/views/views_delegate.h" 55 #include "ui/views/views_delegate.h"
55 #include "ui/views/widget/native_widget_private.h" 56 #include "ui/views/widget/native_widget_private.h"
56 #include "ui/views/widget/root_view.h" 57 #include "ui/views/widget/root_view.h"
57 #include "ui/views/widget/tooltip_manager.h" 58 #include "ui/views/widget/tooltip_manager.h"
58 #include "ui/views/widget/widget.h" 59 #include "ui/views/widget/widget.h"
59 60
60 #if defined(OS_WIN) 61 #if defined(OS_WIN)
61 #include "base/win/scoped_gdi_object.h" 62 #include "base/win/scoped_gdi_object.h"
62 #include "ui/native_theme/native_theme_win.h" 63 #include "ui/native_theme/native_theme_win.h"
63 #endif 64 #endif
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 214
214 if (widget) { 215 if (widget) {
215 RegisterChildrenForVisibleBoundsNotification(view); 216 RegisterChildrenForVisibleBoundsNotification(view);
216 217
217 if (view->visible()) 218 if (view->visible())
218 view->SchedulePaint(); 219 view->SchedulePaint();
219 } 220 }
220 221
221 if (layout_manager_.get()) 222 if (layout_manager_.get())
222 layout_manager_->ViewAdded(this, view); 223 layout_manager_->ViewAdded(this, view);
224
225 for (ViewObserver& observer : observers_)
226 observer.OnChildViewAdded(view);
223 } 227 }
224 228
225 void View::ReorderChildView(View* view, int index) { 229 void View::ReorderChildView(View* view, int index) {
226 DCHECK_EQ(view->parent_, this); 230 DCHECK_EQ(view->parent_, this);
227 if (index < 0) 231 if (index < 0)
228 index = child_count() - 1; 232 index = child_count() - 1;
229 else if (index >= child_count()) 233 else if (index >= child_count())
230 return; 234 return;
231 if (children_[index] == view) 235 if (children_[index] == view)
232 return; 236 return;
233 237
234 const Views::iterator i(std::find(children_.begin(), children_.end(), view)); 238 const Views::iterator i(std::find(children_.begin(), children_.end(), view));
235 DCHECK(i != children_.end()); 239 DCHECK(i != children_.end());
236 children_.erase(i); 240 children_.erase(i);
237 241
238 // Unlink the view first 242 // Unlink the view first
239 View* next_focusable = view->next_focusable_view_; 243 View* next_focusable = view->next_focusable_view_;
240 View* prev_focusable = view->previous_focusable_view_; 244 View* prev_focusable = view->previous_focusable_view_;
241 if (prev_focusable) 245 if (prev_focusable)
242 prev_focusable->next_focusable_view_ = next_focusable; 246 prev_focusable->next_focusable_view_ = next_focusable;
243 if (next_focusable) 247 if (next_focusable)
244 next_focusable->previous_focusable_view_ = prev_focusable; 248 next_focusable->previous_focusable_view_ = prev_focusable;
245 249
246 // Add it in the specified index now. 250 // Add it in the specified index now.
247 InitFocusSiblings(view, index); 251 InitFocusSiblings(view, index);
248 children_.insert(children_.begin() + index, view); 252 children_.insert(children_.begin() + index, view);
249 253
254 for (ViewObserver& observer : observers_)
255 observer.OnChildViewReordered(view);
256
250 ReorderLayers(); 257 ReorderLayers();
251 } 258 }
252 259
253 void View::RemoveChildView(View* view) { 260 void View::RemoveChildView(View* view) {
254 DoRemoveChildView(view, true, true, false, NULL); 261 DoRemoveChildView(view, true, true, false, NULL);
255 } 262 }
256 263
257 void View::RemoveAllChildViews(bool delete_children) { 264 void View::RemoveAllChildViews(bool delete_children) {
258 while (!children_.empty()) 265 while (!children_.empty())
259 DoRemoveChildView(children_.front(), false, false, delete_children, NULL); 266 DoRemoveChildView(children_.front(), false, false, delete_children, NULL);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 if (visible_) { 298 if (visible_) {
292 // Paint where the view is currently. 299 // Paint where the view is currently.
293 SchedulePaintBoundsChanged( 300 SchedulePaintBoundsChanged(
294 bounds_.size() == bounds.size() ? SCHEDULE_PAINT_SIZE_SAME : 301 bounds_.size() == bounds.size() ? SCHEDULE_PAINT_SIZE_SAME :
295 SCHEDULE_PAINT_SIZE_CHANGED); 302 SCHEDULE_PAINT_SIZE_CHANGED);
296 } 303 }
297 304
298 gfx::Rect prev = bounds_; 305 gfx::Rect prev = bounds_;
299 bounds_ = bounds; 306 bounds_ = bounds;
300 BoundsChanged(prev); 307 BoundsChanged(prev);
308
309 for (ViewObserver& observer : observers_)
310 observer.OnViewBoundsChanged(this);
301 } 311 }
302 312
303 void View::SetSize(const gfx::Size& size) { 313 void View::SetSize(const gfx::Size& size) {
304 SetBounds(x(), y(), size.width(), size.height()); 314 SetBounds(x(), y(), size.width(), size.height());
305 } 315 }
306 316
307 void View::SetPosition(const gfx::Point& position) { 317 void View::SetPosition(const gfx::Point& position) {
308 SetBounds(position.x(), position.y(), width(), height()); 318 SetBounds(position.x(), position.y(), width(), height());
309 } 319 }
310 320
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 421
412 visible_ = visible; 422 visible_ = visible;
413 AdvanceFocusIfNecessary(); 423 AdvanceFocusIfNecessary();
414 424
415 // Notify the parent. 425 // Notify the parent.
416 if (parent_) { 426 if (parent_) {
417 parent_->ChildVisibilityChanged(this); 427 parent_->ChildVisibilityChanged(this);
418 parent_->NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, false); 428 parent_->NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, false);
419 } 429 }
420 430
431 for (ViewObserver& observer : observers_)
432 observer.OnViewVisibilityChanged(this);
433
421 // This notifies all sub-views recursively. 434 // This notifies all sub-views recursively.
422 PropagateVisibilityNotifications(this, visible_); 435 PropagateVisibilityNotifications(this, visible_);
423 UpdateLayerVisibility(); 436 UpdateLayerVisibility();
424 437
425 // If we are newly visible, schedule paint. 438 // If we are newly visible, schedule paint.
426 if (visible_) 439 if (visible_)
427 SchedulePaint(); 440 SchedulePaint();
428 } 441 }
429 } 442 }
430 443
431 bool View::IsDrawn() const { 444 bool View::IsDrawn() const {
432 return visible_ && parent_ ? parent_->IsDrawn() : false; 445 return visible_ && parent_ ? parent_->IsDrawn() : false;
433 } 446 }
434 447
435 void View::SetEnabled(bool enabled) { 448 void View::SetEnabled(bool enabled) {
436 if (enabled != enabled_) { 449 if (enabled != enabled_) {
437 enabled_ = enabled; 450 enabled_ = enabled;
438 AdvanceFocusIfNecessary(); 451 AdvanceFocusIfNecessary();
452
439 OnEnabledChanged(); 453 OnEnabledChanged();
454
455 for (ViewObserver& observer : observers_)
456 observer.OnViewEnabledChanged(this);
440 } 457 }
441 } 458 }
442 459
443 void View::OnEnabledChanged() { 460 void View::OnEnabledChanged() {
444 SchedulePaint(); 461 SchedulePaint();
445 } 462 }
446 463
447 // Transformations ------------------------------------------------------------- 464 // Transformations -------------------------------------------------------------
448 465
449 gfx::Transform View::GetTransform() const { 466 gfx::Transform View::GetTransform() const {
(...skipping 907 matching lines...) Expand 10 before | Expand all | Expand 10 after
1357 int View::GetPageScrollIncrement(ScrollView* scroll_view, 1374 int View::GetPageScrollIncrement(ScrollView* scroll_view,
1358 bool is_horizontal, bool is_positive) { 1375 bool is_horizontal, bool is_positive) {
1359 return 0; 1376 return 0;
1360 } 1377 }
1361 1378
1362 int View::GetLineScrollIncrement(ScrollView* scroll_view, 1379 int View::GetLineScrollIncrement(ScrollView* scroll_view,
1363 bool is_horizontal, bool is_positive) { 1380 bool is_horizontal, bool is_positive) {
1364 return 0; 1381 return 0;
1365 } 1382 }
1366 1383
1384 void View::AddObserver(ViewObserver* observer) {
1385 CHECK(observer);
1386 observers_.AddObserver(observer);
1387 }
1388
1389 void View::RemoveObserver(ViewObserver* observer) {
1390 observers_.RemoveObserver(observer);
1391 }
1392
1393 bool View::HasObserver(const ViewObserver* observer) const {
1394 return observers_.HasObserver(observer);
1395 }
1396
1367 //////////////////////////////////////////////////////////////////////////////// 1397 ////////////////////////////////////////////////////////////////////////////////
1368 // View, protected: 1398 // View, protected:
1369 1399
1370 // Size and disposition -------------------------------------------------------- 1400 // Size and disposition --------------------------------------------------------
1371 1401
1372 void View::OnBoundsChanged(const gfx::Rect& previous_bounds) { 1402 void View::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1373 } 1403 }
1374 1404
1375 void View::PreferredSizeChanged() { 1405 void View::PreferredSizeChanged() {
1376 InvalidateLayout(); 1406 InvalidateLayout();
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after
1832 if (delete_removed_view && !view->owned_by_client_) 1862 if (delete_removed_view && !view->owned_by_client_)
1833 view_to_be_deleted.reset(view); 1863 view_to_be_deleted.reset(view);
1834 1864
1835 children_.erase(i); 1865 children_.erase(i);
1836 1866
1837 if (update_tool_tip) 1867 if (update_tool_tip)
1838 UpdateTooltip(); 1868 UpdateTooltip();
1839 1869
1840 if (layout_manager_) 1870 if (layout_manager_)
1841 layout_manager_->ViewRemoved(this, view); 1871 layout_manager_->ViewRemoved(this, view);
1872
1873 for (ViewObserver& observer : observers_)
1874 observer.OnChildViewRemoved(view, this);
1842 } 1875 }
1843 1876
1844 void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) { 1877 void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) {
1845 for (int i = 0, count = child_count(); i < count; ++i) 1878 for (int i = 0, count = child_count(); i < count; ++i)
1846 child_at(i)->PropagateRemoveNotifications(old_parent, new_parent); 1879 child_at(i)->PropagateRemoveNotifications(old_parent, new_parent);
1847 1880
1848 ViewHierarchyChangedDetails details(false, old_parent, this, new_parent); 1881 ViewHierarchyChangedDetails details(false, old_parent, this, new_parent);
1849 for (View* v = this; v; v = v->parent_) 1882 for (View* v = this; v; v = v->parent_)
1850 v->ViewHierarchyChangedImpl(true, details); 1883 v->ViewHierarchyChangedImpl(true, details);
1851 } 1884 }
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after
2415 // Message the RootView to do the drag and drop. That way if we're removed 2448 // Message the RootView to do the drag and drop. That way if we're removed
2416 // the RootView can detect it and avoid calling us back. 2449 // the RootView can detect it and avoid calling us back.
2417 gfx::Point widget_location(event.location()); 2450 gfx::Point widget_location(event.location());
2418 ConvertPointToWidget(this, &widget_location); 2451 ConvertPointToWidget(this, &widget_location);
2419 widget->RunShellDrag(this, data, widget_location, drag_operations, source); 2452 widget->RunShellDrag(this, data, widget_location, drag_operations, source);
2420 // WARNING: we may have been deleted. 2453 // WARNING: we may have been deleted.
2421 return true; 2454 return true;
2422 } 2455 }
2423 2456
2424 } // namespace views 2457 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/view.h ('k') | ui/views/view_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698