---
url: https://nsh.dev/versions/v0.7.2/docs/device-and-environment
description: "Reading the window size, color scheme, font scale, safe areas, the platform, pixel density and layout direction, and reacting when they change."
demo: https://nsh.dev/versions/v0.7.2/play/index.html#guide-device
---

# Device and environment

The environment an app runs in changes while it runs: the window resizes (rotation, split view, a Mac window), the user switches to dark mode or a larger text size, the keyboard comes and goes. NSH exposes each of these as a **reactive accessor**: call it like a signal, and whatever read it (a style, a text, a computed value) updates when it changes. There are no listeners to add or remove.

In Rust the accessors are functions of `nsh_core::prelude`; in TSX they come from the modules listed.

| Rust | TSX | Value |
|---|---|---|
| `window()` | `windowSize()` (`nsh`) | The surface's width and height, in points. |
| `dark()` | `colorScheme()` (`nsh`) | Dark or light: the system's, or the app's own (`set_color_scheme`, `setColorScheme`). |
| `font_scale()` | `fontScale()` (`nsh`) | The user's preferred text size relative to the default (1 is the default). |
| `safe_area()` | `safeArea()` (`nsh`) | Insets of the surface not covered by system UI: top, right, bottom, left. |
| `cx.insets()` | `safeAreaInsets()` (`nsh`) | The insets where the calling component renders (a page, a dialog). |
| `keyboard_height()`, `keyboard_duration()` | `keyboardFrame()` (`nsh`) | The software keyboard's height and animation duration. |
| `locale()` | `locale()` (`nsh`) | The user's locale as a BCP 47 tag (`"en-US"`, `"zh-Hans-CN"`): what `Intl` formats for by default. |
| `locale_settings()` | `localeSettings()` (`nsh`) | The user's locale settings: the preferred languages, the time zone, 12- or 24-hour time, the day weeks start on, the units of measurements and of temperatures ([Locale settings](#locale-settings)). |
| `lifecycle()` | `lifecycle()` (`nsh`) | Whether the app is in use: `active`, `inactive` (visible without the focus) or `background` ([In the background](#in-the-background)). |
| `wake_count()` | `wakeCount()` (`nsh`) | How many times the system woke from sleep while the app ran ([After a sleep](#after-a-sleep)). |
| `network()` | `network()` (`nsh`) | How the device reaches the network: online or not, Wi-Fi, cellular or wired, expensive or constrained, and on iOS whether the app may use cellular data ([The network](#the-network)). |
| `reduce_motion()`, `screen_reader()` | `reduceMotion()`, `screenReaderEnabled()` (`nsh`) | See [Accessibility](https://nsh.dev/versions/v0.7.2/docs/accessibility.md). |

```tsx
import { mount, Text, View, safeArea } from "nsh";
import { colorScheme, fontScale, windowSize } from "nsh";
import { Window } from "nsh/desktop";

function App() {
  return (
    <View style={{ flex: 1, paddingTop: safeArea().top + 16, paddingHorizontal: 16, backgroundColor: colorScheme() === "dark" ? "#0d1117" : "#ffffff" }}>
      <Text style={{ color: colorScheme() === "dark" ? "#e6edf3" : "#1f2328" }}>
        {Math.round(windowSize().width)} × {Math.round(windowSize().height)} pt, text at {fontScale().toFixed(2)}×
      </Text>
    </View>
  );
}

mount(() => (
  <Window id="main">
    <App />
  </Window>
));
```

```rust
use nsh_core::prelude::*;

#[component]
pub fn App() -> impl IntoElement {
    view()
        .flex_1()
        .pt(move || safe_area().top + px(16.0))
        .px(px(16.0))
        // A light and a dark value: the color follows the appearance natively.
        .bg(AdaptiveColor::rgb(0xffffff, 0x0d1117))
        .child(
            text(move || {
                let size = window();
                format!(
                    "{} × {} pt, text at {:.2}×",
                    size.width.0.round(),
                    size.height.0.round(),
                    font_scale()
                )
            })
            .text_color(AdaptiveColor::rgb(0x1f2328, 0xe6edf3)),
        )
}
```

## Window size and responsive layout

`window()` (TSX: `windowSize()`) is the size of the surface NSH renders into: the screen on a phone, the window on an iPad in split view, the host view when NSH is embedded. On the desktops the app host is invisible, so this is the host surface's size, not the calling window's; read a window's own `state()?.contentBounds` and `state()?.scaleFactor` for layout inside it. Use it to change the layout, not only sizes:

```tsx
import { Image, View } from "nsh";
import { windowSize } from "nsh";

function Gallery() {
  const columns = () => (windowSize().width >= 700 ? 4 : windowSize().width >= 500 ? 3 : 2);
  return (
    <View style={{ flexDirection: "row", flexWrap: "wrap", gap: 8 }}>
      <Each of={photos}>
        {(p) => <Image src={p().url} style={{ width: (windowSize().width - 32 - (columns() - 1) * 8) / columns(), aspectRatio: 1 }} />}
      </Each>
    </View>
  );
}
```

```rust
use nsh_core::prelude::*;

fn gallery() -> impl IntoElement {
    let columns = || {
        let width = window().width.0;
        if width >= 700.0 { 4.0 } else if width >= 500.0 { 3.0 } else { 2.0 }
    };
    let side = move || px((window().width.0 - 32.0 - (columns() - 1.0) * 8.0) / columns());
    view().flex_row().flex_wrap().gap_2().child(each(
        photos,
        |p| p.id,
        move |p, _| image(move || p.with(|p| p.url.clone())).w(side).aspect_ratio(1.0),
    ))
}
```

Often flexbox, percentages and `aspect_ratio` (`aspectRatio`) do the job without reading the size at all; see [Layout](https://nsh.dev/versions/v0.7.2/docs/layout.md). Rust's `style_when(|| window().width.0 >= 640.0, |s| s.flex_row())` switches styles with the width; in TSX with Tailwind, the breakpoint variants (`sm:`, `md:`, `lg:`, `xl:`, `2xl:`, `max-*:`, `min-[…]:`) and `portrait:` / `landscape:` follow the window too: `class="grid grid-cols-2 md:grid-cols-4"`.

## Dark mode

The appearance follows the system unless the app sets its own. In Rust, the simplest way to support dark mode is a color with a light and a dark value, `AdaptiveColor::rgb(light, dark)`: it switches natively when the appearance does, with nothing to run. In TSX it is the `dark:` Tailwind variant, which applies when `colorScheme()` is `"dark"`:

```tsx
<View class="flex-1 bg-white dark:bg-zinc-900 p-4">
  <Text class="text-zinc-900 dark:text-zinc-100">Follows the system appearance</Text>
</View>
```

```rust
view()
    .flex_1()
    .p_4()
    .bg(AdaptiveColor::rgb(0xffffff, 0x18181b))
    .child(
        text("Follows the system appearance")
            .text_color(AdaptiveColor::rgb(0x18181b, 0xf4f4f5)),
    )
```

For anything else, read `dark()` (TSX: `colorScheme()`) in a closure or a computed value; in TSX, for more than a few colors, derive a palette once:

```tsx
import { computed, Text, View } from "nsh";
import { colorScheme } from "nsh";

const palette = computed(() =>
  colorScheme() === "dark"
    ? { background: "#0d1117", surface: "#161b22", text: "#e6edf3", muted: "#8b949e" }
    : { background: "#f6f8fa", surface: "#ffffff", text: "#1f2328", muted: "#57606a" },
);

<View style={{ flex: 1, backgroundColor: palette().background }}>
  <Text style={{ color: palette().text }}>Hello</Text>
</View>
```

```rust
use nsh_core::prelude::*;

// `dark()` is reactive: the closure runs again when the appearance changes.
view()
    .flex_1()
    .bg(move || if dark() { rgb(0x0d1117) } else { rgb(0xf6f8fa) })
    .child(text("Hello"))
```

Rust components also take their colors from the theme, which has light and dark values (`nsh.json` `"theme"`, see [Building with Rust](https://nsh.dev/versions/v0.7.2/docs/rust.md)), so they switch with the system as well. To match the status bar to your background, give your pages and dialogs a [`statusBar`](https://nsh.dev/versions/v0.7.2/modules/nsh-base.md#status-bar), or the app one in `nsh.json`.

### An app with its own appearance

An app that is always dark (or always light) says so in `nsh.json`, and `dark()` and `colorScheme()`, adaptive colors, `dark:` classes, the theme, the status bar's default style and alerts follow it from the first frame (packaged apps also launch in it):

```json
{ "appearance": "dark" }
```

`set_color_scheme(Appearance::Dark)` (TSX: `setColorScheme` from `nsh`) changes it at run time, for an in-app appearance setting; `Appearance::System` (`"system"`) follows the system again:

```tsx
import { Text, View } from "nsh";
import { colorScheme, setColorScheme } from "nsh";

<View onPress={() => setColorScheme(colorScheme() === "dark" ? "light" : "dark")} role="button" class="rounded-lg px-4 py-2 bg-neutral-200 dark:bg-neutral-800">
  <Text class="text-neutral-900 dark:text-neutral-50">{colorScheme() === "dark" ? "Light" : "Dark"}</Text>
</View>
```

```rust
use nsh_core::prelude::*;

view()
    .on_press(move || {
        set_color_scheme(if dark() { Appearance::Light } else { Appearance::Dark })
    })
    .role(AccessibilityRole::Button)
    .rounded_lg()
    .px_4()
    .py_2()
    .bg(AdaptiveColor::rgb(0xe5e5e5, 0x262626))
    .child(
        text(|| if dark() { "Light" } else { "Dark" })
            .text_color(AdaptiveColor::rgb(0x171717, 0xfafafa)),
    )
```

> [!TIP]
> Test both appearances while developing: `xcrun simctl ui booted appearance dark` switches an iOS simulator, and Android has a dark theme toggle in Quick Settings. The change reaches your app at once, without a reload.

## Text size

`font_scale()` (TSX: `fontScale()`) is the user's text size setting (Dynamic Type on iOS, font size on Android) relative to the default. Text and inputs follow it by themselves: `fontSize: 16` renders larger when the user asked for larger text. Per element you can opt out or cap it:

| Rust (on `text` and `input`) | TSX (on `<Text>` and `<Input>`) | Default | Description |
|---|---|---|---|
| `.allow_font_scaling(..)` | `allowFontScaling` | `true` | Follow the system text size. |
| `.max_font_size_multiplier(..)` | `maxFontSizeMultiplier` | `0` (none) | The largest scale applied to this text. |

```tsx
<Text style={{ fontSize: 34, fontWeight: "700" }} maxFontSizeMultiplier={1.4}>Today</Text>
```

```rust
text("Today")
    .text_size(px(34.0))
    .font_weight(FontWeight::BOLD)
    .max_font_size_multiplier(1.4)
```

Read `font_scale()` to adapt layout that does not follow text automatically, such as switching a row to a column when the text gets large: `.style_when(|| font_scale() > 1.3, |s| s.flex_col())` in Rust, `flexDirection: fontScale() > 1.3 ? "column" : "row"` in TSX.

The text size scales text: every text and input that allows font scaling, and the platform's alerts and toasts. Lengths keep their size: spacing, widths and heights, corner radii, borders and images, in points or in classes (`p-4`, `rounded-lg`, `w-[2rem]`). Boxes sized by their content grow with their text.

## Language and region

`locale()` is the user's locale, as a BCP 47 tag (`locale().as_str()` in Rust). In TSX, `Intl` formats numbers and dates for it by default, so `new Intl.NumberFormat().format(1234.5)` gives `1,234.5` in `en-US` and `1.234,5` in `de-DE`. What reads it updates when it changes.

The app's own text in each language comes from its translations: `t("hello")` (`nsh/i18n`; Rust `t!("hello")`) shows a key in the user's language, and `setLocale` shows the app in another one. See [I18n](https://nsh.dev/versions/v0.7.2/docs/i18n.md).

```tsx
import { Text } from "nsh";
import { t } from "nsh/i18n";

<Text>{t("welcome")}</Text>
```

```rust
use nsh_core::prelude::*;

text(move || t!("welcome"))
```

## Locale settings

`locale_settings()` (TSX: `localeSettings()`) holds what else the user set for their language and region: the
preferred languages, the device's time zone, and how they read times, weeks and measurements. The systems keep these
together (Language & Region on iPhone and Mac, the language and regional preferences on Android, Time & language on
Windows) and change several at once, so they come as one value, and what reads it updates when any of them changes.
The names and values are `Intl`'s, so they pass into its options as they are.

To follow one of them alone, read it through a `computed`: what reads that updates only when that one
changes, not when the user travels to another time zone or adds a language.

```tsx
import { Text } from "nsh";
import { computed, localeSettings } from "nsh";

// Updates when the user switches between 12- and 24-hour time, and only then.
const twelveHour = computed(() => ["h11", "h12"].includes(localeSettings().hourCycle));

<Text>{twelveHour() ? "3:00 PM" : "15:00"}</Text>
```

```rust
use nsh_core::prelude::*;

// Updates when the user switches between 12- and 24-hour time, and only then.
let twelve_hour = computed(|| locale_settings().hour_cycle.twelve_hour());
text(move || if twelve_hour.get() { "3:00 PM" } else { "15:00" })
```

In tests, `open`'s `localeSettings` sets the ones it names over the locale's (the locale alone as the languages, its
region's preferences, UTC), and `app.setLocaleSettings({ hourCycle: "h23" })` changes the ones it names, keeping the
others ([Testing](https://nsh.dev/versions/v0.7.2/docs/testing-and-trace.md#steps)).

### Preferred languages

`languages` is the user's list of preferred languages, most preferred first, as BCP 47 tags: the system's (Settings' language list on a phone, the UI languages of a desktop, the browser's on the web). An app's text shows in the first one it has a translation for, falling back along the list: a user who prefers Swiss German, then German, then English gets German text from an app that has German and English. `t` (`nsh/i18n`) picks it so ([I18n](https://nsh.dev/versions/v0.7.2/docs/i18n.md)). `locale()` is something else, the region format numbers and dates follow, which can be another (English text, German dates). The list is never empty, and what read it updates when the user changes it.

An app shown inside a native app that sets its own language has that language first.

### Regional preferences

The systems keep a few settings of how the user reads times, weeks and measurements beside the language (Language & Region on iPhone and Mac, regional preferences on Android, Region on Windows):

- `hour_cycle` (TSX: `hourCycle`): 12- or 24-hour time, as `Intl` names its hour cycles: `h12` (`12:30 PM`), `h23` (`00:30`), and the rarer `h11` (`0:30 PM`) and `h24` (`24:30`).
- `first_day_of_week` (TSX: `firstDayOfWeek`): the day weeks start on, a calendar's first column; TSX numbers it as ISO 8601 does, 1 for Monday to 7 for Sunday.
- `measurement_system` (TSX: `measurementSystem`): `metric`, `us` (feet, miles, pounds, gallons) or `uk` (metric, with miles and pints).
- `temperature_unit` (TSX: `temperatureUnit`): `celsius` or `fahrenheit`.

Each is the system's setting where the user made one, else the region's own (as the Unicode CLDR gives them: 12-hour time and weeks from Sunday in the US, 24-hour time and weeks from Monday in Germany).

```tsx
import { localeSettings } from "nsh";

const time = (date: Date) =>
  date.toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit", hourCycle: localeSettings().hourCycle });

const temperature = (celsius: number) =>
  localeSettings().temperatureUnit === "fahrenheit"
    ? `${Math.round((celsius * 9) / 5 + 32)} °F`
    : `${Math.round(celsius)} °C`;

// A calendar's columns from the user's first day: 1 for Monday to 7 for Sunday.
const weekColumns = () =>
  Array.from({ length: 7 }, (_, i) => ((localeSettings().firstDayOfWeek - 1 + i) % 7) + 1);
```

```rust
use nsh_core::{platform::TemperatureUnit, prelude::*};

/// A temperature in degrees Celsius, in the user's unit.
fn temperature(celsius: f32) -> String {
    match locale_settings().temperature_unit {
        TemperatureUnit::Celsius => format!("{celsius:.0} °C"),
        TemperatureUnit::Fahrenheit => format!("{:.0} °F", celsius * 9.0 / 5.0 + 32.0),
    }
}

/// A calendar's columns from the user's first day: 1 for Monday to 7 for
/// Sunday.
fn week_columns() -> [u8; 7] {
    let first = locale_settings().first_day_of_week.number();
    std::array::from_fn(|i| (first - 1 + i as u8) % 7 + 1)
}
```

`Intl` writes times in the locale's own hour cycle: pass `hourCycle` as its `hourCycle` option, as above, to write them as the user reads them. On the web, the browser tells no user settings of these: the hour cycle and the first day of the week are the browser's for its language, the units its region's.

The base components show what the app passes them, in their own forms. `Calendar`'s and `DatePicker`'s
`firstDayOfWeek` number the days from `0` for Sunday to `6` for Saturday: the user's first day is
`localeSettings().firstDayOfWeek % 7` (Rust: `locale_settings().first_day_of_week.number() % 7`). `TimeField`'s and
`DatePicker`'s `hourCycle` is `"h12"` or `"h23"`: the user's is `"h12"` where `localeSettings().hourCycle` is `"h11"`
or `"h12"`, else `"h23"` (Rust: `HourCycle::H12` where `locale_settings().hour_cycle.twelve_hour()`).

### Time zone

`time_zone` (TSX: `timeZone`) is the device's time zone as the system names it: an IANA name such as `Europe/Berlin` or `America/New_York`, `UTC`, or an offset zone where the system has one (`GMT+01:00` on Android). It changes when the user changes the setting or the device moves to another zone, and what read it updates: a clock that names its zone, a list of times in the user's zone, a date saved with the zone it was made in.

```tsx
import { Text } from "nsh";
import { localeSettings } from "nsh";

<Text>{`Times in ${localeSettings().timeZone}`}</Text>
```

```rust
use nsh_core::prelude::*;

text(move || format!("Times in {}", locale_settings().time_zone))
```

`Date`'s local times follow the system's zone, as they always have: `time_zone` names it. `Intl` formats local time in that zone and names it the same way: `Intl.DateTimeFormat().resolvedOptions().timeZone` is `localeSettings().timeZone`, and `{ timeZone: localeSettings().timeZone }` formats in local time. In tests, the zone `open` or `app.setLocaleSettings` gives (`UTC` otherwise) is the one `localeSettings()` reports; local times, `Date`'s and `Intl`'s, stay the machine's, and `Intl` names the machine's zone.

## In an embedding app

An app shown inside a native app follows that app's own settings when it sets them: the embedding APIs' locale and text size (`AppView::set_locale` and `set_font_scale` in Rust on [GPUI](https://nsh.dev/versions/v0.7.2/docs/gpui.md), [macOS](https://nsh.dev/versions/v0.7.2/docs/macos.md), [Linux](https://nsh.dev/versions/v0.7.2/docs/linux.md) and [Windows](https://nsh.dev/versions/v0.7.2/docs/windows.md), `locale` and `fontScale` in Swift on [iOS](https://nsh.dev/versions/v0.7.2/docs/ios.md), `setLocale` and `setFontScale` in Java on [Android](https://nsh.dev/versions/v0.7.2/docs/android.md)) stand in for the system's language and text size. `locale()`, `localeSettings()`'s languages (the embedding app's language first), `Intl`, `fontScale()` and text follow them at once, as they follow a system change, and the system's come back when the embedding app clears them. Use them when the native app lets its users choose a language or a text size of its own.

## In the background

`lifecycle()` (the same in TSX) says whether the user is using the app. It is `active` in the foreground, `inactive` while the app is still visible but another app or window has the focus (or the system shows something over it: the app switcher, an incoming call), and `background` while it is not visible: in the background on a phone, minimized on a desktop, a hidden tab on the web. Like every accessor it updates what read it, so an effect pauses a live feed, polling or a video in the background and resumes them when the user comes back:

```tsx
effect(() => {
  if (lifecycle() === "background") feed.pause();
  else feed.resume();
});
```

```rust
effect(move || {
    if lifecycle() == Lifecycle::Background {
        feed.pause();
    } else {
        feed.resume();
    }
});
```

Use `background` to pause work, and save what the user changes as they change it: the system may suspend an app seconds after it moves to the background, before an effect that reads the change has run. An app shown inside a native app follows that app's state, and where an app has several windows (an iPad, a desktop) a view follows the window it is in. Standalone apps and views embedded in AppKit and Win32 apps are in the `background` too on macOS and Windows while the screen is locked or turned off and while their window is on another desktop (a Space, a virtual desktop). On Windows, an app moves to `background` when the user signs out, Windows shuts down or restarts, or an installer closes it, and its handlers run before the session ends; then the app quits ([`Application.on("quit")`](https://nsh.dev/versions/v0.7.2/modules/nsh-desktop.md#quitting)). A standalone app on Linux does the same when the user signs out or the system shuts down or restarts, and when it is sent `SIGTERM` (as systemd does to end a session or stop a service). In tests, `app.setLifecycle("background")` of `nsh/test` moves the app there ([Testing](https://nsh.dev/versions/v0.7.2/docs/testing-and-trace.md#steps)).

## After a sleep

`wake_count()` (TSX: `wakeCount()`) counts the times the system woke from sleep while the app ran: 0 at the start, one more after each wake. A laptop whose lid closes and opens again with the app in front keeps the app `active` throughout, yet the network connections it held and the timers it set did not live through the sleep. An effect that reads the count runs again after each wake, so the app checks its connections or opens them again there:

```tsx
effect(() => {
  if (wakeCount() > 0) feed.reconnect();
});
```

```rust
effect(move || {
    if wake_count() > 0 {
        feed.reconnect();
    }
});
```

Standalone apps and views embedded in AppKit, Win32 and GTK apps count wakes on macOS, Windows and Linux (Linux through systemd's `logind`). On iOS and Android a sleeping device takes the app to the background, which `lifecycle()` reports. In tests, `app.wake()` of `nsh/test` wakes the system ([Testing](https://nsh.dev/versions/v0.7.2/docs/testing-and-trace.md#steps)).

## The network

`network()` (the same in TSX) says how the device reaches the network now: `online` (false only while the system knows there is no route), the `kind` of link the route takes (`"wifi"`, `"cellular"`, `"wired"`, `"other"`, `"none"` offline, `"unknown"` where the platform does not say, which counts as online), whether that link is `expensive` (cellular data, a phone's hotspot, a metered network) or `constrained` (Low Data Mode, Data Saver), and on iOS whether the user lets the app use cellular data (`cellularData`: `"unknown"`, `"restricted"` or `"notRestricted"`). An effect that reads it runs again when the route changes, so a request that failed offline waits for the network and tries again at once, instead of retrying on a timer:

```tsx
effect(() => {
  if (network().online && sync.failedOffline()) sync.retry();
});
```

```rust
effect(move || {
    if network().online() && sync.failed_offline() {
        sync.retry();
    }
});
```

On iPhones sold in mainland China, the system asks the user whether the app may use wireless data the first time it reaches the network, and the app's requests fail until the user answers. `cellularData` is `"unknown"` until then: a request that failed while it is `"unknown"` waits for it to change and tries again; one that failed while it is `"restricted"` (the user chose Off, or WLAN only on cellular) tells the user, and `AppSettings.open()` of `nsh/mobile` opens the app's page in Settings, where they change it.

iOS and macOS follow the system's path monitor (Network.framework) and, on iOS, CoreTelephony's cellular data state. Android follows the default network (`ConnectivityManager`, with the `ACCESS_NETWORK_STATE` permission that `nsh package` and the Android SDK's AAR declare; without it the kind stays `"unknown"`). The web reads `navigator.onLine` and, where the browser has it, `navigator.connection` (a desktop browser names no link: `"unknown"` while online). Windows, Linux, GPUI and terminals report `"unknown"`. A route is not a promise: a server may still be out of reach on a link that is `online`, so the app keeps handling failed requests. In tests, `app.setNetworkState({ kind: "none" })` of `nsh/test` takes the device offline ([Testing](https://nsh.dev/versions/v0.7.2/docs/testing-and-trace.md#steps)).

## Safe areas

Notches, the home indicator, the status bar, Android's navigation bar, the Mac Catalyst title bar and the window controls of an iPad app in a window cover parts of the surface. NSH apps draw edge to edge on every platform, and get the covered parts as insets. On iPadOS 26 and later, a windowed app's leading inset is as wide as the window controls in its top-leading corner, over the whole leading edge; in full screen and on iPhone it is the plain safe area.

- `safe_area()` (TSX: `safeArea()`) gives the surface's insets.
- A component's `cx.insets()` (TSX: `safeAreaInsets()`) gives the insets **where the component renders**: inside a navigation [page](https://nsh.dev/versions/v0.7.2/docs/navigation.md#safe-areas-under-the-header) they are measured below its header, inside a [dialog](https://nsh.dev/versions/v0.7.2/elements/dialog.md) they are the dialog's. Prefer it in components that may be shown in several places.
- CSS's `env(safe-area-inset-top)`, `-right`, `-bottom` and `-left` pad or space any view by the insets where it renders, in `padding*` and `margin*` (`calc(env(safe-area-inset-bottom) + 8px)` adds a length; Tailwind `pt-[env(safe-area-inset-top)]`; Rust `.p_safe_area()`, `.pt_safe_area()`, `.pb_safe_area_plus(px(8.0))`, ...). The padding updates natively when the insets change ([Safe area](https://nsh.dev/versions/v0.7.2/elements/safe-area.md)).

```tsx
import { View, safeAreaInsets } from "nsh";

function Screen() {
  return (
    <View class="flex-1 bg-white pt-[env(safe-area-inset-top)] pr-[env(safe-area-inset-right)] pl-[env(safe-area-inset-left)]">
      <Content />
      <BottomBar />
    </View>
  );
}

function BottomBar() {
  const insets = safeAreaInsets();
  return <View style={{ paddingBottom: insets().bottom + 8, paddingTop: 8, flexDirection: "row" }}>…</View>;
}
```

```rust
use nsh_core::prelude::*;

fn screen() -> impl IntoElement {
    view()
        .pt_safe_area()
        .px_safe_area()
        .flex_1()
        .bg(white())
        .child(content())
        .child(bottom_bar())
}

// A component, for its `Scope`: the insets where it renders.
#[component]
fn BottomBar(cx: Scope<NoEvent>) -> impl IntoElement {
    // `cx.insets()`: the insets where the component renders.
    view()
        .flex_row()
        .pt(px(8.0))
        .pb(move || cx.insets().bottom + px(8.0))
}
```

On Android, a view embedded below a toolbar gets only the parts of the system bars that overlap it, like UIKit's safe area: no top inset if it does not reach the status bar.

## The platform

`os()`, `os_version()`, `idiom()`, `is_desktop()` and `is_catalyst()` in Rust, and the `platform` constant from `nsh` in TSX, describe where the app runs. They stay the same while the app runs.

| Rust | TSX | Description |
|---|---|---|
| `os()` | `platform.os` | `Os::Ios`, `Os::Android`, `Os::MacOs`, `Os::Windows`, `Os::Linux` or `Os::Web` (`os().name()` is TSX's string). Mac Catalyst is iOS. |
| `os_version()` | `platform.version` | Major and minor; on Android the API level and 0. |
| `idiom()` | `platform.idiom` | `Idiom::Phone`, `Pad`, `Mac`, `Tv`, `Desktop` or `Terminal`; `Terminal` in a terminal, else `Desktop` exactly where `is_desktop()` holds. |
| `is_desktop()` | `platform.desktop` | macOS, Windows or Linux, a desktop browser or a terminal ([Desktop and mobile](https://nsh.dev/versions/v0.7.2/docs/platform-specific.md#desktop-and-mobile)). |
| `is_catalyst()` | `platform.catalyst` | Mac Catalyst, in either of its idioms. |

TSX's `platform` in full:

| Member | Description |
|---|---|
| `platform.os` | `"ios"`, `"android"`, `"macos"`, `"windows"`, `"linux"` or `"web"`. Mac Catalyst reports `"ios"`. |
| `platform.version` | A string: iOS and desktops: the OS version (`"17.5"`); Android: the API level (`"35"`); the web: `"0.0"`. |
| `platform.idiom` | `"phone"`, `"pad"`, `"mac"`, `"tv"`, `"desktop"` or `"terminal"`. `"terminal"` in a terminal, else `"desktop"` exactly where `platform.desktop` is `true`. `"mac"` is UIKit's Mac idiom under Mac Catalyst, a mobile idiom. |
| `platform.desktop` | Desktop behavior applies: macOS, Windows, Linux, desktop browsers and terminals. `desktop={{…}}` props and `platform.select({ desktop })` follow it. |
| `platform.catalyst` | The app runs under Mac Catalyst (`os` `"ios"`), in the Mac or the iPad idiom. |
| `platform.select(spec)` | The value for this platform; see [Platform-specific code](https://nsh.dev/versions/v0.7.2/docs/platform-specific.md#platformselect). |

```tsx
import { platform } from "nsh";

const hapticsSupported = platform.os === "ios" || (platform.os === "android" && Number(platform.version) >= 30);
```

```rust
use nsh_core::{element::Os, prelude::*};

let haptics_supported = os() == Os::Ios || (os() == Os::Android && os_version().0 >= 30);
```

`nsh test` runs headless as iOS.

On [Linux](https://nsh.dev/versions/v0.7.2/docs/linux.md), `platform.os` is `"linux"`, the idiom `"desktop"` and `platform.version` the kernel's major
and minor version (`"6.12"`): distributions number their releases in ways that cannot be compared. There, the safe
area and the keyboard height are 0, `colorScheme()` follows the desktop's setting (the XDG settings portal) live,
`fontScale()` the desktop's text scaling and `pixelRatio()` the display's scale, fractional ones included.

On [macOS](https://nsh.dev/versions/v0.7.2/docs/macos.md) (the AppKit backend), `platform.os` is `"macos"`, the idiom `"desktop"` and
`platform.version` the macOS version. The safe area and the keyboard height are 0, `colorScheme()` follows the
system's appearance live, `fontScale()` is 1 and `pixelRatio()` the display's backing scale.

On [Windows](https://nsh.dev/versions/v0.7.2/docs/windows.md), `platform.os` is `"windows"`, the idiom `"desktop"` and `platform.version` `"10.0"`, on
Windows 11 too (Windows kept the version number). The safe area and the keyboard height are 0, `colorScheme()` follows
Settings > Personalization > Colors live, `fontScale()` the Text size setting (1 to 2.25), `reduceMotion()` the
animation effects setting, `pixelRatio()` the monitor's scale, and `Intl` formats in the user's regional format.

In a browser ([the web runtime](https://nsh.dev/versions/v0.7.2/docs/web.md)), `platform.os` is `"web"` and `platform.version` `"0.0"`. The idiom is
`"desktop"` with a mouse and a window at least 600 points wide, `"pad"` on a tablet and `"phone"` on a phone, decided
when the app starts. The locale, `localeSettings()` (the browser's preferred languages, its regional preferences for
its language and its time zone), `lifecycle()` (`"background"` in a hidden tab, `"inactive"` without the
focus), `reduceMotion()`, `fontScale()` (the browser's text size), `pixelRatio()`, the safe area and the keyboard height
come from the browser and follow it live.

In a terminal ([the terminal backend](https://nsh.dev/versions/v0.7.2/docs/tui.md)), `platform.idiom` is `"terminal"`, `platform.desktop` is `true` and
`platform.os` is the machine's own system. `colorScheme()` follows the terminal's background, `reduceMotion()` is
`true`, and the safe area and the keyboard height are 0.

## The app's data directory

`data_dir()` in Rust, `process.dataDir` in TSX, is the directory the app keeps its own files in, such as a SQLite database, beside its `storage.json`, on every native platform, Android and iOS included. By default it is the app's id in the system's directory for apps' data: `~/Library/Application Support/<id>` on macOS, `%APPDATA%\<id>` on Windows, `$XDG_DATA_HOME/<id>` (`~/.local/share/<id>`) on Linux; an app embedded in another keeps it in the embedding app's. A desktop app can name its own with `nsh.json` `dataDir` ([Project structure](https://nsh.dev/versions/v0.7.2/docs/project-structure.md)), and `process.dataDir` and `${dataDir}` follow it. It is `None` (`null`) where the host keeps no files for the app, such as the web. The directory may not exist yet, so create it before writing.

A packaged desktop app writes its log lines to `logs/app.log` there: `log_file()` in Rust, `process.logFile` in TSX
([Log files and levels on the desktop](https://nsh.dev/versions/v0.7.2/docs/dev-server-and-debugging.md#log-files-and-levels-on-the-desktop)).

```rust
use nsh_core::prelude::*;

if let Some(dir) = data_dir() {
    std::fs::create_dir_all(&dir)?;
    let db = dir.join("app.sqlite");
}
```

## The app's version

`app_info()` in Rust, `appInfo()` from `nsh` in TSX ([`appInfo`](https://nsh.dev/versions/v0.7.2/modules/nsh.md#appinfo)), is the app as it was
built: its id, name, version and build number, and whether it is a development build. A package reports what
`nsh package` names and versions it as, `--id`, `--name`, `--version` and `--build` included, with version `1.0.0` and
build `1` where nothing gives them; other builds report `nsh.json`'s, `None` where it leaves them out. `nsh run`,
`nsh dev` and `nsh test` make development builds of a Rust app; `nsh package` does not.
`app_info()` is `None` in a TSX app, where JS's `appInfo()` reports it.

```rust
use nsh_core::prelude::*;

if let Some(info) = app_info() {
    let about = format!("{} ({})", info.version.unwrap_or_default(), info.build.unwrap_or(0));
}
```

## Pixel density

`pixel_ratio()` and `round_to_nearest_pixel(points)` in Rust, and `pixelRatio()` from `nsh` in TSX, convert between points and device pixels.

| Rust | TSX | Description |
|---|---|---|
| `pixel_ratio()` | `pixelRatio()` | Device pixels per point (2 or 3 on most phones), reactive. |
| `round_to_nearest_pixel(points)` | `Math.round(x * pixelRatio()) / pixelRatio()` | Points rounded to the nearest device pixel. |

A hairline, one device pixel tall:

```tsx
import { View } from "nsh";
import { pixelRatio } from "nsh";

<View style={{ height: 1 / pixelRatio(), backgroundColor: "#d0d7de" }} />
```

```rust
view()
    .h(move || px(1.0 / pixel_ratio()))
    .bg(rgb(0xd0d7de))
```

`pixel_ratio()` and `pixelRatio()` are reactive. On the desktops they report the host display's scale; a window moved to a screen with another density reports its own scale in `state()?.scaleFactor`.

## Right to left

When the device language is written right to left (Arabic, Hebrew, Persian, ...), NSH lays the app out right to left: rows run from the right, the logical style properties take their right-to-left sides, text aligns to the right, and native widgets mirror. The layout direction follows the user's language by itself. A view's `direction` style (`"ltr"`, `"rtl"` or `"inherit"`; `.direction(LayoutDirection::..)` in Rust) sets the direction of its subtree; on the root view it sets the app's.

Changes apply at once, without restarting the app; the demo on this page toggles the `direction` style.

Write layouts that work both ways with the logical properties: in Rust `ms` / `me` and `ps` / `pe` (Tailwind's names), in TSX `marginStart` / `marginEnd`, `paddingStart` / `paddingEnd`, `start` / `end`, `borderTopStartRadius`, `textAlign: "start"` and so on, which mean left and right in left-to-right layouts and the reverse right to left. `left` and `right` (`marginLeft`, `paddingRight`, `left`, `textAlign: "left"`, ...) keep their sides in both, as in CSS. The `direction` style (`"inherit"`, `"ltr"`, `"rtl"`) overrides the direction for a subtree, for example to keep a phone number or a media player's controls left to right. The `rtl:` and `ltr:` Tailwind variants apply per direction.

Horizontal scroll views and lists start at the right end in right-to-left layouts. On Android, the host app must declare `android:supportsRtl="true"` in its manifest for native widgets to mirror.

## Related

- [`nsh`'s device API](https://nsh.dev/versions/v0.7.2/modules/nsh.md#device): the reference.
- [Platform-specific code](https://nsh.dev/versions/v0.7.2/docs/platform-specific.md): `platform.select`, platform props and modules.
- [Accessibility](https://nsh.dev/versions/v0.7.2/docs/accessibility.md): screen readers and reduced motion.
- [Safe area](https://nsh.dev/versions/v0.7.2/elements/safe-area.md), [Keyboard](https://nsh.dev/versions/v0.7.2/elements/keyboard.md) and [the status bar](https://nsh.dev/versions/v0.7.2/modules/nsh-base.md#status-bar).
- [Tailwind](https://nsh.dev/versions/v0.7.2/docs/tailwind.md): `dark:`, breakpoint and orientation variants.
