-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
106 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,64 @@ | ||
# capacitor-plugin-android-insets | ||
|
||
Capacitro plugin for retrieving proper top offset of Android status bar | ||
Capacitor plugin for retrieving proper top offset of Android status bar. | ||
|
||
This repo is published version with changed name of https://github.com/jorisbertomeu/capacitor-insets-v2, which was based on archived https://github.com/igorcd/capacitor-insets-plugin/. It should work perfectly fine in `Capacitor@4`. | ||
|
||
## Install | ||
|
||
```bash | ||
npm install capacitor-plugin-android-insets | ||
# with npm | ||
npm install --save capacitor-plugin-android-insets | ||
# with yarn | ||
yarn add capacitor-plugin-android-insets | ||
# after any install | ||
npx cap sync | ||
``` | ||
|
||
## Why? | ||
|
||
This plugin is required only on Android when using `StatusBar.setOverlaysWebView({ overlay: true })`. There is a problem with top offset counting, when status bar is transparent. Related issue: https://github.com/ionic-team/capacitor/issues/2840. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { AndroidInsets } from 'capacitor-plugin-android-insets' | ||
|
||
const { value } = await AndroidInsets.top(); | ||
``` | ||
|
||
## API | ||
|
||
<docgen-index> | ||
|
||
* [`echo(...)`](#echo) | ||
* [`top()`](#top) | ||
* [Interfaces](#interfaces) | ||
|
||
</docgen-index> | ||
|
||
<docgen-api> | ||
<!--Update the source file JSDoc comments and rerun docgen to update the docs below--> | ||
|
||
### echo(...) | ||
### top() | ||
|
||
Get top offset of status bar | ||
|
||
```typescript | ||
echo(options: { value: string; }) => Promise<{ value: string; }> | ||
top() => Promise<TopReturn> | ||
``` | ||
|
||
| Param | Type | | ||
| ------------- | ------------------------------- | | ||
| **`options`** | <code>{ value: string; }</code> | | ||
|
||
**Returns:** <code>Promise<{ value: string; }></code> | ||
**Returns:** <code>Promise<<a href="#topreturn">TopReturn</a>></code> | ||
|
||
-------------------- | ||
|
||
|
||
### Interfaces | ||
|
||
|
||
#### TopReturn | ||
|
||
| Prop | Type | | ||
| ----------- | ------------------- | | ||
| **`value`** | <code>number</code> | | ||
|
||
</docgen-api> |
22 changes: 18 additions & 4 deletions
22
android/src/main/java/com/owlsdepartment/plugin/android/insets/AndroidInsets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
package com.owlsdepartment.plugin.android.insets; | ||
|
||
import android.util.Log; | ||
import android.util.DisplayMetrics; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
public class AndroidInsets { | ||
|
||
public String echo(String value) { | ||
Log.i("Echo", value); | ||
return value; | ||
private AppCompatActivity activity; | ||
|
||
public AndroidInsets(AppCompatActivity activity) { | ||
this.activity = activity; | ||
} | ||
|
||
public float getTop() { | ||
DisplayMetrics metrics = this.activity.getResources().getDisplayMetrics(); | ||
int resourceId = this.activity.getResources().getIdentifier("status_bar_height", "dimen", "android"); | ||
float titleBarHeight = 0; | ||
|
||
if (resourceId > 0) { | ||
titleBarHeight = this.activity.getResources().getDimensionPixelSize(resourceId); | ||
} | ||
|
||
return titleBarHeight / metrics.density; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import Foundation | ||
|
||
@objc public class AndroidInsets: NSObject { | ||
@objc public func echo(_ value: String) -> String { | ||
print(value) | ||
return value | ||
@objc public func top() -> Float { | ||
return 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export interface AndroidInsetsPlugin { | ||
echo(options: { value: string }): Promise<{ value: string }>; | ||
top(): Promise<TopReturn>; | ||
} | ||
|
||
export interface TopReturn { | ||
value: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import { WebPlugin } from '@capacitor/core'; | ||
|
||
import type { AndroidInsetsPlugin } from './definitions'; | ||
import type { AndroidInsetsPlugin, TopReturn } from './definitions'; | ||
|
||
export class AndroidInsetsWeb extends WebPlugin implements AndroidInsetsPlugin { | ||
async echo(options: { value: string }): Promise<{ value: string }> { | ||
console.log('ECHO', options); | ||
return options; | ||
async top(): Promise<TopReturn> { | ||
return { value: 0 }; | ||
} | ||
} |