|
1 | | -import { Application, Utils } from '@nativescript/core'; |
| 1 | +import { Application, Utils, View } from '@nativescript/core'; |
2 | 2 | import { colorSchemeProperty, ColorSchemeType, colorStyleProperty, ColorStyleType, Configuration, GoogleSignInButtonBase, IUser } from './common'; |
3 | 3 |
|
4 | 4 | export class GoogleError extends Error { |
@@ -261,12 +261,21 @@ export class GoogleSignin { |
261 | 261 | } |
262 | 262 |
|
263 | 263 | export class GoogleSignInButton extends GoogleSignInButtonBase { |
| 264 | + private _tapHandler: NSObject; |
| 265 | + |
264 | 266 | createNativeView() { |
265 | 267 | return GIDSignInButton.new(); |
266 | 268 | } |
267 | 269 |
|
268 | | - initNativeView() { |
| 270 | + public initNativeView(): void { |
269 | 271 | super.initNativeView(); |
| 272 | + this._tapHandler = TapHandlerImpl.initWithOwner(new WeakRef(this)); |
| 273 | + this.nativeViewProtected.addTargetActionForControlEvents(this._tapHandler, 'tap', UIControlEvents.TouchUpInside); |
| 274 | + } |
| 275 | + |
| 276 | + public disposeNativeView(): void { |
| 277 | + this._tapHandler = null; |
| 278 | + super.disposeNativeView(); |
270 | 279 | } |
271 | 280 |
|
272 | 281 | [colorSchemeProperty.setNative](value: ColorSchemeType) { |
@@ -307,12 +316,62 @@ export class GoogleSignInButton extends GoogleSignInButtonBase { |
307 | 316 | } |
308 | 317 | } |
309 | 318 |
|
310 | | - public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number) { |
311 | | - const nativeView = this.nativeView; |
| 319 | + public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void { |
| 320 | + const layout = Utils.layout; |
| 321 | + |
| 322 | + const nativeView = this.nativeViewProtected; |
| 323 | + |
312 | 324 | if (nativeView) { |
313 | | - const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec); |
314 | | - const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec); |
315 | | - this.setMeasuredDimension(width, height); |
| 325 | + const width = layout.getMeasureSpecSize(widthMeasureSpec); |
| 326 | + const widthMode = layout.getMeasureSpecMode(widthMeasureSpec); |
| 327 | + const height = layout.getMeasureSpecSize(heightMeasureSpec); |
| 328 | + const heightMode = layout.getMeasureSpecMode(heightMeasureSpec); |
| 329 | + |
| 330 | + const horizontalPadding = this.effectivePaddingLeft + this.effectiveBorderLeftWidth + this.effectivePaddingRight + this.effectiveBorderRightWidth; |
| 331 | + let verticalPadding = this.effectivePaddingTop + this.effectiveBorderTopWidth + this.effectivePaddingBottom + this.effectiveBorderBottomWidth; |
| 332 | + |
| 333 | + // The default button padding for UIButton - 6dip top and bottom. |
| 334 | + if (verticalPadding === 0) { |
| 335 | + verticalPadding = layout.toDevicePixels(12); |
| 336 | + } |
| 337 | + |
| 338 | + const desiredSize = layout.measureNativeView(nativeView, width - horizontalPadding, widthMode, height - verticalPadding, heightMode); |
| 339 | + |
| 340 | + desiredSize.width = desiredSize.width + horizontalPadding; |
| 341 | + desiredSize.height = desiredSize.height + verticalPadding; |
| 342 | + |
| 343 | + const measureWidth = Math.max(desiredSize.width, this.effectiveMinWidth); |
| 344 | + const measureHeight = Math.max(desiredSize.height, this.effectiveMinHeight); |
| 345 | + |
| 346 | + const widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0); |
| 347 | + const heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0); |
| 348 | + |
| 349 | + this.setMeasuredDimension(widthAndState, heightAndState); |
316 | 350 | } |
317 | 351 | } |
318 | 352 | } |
| 353 | + |
| 354 | +@NativeClass |
| 355 | +class TapHandlerImpl extends NSObject { |
| 356 | + private _owner: WeakRef<GoogleSignInButton>; |
| 357 | + |
| 358 | + public static initWithOwner(owner: WeakRef<GoogleSignInButton>): TapHandlerImpl { |
| 359 | + const handler = <TapHandlerImpl>TapHandlerImpl.new(); |
| 360 | + handler._owner = owner; |
| 361 | + return handler; |
| 362 | + } |
| 363 | + |
| 364 | + public tap(args) { |
| 365 | + // _owner is a {N} view which could get destroyed when a tap initiates (protect!) |
| 366 | + if (this._owner) { |
| 367 | + const owner = this._owner?.deref(); |
| 368 | + if (owner) { |
| 369 | + owner._emit(GoogleSignInButton.tapEvent); |
| 370 | + } |
| 371 | + } |
| 372 | + } |
| 373 | + |
| 374 | + public static ObjCExposedMethods = { |
| 375 | + tap: { returns: interop.types.void, params: [interop.types.id] }, |
| 376 | + }; |
| 377 | +} |
0 commit comments