Skip to content

feat: add runtime safe margin controls#464

Open
Albermonte wants to merge 2 commits intoCap-go:mainfrom
Albermonte:runtime-safe-margins-updates
Open

feat: add runtime safe margin controls#464
Albermonte wants to merge 2 commits intoCap-go:mainfrom
Albermonte:runtime-safe-margins-updates

Conversation

@Albermonte
Copy link
Contributor

@Albermonte Albermonte commented Mar 5, 2026

Adds runtime control for safe margins by introducing setEnabledSafeTopMargin and setEnabledSafeBottomMargin.
It enables dynamic safe-area toggling without reopening the browser.
Tested on iOS and Android, though I don't see many changes on Android as I do on iOS.

Summary by CodeRabbit

  • New Features
    • Added setEnabledSafeTopMargin() and setEnabledSafeBottomMargin() APIs to toggle safe-area margins at runtime.
    • Both accept an optional webview ID (defaults to the active webview).
    • Supported on Android and iOS; on Web these methods are present but are no-ops (no effect).

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 5, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bfef722a-ef57-44d5-8d16-5f653827b367

📥 Commits

Reviewing files that changed from the base of the PR and between ac73149 and 99d7260.

📒 Files selected for processing (4)
  • README.md
  • android/src/main/java/ee/forgr/capacitor_inappbrowser/InAppBrowserPlugin.java
  • ios/Sources/InAppBrowserPlugin/WKWebViewController.swift
  • src/definitions.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • README.md
  • android/src/main/java/ee/forgr/capacitor_inappbrowser/InAppBrowserPlugin.java
  • ios/Sources/InAppBrowserPlugin/WKWebViewController.swift

📝 Walkthrough

Walkthrough

Adds runtime APIs to toggle safe-area top and bottom margins for in-app webviews across TypeScript, web (no-op), Android, and iOS implementations; platform code updates options/constraints and triggers inset/layout reapplication for the targeted webview (optional id or active one).

Changes

Cohort / File(s) Summary
Docs
README.md
Added docs entries for setEnabledSafeTopMargin and setEnabledSafeBottomMargin with signatures and descriptions.
TypeScript Definitions
src/definitions.ts
Added setEnabledSafeTopMargin(options:{enabled:boolean;id?:string}):Promise<void> and setEnabledSafeBottomMargin(...).
Web Implementation (no-op)
src/web.ts
Added stub/no-op implementations that log unsupported platform and return.
Android Plugin
android/src/main/java/.../InAppBrowserPlugin.java
Added plugin methods that parse enabled, resolve the target dialog by id/active, run on UI thread, and delegate to dialog setters with error handling.
Android Dialog
android/src/main/java/.../WebViewDialog.java
Added setEnabledSafeTopMargin(boolean) and setEnabledSafeBottomMargin(boolean) to update options and call ViewCompat.requestApplyInsets on the WebView when changed.
iOS Plugin
ios/Sources/InAppBrowserPlugin/InAppBrowserPlugin.swift
Added plugin methods that read optional enabled flags, dispatch on main queue, locate target controller, and call update methods or reject if controller missing.
iOS ViewController
ios/Sources/InAppBrowserPlugin/WKWebViewController.swift
Added updateSafeTopMargin(_:) and updateSafeBottomMargin(_:) to switch top/bottom constraints between safeAreaLayoutGuide and view anchors and re-layout the webView.

Sequence Diagram(s)

sequenceDiagram
    participant Client as App/Client
    participant PluginA as Android Plugin
    participant UIThread as UI Thread
    participant Dialog as WebViewDialog
    participant WebV as WebView

    Client->>PluginA: setEnabledSafeTopMargin({enabled, id?})
    PluginA->>PluginA: parse options, find dialog
    PluginA->>UIThread: run on UI thread
    UIThread->>Dialog: setEnabledSafeTopMargin(enabled)
    Dialog->>Dialog: update options if changed
    Dialog->>WebV: ViewCompat.requestApplyInsets(WebV)
    WebV->>WebV: recompute insets/layout
    Dialog-->>UIThread: complete
    UIThread-->>PluginA: resolve call
    PluginA-->>Client: Promise<void>
Loading
sequenceDiagram
    participant Client as App/Client
    participant PluginI as iOS Plugin
    participant MainQ as Main Queue
    participant VC as WKWebViewController
    participant Layout as Layout Engine

    Client->>PluginI: setEnabledSafeBottomMargin({enabled, id?})
    PluginI->>MainQ: dispatch async
    MainQ->>VC: locate target controller
    MainQ->>VC: updateSafeBottomMargin(enabled)
    VC->>VC: deactivate old bottom constraint
    VC->>VC: create & activate new bottom constraint (safeArea or view)
    VC->>Layout: setNeedsLayout / layoutIfNeeded
    Layout->>VC: recompute webView frame
    VC-->>MainQ: complete
    MainQ-->>PluginI: resolve call
    PluginI-->>Client: Promise<void>
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • riderx

Poem

🐇 I toggle margins with a joyful hop,

Top and bottom find their snug spot,
Insets shuffle, constraints take flight,
Webviews cozy, snug and right,
A rabbit's cheer for layout delight.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: add runtime safe margin controls' accurately summarizes the main change: introducing two new API methods for runtime control of safe-area margins in the in-app browser across multiple platforms.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@android/src/main/java/ee/forgr/capacitor_inappbrowser/InAppBrowserPlugin.java`:
- Around line 1281-1285: The code in setEnabledSafeTopMargin currently defaults
missing "enabled" to true, hiding client bugs; change it to validate presence of
the "enabled" key and reject the call immediately when absent (use
call.reject("missing 'enabled' boolean") and return) instead of defaulting;
update the same pattern where call.getBoolean("enabled", true) is used (also the
occurrence near the resolveTargetId usage) so both the
call.getData().has("enabled") check leads to a rejection path and subsequent
code uses call.getBoolean("enabled") only after presence is confirmed.

In `@ios/Sources/InAppBrowserPlugin/WKWebViewController.swift`:
- Around line 2035-2070: The methods updateSafeTopMargin(_:) and
updateSafeBottomMargin(_:) should still flip the enabled flags but must avoid
changing constraints when the webView is not a subview of self.view to prevent
"no common ancestor" Auto Layout crashes; after guarding and setting
self.enabledSafeTopMargin/self.enabledSafeBottomMargin and obtaining webView,
add a check that webView.superview === self.view and only then run the
constraint deactivation/activation and layoutIfNeeded logic; leave early
(return) if the webView has been moved to another hierarchy.

In `@README.md`:
- Around line 716-734: Update the runtime docs for setEnabledSafeTopMargin and
setEnabledSafeBottomMargin to explicitly state that these APIs are no-ops on the
web platform; locate the entries for setEnabledSafeTopMargin (around the "Sets
the enabled safe top margin..." description) and setEnabledSafeBottomMargin (the
corresponding bottom margin section) and append a short note like "On web, this
API is a no-op" or similar to each description so readers know the behavior
differs by platform.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 17d14994-67ca-45dd-9a2e-eccc041a2326

📥 Commits

Reviewing files that changed from the base of the PR and between 90fa029 and ac73149.

📒 Files selected for processing (7)
  • README.md
  • android/src/main/java/ee/forgr/capacitor_inappbrowser/InAppBrowserPlugin.java
  • android/src/main/java/ee/forgr/capacitor_inappbrowser/WebViewDialog.java
  • ios/Sources/InAppBrowserPlugin/InAppBrowserPlugin.swift
  • ios/Sources/InAppBrowserPlugin/WKWebViewController.swift
  • src/definitions.ts
  • src/web.ts

Require explicit boolean input for Android runtime safe-margin toggles instead of silently defaulting, preventing hidden client-side bugs.

Guard iOS runtime safe-margin constraint updates when webView is detached from self.view to avoid no-common-ancestor Auto Layout crashes.

Document that web implementations of runtime safe-margin APIs are no-ops in generated API docs.
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 5, 2026

@riderx
Copy link
Member

riderx commented Mar 5, 2026

Build seems to fail can you have a look?

@Albermonte
Copy link
Contributor Author

Albermonte commented Mar 5, 2026

Build seems to fail can you have a look?

Weird, it was working when creating the PR, could be the changes from CodeRabbit?
Will take a look tomorrow.

Edit: Seeing this in the CI, can you try to re-run it?

Internal server error. Correlation ID: 5092d054-ba61-43e7-bbe7-e2970c6340bd
--
The job was not acquired by Runner of type hosted even after multiple attempts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants