OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #import "chrome/browser/ui/cocoa/view_id_util.h" | 5 #import "chrome/browser/ui/cocoa/view_id_util.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 14 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
15 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" | 15 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // TODO(suzhe): After migrating to Mac OS X 10.6, we may use Objective-C's new | 19 // TODO(suzhe): After migrating to Mac OS X 10.6, we may use Objective-C's new |
20 // "Associative References" feature to attach the ViewID to the view directly | 20 // "Associative References" feature to attach the ViewID to the view directly |
21 // rather than using a separated map. | 21 // rather than using a separated map. |
22 typedef std::map<NSView*, ViewID> ViewIDMap; | 22 typedef std::map<NSView*, ViewID> ViewIDMap; |
23 | 23 |
24 static base::LazyInstance<ViewIDMap> g_view_id_map(base::LINKER_INITIALIZED); | 24 static base::LazyInstance<ViewIDMap> g_view_id_map = LAZY_INSTANCE_INITIALIZER; |
25 | 25 |
26 // Returns the view's nearest descendant (including itself) with a specific | 26 // Returns the view's nearest descendant (including itself) with a specific |
27 // ViewID, or nil if no subview has that ViewID. | 27 // ViewID, or nil if no subview has that ViewID. |
28 NSView* FindViewWithID(NSView* view, ViewID viewID) { | 28 NSView* FindViewWithID(NSView* view, ViewID viewID) { |
29 if ([view viewID] == viewID) | 29 if ([view viewID] == viewID) |
30 return view; | 30 return view; |
31 | 31 |
32 for (NSView* subview in [view subviews]) { | 32 for (NSView* subview in [view subviews]) { |
33 NSView* result = FindViewWithID(subview, viewID); | 33 NSView* result = FindViewWithID(subview, viewID); |
34 if (result != nil) | 34 if (result != nil) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 80 |
81 @implementation NSView (ViewID) | 81 @implementation NSView (ViewID) |
82 | 82 |
83 - (ViewID)viewID { | 83 - (ViewID)viewID { |
84 ViewIDMap* map = g_view_id_map.Pointer(); | 84 ViewIDMap* map = g_view_id_map.Pointer(); |
85 ViewIDMap::const_iterator iter = map->find(self); | 85 ViewIDMap::const_iterator iter = map->find(self); |
86 return iter != map->end() ? iter->second : VIEW_ID_NONE; | 86 return iter != map->end() ? iter->second : VIEW_ID_NONE; |
87 } | 87 } |
88 | 88 |
89 @end | 89 @end |
OLD | NEW |