Просмотр исходного кода

ngui: add a tab strip component

Add ngui_tabs: an immediate-mode horizontal tab strip with the active
accent underline, hover/press states, optional fresh-output dots and
press/release selection.  The caller paints the strip background and owns
the tab content and fresh flags.

Use it for the bootscan window's Boot/Serial/Scan tabs (replacing its
private tab drawing and hit-testing; this also fixes a one-frame stale
body after a click) and add a live Tabs section to the gallery with a
content panel and a button that marks the next tab as unseen.  Document
the component in AGENTS.md and the architecture pages.
Johannes Zellner 55 минут назад
Родитель
Сommit
f66c68d9f1
7 измененных файлов с 224 добавлено и 80 удалено
  1. 10 5
      AGENTS.md
  2. 4 2
      docs/graphics.html
  3. 1 1
      docs/userland.html
  4. 20 70
      user/apps/bootscan/ucmd_bootscan.c
  5. 82 2
      user/apps/gallery/gallery.c
  6. 88 0
      user/lib/ngui/ngui.c
  7. 19 0
      user/lib/ngui/ngui.h

+ 10 - 5
AGENTS.md

@@ -824,8 +824,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   of `netmenu`; `ngui_section` draws captioned section headers with an
   of `netmenu`; `ngui_section` draws captioned section headers with an
   optional action button, `ngui_list_row` is a two-line hover/selected row
   optional action button, `ngui_list_row` is a two-line hover/selected row
   with optional signal bars and trailing text, and `ngui_signal_bars` draws
   with optional signal bars and trailing text, and `ngui_signal_bars` draws
-  the four-bar strength icon.  The WM draws no outer chrome, so a popover
-  fills its whole window.  Text fields use the proportional font with
+  the four-bar strength icon.  `ngui_tabs` draws a horizontal tab strip with
+  the active accent underline and fresh-output dots, handles press/release
+  selection and returns 1 when `t->active` changed; the caller paints the
+  strip background and owns the tab content.  The WM draws no outer chrome,
+  so a popover fills its whole window.  Text fields use the proportional font with
   measured caret,
   measured caret,
   selection and horizontal scrolling, and support mouse drag-selection,
   selection and horizontal scrolling, and support mouse drag-selection,
   double-click select-all, Ctrl+Left/Right word jumps,
   double-click select-all, Ctrl+Left/Right word jumps,
@@ -1911,7 +1914,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   use.
   use.
 - **Gallery**: `gallery.c` is the ngui widget gallery: typography and
 - **Gallery**: `gallery.c` is the ngui widget gallery: typography and
   truncation, the drawing primitives and clip stack, the theme palette, every
   truncation, the drawing primitives and clip stack, the theme palette, every
-  icon in `ICON_LIST`, live buttons/icon buttons, text fields (including a disabled,
+  icon in `ICON_LIST`, live buttons/icon buttons, a live `ngui_tabs` strip,
+  text fields (including a disabled,
   read-only field) with event feedback, the multi-line text area, a live
   read-only field) with event feedback, the multi-line text area, a live
   `ngui_file_dialog` example, a nested draggable scroll pane and the
   `ngui_file_dialog` example, a nested draggable scroll pane and the
   `ngui_cut_*` layout helpers.  The real
   `ngui_cut_*` layout helpers.  The real
@@ -1921,8 +1925,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   fallback on `root.img`/FAT mounts.  Like the browser launcher it spawns
   fallback on `root.img`/FAT mounts.  Like the browser launcher it spawns
   the real app parented and waits for it.
   the real app parented and waits for it.
 - **Boot log window**: `ucmd_bootscan.c` is the `bootscan` window, a live
 - **Boot log window**: `ucmd_bootscan.c` is the `bootscan` window, a live
-  tail with three tabs: *Boot* (`bootcon_puts()`'s text log), *Serial* (the
-  always-on COM1 capture) and *Scan* (`boot_storage_main()`'s own trail).
+  tail with three `ngui_tabs` tabs: *Boot* (`bootcon_puts()`'s text log),
+  *Serial* (the always-on COM1 capture) and *Scan* (`boot_storage_main()`'s
+  own trail).
   `SYS_BOOTLOG` (122) selects one of the three sources with its op
   `SYS_BOOTLOG` (122) selects one of the three sources with its op
   (`SYS_BOOTLOG_OP(src, STAT|READ)`): STAT fills `struct sys_bootlog` with
   (`SYS_BOOTLOG_OP(src, STAT|READ)`): STAT fills `struct sys_bootlog` with
   total/start/capacity/active, READ copies one 1 KB chunk from an absolute
   total/start/capacity/active, READ copies one 1 KB chunk from an absolute

+ 4 - 2
docs/graphics.html

@@ -517,7 +517,9 @@
 		<p>
 		<p>
 			<span class="file">ngui.c</span>/<span class="file">ngui.h</span> is an
 			<span class="file">ngui.c</span>/<span class="file">ngui.h</span> is an
 			immediate-mode GUI toolkit shared by the ring-3 apps. It provides
 			immediate-mode GUI toolkit shared by the ring-3 apps. It provides
-			clipped RGB565 primitives, buttons, menu items, text fields, the
+			clipped RGB565 primitives, buttons, menu items, horizontal tab
+			strips (<code>ngui_tabs</code>, with an active underline and
+			fresh-output dots), text fields, the
 			multi-line text area, the modal file dialog, the Adwaita symbolic
 			multi-line text area, the modal file dialog, the Adwaita symbolic
 			icons (see <a href="#icons">UI icons</a>), a theme palette, layout
 			icons (see <a href="#icons">UI icons</a>), a theme palette, layout
 			cut helpers and scrolling panes. It is linked into <code>files</code>, <code>textedit</code>,
 			cut helpers and scrolling panes. It is linked into <code>files</code>, <code>textedit</code>,
@@ -588,7 +590,7 @@
 			</div>
 			</div>
 			<div class="card">
 			<div class="card">
 				<h3>gallery</h3>
 				<h3>gallery</h3>
-				<p>Live showcase of every ngui control and primitive, including the multi-line editor and a working file-dialog example. Ships in the pack as <code>bin/gallery.app</code>; a launcher prefers <code>/share/GALLERY</code> for development.</p>
+				<p>Live showcase of every ngui control and primitive, including the live tab strip (<code>ngui_tabs</code>), the multi-line editor and a working file-dialog example. Ships in the pack as <code>bin/gallery.app</code>; a launcher prefers <code>/share/GALLERY</code> for development.</p>
 			</div>
 			</div>
 			<div class="card">
 			<div class="card">
 				<h3>chat</h3>
 				<h3>chat</h3>

+ 1 - 1
docs/userland.html

@@ -456,7 +456,7 @@
 			<li><strong>bootscan</strong> — live tail of the kernel boot logs (the WM opens it before the terminal, flush right under the top bar). <code>SYS_BOOTLOG</code> exposes three sources, one tab each: <em>Boot</em> is the direct-LFB boot console's text, <em>Serial</em> is the always-on COM1 capture (from the first kernel line, including the deferred USB pass and later driver output) and <em>Scan</em> is the boot storage-scan thread's own trail. Each tab keeps its scrollback and follows its newest line while at the bottom; scrolling up pauses the follow, and a dot marks a hidden tab that received output. Click a tab, press 1/2/3 or Tab to switch; Esc closes the window. Also startable from the shell.</li>
 			<li><strong>bootscan</strong> — live tail of the kernel boot logs (the WM opens it before the terminal, flush right under the top bar). <code>SYS_BOOTLOG</code> exposes three sources, one tab each: <em>Boot</em> is the direct-LFB boot console's text, <em>Serial</em> is the always-on COM1 capture (from the first kernel line, including the deferred USB pass and later driver output) and <em>Scan</em> is the boot storage-scan thread's own trail. Each tab keeps its scrollback and follows its newest line while at the bottom; scrolling up pauses the follow, and a dot marks a hidden tab that received output. Click a tab, press 1/2/3 or Tab to switch; Esc closes the window. Also startable from the shell.</li>
 			<li><strong>process</strong> — graphical task manager (start menu <em>Tasks</em> entry or <code>process</code>), the ngui counterpart of <code>top</code>. It samples once per second into 60 s histories of CPU busy (<code>SYS_SCHEDINFO</code> idle ticks summed over all CPUs), physical memory in use (<code>SYS_MEMINFO</code>) and NIC throughput (<code>SYS_NET_INFO.rx_bytes</code>/<code>tx_bytes</code> from the netdev frame counters), then draws the three graphs with ngui rectangles and lines. The table is a process tree: it lists PID, state, CPU%, memory, total CPU time and name, indents children under their <code>parent_pid</code>, and still sorts sibling groups by PID, state, CPU, memory, time or name when a header is clicked; PgUp/PgDn or the wheel scrolls. Selecting a row shows its pid/ppid and details; <em>End task</em> asks for a second click and then calls <code>SYS_KILL</code> (which kills the subtree), while kernel threads (<code>kernel_thread</code> in the process snapshot), the monitor itself and init are protected. Esc closes it.</li>
 			<li><strong>process</strong> — graphical task manager (start menu <em>Tasks</em> entry or <code>process</code>), the ngui counterpart of <code>top</code>. It samples once per second into 60 s histories of CPU busy (<code>SYS_SCHEDINFO</code> idle ticks summed over all CPUs), physical memory in use (<code>SYS_MEMINFO</code>) and NIC throughput (<code>SYS_NET_INFO.rx_bytes</code>/<code>tx_bytes</code> from the netdev frame counters), then draws the three graphs with ngui rectangles and lines. The table is a process tree: it lists PID, state, CPU%, memory, total CPU time and name, indents children under their <code>parent_pid</code>, and still sorts sibling groups by PID, state, CPU, memory, time or name when a header is clicked; PgUp/PgDn or the wheel scrolls. Selecting a row shows its pid/ppid and details; <em>End task</em> asks for a second click and then calls <code>SYS_KILL</code> (which kills the subtree), while kernel threads (<code>kernel_thread</code> in the process snapshot), the monitor itself and init are protected. Esc closes it.</li>
 			<li><strong>disks</strong> — storage manager (start menu <em>Disks</em> entry, or the <em>Disks</em> button in <code>files</code>): devices, MBR partitions and mounts; per-volume mount name, read-only and automount saved to <code>/data/MOUNTS.CFG</code>; Format and Install NebOS dialogs that run <code>format -y</code>/<code>install -y</code> on a private tty and show the log. See <a href="filesystems.html#disks">Filesystems → Storage manager</a>.</li>
 			<li><strong>disks</strong> — storage manager (start menu <em>Disks</em> entry, or the <em>Disks</em> button in <code>files</code>): devices, MBR partitions and mounts; per-volume mount name, read-only and automount saved to <code>/data/MOUNTS.CFG</code>; Format and Install NebOS dialogs that run <code>format -y</code>/<code>install -y</code> on a private tty and show the log. See <a href="filesystems.html#disks">Filesystems → Storage manager</a>.</li>
-			<li><strong>gallery</strong> — live widget and primitive showcase; the real app ships in the pack.</li>
+			<li><strong>gallery</strong> — live widget and primitive showcase (including a live <code>ngui_tabs</code> strip); the real app ships in the pack.</li>
 			<li><strong>service</strong> — the init client: <code>service</code> lists <code>init</code>, <code>wm</code> and the user services, <code>status/start/stop/restart/kill/logs/logtail</code> act through init, and <code>service --gui</code> (or the start menu's <em>Services</em> entry) opens the ngui service list with Start/Stop/Restart/Kill (Kill needs a second click) and a live log pane. It is a stateless client: state and logs are read from <code>/share</code>, actions go over init's control tty.</li>
 			<li><strong>service</strong> — the init client: <code>service</code> lists <code>init</code>, <code>wm</code> and the user services, <code>status/start/stop/restart/kill/logs/logtail</code> act through init, and <code>service --gui</code> (or the start menu's <em>Services</em> entry) opens the ngui service list with Start/Stop/Restart/Kill (Kill needs a second click) and a live log pane. It is a stateless client: state and logs are read from <code>/share</code>, actions go over init's control tty.</li>
 			<li><strong>chat</strong> — native Cloudron Chat client (start menu <em>Chat</em> entry, or <code>chat [config-path]</code>): username/password/TOTP login through the Cloudron OIDC API or a pasted session cookie, channels and DMs with unread badges and presence, history with older paging, inline image attachments, link-preview cards (click one to open it in the browser) and profile-picture avatars, sending, REST polling, read markers, browse/join and new DM.  The session cookie (never the password) is saved in <code>/data/CHAT.CFG</code> when the persistent volume is mounted, or in the RAM-only <code>/share/chat.cfg</code> otherwise.  Keys: Enter sends, PgUp/PgDn scroll, Home at the top pages in older history, Ctrl+B browse, Ctrl+N new DM, Ctrl+R refresh, and with the composer unfocused (Esc) Up/Down switch conversations.  See <a href="network.html#chat">Network → Chat client</a>.</li>
 			<li><strong>chat</strong> — native Cloudron Chat client (start menu <em>Chat</em> entry, or <code>chat [config-path]</code>): username/password/TOTP login through the Cloudron OIDC API or a pasted session cookie, channels and DMs with unread badges and presence, history with older paging, inline image attachments, link-preview cards (click one to open it in the browser) and profile-picture avatars, sending, REST polling, read markers, browse/join and new DM.  The session cookie (never the password) is saved in <code>/data/CHAT.CFG</code> when the persistent volume is mounted, or in the RAM-only <code>/share/chat.cfg</code> otherwise.  Keys: Enter sends, PgUp/PgDn scroll, Home at the top pages in older history, Ctrl+B browse, Ctrl+N new DM, Ctrl+R refresh, and with the composer unfocused (Esc) Up/Down switch conversations.  See <a href="network.html#chat">Network → Chat client</a>.</li>
 			<li><strong>solitaire</strong> — Klondike with drag-and-drop, scoring and a win animation.</li>
 			<li><strong>solitaire</strong> — Klondike with drag-and-drop, scoring and a win animation.</li>

+ 20 - 70
user/apps/bootscan/ucmd_bootscan.c

@@ -61,10 +61,11 @@ static struct logsrc src[TAB_COUNT] = {
 
 
 static struct ngui ui;
 static struct ngui ui;
 static struct ngui_scroll sc;
 static struct ngui_scroll sc;
+static struct ngui_tabs tabbar = { .active = TAB_SERIAL };
+static struct ngui_tab tab_view[TAB_COUNT];
 static int cur = TAB_SERIAL;
 static int cur = TAB_SERIAL;
 static int follow = 1;
 static int follow = 1;
 static int pending_tail;
 static int pending_tail;
-static int pressed_tab = -1;
 static int quit;
 static int quit;
 
 
 static unsigned int count_lines(const struct logsrc *s)
 static unsigned int count_lines(const struct logsrc *s)
@@ -191,69 +192,28 @@ static int pull(struct logsrc *s)
 	return changed;
 	return changed;
 }
 }
 
 
-static struct ngui_rect tab_rect(int i)
-{
-	struct ngui_rect r;
-	int x = PAD, j;
-
-	r.y = 3;
-	r.h = TAB_H - 6;
-	for (j = 0; j < i; j++)
-		x += (int)ngui_text_width_ui(&ui, src[j].label) + 32;
-	r.x = x;
-	r.w = (int)ngui_text_width_ui(&ui, src[i].label) + 30;
-	return r;
-}
-
 static void draw_tabs(void)
 static void draw_tabs(void)
 {
 {
 	struct ngui_rect hr = { 0, 0, ui.w, TAB_H };
 	struct ngui_rect hr = { 0, 0, ui.w, TAB_H };
+	struct ngui_rect tr = { PAD, 3, ui.w - 2 * PAD, TAB_H - 6 };
 	struct ngui_rect br = { 0, TAB_H - 1, ui.w, 1 };
 	struct ngui_rect br = { 0, TAB_H - 1, ui.w, 1 };
 	int i;
 	int i;
 
 
 	ngui_fill_rect(&ui, hr, ui.theme.panel);
 	ngui_fill_rect(&ui, hr, ui.theme.panel);
 	ngui_fill_rect(&ui, br, ui.theme.border);
 	ngui_fill_rect(&ui, br, ui.theme.border);
+	tabbar.active = cur;
 	for (i = 0; i < TAB_COUNT; i++) {
 	for (i = 0; i < TAB_COUNT; i++) {
-		struct ngui_rect r = tab_rect(i);
-		int hot = ui.inside &&
-			ngui_rect_contains(&r, ui.mouse_x, ui.mouse_y);
-		int lh = ngui_text_line_height(&ui, LINE_PX, 0);
-		int ty = r.y + (r.h - lh) / 2;
-		unsigned short color;
-
-		if (i == cur)
-			ngui_fill_rect(&ui, r, ui.theme.panel_alt);
-		else if (hot)
-			ngui_fill_rect(&ui, r, ui.theme.button);
-		color = (i == cur || hot) ? ui.theme.text : ui.theme.text_dim;
-		if (ty < r.y)
-			ty = r.y;
-		ngui_text(&ui, r.x + 10, ty, src[i].label, color);
-		if (i == cur)
-			ngui_fill_rect(&ui, (struct ngui_rect){ r.x, TAB_H - 3,
-							       r.w, 2 },
-				       ui.theme.accent);
-		if (src[i].fresh && i != cur)
-			ngui_fill_round_rect(&ui, (struct ngui_rect){
-						     r.x + r.w - 12,
-						     r.y + r.h / 2 - 2, 4, 4 },
-					     2, 2, 2, 2, ui.theme.accent);
-		if (hot)
-			ngui_set_cursor(&ui, CURSOR_DEFAULT);
+		tab_view[i].label = src[i].label;
+		tab_view[i].fresh = src[i].fresh;
 	}
 	}
-}
-
-static int tab_hit(int x, int y)
-{
-	int i;
-
-	for (i = 0; i < TAB_COUNT; i++) {
-		struct ngui_rect r = tab_rect(i);
-
-		if (ngui_rect_contains(&r, x, y))
-			return i;
+	if (ngui_tabs(&ui, &tabbar, tr, tab_view, TAB_COUNT)) {
+		cur = tabbar.active;
+		src[cur].fresh = 0;
+		src[cur].trim_lines = 0;
+		sc.offset = 0;
+		follow = 1;
+		pending_tail = 1;
 	}
 	}
-	return -1;
 }
 }
 
 
 static void select_tab(int i)
 static void select_tab(int i)
@@ -261,6 +221,7 @@ static void select_tab(int i)
 	if (i < 0 || i >= TAB_COUNT || i == cur)
 	if (i < 0 || i >= TAB_COUNT || i == cur)
 		return;
 		return;
 	cur = i;
 	cur = i;
+	tabbar.active = i;
 	src[i].fresh = 0;
 	src[i].fresh = 0;
 	src[i].trim_lines = 0;
 	src[i].trim_lines = 0;
 	sc.offset = 0;
 	sc.offset = 0;
@@ -269,20 +230,6 @@ static void select_tab(int i)
 	ngui_invalidate(&ui);
 	ngui_invalidate(&ui);
 }
 }
 
 
-static void tab_input(void)
-{
-	int i;
-
-	if (ui.pressed)
-		pressed_tab = tab_hit(ui.press_x, ui.press_y);
-	if (ui.released) {
-		i = tab_hit(ui.release_x, ui.release_y);
-		if (i >= 0 && i == pressed_tab)
-			select_tab(i);
-		pressed_tab = -1;
-	}
-}
-
 static void draw_footer(struct ngui_rect r)
 static void draw_footer(struct ngui_rect r)
 {
 {
 	const char *status;
 	const char *status;
@@ -323,9 +270,9 @@ static void draw_footer(struct ngui_rect r)
 static void draw(void)
 static void draw(void)
 {
 {
 	struct ngui_rect body, foot;
 	struct ngui_rect body, foot;
-	struct logsrc *s = &src[cur];
+	struct logsrc *s;
 	int line_h = ngui_text_line_height(&ui, LINE_PX, 1);
 	int line_h = ngui_text_line_height(&ui, LINE_PX, 1);
-	unsigned int lines = count_lines(s);
+	unsigned int lines;
 	unsigned int pos = 0, idx = 0;
 	unsigned int pos = 0, idx = 0;
 	int max, y;
 	int max, y;
 
 
@@ -336,6 +283,10 @@ static void draw(void)
 		       ui.theme.bg);
 		       ui.theme.bg);
 	draw_tabs();
 	draw_tabs();
 
 
+	/* draw_tabs() may have switched the tab on a click this frame. */
+	s = &src[cur];
+	lines = count_lines(s);
+
 	body.x = PAD;
 	body.x = PAD;
 	body.y = TAB_H + 2;
 	body.y = TAB_H + 2;
 	body.w = ui.w - 2 * PAD;
 	body.w = ui.w - 2 * PAD;
@@ -484,7 +435,6 @@ void _start(const char *arg)
 			u_sleep_ms(POLL_MS);
 			u_sleep_ms(POLL_MS);
 			continue;
 			continue;
 		}
 		}
-		tab_input();
 		scroll_keys();
 		scroll_keys();
 		if (quit)
 		if (quit)
 			break;
 			break;

+ 82 - 2
user/apps/gallery/gallery.c

@@ -27,6 +27,7 @@
 #define THEME_H         140
 #define THEME_H         140
 #define ICONS_H         100
 #define ICONS_H         100
 #define BUTTONS_H       140
 #define BUTTONS_H       140
+#define TABS_H          166
 #define INPUTS_H        440
 #define INPUTS_H        440
 #define SCROLL_H        220
 #define SCROLL_H        220
 #define LAYOUT_H        190
 #define LAYOUT_H        190
@@ -41,6 +42,7 @@ enum section {
 	SEC_THEME,
 	SEC_THEME,
 	SEC_ICONS,
 	SEC_ICONS,
 	SEC_BUTTONS,
 	SEC_BUTTONS,
+	SEC_TABS,
 	SEC_INPUTS,
 	SEC_INPUTS,
 	SEC_SCROLL,
 	SEC_SCROLL,
 	SEC_LAYOUT,
 	SEC_LAYOUT,
@@ -67,6 +69,7 @@ enum {
 	ID_IN_TEXTAREA,
 	ID_IN_TEXTAREA,
 	ID_BTN_RESET_FIELDS,
 	ID_BTN_RESET_FIELDS,
 	ID_BTN_FILE_DIALOG,
 	ID_BTN_FILE_DIALOG,
+	ID_BTN_TAB_FRESH,
 
 
 	ID_NAV_BASE = 0x100,
 	ID_NAV_BASE = 0x100,
 	ID_ICON_BASE = 0x200
 	ID_ICON_BASE = 0x200
@@ -74,12 +77,12 @@ enum {
 
 
 static const int sec_card_h[SEC_COUNT] = {
 static const int sec_card_h[SEC_COUNT] = {
 	TYPO_H, SHAPES_H, THEME_H, ICONS_H,
 	TYPO_H, SHAPES_H, THEME_H, ICONS_H,
-	BUTTONS_H, INPUTS_H, SCROLL_H, LAYOUT_H, CURSORS_H
+	BUTTONS_H, TABS_H, INPUTS_H, SCROLL_H, LAYOUT_H, CURSORS_H
 };
 };
 
 
 static const char *const sec_titles[SEC_COUNT] = {
 static const char *const sec_titles[SEC_COUNT] = {
 	"Text", "Shapes", "Theme", "Icons",
 	"Text", "Shapes", "Theme", "Icons",
-	"Buttons", "Inputs", "Scroll", "Layout", "Cursors"
+	"Buttons", "Tabs", "Inputs", "Scroll", "Layout", "Cursors"
 };
 };
 
 
 static const char *const sec_descs[SEC_COUNT] = {
 static const char *const sec_descs[SEC_COUNT] = {
@@ -88,6 +91,7 @@ static const char *const sec_descs[SEC_COUNT] = {
 	"the ngui_theme_dark palette",
 	"the ngui_theme_dark palette",
 	"every Adwaita symbolic icon",
 	"every Adwaita symbolic icon",
 	"buttons and icon buttons",
 	"buttons and icon buttons",
+	"horizontal tab strips and fresh-output dots",
 	"single-line fields, the text area and file dialog",
 	"single-line fields, the text area and file dialog",
 	"a nested draggable scroll pane",
 	"a nested draggable scroll pane",
 	"ngui_cut_* layout helpers",
 	"ngui_cut_* layout helpers",
@@ -131,6 +135,22 @@ static char event_text[96];
 static char input_feedback[160];
 static char input_feedback[160];
 static int quit;
 static int quit;
 
 
+/* Live ngui_tabs demo: four tabs, one marked with unseen output. */
+static struct ngui_tabs demo_tabs;
+static struct ngui_tab demo_tab_list[4] = {
+	{ "Overview", 0 },
+	{ "Activity", 1 },
+	{ "Members", 0 },
+	{ "Settings", 0 },
+};
+
+static const char *const demo_tab_bodies[4] = {
+	"The active tab fills its background and underlines in the accent colour.",
+	"The dot marks a hidden tab that received new output; clicking clears it.",
+	"ngui_tabs() returns 1 when the user clicks a different tab.",
+	"The caller owns the selection, content and fresh flags.",
+};
+
 /* ---- tiny freestanding string helpers ---- */
 /* ---- tiny freestanding string helpers ---- */
 
 
 static unsigned int s_len(const char *s)
 static unsigned int s_len(const char *s)
@@ -782,6 +802,63 @@ static void draw_buttons(struct ngui *g, struct ngui_rect card)
 			   g->theme.border);
 			   g->theme.border);
 }
 }
 
 
+static void draw_tabs_demo(struct ngui *g, struct ngui_rect card)
+{
+	int x = card.x + CARD_PAD;
+	int y = card.y + 12;
+	int w = card.w - 2 * CARD_PAD;
+	struct ngui_rect strip, panel;
+
+	ngui_text_ellipsis(g, rect_make(x, y, w, 16), x, y,
+			   "ngui_tabs - click a tab; the dot marks unseen output.",
+			   g->theme.text_dim);
+	y += 22;
+
+	strip = rect_make(x, y, w, 28);
+	ngui_fill_rect(g, strip, g->theme.panel);
+	ngui_fill_rect(g, rect_make(strip.x, strip.y + strip.h - 1,
+				    strip.w, 1), g->theme.border);
+	if (rect_visible(g, strip) &&
+	    ngui_tabs(g, &demo_tabs, rect_make(x + 4, y + 2, w - 8, 24),
+		      demo_tab_list, 4)) {
+		char b[64];
+		unsigned int n = 0;
+
+		demo_tab_list[demo_tabs.active].fresh = 0;
+		s_append(b, &n, "tabs: switched to ", sizeof(b));
+		s_append(b, &n, demo_tab_list[demo_tabs.active].label,
+			 sizeof(b));
+		event_set(b);
+	}
+	y += 28 + 10;
+
+	panel = rect_make(x, y, w, 48);
+	ngui_fill_round_rect(g, panel, 6, 6, 6, 6, g->theme.panel_alt);
+	ngui_text(g, panel.x + 12, panel.y + 9,
+		  demo_tab_list[demo_tabs.active].label, g->theme.text);
+	ngui_text_ellipsis(g, rect_make(panel.x + 12, panel.y + 28,
+					panel.w - 24, 16),
+			   panel.x + 12, panel.y + 28,
+			   demo_tab_bodies[demo_tabs.active],
+			   g->theme.text_dim);
+	y += 48 + 10;
+
+	if (gallery_button(g, ID_BTN_TAB_FRESH, rect_make(x, y, 170, 26),
+			   "Mark a tab as unseen")) {
+		int mark = (demo_tabs.active + 1) % 4;
+
+		demo_tab_list[mark].fresh = 1;
+		event_set("tabs: the next tab now shows an unseen dot");
+		ngui_invalidate(g);
+	}
+	if (w > 380)
+		ngui_text_ellipsis(g,
+				   rect_make(x + 182, y + 5, w - 182, 16),
+				   x + 182, y + 5,
+				   "ngui_tabs() handles press/release and returns 1 on a change.",
+				   g->theme.text_dim);
+}
+
 static void area_reset(void)
 static void area_reset(void)
 {
 {
 	static const char demo[] =
 	static const char demo[] =
@@ -1239,6 +1316,9 @@ static void draw_body(struct ngui *g, struct ngui_rect body)
 		case SEC_BUTTONS:
 		case SEC_BUTTONS:
 			draw_buttons(g, card);
 			draw_buttons(g, card);
 			break;
 			break;
+		case SEC_TABS:
+			draw_tabs_demo(g, card);
+			break;
 		case SEC_INPUTS:
 		case SEC_INPUTS:
 			draw_inputs(g, card);
 			draw_inputs(g, card);
 			break;
 			break;

+ 88 - 0
user/lib/ngui/ngui.c

@@ -1541,6 +1541,94 @@ int ngui_section(struct ngui *ui, struct ngui_rect r, const char *caption,
 	return fired;
 	return fired;
 }
 }
 
 
+/* Width of one tab: the label plus 10 px on each side and room for the
+ * fresh-output dot.  Reserved even without a dot, so the row does not move
+ * when a tab becomes fresh. */
+static int ngui_tab_width(struct ngui *ui, const struct ngui_tab *tab)
+{
+	return (int)ngui_text_width_ui(ui, tab->label ? tab->label : "") + 30;
+}
+
+static int ngui_tab_at(struct ngui *ui, struct ngui_rect r,
+		       const struct ngui_tab *tabs, int count, int x, int y)
+{
+	int i, tx = r.x;
+
+	if (!ngui_rect_contains(&r, x, y))
+		return -1;
+	for (i = 0; i < count && tx < r.x + r.w; i++) {
+		int w = ngui_tab_width(ui, &tabs[i]);
+
+		if (x >= tx && x < tx + w)
+			return i;
+		tx += w + 2;
+	}
+	return -1;
+}
+
+int ngui_tabs(struct ngui *ui, struct ngui_tabs *t, struct ngui_rect r,
+	      const struct ngui_tab *tabs, int count)
+{
+	int i, x, changed = 0;
+	int lh, ty;
+
+	if (!ui || !t || !tabs || count <= 0)
+		return 0;
+	if (t->active < 0 || t->active >= count)
+		t->active = 0;
+	if (!ui->pressed && !ui->released && !ui->buttons)
+		t->press = 0;
+
+	if (ui->pressed)
+		t->press = ngui_tab_at(ui, r, tabs, count,
+				       ui->press_x, ui->press_y) + 1;
+	if (ui->released) {
+		int hit = ngui_tab_at(ui, r, tabs, count,
+				      ui->release_x, ui->release_y);
+
+		if (hit >= 0 && hit + 1 == t->press && hit != t->active) {
+			t->active = hit;
+			changed = 1;
+		}
+		t->press = 0;
+	}
+
+	lh = ngui_text_line_height(ui, ui->font_px, 0);
+	ty = r.y + (r.h - lh) / 2;
+	if (ty < r.y)
+		ty = r.y;
+
+	ngui_push_clip(ui, r);
+	x = r.x;
+	for (i = 0; i < count && x < r.x + r.w; i++) {
+		const char *label = tabs[i].label ? tabs[i].label : "";
+		int w = ngui_tab_width(ui, &tabs[i]);
+		struct ngui_rect tr = rect_make(x, r.y, w, r.h);
+		int hot = ui->inside &&
+			ngui_rect_contains(&tr, ui->mouse_x, ui->mouse_y);
+		unsigned short color;
+
+		if (i == t->active)
+			ngui_fill_rect(ui, tr, ui->theme.panel_alt);
+		else if (hot || t->press == i + 1)
+			ngui_fill_rect(ui, tr, ui->theme.button);
+		color = (i == t->active || hot) ? ui->theme.text :
+			ui->theme.text_dim;
+		ngui_text(ui, x + 10, ty, label, color);
+		if (i == t->active)
+			ngui_fill_rect(ui, rect_make(x, r.y + r.h - 2, w, 2),
+				       ui->theme.accent);
+		if (tabs[i].fresh && i != t->active)
+			ngui_fill_round_rect(ui,
+					     rect_make(x + w - 12,
+						       r.y + r.h / 2 - 2, 4, 4),
+					     2, 2, 2, 2, ui->theme.accent);
+		x += w + 2;
+	}
+	ngui_pop_clip(ui);
+	return changed;
+}
+
 /* Shared by the byte and UTF-8 variants: one hover/selection row with an
 /* Shared by the byte and UTF-8 variants: one hover/selection row with an
  * optional signal icon and right-aligned text. */
  * optional signal icon and right-aligned text. */
 static int ngui_list_row_common(struct ngui *ui, unsigned int id,
 static int ngui_list_row_common(struct ngui *ui, unsigned int id,

+ 19 - 0
user/lib/ngui/ngui.h

@@ -192,6 +192,20 @@ struct ngui_slider {
 	int dragging;
 	int dragging;
 };
 };
 
 
+/* A horizontal tab strip.  `label` is required; `fresh` draws a small accent
+ * dot for a hidden tab that received new output.  The caller paints the strip
+ * background and owns `active`; `press` is component state and may be left
+ * zero.  r is the tab row (the caller leaves room for its own border). */
+struct ngui_tab {
+	const char *label;
+	int fresh;
+};
+
+struct ngui_tabs {
+	int active;     /* in/out: selected tab index */
+	int press;      /* internal: pressed index + 1, 0 = none */
+};
+
 /* A flat menu panel: the surface, a body rectangle for the caller's widgets
 /* A flat menu panel: the surface, a body rectangle for the caller's widgets
  * and a one-line footer for status text.  There is no title bar or close
  * and a one-line footer for status text.  There is no title bar or close
  * button; the app is dismissed by clicking outside it or pressing Esc.  Use
  * button; the app is dismissed by clicking outside it or pressing Esc.  Use
@@ -393,6 +407,11 @@ int  ngui_tile(struct ngui *ui, unsigned int id, struct ngui_rect r,
 /* Horizontal slider; writes *value and returns 1 when it changed. */
 /* Horizontal slider; writes *value and returns 1 when it changed. */
 int  ngui_slider(struct ngui *ui, unsigned int id, struct ngui_slider *s,
 int  ngui_slider(struct ngui *ui, unsigned int id, struct ngui_slider *s,
 		 struct ngui_rect r, int *value, int min, int max);
 		 struct ngui_rect r, int *value, int min, int max);
+/* Horizontal tab strip: draws every tab that fits in r, the active accent
+ * underline and the fresh dots, and handles press/release selection.  Updates
+ * t->active and returns 1 when the user clicked another tab. */
+int  ngui_tabs(struct ngui *ui, struct ngui_tabs *t, struct ngui_rect r,
+	       const struct ngui_tab *tabs, int count);
 int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		    struct ngui_input *in);
 		    struct ngui_input *in);
 int ngui_text_area(struct ngui *ui, unsigned int id, struct ngui_rect r,
 int ngui_text_area(struct ngui *ui, unsigned int id, struct ngui_rect r,