Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.webkit.WebViewCompat;
import com.getcapacitor.JSObject;
import com.getcapacitor.Logger;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
Expand Down Expand Up @@ -157,6 +158,17 @@ public void setStyle(final PluginCall call) {
});
}

@PluginMethod
public void getStyle(final PluginCall call) {
String bar = call.getString("bar", BAR_STATUS_BAR);

getBridge().executeOnMainThread(() -> {
JSObject ret = new JSObject();
ret.put("style", bar.equals(BAR_GESTURE_BAR) ? currentGestureBarStyle : currentStatusBarStyle);
call.resolve(ret);
});
}

@PluginMethod
public void show(final PluginCall call) {
String bar = call.getString("bar", "");
Expand Down Expand Up @@ -299,20 +311,18 @@ private void injectSafeAreaCSS(WindowInsetsCompat insets) {
}

private void setStyle(String style, String bar) {
if (style.equals(STYLE_DEFAULT)) {
style = getStyleForTheme();
}
String resolvedStyle = style.equals(STYLE_DEFAULT) ? getStyleForTheme() : style;

Window window = getActivity().getWindow();
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, window.getDecorView());
if (bar.isEmpty() || bar.equals(BAR_STATUS_BAR)) {
currentStatusBarStyle = style;
windowInsetsControllerCompat.setAppearanceLightStatusBars(!style.equals(STYLE_DARK));
windowInsetsControllerCompat.setAppearanceLightStatusBars(!resolvedStyle.equals(STYLE_DARK));
}

if (bar.isEmpty() || bar.equals(BAR_GESTURE_BAR)) {
currentGestureBarStyle = style;
windowInsetsControllerCompat.setAppearanceLightNavigationBars(!style.equals(STYLE_DARK));
windowInsetsControllerCompat.setAppearanceLightNavigationBars(!resolvedStyle.equals(STYLE_DARK));

@OS-pedrogustavobilro OS-pedrogustavobilro Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 Claude noticed this difference between iOS and Android, it's not introduced by your PR, so this isn't necessarily the place to fix it, but I'll point it out here and leave that decision to you:

Passing an undefined style like Purple (which shouldn't happen if they're using typescript, but can happen in plain JS), on iOS it sets it to default, on Android the raw string is stored in current variable, and sets the appearance as light theme (dark text).

This issue is probably happening in 8.x too.

Makes more sense for Android to behave like iOS to me in this scenario. If you do this change, adding a test may also be nice.

}

getActivity().getWindow().getDecorView().setBackgroundColor(getThemeColor(getContext(), android.R.attr.windowBackground));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.getcapacitor.plugin;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -15,8 +22,12 @@
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import com.getcapacitor.Bridge;
import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import java.lang.reflect.Method;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;

public class SystemBarsTest {
Expand Down Expand Up @@ -48,6 +59,111 @@ public void showWithNavigationBarShowsOnlyNavigationBars() throws Exception {
verify(controller, never()).show(WindowInsetsCompat.Type.statusBars());
}

@Test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 Some suggestions from Claude on the android unit tests, take em with a grain of salt

Suggested new testst to add:

  • getStyle returns DEFAULT before any style is set.
  • Styling only the status bar leaves the navigation bar untouched, both its appearance and its reported style.
  • DEFAULT resolves to dark appearance in night mode while still reporting DEFAULT.
  • An explicit DARK style "survives" a configuration change.

public void getStyleReturnsDefaultAfterSettingDefault() throws Exception {
try (StyleFixture fixture = new StyleFixture()) {
fixture.setStyle("DEFAULT", "");

verify(fixture.controller).setAppearanceLightStatusBars(true);
assertEquals("DEFAULT", fixture.getStyle("StatusBar"));
assertEquals("DEFAULT", fixture.getStyle("NavigationBar"));
}
}

@Test
public void getStyleReturnsStyleOfRequestedBar() throws Exception {
try (StyleFixture fixture = new StyleFixture()) {
fixture.setStyle("LIGHT", "");
fixture.setStyle("DARK", "NavigationBar");

assertEquals("LIGHT", fixture.getStyle("StatusBar"));
assertEquals("DARK", fixture.getStyle("NavigationBar"));
}
}

@Test
public void getStyleDefaultsToStatusBar() throws Exception {
try (StyleFixture fixture = new StyleFixture()) {
fixture.setStyle("DARK", "StatusBar");

assertEquals("DARK", fixture.getStyle(null));
}
}

@Test
public void configurationChangeReappliesDefaultStyleForNewTheme() throws Exception {
try (StyleFixture fixture = new StyleFixture()) {
fixture.setStyle("DEFAULT", "");
verify(fixture.controller).setAppearanceLightStatusBars(true);
verify(fixture.controller).setAppearanceLightNavigationBars(true);

fixture.configuration.uiMode = Configuration.UI_MODE_NIGHT_YES;
fixture.plugin.handleOnConfigurationChanged(fixture.configuration);

verify(fixture.controller).setAppearanceLightStatusBars(false);
verify(fixture.controller).setAppearanceLightNavigationBars(false);
}
}

private static class StyleFixture implements AutoCloseable {

final SystemBars plugin = new SystemBars();
final Configuration configuration = mock(Configuration.class);
final WindowInsetsControllerCompat controller = mock(WindowInsetsControllerCompat.class);
private final MockedStatic<WindowCompat> windowCompat = mockStatic(WindowCompat.class);
private final MockedConstruction<TypedValue> typedValues = mockConstruction(TypedValue.class);

StyleFixture() {
Bridge bridge = mock(Bridge.class);
AppCompatActivity activity = mock(AppCompatActivity.class);
Resources resources = mock(Resources.class);
Context context = mock(Context.class);
Window window = mock(Window.class);
View decorView = mock(View.class);

configuration.uiMode = Configuration.UI_MODE_NIGHT_NO;
when(bridge.getActivity()).thenReturn(activity);
when(bridge.getContext()).thenReturn(context);
when(context.getTheme()).thenReturn(mock(Resources.Theme.class));
when(activity.getResources()).thenReturn(resources);
when(resources.getConfiguration()).thenReturn(configuration);
when(activity.getWindow()).thenReturn(window);
when(window.getDecorView()).thenReturn(decorView);
doAnswer((invocation) -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(bridge)
.executeOnMainThread(any(Runnable.class));
windowCompat.when(() -> WindowCompat.getInsetsController(window, decorView)).thenReturn(controller);

plugin.setBridge(bridge);
}

void setStyle(String style, String bar) throws Exception {
Method setStyle = SystemBars.class.getDeclaredMethod("setStyle", String.class, String.class);
setStyle.setAccessible(true);
setStyle.invoke(plugin, style, bar);
}

String getStyle(String bar) {
PluginCall call = mock(PluginCall.class);
when(call.getString("bar", "StatusBar")).thenReturn(bar == null ? "StatusBar" : bar);

plugin.getStyle(call);

ArgumentCaptor<JSObject> result = ArgumentCaptor.forClass(JSObject.class);
verify(call).resolve(result.capture());
return result.getValue().getString("style");
}

@Override
public void close() {
typedValues.close();
windowCompat.close();
}
}

@Test
public void bottomInsetIsNavigationBarWhenKeyboardHidden() throws Exception {
assertEquals(126, invokeGetBottomInset(126, 0, false));
Expand Down
53 changes: 52 additions & 1 deletion core/src/core-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin } from './definitions';
import type { Plugin, PluginListenerHandle } from './definitions';
import { registerPlugin } from './global';
import { WebPlugin } from './web-plugin';

Expand Down Expand Up @@ -604,6 +604,27 @@ export interface SystemBarsAnimationOptions {
animation: SystemBarsAnimation;
}

export interface SystemBarsGetStyleOptions {
/**
* The system bar to get the style of.
*
* On iOS, only the status bar has a style, so this option is ignored.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor: since you added this line, maybe worth mentioning the same for SystemBarsStyleOptions of setStyle

*
* @since 9.0.0
* @default 'StatusBar'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor: to align with other places that have a SystemBarType parameter, maybe should be

Suggested change
* @default 'StatusBar'
* @default SystemBarType.StatusBar

*/
bar?: SystemBarType;
}

export interface SystemBarsGetStyleResult {
/**
* The style last set for the system bar, using `setStyle()` or the `style` configuration value.
*
* @since 9.0.0
*/
style: SystemBarsStyle;
}

export interface SystemBarsPlugin {
/**
* Set the current style of the system bars.
Expand All @@ -612,6 +633,16 @@ export interface SystemBarsPlugin {
*/
setStyle(options: SystemBarsStyleOptions): Promise<void>;

/**
* Get the current style of a system bar.
*
* Returns the style that was requested, so `DEFAULT` is returned as `DEFAULT`
* rather than the `LIGHT` or `DARK` style it currently resolves to.
*
* @since 9.0.0
*/
getStyle(options?: SystemBarsGetStyleOptions): Promise<SystemBarsGetStyleResult>;
Comment on lines +637 to +644

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This works fine, but there could also be people that want to know, when DEFAULT, is the system bars LIGHT or DARK, i.e. what is the app theme.

Afaik there's no way in an official plugin to check if app is (natively) in dark mode or light mode. Which doesn't make sense to add here anyways.

Wondering if it's worth having the getStyle method return style and resolvedStyle to overcome this kind of situation, or would you say it doesn't make that much sense?


/**
* Show the system bars.
*
Expand All @@ -634,13 +665,33 @@ export interface SystemBarsPlugin {
* @since 8.0.0
*/
setAnimation(options: SystemBarsAnimationOptions): Promise<void>;

/**
* Listen for taps on the status bar.
*
* Only available on iOS.
*
* @since 9.0.0
*/
addListener(eventName: 'statusTap', listenerFunc: () => void): Promise<PluginListenerHandle>;

/**
* Remove all listeners for this plugin.
*
* @since 9.0.0
*/
removeAllListeners(): Promise<void>;
}

export class SystemBarsPluginWeb extends WebPlugin implements SystemBarsPlugin {
async setStyle(): Promise<void> {
this.unavailable('not available for web');
}

async getStyle(): Promise<SystemBarsGetStyleResult> {
throw this.unavailable('not available for web');
}
Comment on lines 687 to +693

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Any particular reason why we added the throw here since other methods don't have it?


async setAnimation(): Promise<void> {
this.unavailable('not available for web');
}
Expand Down
Loading