Skip to content

Chapter 6 of 17

Adapt the Layout ​

Show the note list and the open note one at a time in narrow windows, size the sidebar, and keep the window usable at phone size.

Collapse on narrow windows ​

Add these props to Split.Navigator in src/app.tsx:

src/app.tsx
tsx
import { ToastOverlay, ToastProvider, useToast } from "@gtkx/components";
import { useTranslation } from "@gtkx/i18n";
import {
    AdwApplication,
    AdwApplicationWindow,
    AdwStatusPage,
} from "@gtkx/jsx/adw";
import { GtkToggleButton } from "@gtkx/jsx/gtk";
import { NavigationContainer } from "@gtkx/navigation";
import { quit } from "@gtkx/react";
import { useEffect, useEffectEvent, useState } from "react";
import { FolderBoundary } from "./components/folder-boundary.js";
import { Split } from "./navigation.js";
import { defaultFolder } from "./notes/files.js";
import { useNotesStore } from "./notes/notes-store.js";
import { NoteScreen } from "./screens/note-screen.js";
import { NotesScreen } from "./screens/notes-screen.js";

const NoteNavigation = () => {
    const { t } = useTranslation();
    const { show } = useToast();
    const open = useNotesStore((state) => state.open);
    const folder = defaultFolder();
    const [preview, setPreview] = useState(false);
    const showSaveError = useEffectEvent((name: string) => {
        show({
            useMarkup: false,
            title: t("Couldn’t save “{{name}}”", { name }),
            timeout: 0,
        });
    });
    const showReplaced = useEffectEvent((name: string) => {
        show({
            useMarkup: false,
            title: t(
                "Another program replaced your unsaved edits to “{{name}}”",
                { name },
            ),
            timeout: 0,
        });
    });
    const showReadError = useEffectEvent((name: string) => {
        show({
            useMarkup: false,
            title: t("Couldn’t read “{{name}}”", { name }),
            timeout: 0,
        });
    });

    useEffect(
        () =>
            open(folder, {
                onSaveError: (name) => {
                    showSaveError(name);
                },
                onReplaced: (name) => {
                    showReplaced(name);
                },
                onReadError: (name) => {
                    showReadError(name);
                },
            }),
        [open, folder],
    );

    return (
        <FolderBoundary folder={folder}>
            <NavigationContainer>
                <Split.Navigator
                    collapseCondition="max-width: 500sp"
                    sidebarWidthFraction={0.3}
                    minSidebarWidth={220}
                    maxSidebarWidth={320}
                    contentPlaceholder={
                        <AdwStatusPage
                            iconName="document-edit-symbolic"
                            title={t("No Note Selected")}
                        />
                    }
                >
                    <Split.Screen
                        name="Notes"
                        component={NotesScreen}
                        options={{ title: t("Notes") }}
                    />
                    <Split.Screen
                        name="Note"
                        options={({ route }) => ({
                            title: route.params.name,
                            headerEnd: (
                                <GtkToggleButton
                                    iconName="view-reveal-symbolic"
                                    tooltipText={t("Preview")}
                                    active={preview}
                                    onNotifyActive={setPreview}
                                />
                            ),
                        })}
                    >
                        {(props) => <NoteScreen {...props} preview={preview} />}
                    </Split.Screen>
                </Split.Navigator>
            </NavigationContainer>
        </FolderBoundary>
    );
};

export const App = () => {
    const { t } = useTranslation();

    return (
        <AdwApplication>
            <AdwApplicationWindow
                title={t("Notes")}
                defaultWidth={720}
                defaultHeight={480}
                widthRequest={360}
                heightRequest={294}
                onCloseRequest={quit}
            >
                <ToastProvider>
                    <ToastOverlay>
                        <NoteNavigation />
                    </ToastOverlay>
                </ToastProvider>
            </AdwApplicationWindow>
        </AdwApplication>
    );
};

collapseCondition takes a libadwaita breakpoint condition. While the navigator is 500sp wide or narrower, the split view collapses, and each pane fits the window on its own down to its minimum width of 360: the note list fills the window, and selecting a note opens it in its place, with a back button in its header bar that returns to the list. Widen the window past 500sp and the panes sit side by side again, with the open note still selected.

An sp is a pixel at the default text size and grows with the Large Text setting (AdwLengthUnit), so readers with larger text get the one-pane layout in wider windows.

Size the sidebar ​

The other props bound the sidebar while the panes sit side by side. sidebarWidthFraction asks for 30% of the width, and minSidebarWidth and maxSidebarWidth keep it between 220 and 320, in sp like the condition. At the default size the sidebar gets 220; in a maximized window it stops at 320 and the note takes the rest.

Run it ​

Narrow the window until the sidebar and the note no longer sit side by side, then open Website relaunch. The note fills the window, and the back button in its header bar returns to the list.

The Notes window at 360 pixels wide, collapsed to one pane: Website relaunch open in the editor under a header bar with a back button.The Notes window at 360 pixels wide, collapsed to one pane: Website relaunch open in the editor under a header bar with a back button.
Captured from src/app.tsx above, with the window at 360 by 480 after opening Website relaunch.