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

Add a disabled state to ngui text fields

A field marked disabled ignores pointer and keyboard input and gives up
the focus if it was held.  ngui_text_input() paints its value in the dim
text colour without a caret or selection, but keeps the proportional
face, so a read-only field does not fall back to the bitmap cells.

The gallery's Inputs section gets a fourth, always-disabled field so the
state is exercised alongside the editable ones.  Verified in QEMU:
clicking it does not take focus or show a caret, typing while it is
under the pointer leaves its value alone, and an editable field still
focuses normally.  make all passes and the command pack stays under its
cap.
Johannes Zellner 1 неделя назад
Родитель
Сommit
f81f3bacc5
5 измененных файлов с 49 добавлено и 20 удалено
  1. 5 4
      AGENTS.md
  2. 3 3
      docs/graphics.html
  3. 24 7
      gallery.c
  4. 16 6
      ngui.c
  5. 1 0
      ngui.h

+ 5 - 4
AGENTS.md

@@ -648,8 +648,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   C string on the top of the user stack (`process_create_user(..., arg)`), so
   `_start(const char *arg)` needs no crt0.  `ngui.c/h` is the shared userland
   GUI toolkit linked into `ucmd_files.c`, `ucmd_mines.c`, `ucmd_solitaire.c`
-  and the external `gallery.elf` (buttons, menu items, text fields, icons,
-  layout cuts and scroll panes); `files` opens
+  and the external `gallery.elf` (buttons, menu items, text fields with a
+  disabled state, icons, layout cuts and scroll panes); `files` opens
   the selected text file through
   `/ramfs/bin/edit`, image files through `/ramfs/bin/view`, and `.htm`/`.html`
   files through `/ramfs/bin/browser`.  `ngui_end_ex()`
@@ -657,8 +657,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   apps that poll input without redrawing.
 - **Gallery**: `gallery.c` is the ngui widget gallery: typography and
   truncation, the drawing primitives and clip stack, the theme palette, all
-  nine icons, live buttons/icon buttons, text fields with event feedback, a
-  nested draggable scroll pane and the `ngui_cut_*` layout helpers.  The real
+  nine icons, live buttons/icon buttons, text fields (including a disabled,
+  read-only field) with event feedback, a nested draggable scroll pane and the
+  `ngui_cut_*` layout helpers.  The real
   ELF ships in the pack as `bin/gallery.app`; `ucmd_gallery.c` is the tiny
   `bin/gallery` launcher, which prefers `/share/GALLERY` (development upload
   via `make gallery-dev`), then the pack copy, then the `GALLERY` disk

+ 3 - 3
docs/graphics.html

@@ -266,7 +266,7 @@
 			<code>solitaire</code> and the real <code>gallery.elf</code>.
 		</p>
 		<ul>
-			<li><strong>Text fields</strong> support drag-selection, double-click select-all, Ctrl+Left/Right word jumps, Ctrl+Delete/Backspace word deletion and Ctrl+A. They use the proportional face with measured caret, selection and horizontal scrolling; the terminal and editor keep the 8×16 cell grid.</li>
+			<li><strong>Text fields</strong> support drag-selection, double-click select-all, Ctrl+Left/Right word jumps, Ctrl+Delete/Backspace word deletion and Ctrl+A. They use the proportional face with measured caret, selection and horizontal scrolling; a field marked <code>disabled</code> ignores pointer and keyboard input, drops the focus and paints its value dimmed in the same proportional face. The terminal and editor keep the 8×16 cell grid.</li>
 			<li><strong>Clip stack</strong> keeps nested panes and popups inside their parents.</li>
 			<li><strong>Layout cuts</strong> split a rectangle into rows, columns and fixed margins without a constraint solver.</li>
 			<li><strong>No retained widget tree.</strong> Apps redraw from state every frame, which keeps the toolkit small and predictable.</li>
@@ -281,8 +281,8 @@
 			<code>font-style</code> and <code>font-family: monospace</code> to
 			the faces. Missing fonts fall back to the 8×16 bitmap. The browser
 			detaches the font arena before <code>heap_reset()</code> and
-			reattaches after <code>heap_start()</code>; terminal content, the
-			editor and text fields keep the bitmap cells.
+			reattaches after <code>heap_start()</code>; terminal content and
+			the editor keep the bitmap cells.
 		</p>
 	</section>
 

+ 24 - 7
gallery.c

@@ -27,7 +27,7 @@
 #define THEME_H         140
 #define ICONS_H         100
 #define BUTTONS_H       140
-#define INPUTS_H        200
+#define INPUTS_H        250
 #define SCROLL_H        220
 #define LAYOUT_H        190
 
@@ -61,6 +61,7 @@ enum {
 	ID_IN_NAME,
 	ID_IN_EMAIL,
 	ID_IN_EVENTS,
+	ID_IN_DISABLED,
 	ID_BTN_RESET_FIELDS,
 
 	ID_NAV_BASE = 0x100,
@@ -118,6 +119,7 @@ static struct ngui_scroll list_scroll;
 static struct ngui_input name_in;
 static struct ngui_input email_in;
 static struct ngui_input events_in;
+static struct ngui_input disabled_in;
 
 static int sec_y[SEC_COUNT];
 static int sec_content_h;
@@ -306,6 +308,7 @@ static void reset_demo(struct ngui *g)
 	ngui_input_set(&name_in, "");
 	ngui_input_set(&email_in, "ada@nebos.org");
 	ngui_input_set(&events_in, "");
+	ngui_input_set(&disabled_in, "Read-only value");
 	ngui_set_focus(g, 0);
 	s_copy(input_feedback, "No events yet - edit a field.",
 		sizeof(input_feedback));
@@ -784,7 +787,7 @@ static void draw_inputs(struct ngui *g, struct ngui_rect card)
 	int field_y = top;
 	int help_x, help_w, help_h;
 	int result;
-	int feedback_y = top + 102;
+	int feedback_y = top + 144;
 
 	if (iw < 220)
 		iw = 220;
@@ -810,10 +813,18 @@ static void draw_inputs(struct ngui *g, struct ngui_rect card)
 			       rect_make(x + label_w, field_y, iw, 24),
 			       &events_in);
 	note_input(g, result, "events", &events_in);
+	field_y += 36;
+
+	ngui_text(g, x, field_y + 4, "Disabled", g->theme.text_dim);
+	/* A disabled field ignores clicks and keys, so it never reports an
+	 * event; the value stays in the same proportional face, dimmed. */
+	gallery_input(g, ID_IN_DISABLED,
+		      rect_make(x + label_w, field_y, iw, 24),
+		      &disabled_in);
 
 	help_x = x + label_w + iw + 24;
 	help_w = card.x + card.w - CARD_PAD - help_x;
-	help_h = 182;
+	help_h = 200;
 
 	if (help_w >= 150) {
 		int hx = help_x;
@@ -841,6 +852,8 @@ static void draw_inputs(struct ngui *g, struct ngui_rect card)
 			  g->theme.text_dim);
 		ngui_text(g, hx + 10, hy + 162, "Tab - focus first",
 			  g->theme.text_dim);
+		ngui_text(g, hx + 10, hy + 180, "Disabled - no input",
+			  g->theme.text_dim);
 	}
 
 	if (input_feedback[0])
@@ -852,20 +865,21 @@ static void draw_inputs(struct ngui *g, struct ngui_rect card)
 				   x, feedback_y, input_feedback,
 				   feedback_error ? g->theme.error :
 						    g->theme.accent);
-	ngui_text_ellipsis(g, rect_make(x, top + 124,
+	ngui_text_ellipsis(g, rect_make(x, top + 166,
 					(help_w >= 150 ?
 					 help_x - x - 12 :
 					 card.w - 2 * CARD_PAD), 16),
-			   x, top + 124,
+			   x, top + 166,
 			   "Return submits, Esc cancels, Ctrl+U clears.",
 			   g->theme.text_dim);
 
 	if (gallery_button(g, ID_BTN_RESET_FIELDS,
-			   rect_make(x, top + 144, 104, 26),
+			   rect_make(x, top + 186, 104, 26),
 			   "Reset fields")) {
 		ngui_input_set(&name_in, "");
 		ngui_input_set(&email_in, "ada@nebos.org");
 		ngui_input_set(&events_in, "");
+		ngui_input_set(&disabled_in, "Read-only value");
 		ngui_set_focus(g, 0);
 		s_copy(input_feedback, "Fields reset to their defaults.",
 			sizeof(input_feedback));
@@ -873,7 +887,7 @@ static void draw_inputs(struct ngui *g, struct ngui_rect card)
 		event_set("input fields reset");
 	}
 	if (help_w >= 150)
-		ngui_text(g, x + 120, top + 150,
+		ngui_text(g, x + 120, top + 192,
 			  "ngui_input_set() rewrites a field.",
 			  g->theme.border);
 }
@@ -1259,6 +1273,9 @@ void _start(const char *arg)
 	name_in.placeholder = "Type a name";
 	email_in.placeholder = "you@example.com";
 	events_in.placeholder = "Type and press Return";
+	disabled_in.placeholder = "Cannot edit";
+	disabled_in.disabled = 1;
+	ngui_input_set(&disabled_in, "Read-only value");
 
 	gallery_layout();
 	page.content_h = sec_content_h;

+ 16 - 6
ngui.c

@@ -1334,7 +1334,7 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		    struct ngui_input *in)
 {
 	struct ngui_rect inner;
-	int focused = (ui->focus == id);
+	int focused = !in->disabled && (ui->focus == id);
 	int result = 0;
 	int avail, line_h, k;
 	int changed = 0;
@@ -1350,8 +1350,14 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		inner.h = 1;
 	avail = inner.w;
 
-	if (ui->pressed &&
-	    ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
+	if (in->disabled) {
+		/* A field that became disabled while focused gives the focus
+		 * back, and a press cannot start a drag or a selection. */
+		if (ui->focus == id)
+			ngui_set_focus(ui, 0);
+		in->dragging = 0;
+	} else if (ui->pressed &&
+		   ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
 		unsigned int now = u_ticks_ms();
 		int dbl = in->last_click_ms &&
 			  (unsigned int)(now - in->last_click_ms) <=
@@ -1385,7 +1391,8 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	 * still held.  Sampled coordinates may be outside the canvas.  The
 	 * release *event* is authoritative: the sampled button state can lag
 	 * the event queue by a frame. */
-	if (in->dragging && (ui->buttons & 1) && !ui->released) {
+	if (!in->disabled && in->dragging && (ui->buttons & 1) &&
+	    !ui->released) {
 		in->cursor = input_pos_at(ui, in, inner.x, ui->mouse_x);
 		/* Keep redrawing when the pointer leaves the window. */
 		ngui_invalidate(ui);
@@ -1468,7 +1475,8 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
 	ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
 
-	input_scroll_to_cursor(ui, in, avail);
+	if (!in->disabled)
+		input_scroll_to_cursor(ui, in, avail);
 
 	ngui_push_clip(ui, inner);
 	if (in->len == 0 && in->placeholder) {
@@ -1479,7 +1487,9 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		if (focused)
 			input_draw_selection(ui, in, inner);
 		ngui_text_px(ui, inner.x, inner.y, in->text + in->scroll,
-			     slen(in->text + in->scroll), ui->theme.text,
+			     slen(in->text + in->scroll),
+			     in->disabled ? ui->theme.text_dim :
+					    ui->theme.text,
 			     ui->font_px, 0, 0, 0);
 	}
 

+ 1 - 0
ngui.h

@@ -83,6 +83,7 @@ struct ngui_input {
 	unsigned int scroll;          /* first visible character */
 	unsigned int anchor;          /* mouse selection anchor (== cursor: none) */
 	int dragging;                 /* a press in the field is extending a selection */
+	int disabled;                 /* ignore input and paint the value dimmed */
 	const char *placeholder;
 	unsigned int last_click_ms;   /* previous press, for double-click select-all */
 };