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
3 // found in the LICENSE file.
5 #include "content/browser/theme_helper_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/command_line.h"
10 #include "content/common/view_messages.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/browser/notification_types.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/common/content_switches.h"
17 // Declare notification names from the 10.7 SDK.
18 #if !defined(MAC_OS_X_VERSION_10_7) || \
19 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
20 NSString* NSPreferredScrollerStyleDidChangeNotification =
21 @"NSPreferredScrollerStyleDidChangeNotification";
23 @interface NSScroller (LionSDK)
24 + (NSInteger)preferredScrollerStyle;
28 @interface ScrollbarPrefsObserver : NSObject
30 + (void)registerAsObserver;
31 + (void)appearancePrefsChanged:(NSNotification*)notification;
32 + (void)behaviorPrefsChanged:(NSNotification*)notification;
33 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw;
37 @implementation ScrollbarPrefsObserver
39 + (void)registerAsObserver {
40 [[NSDistributedNotificationCenter defaultCenter]
42 selector:@selector(appearancePrefsChanged:)
43 name:@"AppleAquaScrollBarVariantChanged"
45 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
47 [[NSDistributedNotificationCenter defaultCenter]
49 selector:@selector(behaviorPrefsChanged:)
50 name:@"AppleNoRedisplayAppearancePreferenceChanged"
52 suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
54 // In single-process mode, renderers will catch these notifications
55 // themselves and listening for them here may trigger the DCHECK in Observe().
56 if ([NSScroller respondsToSelector:@selector(preferredScrollerStyle)] &&
57 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess)) {
58 [[NSNotificationCenter defaultCenter]
60 selector:@selector(behaviorPrefsChanged:)
61 name:NSPreferredScrollerStyleDidChangeNotification
66 + (void)appearancePrefsChanged:(NSNotification*)notification {
67 [self notifyPrefsChangedWithRedraw:YES];
70 + (void)behaviorPrefsChanged:(NSNotification*)notification {
71 [self notifyPrefsChangedWithRedraw:NO];
74 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw {
75 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
76 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
77 [defaults synchronize];
79 content::ThemeHelperMac::SendThemeChangeToAllRenderers(
80 [defaults floatForKey:@"NSScrollerButtonDelay"],
81 [defaults floatForKey:@"NSScrollerButtonPeriod"],
82 [defaults boolForKey:@"AppleScrollerPagingBehavior"],
83 content::ThemeHelperMac::GetPreferredScrollerStyle(),
92 ThemeHelperMac* ThemeHelperMac::GetInstance() {
93 return Singleton<ThemeHelperMac,
94 LeakySingletonTraits<ThemeHelperMac> >::get();
98 blink::ScrollerStyle ThemeHelperMac::GetPreferredScrollerStyle() {
99 if (![NSScroller respondsToSelector:@selector(preferredScrollerStyle)])
100 return blink::ScrollerStyleLegacy;
101 return static_cast<blink::ScrollerStyle>([NSScroller preferredScrollerStyle]);
105 void ThemeHelperMac::SendThemeChangeToAllRenderers(
106 float initial_button_delay,
107 float autoscroll_button_delay,
108 bool jump_on_track_click,
109 blink::ScrollerStyle preferred_scroller_style,
111 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
114 it.GetCurrentValue()->Send(new ViewMsg_UpdateScrollbarTheme(
115 initial_button_delay,
116 autoscroll_button_delay,
118 preferred_scroller_style,
123 ThemeHelperMac::ThemeHelperMac() {
124 [ScrollbarPrefsObserver registerAsObserver];
126 NOTIFICATION_RENDERER_PROCESS_CREATED,
127 NotificationService::AllSources());
130 ThemeHelperMac::~ThemeHelperMac() {
133 void ThemeHelperMac::Observe(int type,
134 const NotificationSource& source,
135 const NotificationDetails& details) {
136 DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type);
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
139 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
140 [defaults synchronize];
142 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
143 rph->Send(new ViewMsg_UpdateScrollbarTheme(
144 [defaults floatForKey:@"NSScrollerButtonDelay"],
145 [defaults floatForKey:@"NSScrollerButtonPeriod"],
146 [defaults boolForKey:@"AppleScrollerPagingBehavior"],
147 GetPreferredScrollerStyle(),
151 } // namespace content