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 #import "ui/base/cocoa/appkit_utils.h" | 5 #import "ui/base/cocoa/appkit_utils.h" |
6 | 6 |
7 #include "base/mac/mac_util.h" | 7 #include "base/mac/mac_util.h" |
8 #include "ui/base/resource/resource_bundle.h" | 8 #include "ui/base/resource/resource_bundle.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
12 // Gets an NSImage given an image id. | 12 // Gets an NSImage given an image id. |
13 NSImage* GetImage(int image_id) { | 13 NSImage* GetImage(int image_id) { |
14 return ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(image_id) | 14 return ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(image_id) |
15 .ToNSImage(); | 15 .ToNSImage(); |
16 } | 16 } |
17 | 17 |
18 // Whether windows should miniaturize on a double-click on the title bar. | 18 // Double-click in window title bar actions. |
19 bool ShouldWindowsMiniaturizeOnDoubleClick() { | 19 enum class DoubleClickAction { |
20 // We use an undocumented method in Cocoa; if it doesn't exist, default to | 20 NONE, |
21 // |true|. If it ever goes away, we can do (using an undocumented pref key): | 21 MINIMIZE, |
22 // NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | 22 MAXIMIZE, |
23 // return ![defaults objectForKey:@"AppleMiniaturizeOnDoubleClick"] || | 23 }; |
24 // [defaults boolForKey:@"AppleMiniaturizeOnDoubleClick"]; | 24 |
| 25 // The action to take when the user double-clicks in the window title bar. |
| 26 DoubleClickAction WindowTitleBarDoubleClickAction() { |
| 27 // El Capitan introduced a Dock preference to configure the window title bar |
| 28 // double-click action (Minimize, Maximize, or nothing). |
| 29 if (base::mac::IsOSElCapitanOrLater()) { |
| 30 NSString* doubleClickAction = [[NSUserDefaults standardUserDefaults] |
| 31 objectForKey:@"AppleActionOnDoubleClick"]; |
| 32 |
| 33 if ([doubleClickAction isEqualToString:@"Minimize"]) { |
| 34 return DoubleClickAction::MINIMIZE; |
| 35 } else if ([doubleClickAction isEqualToString:@"Maximize"]) { |
| 36 return DoubleClickAction::MAXIMIZE; |
| 37 } |
| 38 |
| 39 return DoubleClickAction::NONE; |
| 40 } |
| 41 |
| 42 // Determine minimize using an undocumented method in Cocoa. If we're |
| 43 // running on an earlier version of the OS that doesn't implement it, |
| 44 // just default to the minimize action. |
25 BOOL methodImplemented = | 45 BOOL methodImplemented = |
26 [NSWindow respondsToSelector:@selector(_shouldMiniaturizeOnDoubleClick)]; | 46 [NSWindow respondsToSelector:@selector(_shouldMiniaturizeOnDoubleClick)]; |
27 DCHECK(methodImplemented); | 47 if (!methodImplemented || |
28 return !methodImplemented || | 48 [NSWindow performSelector:@selector(_shouldMiniaturizeOnDoubleClick)]) { |
29 [NSWindow performSelector:@selector(_shouldMiniaturizeOnDoubleClick)]; | 49 return DoubleClickAction::MINIMIZE; |
| 50 } |
| 51 |
| 52 // At this point _shouldMiniaturizeOnDoubleClick has returned |NO|. On |
| 53 // Yosemite, that means a double-click should Maximize the window, and on |
| 54 // all prior OSes a double-click should do nothing. |
| 55 return base::mac::IsOSYosemite() ? DoubleClickAction::MAXIMIZE |
| 56 : DoubleClickAction::NONE; |
30 } | 57 } |
31 | 58 |
32 } // namespace | 59 } // namespace |
33 | 60 |
34 namespace ui { | 61 namespace ui { |
35 | 62 |
36 void DrawNinePartImage(NSRect frame, | 63 void DrawNinePartImage(NSRect frame, |
37 const NinePartImageIds& image_ids, | 64 const NinePartImageIds& image_ids, |
38 NSCompositingOperation operation, | 65 NSCompositingOperation operation, |
39 CGFloat alpha, | 66 CGFloat alpha, |
40 BOOL flipped) { | 67 BOOL flipped) { |
41 NSDrawNinePartImage(frame, | 68 NSDrawNinePartImage(frame, |
42 GetImage(image_ids.top_left), | 69 GetImage(image_ids.top_left), |
43 GetImage(image_ids.top), | 70 GetImage(image_ids.top), |
44 GetImage(image_ids.top_right), | 71 GetImage(image_ids.top_right), |
45 GetImage(image_ids.left), | 72 GetImage(image_ids.left), |
46 GetImage(image_ids.center), | 73 GetImage(image_ids.center), |
47 GetImage(image_ids.right), | 74 GetImage(image_ids.right), |
48 GetImage(image_ids.bottom_left), | 75 GetImage(image_ids.bottom_left), |
49 GetImage(image_ids.bottom), | 76 GetImage(image_ids.bottom), |
50 GetImage(image_ids.bottom_right), | 77 GetImage(image_ids.bottom_right), |
51 operation, | 78 operation, |
52 alpha, | 79 alpha, |
53 flipped); | 80 flipped); |
54 } | 81 } |
55 | 82 |
56 void WindowTitlebarReceivedDoubleClick(NSWindow* window, id sender) { | 83 void WindowTitlebarReceivedDoubleClick(NSWindow* window, id sender) { |
57 if (ShouldWindowsMiniaturizeOnDoubleClick()) { | 84 switch (WindowTitleBarDoubleClickAction()) { |
58 [window performMiniaturize:sender]; | 85 case DoubleClickAction::MINIMIZE: |
59 } else if (base::mac::IsOSYosemiteOrLater()) { | 86 [window performMiniaturize:sender]; |
60 [window performZoom:sender]; | 87 break; |
| 88 |
| 89 case DoubleClickAction::MAXIMIZE: |
| 90 [window performZoom:sender]; |
| 91 break; |
| 92 |
| 93 case DoubleClickAction::NONE: |
| 94 break; |
61 } | 95 } |
62 } | 96 } |
63 | 97 |
64 } // namespace ui | 98 } // namespace ui |
OLD | NEW |