UNPKG

@nativescript-community/ui-popover

Version:
179 lines 9.26 kB
import { Application, Color, IOSHelper, Screen, Trace, Utils, View } from '@nativescript/core'; import { HorizontalPosition, VerticalPosition, _commonPopoverDismissed, _commonShowNativePopover } from '.'; export * from './index.common'; var UIPopoverPresentationControllerDelegateImpl = /** @class */ (function (_super) { __extends(UIPopoverPresentationControllerDelegateImpl, _super); function UIPopoverPresentationControllerDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } UIPopoverPresentationControllerDelegateImpl.initWithOptions = function (options) { var delegate = new UIPopoverPresentationControllerDelegateImpl(); delegate._options = options; return delegate; }; UIPopoverPresentationControllerDelegateImpl.prototype.adaptivePresentationStyleForPresentationControllerTraitCollection = function (controller, traitCollection) { return UIModalPresentationStyle.None; }; UIPopoverPresentationControllerDelegateImpl.prototype.popoverPresentationControllerDidDismissPopover = function (popoverPresentationController) { if (this._options.onDismiss) { this._options.onDismiss(); } }; UIPopoverPresentationControllerDelegateImpl.prototype.popoverPresentationControllerShouldDismissPopover = function (popoverPresentationController) { var _a; return (_a = this._options) === null || _a === void 0 ? void 0 : _a.outsideTouchable; }; UIPopoverPresentationControllerDelegateImpl.ObjCProtocols = [UIPopoverPresentationControllerDelegate]; return UIPopoverPresentationControllerDelegateImpl; }(NSObject)); var PopoverViewController = /** @class */ (function (_super) { __extends(PopoverViewController, _super); function PopoverViewController() { return _super !== null && _super.apply(this, arguments) || this; } PopoverViewController.initWithOwner = function (owner) { var controller = PopoverViewController.new(); controller.owner = owner; return controller; }; PopoverViewController.prototype.loadView = function () { this.view = createUIViewAutoSizeUIViewAutoSize(this.owner.get()); }; PopoverViewController.prototype.viewDidLoad = function () { var _a; _super.prototype.viewDidLoad.call(this); (_a = this.owner.get()) === null || _a === void 0 ? void 0 : _a.callLoaded(); var size = this.view.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize); this.preferredContentSize = size; }; PopoverViewController.prototype.viewWillAppear = function (animated) { if (this.nBackgroundColor) { this.view.superview.backgroundColor = this.nBackgroundColor; } _super.prototype.viewWillAppear.call(this, animated); }; return PopoverViewController; }(UIViewController)); var UIViewAutoSizeUIViewAutoSize = /** @class */ (function (_super) { __extends(UIViewAutoSizeUIViewAutoSize, _super); function UIViewAutoSizeUIViewAutoSize() { return _super !== null && _super.apply(this, arguments) || this; } UIViewAutoSizeUIViewAutoSize.prototype.systemLayoutSizeFittingSize = function (boundsSize) { var _a; var view = (_a = this._view) === null || _a === void 0 ? void 0 : _a.get(); if (!view) { return CGSizeZero; } var widthSpec = Utils.layout.makeMeasureSpec(Math.max(Screen.mainScreen.widthPixels, Utils.layout.toDevicePixels(boundsSize.width)), Utils.layout.AT_MOST); var heighthSpec = Utils.layout.makeMeasureSpec(Math.max(Screen.mainScreen.widthPixels, Utils.layout.toDevicePixels(boundsSize.height)), Utils.layout.AT_MOST); var measuredSize = View.measureChild(null, view, widthSpec, heighthSpec); view.setMeasuredDimension(measuredSize.measuredWidth, measuredSize.measuredHeight); var size = CGSizeMake(Utils.layout.toDeviceIndependentPixels(measuredSize.measuredWidth), Utils.layout.toDeviceIndependentPixels(measuredSize.measuredHeight)); return size; }; UIViewAutoSizeUIViewAutoSize.prototype.layoutSubviews = function () { var _a; var view = (_a = this._view) === null || _a === void 0 ? void 0 : _a.get(); if (!view) { return; } var size = this.frame.size; View.layoutChild(null, view, 0, 0, Utils.layout.toDevicePixels(size.width), Utils.layout.toDevicePixels(size.height)); }; return UIViewAutoSizeUIViewAutoSize; }(UIView)); function createUIViewAutoSizeUIViewAutoSize(view) { const self = UIViewAutoSizeUIViewAutoSize.new(); view._setupAsRootView({}); view.parent = Application.getRootView(); view._isAddedToNativeVisualTree = true; // view.callLoaded(); // will be called in viewDidLoad. doing it here would trigger requestLayout before // controller view is set and thus call loadView again self._view = new WeakRef(view); self.addSubview(view.nativeViewProtected); view.nativeViewProtected.autoresizingMask = 2 /* UIViewAutoresizing.FlexibleWidth */ | 16 /* UIViewAutoresizing.FlexibleHeight */; return self; } export function showPopover(view, { anchor, vertPos = VerticalPosition.BELOW, horizPos = HorizontalPosition.CENTER, x = 0, y = 0, fitInScreen = true, transparent = false, onDismiss, outsideTouchable = true, backgroundColor, canOverlapSourceViewRect = false, passthroughViews = null, context = {}, hideArrow = false }) { _commonShowNativePopover(view); let parentWithController = IOSHelper.getParentWithViewController(anchor); if (!parentWithController) { Trace.write(`Could not find parent with viewController for ${parent} while showing bottom sheet view.`, Trace.categories.ViewHierarchy, Trace.messageType.error); throw new Error('missing_parent_controller'); } let parentController = parentWithController.viewController; // we loop to ensure we are showing from the top presented view controller while (parentController.presentedViewController) { parentController = parentController.presentedViewController; parentWithController = parentWithController['_modal'] || parentWithController; } const controller = PopoverViewController.initWithOwner(new WeakRef(view)); view.viewController = controller; let result; function _onDismiss() { onDismiss?.(result); controller.popoverPresentationController.delegate = null; if (view && view.isLoaded) { view.callUnloaded(); } _commonPopoverDismissed(view); view._isAddedToNativeVisualTree = false; view._tearDownUI(); view.parent = null; } controller.modalPresentationStyle = 7 /* UIModalPresentationStyle.Popover */; if (!controller.popoverPresentationController.delegate) { controller.popoverPresentationController.delegate = UIPopoverPresentationControllerDelegateImpl.initWithOptions({ outsideTouchable, onDismiss: _onDismiss }); } //@ts-ignore controller.popoverPresentationController.passthroughViews = passthroughViews?.map((v) => v?.nativeViewProtected).filter((v) => !!v); controller.popoverPresentationController.canOverlapSourceViewRect = canOverlapSourceViewRect; if (transparent) { controller.nBackgroundColor = UIColor.clearColor; } else if (backgroundColor) { controller.nBackgroundColor = (backgroundColor instanceof Color ? backgroundColor : new Color(backgroundColor)).ios; // controller.popoverPresentationController.backgroundColor = backgroundColor.ios; } else if (view.style.backgroundColor) { controller.nBackgroundColor = view.style.backgroundColor.ios; controller.popoverPresentationController.backgroundColor = view.style.backgroundColor.ios; } const bounds = anchor.nativeViewProtected.bounds; const width = bounds.size.width; const height = bounds.size.height; const deltaX = 0; const deltaY = 0; if (hideArrow || transparent) { controller.popoverPresentationController.permittedArrowDirections = 0; // deltaX = Utils.layout.toDeviceIndependentPixels(x); // switch (horizPos) { // case HorizontalPosition.ALIGN_LEFT: // deltaX -= width / 2; // break; // case HorizontalPosition.ALIGN_RIGHT: // deltaX += width / 2; // break; // } } controller.popoverPresentationController.sourceView = anchor.nativeViewProtected; controller.popoverPresentationController.sourceRect = CGRectOffset(bounds, Math.min(deltaX, width / 2), Math.min(deltaY, height / 2)); parentWithController.viewController.presentModalViewControllerAnimated(controller, true); return { ios: controller, close: async (r) => { result = r; return new Promise((resolve) => { parentController.dismissViewControllerAnimatedCompletion(true, () => { _onDismiss(); resolve(); }); }); } }; } //# sourceMappingURL=index.ios.js.map