Explorar o código

Add double-click select-all to ngui text inputs

Johannes Zellner hai 2 semanas
pai
achega
763cdbc9db
Modificáronse 3 ficheiros con 30 adicións e 10 borrados
  1. 4 4
      AGENTS.md
  2. 25 6
      ngui.c
  3. 1 0
      ngui.h

+ 4 - 4
AGENTS.md

@@ -256,10 +256,10 @@ run area), so adding code no longer moves any address.
   the toolkit exports.  Buttons fire on the release edge and keep `active`
   until `ui->released`: inferring release from the sampled `ui->buttons`
   races the WM event queue and drops clicks.  Text fields support mouse
-  drag-selection, Ctrl+Left/Right word jumps, Ctrl+Delete/Ctrl+Backspace word
-  deletion and Ctrl+A select-all; typing or Backspace/Delete replaces a
-  selection.  At the end of the line Ctrl+Delete removes the word before the
-  caret instead of doing nothing.
+  drag-selection, double-click select-all, Ctrl+Left/Right word jumps,
+  Ctrl+Delete/Ctrl+Backspace word deletion and Ctrl+A select-all; typing or
+  Backspace/Delete replaces a selection.  At the end of the line Ctrl+Delete
+  removes the word before the caret instead of doing nothing.
 - **Graphics**: `vga.c/h`, `vga_copy.S`, `blit.c/h`, `gpu.c`, `ati.c`, `nvidia.c`,
   `nv50.c`, `pci.h` — VESA LFB + shadow buffer.  Drawing goes into a
   `struct surface` render target: `vga_target()` is the screen's shadow buffer

+ 25 - 6
ngui.c

@@ -760,6 +760,7 @@ void ngui_input_set(struct ngui_input *in, const char *s)
 	in->scroll = 0;
 	in->anchor = in->cursor;
 	in->dragging = 0;
+	in->last_click_ms = 0;
 }
 
 static int input_word_char(char c)
@@ -897,6 +898,9 @@ static void input_move_to(struct ngui_input *in, unsigned int pos)
 	in->anchor = pos;
 }
 
+/* Two presses in the same field within this window select all of its text. */
+#define NGUI_DOUBLE_CLICK_MS 500
+
 /* Character position under a content-relative x, clamped to the visible
  * part of the field. */
 static unsigned int input_pos_at(const struct ngui_input *in, int inner_x,
@@ -950,14 +954,29 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 
 	if (ui->pressed &&
 	    ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
-		unsigned int pos = input_pos_at(in, inner.x, cols, ui->press_x);
-
-		in->cursor = pos;
-		in->anchor = pos;
-		in->dragging = 1;
+		unsigned int now = u_ticks_ms();
+		int dbl = in->last_click_ms &&
+			  (unsigned int)(now - in->last_click_ms) <=
+			  NGUI_DOUBLE_CLICK_MS;
+
+		if (dbl) {
+			/* Double click: select the whole text and keep it
+			 * selected while the second press is held. */
+			in->anchor = 0;
+			in->cursor = in->len;
+			in->dragging = 0;
+		} else {
+			unsigned int pos = input_pos_at(in, inner.x, cols,
+							ui->press_x);
+
+			in->cursor = pos;
+			in->anchor = pos;
+			in->dragging = 1;
+		}
+		in->last_click_ms = now;
 		ui->focus = id;
 		ui->focus_claimed = 1;
-		ui->last_blink = u_ticks_ms();
+		ui->last_blink = now;
 		ui->caret_on = 1;
 		focused = 1;
 	} else if (ui->pressed) {

+ 1 - 0
ngui.h

@@ -81,6 +81,7 @@ struct ngui_input {
 	unsigned int anchor;          /* mouse selection anchor (== cursor: none) */
 	int dragging;                 /* a press in the field is extending a selection */
 	const char *placeholder;
+	unsigned int last_click_ms;   /* previous press, for double-click select-all */
 };
 
 struct ngui_scroll {