OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ui/base/test/scoped_preferred_scroller_style_mac.h" | 5 #import "ui/base/test/scoped_preferred_scroller_style_mac.h" |
6 | 6 |
7 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
8 | 8 |
| 9 #include "base/logging.h" |
9 #import "base/mac/scoped_objc_class_swizzler.h" | 10 #import "base/mac/scoped_objc_class_swizzler.h" |
10 | 11 |
11 using base::mac::ScopedObjCClassSwizzler; | 12 using base::mac::ScopedObjCClassSwizzler; |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
| 16 // Swizzling can be stacked, but not interleaved without creating unexpected |
| 17 // states. Require that there is only one swizzler rather than tracking a stack. |
| 18 bool g_swizzling = false; |
| 19 |
15 void NotifyStyleChanged() { | 20 void NotifyStyleChanged() { |
16 [[NSNotificationCenter defaultCenter] | 21 [[NSNotificationCenter defaultCenter] |
17 postNotificationName:NSPreferredScrollerStyleDidChangeNotification | 22 postNotificationName:NSPreferredScrollerStyleDidChangeNotification |
18 object:nil]; | 23 object:nil]; |
19 } | 24 } |
20 | 25 |
21 NSScrollerStyle GetScrollerStyle(bool overlay) { | 26 NSScrollerStyle GetScrollerStyle(bool overlay) { |
22 return overlay ? NSScrollerStyleOverlay : NSScrollerStyleLegacy; | 27 return overlay ? NSScrollerStyleOverlay : NSScrollerStyleLegacy; |
23 } | 28 } |
24 | 29 |
(...skipping 28 matching lines...) Expand all Loading... |
53 namespace ui { | 58 namespace ui { |
54 namespace test { | 59 namespace test { |
55 | 60 |
56 ScopedPreferredScrollerStyle::ScopedPreferredScrollerStyle(bool overlay) | 61 ScopedPreferredScrollerStyle::ScopedPreferredScrollerStyle(bool overlay) |
57 : overlay_(overlay) { | 62 : overlay_(overlay) { |
58 NSInteger previous_style = [NSScroller preferredScrollerStyle]; | 63 NSInteger previous_style = [NSScroller preferredScrollerStyle]; |
59 Class style_class = overlay_ | 64 Class style_class = overlay_ |
60 ? [FakeNSScrollerPreferredStyleOverlayDonor class] | 65 ? [FakeNSScrollerPreferredStyleOverlayDonor class] |
61 : [FakeNSScrollerPreferredStyleLegacyDonor class]; | 66 : [FakeNSScrollerPreferredStyleLegacyDonor class]; |
62 | 67 |
| 68 DCHECK(!g_swizzling); |
| 69 g_swizzling = true; |
63 swizzler_.reset(new ScopedObjCClassSwizzler( | 70 swizzler_.reset(new ScopedObjCClassSwizzler( |
64 [NSScroller class], style_class, @selector(preferredScrollerStyle))); | 71 [NSScroller class], style_class, @selector(preferredScrollerStyle))); |
65 | 72 |
66 if (previous_style != GetScrollerStyle(overlay_)) | 73 if (previous_style != GetScrollerStyle(overlay_)) |
67 NotifyStyleChanged(); | 74 NotifyStyleChanged(); |
68 } | 75 } |
69 | 76 |
70 ScopedPreferredScrollerStyle::~ScopedPreferredScrollerStyle() { | 77 ScopedPreferredScrollerStyle::~ScopedPreferredScrollerStyle() { |
71 swizzler_.reset(); | 78 swizzler_.reset(); |
| 79 DCHECK(g_swizzling); |
| 80 g_swizzling = false; |
72 | 81 |
73 if ([NSScroller preferredScrollerStyle] != GetScrollerStyle(overlay_)) | 82 if ([NSScroller preferredScrollerStyle] != GetScrollerStyle(overlay_)) |
74 NotifyStyleChanged(); | 83 NotifyStyleChanged(); |
75 } | 84 } |
76 | 85 |
77 } // namespace test | 86 } // namespace test |
78 } // namespace ui | 87 } // namespace ui |
OLD | NEW |