ngui.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* ngui: tiny immediate-mode GUI toolkit for ring-3 canvas windows.
  2. *
  3. * The toolkit owns no memory: the app passes its framebuffer and keeps the
  4. * state structs for text fields and scroll panes. Everything is RGB565 and
  5. * ASCII, using the kernel's 8x16 font. See ucmd_files.c for a user. */
  6. #ifndef NGUI_H
  7. #define NGUI_H
  8. #include "syscall.h"
  9. #include "wm_abi.h"
  10. #include "icon_abi.h"
  11. #define NGUI_INPUT_MAX 512
  12. #define NGUI_MOTION_REDRAW_MS 33
  13. /* Corner radius shared by cards, tiles, list rows and dialogs. */
  14. #define NGUI_RADIUS 6
  15. #define NGUI_INPUT_CHANGED 1
  16. #define NGUI_INPUT_SUBMIT 2
  17. #define NGUI_INPUT_CANCEL 4
  18. #define NGUI_INPUT_NEXT 8 /* Tab: move to the next field (with CANCEL) */
  19. /* Key codes from io.c. Arrows are 0x100..; the page/home/end keys are
  20. * produced by the extended PS/2 path. */
  21. #define NGUI_KEY_ESC 0x1B
  22. #define NGUI_KEY_BACKSPACE 0x08
  23. #define NGUI_KEY_TAB 0x09
  24. #define NGUI_KEY_LEFT 0x100
  25. #define NGUI_KEY_RIGHT 0x101
  26. #define NGUI_KEY_UP 0x102
  27. #define NGUI_KEY_DOWN 0x103
  28. #define NGUI_KEY_PAGE_UP 0x104
  29. #define NGUI_KEY_PAGE_DOWN 0x105
  30. #define NGUI_KEY_HOME 0x106
  31. #define NGUI_KEY_END 0x107
  32. #define NGUI_KEY_DELETE 0x108
  33. #define NGUI_KEY_CTRL_LEFT 0x109
  34. #define NGUI_KEY_CTRL_RIGHT 0x10A
  35. #define NGUI_KEY_CTRL_DELETE 0x10B
  36. #define NGUI_KEY_CTRL_BACKSPACE 0x10C
  37. #define NGUI_RGB(r, g, b) \
  38. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  39. struct ufont_face;
  40. struct ngui_rect {
  41. int x, y, w, h;
  42. };
  43. struct ngui_theme {
  44. unsigned short bg;
  45. unsigned short panel;
  46. unsigned short panel_alt;
  47. unsigned short border;
  48. unsigned short text;
  49. unsigned short text_dim;
  50. unsigned short accent;
  51. unsigned short selection;
  52. unsigned short button;
  53. unsigned short button_hot;
  54. unsigned short button_down;
  55. unsigned short icon_folder;
  56. unsigned short icon_file;
  57. unsigned short icon_disk;
  58. unsigned short error;
  59. unsigned short success;
  60. unsigned short warning;
  61. };
  62. extern const struct ngui_theme ngui_theme_dark;
  63. /* Icons come from the Adwaita symbolic set (include/icon_abi.h): ngui loads
  64. * /ramfs/icons/adwaita16.icn on first use and tints the 16 px masks with the
  65. * colour passed to ngui_icon(). Both packs carry the bundle; without it
  66. * ngui_icon() draws nothing. */
  67. #define NGUI_ICON_ENUM_(e, name, file) NGUI_ICON_##e,
  68. enum ngui_icon {
  69. ICON_LIST(NGUI_ICON_ENUM_)
  70. NGUI_ICON_COUNT
  71. };
  72. #undef NGUI_ICON_ENUM_
  73. #define NGUI_ICON_PATH ICON_BUNDLE_PATH
  74. /* Short display name of an icon ("folder", "back", ...). */
  75. const char *ngui_icon_name(enum ngui_icon icon);
  76. struct ngui_input {
  77. char text[NGUI_INPUT_MAX];
  78. unsigned int len;
  79. unsigned int cursor;
  80. unsigned int scroll; /* first visible character */
  81. unsigned int anchor; /* mouse selection anchor (== cursor: none) */
  82. int dragging; /* a press in the field is extending a selection */
  83. int disabled; /* ignore input and paint the value dimmed */
  84. int secret; /* paint '*' per character (passwords) */
  85. const char *placeholder;
  86. unsigned int last_click_ms; /* previous press, for double-click select-all */
  87. };
  88. struct ngui_scroll {
  89. int offset; /* pixels scrolled from the top */
  90. int content_h;
  91. int view_h;
  92. int dragging;
  93. int drag_dy;
  94. int hide_bar; /* wheel/keys only; draw no scrollbar */
  95. /* The bar is an overlay: it reserves no layout width, stays hidden
  96. * while the pointer is outside the pane and slides in on hover. */
  97. int bar_vis; /* 0 hidden .. animation target shown */
  98. int bar_target; /* requested visibility (0 or full) */
  99. int bar_hot; /* pointer is over the scrollbar strip */
  100. unsigned int bar_ms; /* last animation step (u_ticks_ms) */
  101. };
  102. /* A multi-line, soft-wrapping plain-text editor. The caller owns the text
  103. * buffer and keeps the state; the widget edits it in place, supports a caret,
  104. * mouse drag selection, double-click word selection, Ctrl+A, the arrow /
  105. * Home / End / PageUp / PageDown / word-jump keys, Backspace/Delete and
  106. * Ctrl+Backspace/Delete, inserts Tab, scrolls to the caret and draws its own
  107. * vertical scrollbar. The NUL at text[len] is maintained. Esc is consumed:
  108. * it clears the focus and returns NGUI_TEXTAREA_CANCEL so the app can close a
  109. * dialog or quit. Other Ctrl shortcuts are left in the key queue for the
  110. * app. line/column are 1-based and updated by every call. */
  111. struct ngui_text_area {
  112. char *text;
  113. unsigned int cap; /* bytes available in text, including NUL */
  114. unsigned int len;
  115. unsigned int cursor;
  116. unsigned int anchor; /* selection anchor (== cursor: none) */
  117. int dragging;
  118. int read_only;
  119. struct ngui_scroll scroll;
  120. unsigned int last_click_ms; /* previous press, for double-click word select */
  121. unsigned int line, column; /* caret position after the last call */
  122. };
  123. #define NGUI_TEXTAREA_CHANGED 1
  124. #define NGUI_TEXTAREA_CANCEL 2
  125. /* A modal file Open/Save dialog. The caller owns the state; open it with a
  126. * directory or file path (and optionally a suggested file name), then call
  127. * ngui_file_dialog() every frame. It fills the canvas with a dark modal
  128. * backdrop and lists the current directory (the VFS root shows the mount
  129. * table), with Up/Home/Refresh, a scrollable list, a file-name field and
  130. * Cancel/Open|Save buttons. Click a folder to enter it, click a file to
  131. * select it, double-click a file (or press Enter) to accept, Esc to cancel.
  132. *
  133. * The returned result path is absolute and shorter than NGUI_FILE_PATH_MAX.
  134. * NGUI_FILE_ACCEPTED leaves the dialog active so the app can perform the I/O
  135. * and then either ngui_file_dialog_close() it or ngui_file_dialog_error() it
  136. * (which keeps it open for another try). NGUI_FILE_CANCELLED closes it. */
  137. #define NGUI_FILE_OPEN 0
  138. #define NGUI_FILE_SAVE 1
  139. #define NGUI_FILE_NONE 0
  140. #define NGUI_FILE_ACCEPTED 1
  141. #define NGUI_FILE_CANCELLED 2
  142. #define NGUI_FILE_PATH_MAX 64
  143. #define NGUI_FILE_NAME_MAX 64
  144. #define NGUI_FILE_MAX_ENTRIES 128
  145. struct ngui_file_entry {
  146. char name[NGUI_FILE_NAME_MAX];
  147. unsigned int size;
  148. unsigned char is_dir;
  149. unsigned char is_mount;
  150. };
  151. struct ngui_file_dialog {
  152. int active;
  153. int mode;
  154. unsigned int id_base;
  155. char path[NGUI_FILE_PATH_MAX];
  156. struct ngui_file_entry entries[NGUI_FILE_MAX_ENTRIES];
  157. unsigned int count;
  158. int selected;
  159. int truncated;
  160. struct ngui_scroll scroll;
  161. struct ngui_input name;
  162. char error[96];
  163. char result[NGUI_FILE_PATH_MAX];
  164. int last_click;
  165. unsigned int last_click_ms;
  166. int need_focus;
  167. };
  168. struct ngui_slider {
  169. int dragging;
  170. };
  171. /* A horizontal tab strip. `label` is required; `fresh` draws a small accent
  172. * dot for a hidden tab that received new output. The caller paints the strip
  173. * background and owns `active`; `press` is component state and may be left
  174. * zero. r is the tab row (the caller leaves room for its own border). */
  175. struct ngui_tab {
  176. const char *label;
  177. int fresh;
  178. };
  179. struct ngui_tabs {
  180. int active; /* in/out: selected tab index */
  181. int press; /* internal: pressed index + 1, 0 = none */
  182. };
  183. /* A flat menu panel: the surface, a body rectangle for the caller's widgets
  184. * and a one-line footer for status text. There is no title bar or close
  185. * button; the app is dismissed by clicking outside it or pressing Esc. Use
  186. * on one chromeless canvas window created with u_win_create_popover(). */
  187. struct ngui_popover {
  188. struct ngui_rect body;
  189. struct ngui_rect footer;
  190. };
  191. /* A two-line list row with optional signal bars and right-aligned text. */
  192. struct ngui_row {
  193. const char *title;
  194. const char *subtitle;
  195. const char *trailing;
  196. int bars; /* -1 none, 0..4 signal strength bars */
  197. int selected;
  198. };
  199. struct ngui {
  200. unsigned short *px;
  201. int w, h;
  202. struct ngui_theme theme;
  203. /* Fonts are owned by ufont.c's heap arena. ngui_font_attach() opens
  204. * them; ngui_font_detach() frees the arena (the browser calls it before
  205. * heap_reset()). NULL means the 8x16 bitmap fallback. */
  206. struct ufont_face *font;
  207. struct ufont_face *font_bold;
  208. struct ufont_face *font_italic;
  209. struct ufont_face *font_bold_italic;
  210. struct ufont_face *font_mono;
  211. struct ufont_face *font_emoji; /* lazy fallback face */
  212. int emoji_loaded;
  213. int font_px;
  214. /* Current pointer state, content-relative. */
  215. int mouse_x, mouse_y;
  216. unsigned int buttons;
  217. int inside;
  218. int focused;
  219. /* Scroll accumulated since the previous frame, in pixels. */
  220. int scroll_x, scroll_y;
  221. /* Press/release edges collected by ngui_begin(). `pressed` and
  222. * `released` are the left button; the right-button edges let an app
  223. * open a context menu without taking the normal click path. */
  224. unsigned int pressed;
  225. unsigned int released;
  226. unsigned int right_pressed;
  227. unsigned int right_released;
  228. int press_x, press_y;
  229. int release_x, release_y;
  230. int right_press_x, right_press_y;
  231. int right_release_x, right_release_y;
  232. int clicked;
  233. int focus_claimed;
  234. unsigned int hot;
  235. unsigned int active;
  236. unsigned int focus;
  237. struct ngui_rect clip[4];
  238. int nclip;
  239. int dirty;
  240. unsigned int last_blink;
  241. int caret_on;
  242. int keys[32];
  243. unsigned int key_head, key_tail;
  244. int last_mouse_x, last_mouse_y;
  245. int last_inside, last_focused;
  246. /* Pointer motion is throttled: a redraw is needed for hover feedback,
  247. * but not for every 200 Hz mouse report. A pending motion is flushed
  248. * once 33 ms have passed, so the final hover state still appears. */
  249. unsigned int motion_ms;
  250. int motion_pending;
  251. /* Mouse cursor (enum cursor_shape): widgets set `cursor` while they
  252. * draw a frame (a text field under the pointer asks for the I-beam);
  253. * ngui_begin() resets it to the arrow for every redrawn frame and
  254. * ngui_end_ex() sends it to the kernel when it changed. */
  255. int cursor;
  256. int cursor_sent;
  257. int cursor_frame; /* this frame was redrawn (begin returned 1) */
  258. };
  259. void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h);
  260. void ngui_font_attach(struct ngui *ui); /* opens /ramfs/fonts; no-op if absent */
  261. void ngui_font_detach(struct ngui *ui); /* frees the ufont arena */
  262. int ngui_begin(struct ngui *ui); /* 1 = app should redraw */
  263. void ngui_end(struct ngui *ui);
  264. /* Same bookkeeping, but only publish the canvas when `present` is nonzero.
  265. * Lets an app poll input without copying an unchanged frame every loop. */
  266. void ngui_end_ex(struct ngui *ui, int present);
  267. void ngui_invalidate(struct ngui *ui);
  268. void ngui_set_focus(struct ngui *ui, unsigned int id);
  269. unsigned int ngui_focus(struct ngui *ui);
  270. int ngui_key(struct ngui *ui);
  271. int ngui_key_peek(struct ngui *ui);
  272. /* Layout: cut a rectangle off one side of `r` and return the piece. */
  273. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h);
  274. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h);
  275. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w);
  276. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w);
  277. void ngui_inset(struct ngui_rect *r, int n);
  278. int ngui_rect_contains(const struct ngui_rect *r, int x, int y);
  279. /* Primitives. All drawing is clipped to the current clip stack and canvas. */
  280. void ngui_push_clip(struct ngui *ui, struct ngui_rect r);
  281. void ngui_pop_clip(struct ngui *ui);
  282. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color);
  283. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color);
  284. void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
  285. int tl, int tr, int br, int bl,
  286. unsigned short color);
  287. void ngui_frame_round(struct ngui *ui, struct ngui_rect r,
  288. int tl, int tr, int br, int bl, int thickness,
  289. unsigned short color);
  290. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  291. unsigned short color);
  292. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  293. unsigned short color);
  294. void ngui_text_n(struct ngui *ui, int x, int y, const char *s,
  295. unsigned int n, unsigned short color);
  296. /* UTF-8 variants with the primary face -> emoji fallback stack. */
  297. void ngui_text_utf8(struct ngui *ui, int x, int y, const char *s,
  298. unsigned short color);
  299. void ngui_text_n_utf8(struct ngui *ui, int x, int y, const char *s,
  300. unsigned int n, unsigned short color);
  301. unsigned int ngui_text_width_utf8(struct ngui *ui, const char *s);
  302. void ngui_text_ellipsis_utf8(struct ngui *ui, struct ngui_rect r,
  303. int x, int y, const char *s,
  304. unsigned short color);
  305. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  306. unsigned int n, unsigned short color, int scale, int bold);
  307. /* Browser-oriented style: arbitrary pixel size, optional italic and mono. */
  308. void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
  309. unsigned int n, unsigned short color, int px, int bold,
  310. int italic, int mono);
  311. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  312. unsigned int n, unsigned short color);
  313. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  314. const char *s, unsigned short color);
  315. unsigned int ngui_text_width(const char *s);
  316. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale);
  317. unsigned int ngui_text_width_ui(struct ngui *ui, const char *s);
  318. unsigned int ngui_text_width_px(struct ngui *ui, const char *s, unsigned int n,
  319. int px, int bold, int italic, int mono);
  320. int ngui_text_ascent(struct ngui *ui, int px, int mono);
  321. int ngui_text_line_height(struct ngui *ui, int px, int mono);
  322. /* Face-aware variants: a non-NULL `face` (an @font-face loaded by the
  323. * caller) overrides the built-in regular/bold/italic/mono selection. */
  324. unsigned int ngui_text_width_face(struct ngui *ui, struct ufont_face *face,
  325. const char *s, unsigned int n, int px,
  326. int bold, int italic, int mono);
  327. int ngui_text_ascent_face(struct ngui *ui, struct ufont_face *face, int px,
  328. int mono);
  329. int ngui_text_line_height_face(struct ngui *ui, struct ufont_face *face,
  330. int px, int mono);
  331. void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x,
  332. int y, const char *s, unsigned int n,
  333. unsigned short color, int px, int bold, int italic,
  334. int mono);
  335. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  336. unsigned short color);
  337. /* Widgets. IDs must be stable across frames. */
  338. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  339. const char *label);
  340. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  341. const char *label, const char *shortcut, int checked);
  342. int ngui_menu_item_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
  343. const char *label, const char *shortcut, int checked);
  344. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  345. enum ngui_icon icon);
  346. /* Menu panel component (see struct ngui_popover). */
  347. void ngui_popover_begin(struct ngui *ui, struct ngui_popover *p);
  348. void ngui_popover_footer(struct ngui *ui, const struct ngui_popover *p,
  349. const char *text, unsigned short color);
  350. /* Section caption with an optional right-aligned action button; returns 1
  351. * when the action fired. */
  352. int ngui_section(struct ngui *ui, struct ngui_rect r, const char *caption,
  353. unsigned int action_id, const char *action);
  354. int ngui_list_row(struct ngui *ui, unsigned int id, struct ngui_rect r,
  355. const struct ngui_row *row);
  356. int ngui_list_row_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
  357. const struct ngui_row *row);
  358. void ngui_signal_bars(struct ngui *ui, struct ngui_rect r, int bars,
  359. unsigned short color);
  360. /* A two-line quick-settings tile (rounded, active when `on`). */
  361. int ngui_tile(struct ngui *ui, unsigned int id, struct ngui_rect r,
  362. const char *title, const char *subtitle, int on);
  363. /* Horizontal slider; writes *value and returns 1 when it changed. */
  364. int ngui_slider(struct ngui *ui, unsigned int id, struct ngui_slider *s,
  365. struct ngui_rect r, int *value, int min, int max);
  366. /* Horizontal tab strip: draws every tab that fits in r, the active accent
  367. * underline and the fresh dots, and handles press/release selection. Updates
  368. * t->active and returns 1 when the user clicked another tab. */
  369. int ngui_tabs(struct ngui *ui, struct ngui_tabs *t, struct ngui_rect r,
  370. const struct ngui_tab *tabs, int count);
  371. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  372. struct ngui_input *in);
  373. int ngui_text_area(struct ngui *ui, unsigned int id, struct ngui_rect r,
  374. struct ngui_text_area *ta);
  375. void ngui_file_dialog_open(struct ngui_file_dialog *fd, int mode,
  376. const char *start_path,
  377. const char *suggested_name,
  378. unsigned int id_base);
  379. int ngui_file_dialog(struct ngui *ui, struct ngui_file_dialog *fd);
  380. void ngui_file_dialog_close(struct ngui_file_dialog *fd);
  381. void ngui_file_dialog_error(struct ngui_file_dialog *fd, const char *msg);
  382. const char *ngui_file_dialog_result(const struct ngui_file_dialog *fd);
  383. /* Ask for `shape` (enum cursor_shape) while the pointer is in `r` (and in
  384. * the current clip); returns 1 when it is. ngui_set_cursor() sets it
  385. * unconditionally for the current frame. */
  386. int ngui_cursor_rect(struct ngui *ui, struct ngui_rect r, int shape);
  387. void ngui_set_cursor(struct ngui *ui, int shape);
  388. void ngui_input_set(struct ngui_input *in, const char *s);
  389. /* Copy a field's real value out (a secret field's '*' mask is bypassed). */
  390. void ngui_input_value(const struct ngui_input *in, char *out,
  391. unsigned int max);
  392. /* Scrolling pane: call begin, draw the content with scroll->offset, end. */
  393. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  394. struct ngui_scroll *s);
  395. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  396. struct ngui_scroll *s);
  397. /* Apply pending wheel/touchpad scroll to s when the pointer is over r and
  398. * consume it. Call after ngui_scroll_begin() set view_h/content_h. */
  399. int ngui_scroll_wheel(struct ngui *ui, struct ngui_rect r,
  400. struct ngui_scroll *s);
  401. void ngui_scroll_to(struct ngui_scroll *s, int offset);
  402. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h);
  403. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s);
  404. #endif