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

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

Issue 2500623002: Add ViewObserver to View for view updates (Closed)
Patch Set: skys comments Created 4 years, 1 month 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
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 NotifyViewParentChanged(view, nullptr, this);
223 } 226 }
224 227
225 void View::ReorderChildView(View* view, int index) { 228 void View::ReorderChildView(View* view, int index) {
226 DCHECK_EQ(view->parent_, this); 229 DCHECK_EQ(view->parent_, this);
227 if (index < 0) 230 if (index < 0)
228 index = child_count() - 1; 231 index = child_count() - 1;
229 else if (index >= child_count()) 232 else if (index >= child_count())
230 return; 233 return;
231 if (children_[index] == view) 234 if (children_[index] == view)
232 return; 235 return;
233 236
234 const Views::iterator i(std::find(children_.begin(), children_.end(), view)); 237 const Views::iterator i(std::find(children_.begin(), children_.end(), view));
235 DCHECK(i != children_.end()); 238 DCHECK(i != children_.end());
236 children_.erase(i); 239 children_.erase(i);
237 240
238 // Unlink the view first 241 // Unlink the view first
239 View* next_focusable = view->next_focusable_view_; 242 View* next_focusable = view->next_focusable_view_;
240 View* prev_focusable = view->previous_focusable_view_; 243 View* prev_focusable = view->previous_focusable_view_;
241 if (prev_focusable) 244 if (prev_focusable)
242 prev_focusable->next_focusable_view_ = next_focusable; 245 prev_focusable->next_focusable_view_ = next_focusable;
243 if (next_focusable) 246 if (next_focusable)
244 next_focusable->previous_focusable_view_ = prev_focusable; 247 next_focusable->previous_focusable_view_ = prev_focusable;
245 248
246 // Add it in the specified index now. 249 // Add it in the specified index now.
247 InitFocusSiblings(view, index); 250 InitFocusSiblings(view, index);
248 children_.insert(children_.begin() + index, view); 251 children_.insert(children_.begin() + index, view);
249 252
253 for (ViewObserver& observer : observers_)
254 observer.OnChildViewReordered(view, index);
sky 2016/11/21 16:33:37 Is index really useful for this notification?
Sarmad Hashmi 2016/11/21 17:09:53 Avoids looking up where the view was placed after
255
250 ReorderLayers(); 256 ReorderLayers();
251 } 257 }
252 258
253 void View::RemoveChildView(View* view) { 259 void View::RemoveChildView(View* view) {
254 DoRemoveChildView(view, true, true, false, NULL); 260 DoRemoveChildView(view, true, true, false, NULL);
255 } 261 }
256 262
257 void View::RemoveAllChildViews(bool delete_children) { 263 void View::RemoveAllChildViews(bool delete_children) {
258 while (!children_.empty()) 264 while (!children_.empty())
259 DoRemoveChildView(children_.front(), false, false, delete_children, NULL); 265 DoRemoveChildView(children_.front(), false, false, delete_children, NULL);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 if (visible_) { 297 if (visible_) {
292 // Paint where the view is currently. 298 // Paint where the view is currently.
293 SchedulePaintBoundsChanged( 299 SchedulePaintBoundsChanged(
294 bounds_.size() == bounds.size() ? SCHEDULE_PAINT_SIZE_SAME : 300 bounds_.size() == bounds.size() ? SCHEDULE_PAINT_SIZE_SAME :
295 SCHEDULE_PAINT_SIZE_CHANGED); 301 SCHEDULE_PAINT_SIZE_CHANGED);
296 } 302 }
297 303
298 gfx::Rect prev = bounds_; 304 gfx::Rect prev = bounds_;
299 bounds_ = bounds; 305 bounds_ = bounds;
300 BoundsChanged(prev); 306 BoundsChanged(prev);
307
308 for (ViewObserver& observer : observers_)
309 observer.OnViewBoundsChanged(this);
301 } 310 }
302 311
303 void View::SetSize(const gfx::Size& size) { 312 void View::SetSize(const gfx::Size& size) {
304 SetBounds(x(), y(), size.width(), size.height()); 313 SetBounds(x(), y(), size.width(), size.height());
305 } 314 }
306 315
307 void View::SetPosition(const gfx::Point& position) { 316 void View::SetPosition(const gfx::Point& position) {
308 SetBounds(position.x(), position.y(), width(), height()); 317 SetBounds(position.x(), position.y(), width(), height());
309 } 318 }
310 319
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 420
412 visible_ = visible; 421 visible_ = visible;
413 AdvanceFocusIfNecessary(); 422 AdvanceFocusIfNecessary();
414 423
415 // Notify the parent. 424 // Notify the parent.
416 if (parent_) { 425 if (parent_) {
417 parent_->ChildVisibilityChanged(this); 426 parent_->ChildVisibilityChanged(this);
418 parent_->NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, false); 427 parent_->NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, false);
419 } 428 }
420 429
430 for (ViewObserver& observer : observers_)
431 observer.OnViewVisibilityChanged(this);
432
421 // This notifies all sub-views recursively. 433 // This notifies all sub-views recursively.
422 PropagateVisibilityNotifications(this, visible_); 434 PropagateVisibilityNotifications(this, visible_);
423 UpdateLayerVisibility(); 435 UpdateLayerVisibility();
424 436
425 // If we are newly visible, schedule paint. 437 // If we are newly visible, schedule paint.
426 if (visible_) 438 if (visible_)
427 SchedulePaint(); 439 SchedulePaint();
428 } 440 }
429 } 441 }
430 442
431 bool View::IsDrawn() const { 443 bool View::IsDrawn() const {
432 return visible_ && parent_ ? parent_->IsDrawn() : false; 444 return visible_ && parent_ ? parent_->IsDrawn() : false;
433 } 445 }
434 446
435 void View::SetEnabled(bool enabled) { 447 void View::SetEnabled(bool enabled) {
436 if (enabled != enabled_) { 448 if (enabled != enabled_) {
437 enabled_ = enabled; 449 enabled_ = enabled;
438 AdvanceFocusIfNecessary(); 450 AdvanceFocusIfNecessary();
451
452 for (ViewObserver& observer : observers_)
453 observer.OnViewEnabledChanged(this);
sky 2016/11/21 16:33:37 I mildly favor calling this after OnEnabledChanged
Sarmad Hashmi 2016/11/21 17:09:53 Done.
454
439 OnEnabledChanged(); 455 OnEnabledChanged();
440 } 456 }
441 } 457 }
442 458
443 void View::OnEnabledChanged() { 459 void View::OnEnabledChanged() {
444 SchedulePaint(); 460 SchedulePaint();
445 } 461 }
446 462
447 // Transformations ------------------------------------------------------------- 463 // Transformations -------------------------------------------------------------
448 464
(...skipping 908 matching lines...) Expand 10 before | Expand all | Expand 10 after
1357 int View::GetPageScrollIncrement(ScrollView* scroll_view, 1373 int View::GetPageScrollIncrement(ScrollView* scroll_view,
1358 bool is_horizontal, bool is_positive) { 1374 bool is_horizontal, bool is_positive) {
1359 return 0; 1375 return 0;
1360 } 1376 }
1361 1377
1362 int View::GetLineScrollIncrement(ScrollView* scroll_view, 1378 int View::GetLineScrollIncrement(ScrollView* scroll_view,
1363 bool is_horizontal, bool is_positive) { 1379 bool is_horizontal, bool is_positive) {
1364 return 0; 1380 return 0;
1365 } 1381 }
1366 1382
1383 // Observers -------------------------------------------------------------------
sky 2016/11/21 16:33:37 No need for this comment, please remove.
Sarmad Hashmi 2016/11/21 17:09:53 Done.
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
1397 void View::NotifyViewParentChanged(View* view,
1398 View* old_parent,
1399 View* new_parent) {
1400 for (ViewObserver& observer : observers_)
1401 observer.OnViewParentChanged(view, old_parent, new_parent);
1402 }
1403
1367 //////////////////////////////////////////////////////////////////////////////// 1404 ////////////////////////////////////////////////////////////////////////////////
1368 // View, protected: 1405 // View, protected:
1369 1406
1370 // Size and disposition -------------------------------------------------------- 1407 // Size and disposition --------------------------------------------------------
1371 1408
1372 void View::OnBoundsChanged(const gfx::Rect& previous_bounds) { 1409 void View::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1373 } 1410 }
1374 1411
1375 void View::PreferredSizeChanged() { 1412 void View::PreferredSizeChanged() {
1376 InvalidateLayout(); 1413 InvalidateLayout();
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after
1832 if (delete_removed_view && !view->owned_by_client_) 1869 if (delete_removed_view && !view->owned_by_client_)
1833 view_to_be_deleted.reset(view); 1870 view_to_be_deleted.reset(view);
1834 1871
1835 children_.erase(i); 1872 children_.erase(i);
1836 1873
1837 if (update_tool_tip) 1874 if (update_tool_tip)
1838 UpdateTooltip(); 1875 UpdateTooltip();
1839 1876
1840 if (layout_manager_) 1877 if (layout_manager_)
1841 layout_manager_->ViewRemoved(this, view); 1878 layout_manager_->ViewRemoved(this, view);
1879
1880 NotifyViewParentChanged(view, this, nullptr);
sky 2016/11/21 16:33:37 DoRemoveChildView is called from AddChildView. So,
Sarmad Hashmi 2016/11/21 17:09:53 Already have this under the ViewParentChanged test
sky 2016/11/21 18:32:12 Sure, but your test is verifying the observer is c
1842 } 1881 }
1843 1882
1844 void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) { 1883 void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) {
1845 for (int i = 0, count = child_count(); i < count; ++i) 1884 for (int i = 0, count = child_count(); i < count; ++i)
1846 child_at(i)->PropagateRemoveNotifications(old_parent, new_parent); 1885 child_at(i)->PropagateRemoveNotifications(old_parent, new_parent);
1847 1886
1848 ViewHierarchyChangedDetails details(false, old_parent, this, new_parent); 1887 ViewHierarchyChangedDetails details(false, old_parent, this, new_parent);
1849 for (View* v = this; v; v = v->parent_) 1888 for (View* v = this; v; v = v->parent_)
1850 v->ViewHierarchyChangedImpl(true, details); 1889 v->ViewHierarchyChangedImpl(true, details);
1851 } 1890 }
(...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 2454 // 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. 2455 // the RootView can detect it and avoid calling us back.
2417 gfx::Point widget_location(event.location()); 2456 gfx::Point widget_location(event.location());
2418 ConvertPointToWidget(this, &widget_location); 2457 ConvertPointToWidget(this, &widget_location);
2419 widget->RunShellDrag(this, data, widget_location, drag_operations, source); 2458 widget->RunShellDrag(this, data, widget_location, drag_operations, source);
2420 // WARNING: we may have been deleted. 2459 // WARNING: we may have been deleted.
2421 return true; 2460 return true;
2422 } 2461 }
2423 2462
2424 } // namespace views 2463 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698