Questa pagina non è ancora stata tradotta. Leggi l'originale in inglese

Elementi

TitleBar

A window with titleBar: { style: "hidden" } keeps the system’s window controls (macOS’s traffic lights, Windows’ caption buttons, the window buttons of a Linux desktop) and leaves the rest of its top row to the app. TitleBar is that row: pressing and dragging it moves the window, double clicking it does what the system’s title bar does, and the buttons, inputs and links inside it still take their presses. It is as tall as the window’s title bar area at least, lays its children out in a row, and adds padding at its left and right edges only where it overlaps the window controls, so its background still reaches the window’s edge under them.

el-title-bar.tsx
el-title-bar.tsxLive
import { mount, signal, Text, View } from "nsh";
import { Button, TitleBar } from "nsh/base";
import { desktop, ui } from "./palette";

// A window's chrome split in two, as an app with a sidebar draws it: the
// sidebar's bar and the content pane's bar. In a window with a hidden
// title bar each one steps aside of the window's controls only where it
// meets them; here (no window controls) neither pads.
function App() {
  const sidebar = signal(true);
  return (
    <View style={{ flex: 1, backgroundColor: ui().page, padding: 20, paddingTop: 24, gap: 14 }}>
      <Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>title bar</Text>
      <Text style={{ color: ui().muted }}>{desktop ? "Click the toggle to hide the sidebar" : "Tap the toggle to hide the sidebar"}</Text>
      <View style={{ height: 220, flexDirection: "row", borderWidth: 1, borderColor: ui().line, borderRadius: 10, overflow: "hidden" }}>
        <If when={sidebar()}>
          <View style={{ width: 180, ...ui().inset, borderWidth: 0, borderEndWidth: 1, borderEndColor: ui().line }}>
            <TitleBar>
              <View style={{ flex: 1, height: 40, flexDirection: "row", alignItems: "center", paddingHorizontal: 12 }}>
                <Text style={{ fontSize: 13, fontWeight: "600", color: ui().text }}>Library</Text>
              </View>
            </TitleBar>
          </View>
        </If>
        <View style={{ flex: 1, backgroundColor: ui().page }}>
          <TitleBar style={{ borderBottomWidth: 1, borderBottomColor: ui().line }}>
            <View style={{ flex: 1, height: 40, flexDirection: "row", alignItems: "center", gap: 8, paddingHorizontal: 12 }}>
              <Button accessibilityLabel="Toggle sidebar" onPress={() => sidebar.set(!sidebar())} style={{ paddingHorizontal: 8, height: 26, borderRadius: 6, justifyContent: "center", ...ui().raised }}>
                <Text style={{ fontSize: 12, color: ui().text }}>{sidebar() ? "Hide sidebar" : "Show sidebar"}</Text>
              </Button>
              <Text style={{ fontSize: 13, fontWeight: "600", color: ui().text }}>Inbox</Text>
            </View>
          </TitleBar>
        </View>
      </View>
    </View>
  );
}

mount(App);

The overlap is measured where the bar is. A split view gives each pane a bar of its own: the sidebar’s bar steps aside of the traffic lights at the left, the content pane’s bar starts past them and gets none, and once the sidebar hides, the content pane’s bar meets them and steps aside. Entering full screen (no controls) takes the padding away. The controls are those of the window the bar renders in: a TitleBar in a second <Window> follows that window’s.

import { mount, Text, View } from "nsh";
import { TitleBar } from "nsh/base";
import { Window } from "nsh/desktop";

function App() {
  return (
    <View class="flex-1 flex-row">
      {/* The sidebar's part of the title bar: past the traffic lights. */}
      <TitleBar class="h-11 w-60 bg-muted">
        <Text class="px-3">Library</Text>
      </TitleBar>
      {/* The content's part: no padding while the sidebar is shown. */}
      <TitleBar class="h-11 flex-1 border-b border-border">
        <Text class="px-3">Inbox</Text>
      </TitleBar>
    </View>
  );
}

mount(() => (
  <Window id="main" titleBar={{ style: "hidden" }}>
    <App />
  </Window>
));

TitleBar comes from nsh/base, the nsh-base crate in Rust (title_bar()). The padding it adds is the part of the controls’ width it overlaps: an app’s own padding goes on a view inside it, as a padding on the bar itself would replace it. A window with the system’s title bar (the default) reports no title bar area: the bar adds nothing and is just a row. On platforms without windows it is a row as well.

#Props

Prop Rust Type Default Description
children .child(..) JSX.Element none What the bar holds: a title, a sidebar toggle, the app’s buttons.
class, style style methods string, StyleProp<ViewStyle> none The bar’s look and layout: its background, border, height above the title bar area’s, gaps.

To read the title bar area yourself (a layout of your own), use titleBar() from nsh/desktop (current_window().title_bar() in Rust): its height and the left and right widths the controls take.