OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser_window_controller.h" | 5 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
6 | 6 |
7 #include <Carbon/Carbon.h> | 7 #include <Carbon/Carbon.h> |
| 8 #include <numeric> |
8 | 9 |
9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
10 #include "base/mac/mac_util.h" | 11 #include "base/mac/mac_util.h" |
11 #import "base/memory/scoped_nsobject.h" | 12 #import "base/memory/scoped_nsobject.h" |
12 #include "base/sys_string_conversions.h" | 13 #include "base/sys_string_conversions.h" |
13 #include "chrome/app/chrome_command_ids.h" // IDC_* | 14 #include "chrome/app/chrome_command_ids.h" // IDC_* |
14 #include "chrome/browser/bookmarks/bookmark_editor.h" | 15 #include "chrome/browser/bookmarks/bookmark_editor.h" |
15 #include "chrome/browser/google/google_util.h" | 16 #include "chrome/browser/google/google_util.h" |
16 #include "chrome/browser/instant/instant_controller.h" | 17 #include "chrome/browser/instant/instant_controller.h" |
17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 // animated. Basically, the BWC doesn't see the global picture, but it sees | 135 // animated. Basically, the BWC doesn't see the global picture, but it sees |
135 // grows and shrinks in small increments (as dictated by the animation). Thus | 136 // grows and shrinks in small increments (as dictated by the animation). Thus |
136 // window growth/shrinkage (at the top/bottom) have to be tracked incrementally. | 137 // window growth/shrinkage (at the top/bottom) have to be tracked incrementally. |
137 // Allowing shrinking from the zoomed state also requires tracking: We check on | 138 // Allowing shrinking from the zoomed state also requires tracking: We check on |
138 // any shrink whether we're both zoomed and have previously grown -- if so, we | 139 // any shrink whether we're both zoomed and have previously grown -- if so, we |
139 // set a flag, and constrain any resize by the allowed amounts. On further | 140 // set a flag, and constrain any resize by the allowed amounts. On further |
140 // shrinks, we check the flag (since the size/position of the window will no | 141 // shrinks, we check the flag (since the size/position of the window will no |
141 // longer indicate that the window is shrinking from an apparent zoomed state) | 142 // longer indicate that the window is shrinking from an apparent zoomed state) |
142 // and if it's set we continue to constrain the resize. | 143 // and if it's set we continue to constrain the resize. |
143 | 144 |
144 | |
145 @interface NSWindow (NSPrivateApis) | 145 @interface NSWindow (NSPrivateApis) |
146 // Note: These functions are private, use -[NSObject respondsToSelector:] | 146 // Note: These functions are private, use -[NSObject respondsToSelector:] |
147 // before calling them. | 147 // before calling them. |
148 | 148 |
149 - (void)setBottomCornerRounded:(BOOL)rounded; | 149 - (void)setBottomCornerRounded:(BOOL)rounded; |
150 | 150 |
151 - (NSRect)_growBoxRect; | 151 - (NSRect)_growBoxRect; |
152 | 152 |
153 @end | 153 @end |
154 | 154 |
| 155 // Forward-declare symbols that are part of the 10.6 SDK. |
| 156 #if !defined(MAC_OS_X_VERSION_10_6) || \ |
| 157 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
| 158 |
| 159 enum { |
| 160 NSTouchPhaseBegan = 1U << 0, |
| 161 NSTouchPhaseMoved = 1U << 1, |
| 162 NSTouchPhaseStationary = 1U << 2, |
| 163 NSTouchPhaseEnded = 1U << 3, |
| 164 NSTouchPhaseCancelled = 1U << 4, |
| 165 NSTouchPhaseTouching = NSTouchPhaseBegan | NSTouchPhaseMoved | |
| 166 NSTouchPhaseStationary, |
| 167 NSTouchPhaseAny = NSUIntegerMax |
| 168 }; |
| 169 typedef NSUInteger NSTouchPhase; |
| 170 |
| 171 @interface NSEvent (SnowLeopardDeclarations) |
| 172 - (NSSet*)touchesMatchingPhase:(NSTouchPhase)phase inView:(NSView*)view; |
| 173 @end |
| 174 |
| 175 @interface NSTouch : NSObject |
| 176 - (NSPoint)normalizedPosition; |
| 177 - (id<NSObject, NSCopying>)identity; |
| 178 @end |
| 179 |
| 180 #endif // MAC_OS_X_VERSION_10_6 |
| 181 |
155 // Provide the forward-declarations of new 10.7 SDK symbols so they can be | 182 // Provide the forward-declarations of new 10.7 SDK symbols so they can be |
156 // called when building with the 10.5 SDK. | 183 // called when building with the 10.5 SDK. |
157 #if !defined(MAC_OS_X_VERSION_10_7) || \ | 184 #if !defined(MAC_OS_X_VERSION_10_7) || \ |
158 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | 185 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 |
159 | 186 |
160 enum { | 187 enum { |
161 NSWindowAnimationBehaviorDefault = 0, | 188 NSWindowAnimationBehaviorDefault = 0, |
162 NSWindowAnimationBehaviorNone = 2, | 189 NSWindowAnimationBehaviorNone = 2, |
163 NSWindowAnimationBehaviorDocumentWindow = 3, | 190 NSWindowAnimationBehaviorDocumentWindow = 3, |
164 NSWindowAnimationBehaviorUtilityWindow = 4, | 191 NSWindowAnimationBehaviorUtilityWindow = 4, |
(...skipping 1533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1698 browser_->ExecuteCommandWithDisposition(command, | 1725 browser_->ExecuteCommandWithDisposition(command, |
1699 event_utils::WindowOpenDispositionFromNSEvent(event)); | 1726 event_utils::WindowOpenDispositionFromNSEvent(event)); |
1700 } | 1727 } |
1701 } | 1728 } |
1702 | 1729 |
1703 // Documented in 10.6+, but present starting in 10.5. Called at the beginning | 1730 // Documented in 10.6+, but present starting in 10.5. Called at the beginning |
1704 // of a gesture. | 1731 // of a gesture. |
1705 - (void)beginGestureWithEvent:(NSEvent*)event { | 1732 - (void)beginGestureWithEvent:(NSEvent*)event { |
1706 totalMagnifyGestureAmount_ = 0; | 1733 totalMagnifyGestureAmount_ = 0; |
1707 currentZoomStepDelta_ = 0; | 1734 currentZoomStepDelta_ = 0; |
| 1735 |
| 1736 // On Lion, there's support controlled by a System Preference for two- and |
| 1737 // three-finger navigational gestures. If set to allow three-finger gestures, |
| 1738 // the system gesture recognizer will automatically call |-swipeWithEvent:| |
| 1739 // and that will be handled as it would be on Snow Leopard. The two-finger |
| 1740 // gesture does not do this, so it must be manually recognized. See the note |
| 1741 // inside |-recognizeTwoFingerGestures| for detailed information on the |
| 1742 // interaction of the different preferences. |
| 1743 NSSet* touches = [event touchesMatchingPhase:NSTouchPhaseAny |
| 1744 inView:nil]; |
| 1745 if ([self recognizeTwoFingerGestures] && [touches count] >= 2) { |
| 1746 twoFingerGestureTouches_.reset([[NSMutableDictionary alloc] init]); |
| 1747 for (NSTouch* touch in touches) { |
| 1748 [twoFingerGestureTouches_ setObject:touch forKey:touch.identity]; |
| 1749 } |
| 1750 } |
| 1751 } |
| 1752 |
| 1753 - (void)endGestureWithEvent:(NSEvent*)event { |
| 1754 // This method only needs to process gesture events for two-finger navigation. |
| 1755 if (!twoFingerGestureTouches_.get()) |
| 1756 return; |
| 1757 |
| 1758 // When a multi-touch gesture ends, only one touch will be in the "End" phase. |
| 1759 // Other touches will be in "Moved" or "Unknown" phases. So long as one is |
| 1760 // ended, which it is by virtue of this method being called, the gesture can |
| 1761 // be committed so long as the magnitude is great enough. |
| 1762 NSSet* touches = [event touchesMatchingPhase:NSTouchPhaseAny |
| 1763 inView:nil]; |
| 1764 |
| 1765 // Store the touch data locally and reset the ivar so that new gestures can |
| 1766 // begin. |
| 1767 scoped_nsobject<NSDictionary> beginTouches( |
| 1768 twoFingerGestureTouches_.release()); |
| 1769 |
| 1770 // Construct a vector of magnitudes. Since gesture events do not have the |
| 1771 // |-deltaX| property set, this creates the X magnitude for each finger. |
| 1772 std::vector<CGFloat> magnitudes; |
| 1773 for (NSTouch* touch in touches) { |
| 1774 NSTouch* beginTouch = [beginTouches objectForKey:touch.identity]; |
| 1775 if (!beginTouch) |
| 1776 continue; |
| 1777 |
| 1778 // The |normalizedPosition| is scaled from (0,1). |
| 1779 magnitudes.push_back(touch.normalizedPosition.x - |
| 1780 beginTouch.normalizedPosition.x); |
| 1781 } |
| 1782 |
| 1783 // Need at least two points to gesture. |
| 1784 if (magnitudes.size() < 2) |
| 1785 return; |
| 1786 |
| 1787 CGFloat sum = std::accumulate(magnitudes.begin(), magnitudes.end(), 0.0f); |
| 1788 int command_id = 0; |
| 1789 if (sum > 0.3) |
| 1790 command_id = IDC_FORWARD; |
| 1791 else if (sum < -0.3) |
| 1792 command_id = IDC_BACK; |
| 1793 else |
| 1794 return; |
| 1795 |
| 1796 if (browser_->command_updater()->IsCommandEnabled(command_id)) { |
| 1797 browser_->ExecuteCommandWithDisposition(command_id, |
| 1798 event_utils::WindowOpenDispositionFromNSEvent(event)); |
| 1799 } |
1708 } | 1800 } |
1709 | 1801 |
1710 // Delegate method called when window is resized. | 1802 // Delegate method called when window is resized. |
1711 - (void)windowDidResize:(NSNotification*)notification { | 1803 - (void)windowDidResize:(NSNotification*)notification { |
1712 // Resize (and possibly move) the status bubble. Note that we may get called | 1804 // Resize (and possibly move) the status bubble. Note that we may get called |
1713 // when the status bubble does not exist. | 1805 // when the status bubble does not exist. |
1714 if (statusBubble_) { | 1806 if (statusBubble_) { |
1715 statusBubble_->UpdateSizeAndPosition(); | 1807 statusBubble_->UpdateSizeAndPosition(); |
1716 } | 1808 } |
1717 | 1809 |
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2189 | 2281 |
2190 - (BOOL)supportsBookmarkBar { | 2282 - (BOOL)supportsBookmarkBar { |
2191 return [self supportsWindowFeature:Browser::FEATURE_BOOKMARKBAR]; | 2283 return [self supportsWindowFeature:Browser::FEATURE_BOOKMARKBAR]; |
2192 } | 2284 } |
2193 | 2285 |
2194 - (BOOL)isTabbedWindow { | 2286 - (BOOL)isTabbedWindow { |
2195 return browser_->is_type_tabbed(); | 2287 return browser_->is_type_tabbed(); |
2196 } | 2288 } |
2197 | 2289 |
2198 @end // @implementation BrowserWindowController(WindowType) | 2290 @end // @implementation BrowserWindowController(WindowType) |
OLD | NEW |