-
Notifications
You must be signed in to change notification settings - Fork 24.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Viewport] Add utility to get viewport dimensions #418
Changes from 4 commits
b4bdf5a
9f9c976
3f17af4
d730283
0ed79bf
429d86a
faeea27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule Viewport | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
var NativeModules = require('NativeModules'); | ||
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter'); | ||
var RCTDimensionManager = NativeModules.DimensionManager; | ||
|
||
var logError = require('logError'); | ||
|
||
var DEVICE_DIMENSIONS_EVENT = 'dimensionsDidChange'; | ||
|
||
/** | ||
* ViewPortDimensions class gives access to the viewport's height and width. | ||
* | ||
* This could be used if someone wanted an image which stretched the width of | ||
* the device. | ||
* | ||
* Note: The values returned here are all ready in terms of the pixel ratio of | ||
* the device. For example, the iPhone 6 renders 750x1334 pixels. However, it has | ||
* a pixel ratio of two. Therefore the return values of these functions will | ||
* all ready be modified by the pixel ratio. In the iPhone 6's case this means | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll change, but 'all ready' is a real thing! 'already' is just a bastardization! haha |
||
* that the resolution that gets returned will be 375x667. | ||
*/ | ||
var Viewport = {}; | ||
|
||
if (RCTDimensionManager) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this check... this library should be always present since it's part of the core, and it doesn't call any method at initialization time anyway. |
||
var _dimensionSubscriptions = {}; | ||
|
||
Viewport.addEventListener = function( | ||
eventName: ChangeEventName, | ||
handler: Function | ||
): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use the |
||
_dimensionSubscriptions[handler] = RCTDeviceEventEmitter.addListener( | ||
DEVICE_DIMENSIONS_EVENT, | ||
handler | ||
); | ||
}; | ||
|
||
Viewport.removeEventListener = function( | ||
eventName: ChangeEventName, | ||
handler: Function | ||
): void { | ||
if (!_dimensionSubscriptions[handler]) { | ||
return; | ||
} | ||
_dimensionSubscriptions[handler].remove(); | ||
_dimensionSubscriptions[handler] = null; | ||
}; | ||
|
||
Viewport.getDimensions = function( | ||
handler: Function | ||
) { | ||
RCTDimensionManager.getCurrentDimensions(handler, logError); | ||
} | ||
} | ||
|
||
module.exports = Viewport; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import "RCTBridgeModule.h" | ||
|
||
@interface RCTDimensionManager : NSObject<RCTBridgeModule> | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please indent it with 2 spaces? 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Derp...will do. |
||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import "RCTDimensionManager.h" | ||
#import "RCTBridge.h" | ||
#import "RCTEventDispatcher.h" | ||
#import "RCTUtils.h" | ||
|
||
static NSDictionary *RCTCurrentDimensions() | ||
{ | ||
static NSDictionary *dimensions; | ||
|
||
CGSize frameSize = [UIScreen mainScreen].applicationFrame.size; | ||
if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) | ||
&& UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) { | ||
frameSize = CGSizeMake(frameSize.height, frameSize.width); | ||
} | ||
|
||
dimensions = @{ | ||
@"width": [NSNumber numberWithFloat:frameSize.width], | ||
@"height": [NSNumber numberWithFloat:frameSize.height] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @{
@"width": @(frameSize.width),
@"height": @(frameSize.height),
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had it like that at first but it wouldn't compile. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pjjanak The syntax @(someFloatValue) is a shorthand in Objective-C for [NSNumber numberWithFloat:someFloatValue]. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, gotcha. I'll switch that out then. |
||
}; | ||
|
||
return dimensions; | ||
} | ||
|
||
|
||
@implementation RCTDimensionManager | ||
{ | ||
NSDictionary *_lastKnownDimensions; | ||
} | ||
|
||
@synthesize bridge = _bridge; | ||
|
||
#pragma mark - Lifecycle | ||
|
||
- (instancetype)init | ||
{ | ||
if ((self = [super init])) { | ||
_lastKnownDimensions = RCTCurrentDimensions(); | ||
|
||
[[NSNotificationCenter defaultCenter] addObserver:self | ||
selector:@selector(deviceOrientationDidChangeNotification:) | ||
name:UIDeviceOrientationDidChangeNotification | ||
object:nil]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[[NSNotificationCenter defaultCenter] removeObserver:self]; | ||
} | ||
|
||
#pragma mark - Notification methods | ||
|
||
- (void)deviceOrientationDidChangeNotification:(NSNotification*)note | ||
{ | ||
_lastKnownDimensions = RCTCurrentDimensions(); | ||
[_bridge.eventDispatcher sendDeviceEventWithName:@"dimensionsDidChange" body:_lastKnownDimensions]; | ||
} | ||
|
||
#pragma mark - Public API | ||
/** | ||
* Get the current dimensions of the viewport | ||
*/ | ||
- (void)getCurrentDimensions:(RCTResponseSenderBlock)callback | ||
error:(__unused RCTResponseSenderBlock)error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was just basing it off of |
||
{ | ||
RCT_EXPORT(); | ||
_lastKnownDimensions = RCTCurrentDimensions(); | ||
|
||
callback(@[_lastKnownDimensions]); | ||
} | ||
|
||
|
||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Viewport
instead ofViewPortDimensions