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

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 view->NotifyViewParentChanged(nullptr);
sky 2016/11/21 18:32:12 Don't you want to pass parent here? Please add tes
Sarmad Hashmi 2016/11/21 19:14:13 Done.
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);
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
439 OnEnabledChanged(); 452 OnEnabledChanged();
453
454 for (ViewObserver& observer : observers_)
455 observer.OnViewEnabledChanged(this);
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
449 gfx::Transform View::GetTransform() const { 465 gfx::Transform View::GetTransform() const {
(...skipping 907 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 void View::AddObserver(ViewObserver* observer) {
1384 CHECK(observer);
1385 observers_.AddObserver(observer);
1386 }
1387
1388 void View::RemoveObserver(ViewObserver* observer) {
1389 observers_.RemoveObserver(observer);
1390 }
1391
1392 bool View::HasObserver(const ViewObserver* observer) const {
1393 return observers_.HasObserver(observer);
1394 }
1395
1367 //////////////////////////////////////////////////////////////////////////////// 1396 ////////////////////////////////////////////////////////////////////////////////
1368 // View, protected: 1397 // View, protected:
1369 1398
1370 // Size and disposition -------------------------------------------------------- 1399 // Size and disposition --------------------------------------------------------
1371 1400
1372 void View::OnBoundsChanged(const gfx::Rect& previous_bounds) { 1401 void View::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1373 } 1402 }
1374 1403
1375 void View::PreferredSizeChanged() { 1404 void View::PreferredSizeChanged() {
1376 InvalidateLayout(); 1405 InvalidateLayout();
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after
1832 if (delete_removed_view && !view->owned_by_client_) 1861 if (delete_removed_view && !view->owned_by_client_)
1833 view_to_be_deleted.reset(view); 1862 view_to_be_deleted.reset(view);
1834 1863
1835 children_.erase(i); 1864 children_.erase(i);
1836 1865
1837 if (update_tool_tip) 1866 if (update_tool_tip)
1838 UpdateTooltip(); 1867 UpdateTooltip();
1839 1868
1840 if (layout_manager_) 1869 if (layout_manager_)
1841 layout_manager_->ViewRemoved(this, view); 1870 layout_manager_->ViewRemoved(this, view);
1871
1872 view->NotifyViewParentChanged(this);
1842 } 1873 }
1843 1874
1844 void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) { 1875 void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) {
1845 for (int i = 0, count = child_count(); i < count; ++i) 1876 for (int i = 0, count = child_count(); i < count; ++i)
1846 child_at(i)->PropagateRemoveNotifications(old_parent, new_parent); 1877 child_at(i)->PropagateRemoveNotifications(old_parent, new_parent);
1847 1878
1848 ViewHierarchyChangedDetails details(false, old_parent, this, new_parent); 1879 ViewHierarchyChangedDetails details(false, old_parent, this, new_parent);
1849 for (View* v = this; v; v = v->parent_) 1880 for (View* v = this; v; v = v->parent_)
1850 v->ViewHierarchyChangedImpl(true, details); 1881 v->ViewHierarchyChangedImpl(true, details);
1851 } 1882 }
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after
2414 2445
2415 // Message the RootView to do the drag and drop. That way if we're removed 2446 // 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. 2447 // the RootView can detect it and avoid calling us back.
2417 gfx::Point widget_location(event.location()); 2448 gfx::Point widget_location(event.location());
2418 ConvertPointToWidget(this, &widget_location); 2449 ConvertPointToWidget(this, &widget_location);
2419 widget->RunShellDrag(this, data, widget_location, drag_operations, source); 2450 widget->RunShellDrag(this, data, widget_location, drag_operations, source);
2420 // WARNING: we may have been deleted. 2451 // WARNING: we may have been deleted.
2421 return true; 2452 return true;
2422 } 2453 }
2423 2454
2455 void View::NotifyViewParentChanged(View* old_parent) {
2456 for (ViewObserver& observer : observers_)
2457 observer.OnViewParentChanged(this, old_parent);
2458 }
2459
2424 } // namespace views 2460 } // namespace views
OLDNEW
« ui/views/view.h ('K') | « 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