OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "content/browser/theme_helper_mac.h" | 5 #include "content/browser/theme_helper_mac.h" |
6 | 6 |
7 #include <Foundation/Foundation.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
12 #include "content/public/browser/notification_types.h" | 12 #include "content/public/browser/notification_types.h" |
13 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
14 | 14 |
| 15 // Declare notification names from the 10.7 SDK. |
| 16 #if !defined(MAC_OS_X_VERSION_10_7) || \ |
| 17 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 |
| 18 NSString* NSPreferredScrollerStyleDidChangeNotification = |
| 19 @"NSPreferredScrollerStyleDidChangeNotification"; |
| 20 |
| 21 @interface NSScroller (LionSDK) |
| 22 + (NSInteger)preferredScrollerStyle; |
| 23 @end |
| 24 #endif |
| 25 |
15 @interface ScrollbarPrefsObserver : NSObject | 26 @interface ScrollbarPrefsObserver : NSObject |
16 | 27 |
17 + (void)registerAsObserver; | 28 + (void)registerAsObserver; |
18 + (void)appearancePrefsChanged:(NSNotification*)notification; | 29 + (void)appearancePrefsChanged:(NSNotification*)notification; |
19 + (void)behaviorPrefsChanged:(NSNotification*)notification; | 30 + (void)behaviorPrefsChanged:(NSNotification*)notification; |
20 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw; | 31 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw; |
21 | 32 |
22 @end | 33 @end |
23 | 34 |
24 @implementation ScrollbarPrefsObserver | 35 @implementation ScrollbarPrefsObserver |
25 | 36 |
26 + (void)registerAsObserver { | 37 + (void)registerAsObserver { |
27 [[NSDistributedNotificationCenter defaultCenter] | 38 [[NSDistributedNotificationCenter defaultCenter] |
28 addObserver:self | 39 addObserver:self |
29 selector:@selector(appearancePrefsChanged:) | 40 selector:@selector(appearancePrefsChanged:) |
30 name:@"AppleAquaScrollBarVariantChanged" | 41 name:@"AppleAquaScrollBarVariantChanged" |
31 object:nil | 42 object:nil |
32 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately]; | 43 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately]; |
33 | 44 |
34 [[NSDistributedNotificationCenter defaultCenter] | 45 [[NSDistributedNotificationCenter defaultCenter] |
35 addObserver:self | 46 addObserver:self |
36 selector:@selector(behaviorPrefsChanged:) | 47 selector:@selector(behaviorPrefsChanged:) |
37 name:@"AppleNoRedisplayAppearancePreferenceChanged" | 48 name:@"AppleNoRedisplayAppearancePreferenceChanged" |
38 object:nil | 49 object:nil |
39 suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce]; | 50 suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce]; |
| 51 |
| 52 if ([NSScroller respondsToSelector:@selector(preferredScrollerStyle)]) { |
| 53 [[NSNotificationCenter defaultCenter] |
| 54 addObserver:self |
| 55 selector:@selector(behaviorPrefsChanged:) |
| 56 name:NSPreferredScrollerStyleDidChangeNotification |
| 57 object:nil]; |
| 58 } |
40 } | 59 } |
41 | 60 |
42 + (void)appearancePrefsChanged:(NSNotification*)notification { | 61 + (void)appearancePrefsChanged:(NSNotification*)notification { |
43 [self notifyPrefsChangedWithRedraw:YES]; | 62 [self notifyPrefsChangedWithRedraw:YES]; |
44 } | 63 } |
45 | 64 |
46 + (void)behaviorPrefsChanged:(NSNotification*)notification { | 65 + (void)behaviorPrefsChanged:(NSNotification*)notification { |
47 [self notifyPrefsChangedWithRedraw:NO]; | 66 [self notifyPrefsChangedWithRedraw:NO]; |
48 } | 67 } |
49 | 68 |
50 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw { | 69 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw { |
51 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 70 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
52 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | 71 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
53 [defaults synchronize]; | 72 [defaults synchronize]; |
54 | 73 |
55 content::ThemeHelperMac::SendThemeChangeToAllRenderers( | 74 content::ThemeHelperMac::SendThemeChangeToAllRenderers( |
56 [defaults floatForKey:@"NSScrollerButtonDelay"], | 75 [defaults floatForKey:@"NSScrollerButtonDelay"], |
57 [defaults floatForKey:@"NSScrollerButtonPeriod"], | 76 [defaults floatForKey:@"NSScrollerButtonPeriod"], |
58 [defaults boolForKey:@"AppleScrollerPagingBehavior"], | 77 [defaults boolForKey:@"AppleScrollerPagingBehavior"], |
| 78 content::ThemeHelperMac::GetPreferredScrollerStyle(), |
59 redraw); | 79 redraw); |
60 } | 80 } |
61 | 81 |
62 @end | 82 @end |
63 | 83 |
64 namespace content { | 84 namespace content { |
65 | 85 |
| 86 // static |
| 87 ThemeHelperMac* ThemeHelperMac::GetInstance() { |
| 88 return Singleton<ThemeHelperMac, |
| 89 LeakySingletonTraits<ThemeHelperMac> >::get(); |
| 90 } |
| 91 |
| 92 // static |
| 93 blink::ScrollerStyle ThemeHelperMac::GetPreferredScrollerStyle() { |
| 94 if (![NSScroller respondsToSelector:@selector(preferredScrollerStyle)]) |
| 95 return blink::ScrollerStyleLegacy; |
| 96 return static_cast<blink::ScrollerStyle>([NSScroller preferredScrollerStyle]); |
| 97 } |
| 98 |
| 99 // static |
| 100 void ThemeHelperMac::SendThemeChangeToAllRenderers( |
| 101 float initial_button_delay, |
| 102 float autoscroll_button_delay, |
| 103 bool jump_on_track_click, |
| 104 blink::ScrollerStyle preferred_scroller_style, |
| 105 bool redraw) { |
| 106 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); |
| 107 !it.IsAtEnd(); |
| 108 it.Advance()) { |
| 109 it.GetCurrentValue()->Send(new ViewMsg_UpdateScrollbarTheme( |
| 110 initial_button_delay, |
| 111 autoscroll_button_delay, |
| 112 jump_on_track_click, |
| 113 preferred_scroller_style, |
| 114 redraw)); |
| 115 } |
| 116 } |
| 117 |
66 ThemeHelperMac::ThemeHelperMac() { | 118 ThemeHelperMac::ThemeHelperMac() { |
67 [ScrollbarPrefsObserver registerAsObserver]; | 119 [ScrollbarPrefsObserver registerAsObserver]; |
68 registrar_.Add(this, | 120 registrar_.Add(this, |
69 NOTIFICATION_RENDERER_PROCESS_CREATED, | 121 NOTIFICATION_RENDERER_PROCESS_CREATED, |
70 NotificationService::AllSources()); | 122 NotificationService::AllSources()); |
71 } | 123 } |
72 | 124 |
73 ThemeHelperMac::~ThemeHelperMac() { | 125 ThemeHelperMac::~ThemeHelperMac() { |
74 } | 126 } |
75 | 127 |
76 // static | |
77 ThemeHelperMac* ThemeHelperMac::GetInstance() { | |
78 return Singleton<ThemeHelperMac, | |
79 LeakySingletonTraits<ThemeHelperMac> >::get(); | |
80 } | |
81 | |
82 | |
83 void ThemeHelperMac::Observe(int type, | 128 void ThemeHelperMac::Observe(int type, |
84 const NotificationSource& source, | 129 const NotificationSource& source, |
85 const NotificationDetails& details) { | 130 const NotificationDetails& details) { |
86 DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type); | 131 DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type); |
87 | 132 |
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
89 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | 134 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
90 [defaults synchronize]; | 135 [defaults synchronize]; |
91 | 136 |
92 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); | 137 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); |
93 rph->Send(new ViewMsg_UpdateScrollbarTheme( | 138 rph->Send(new ViewMsg_UpdateScrollbarTheme( |
94 [defaults floatForKey:@"NSScrollerButtonDelay"], | 139 [defaults floatForKey:@"NSScrollerButtonDelay"], |
95 [defaults floatForKey:@"NSScrollerButtonPeriod"], | 140 [defaults floatForKey:@"NSScrollerButtonPeriod"], |
96 [defaults boolForKey:@"AppleScrollerPagingBehavior"], | 141 [defaults boolForKey:@"AppleScrollerPagingBehavior"], |
| 142 GetPreferredScrollerStyle(), |
97 false)); | 143 false)); |
98 } | 144 } |
99 | 145 |
100 // static | |
101 void ThemeHelperMac::SendThemeChangeToAllRenderers( | |
102 float initial_button_delay, | |
103 float autoscroll_button_delay, | |
104 bool jump_on_track_click, | |
105 bool redraw) { | |
106 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); | |
107 !it.IsAtEnd(); | |
108 it.Advance()) { | |
109 it.GetCurrentValue()->Send(new ViewMsg_UpdateScrollbarTheme( | |
110 initial_button_delay, | |
111 autoscroll_button_delay, | |
112 jump_on_track_click, | |
113 redraw)); | |
114 } | |
115 } | |
116 | |
117 } // namespace content | 146 } // namespace content |
OLD | NEW |