ngui.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #define NGUI_INPUT_CHANGED 1
  14. #define NGUI_INPUT_SUBMIT 2
  15. #define NGUI_INPUT_CANCEL 4
  16. #define NGUI_INPUT_NEXT 8 /* Tab: move to the next field (with CANCEL) */
  17. /* Key codes from io.c. Arrows are 0x100..; the page/home/end keys are
  18. * produced by the extended PS/2 path. */
  19. #define NGUI_KEY_ESC 0x1B
  20. #define NGUI_KEY_BACKSPACE 0x08
  21. #define NGUI_KEY_TAB 0x09
  22. #define NGUI_KEY_LEFT 0x100
  23. #define NGUI_KEY_RIGHT 0x101
  24. #define NGUI_KEY_UP 0x102
  25. #define NGUI_KEY_DOWN 0x103
  26. #define NGUI_KEY_PAGE_UP 0x104
  27. #define NGUI_KEY_PAGE_DOWN 0x105
  28. #define NGUI_KEY_HOME 0x106
  29. #define NGUI_KEY_END 0x107
  30. #define NGUI_KEY_DELETE 0x108
  31. #define NGUI_KEY_CTRL_LEFT 0x109
  32. #define NGUI_KEY_CTRL_RIGHT 0x10A
  33. #define NGUI_KEY_CTRL_DELETE 0x10B
  34. #define NGUI_KEY_CTRL_BACKSPACE 0x10C
  35. #define NGUI_RGB(r, g, b) \
  36. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  37. struct ufont_face;
  38. struct ngui_rect {
  39. int x, y, w, h;
  40. };
  41. struct ngui_theme {
  42. unsigned short bg;
  43. unsigned short panel;
  44. unsigned short panel_alt;
  45. unsigned short border;
  46. unsigned short text;
  47. unsigned short text_dim;
  48. unsigned short accent;
  49. unsigned short selection;
  50. unsigned short button;
  51. unsigned short button_hot;
  52. unsigned short button_down;
  53. unsigned short icon_folder;
  54. unsigned short icon_file;
  55. unsigned short icon_disk;
  56. unsigned short error;
  57. unsigned short success;
  58. unsigned short warning;
  59. };
  60. extern const struct ngui_theme ngui_theme_dark;
  61. /* Icons come from the Adwaita symbolic set (include/icon_abi.h): ngui loads
  62. * /ramfs/icons/adwaita16.icn on first use and tints the 16 px masks with the
  63. * colour passed to ngui_icon(). Both packs carry the bundle; without it
  64. * ngui_icon() draws nothing. */
  65. #define NGUI_ICON_ENUM_(e, name, file) NGUI_ICON_##e,
  66. enum ngui_icon {
  67. ICON_LIST(NGUI_ICON_ENUM_)
  68. NGUI_ICON_COUNT
  69. };
  70. #undef NGUI_ICON_ENUM_
  71. #define NGUI_ICON_PATH "/ramfs/icons/adwaita16.icn"
  72. /* Short display name of an icon ("folder", "back", ...). */
  73. const char *ngui_icon_name(enum ngui_icon icon);
  74. struct ngui_input {
  75. char text[NGUI_INPUT_MAX];
  76. unsigned int len;
  77. unsigned int cursor;
  78. unsigned int scroll; /* first visible character */
  79. unsigned int anchor; /* mouse selection anchor (== cursor: none) */
  80. int dragging; /* a press in the field is extending a selection */
  81. int disabled; /* ignore input and paint the value dimmed */
  82. int secret; /* paint '*' per character (passwords) */
  83. const char *placeholder;
  84. unsigned int last_click_ms; /* previous press, for double-click select-all */
  85. };
  86. struct ngui_scroll {
  87. int offset; /* pixels scrolled from the top */
  88. int content_h;
  89. int view_h;
  90. int dragging;
  91. int drag_dy;
  92. int hide_bar; /* wheel/keys only; draw no scrollbar */
  93. };
  94. struct ngui_slider {
  95. int dragging;
  96. };
  97. /* A flat menu panel: the surface, a body rectangle for the caller's widgets
  98. * and a one-line footer for status text. There is no title bar or close
  99. * button; the app is dismissed by clicking outside it or pressing Esc. Use
  100. * on one chromeless canvas window created with u_win_create_popover(). */
  101. struct ngui_popover {
  102. struct ngui_rect body;
  103. struct ngui_rect footer;
  104. };
  105. /* A two-line list row with optional signal bars and right-aligned text. */
  106. struct ngui_row {
  107. const char *title;
  108. const char *subtitle;
  109. const char *trailing;
  110. int bars; /* -1 none, 0..4 signal strength bars */
  111. int selected;
  112. };
  113. struct ngui {
  114. unsigned short *px;
  115. int w, h;
  116. struct ngui_theme theme;
  117. /* Fonts are owned by ufont.c's heap arena. ngui_font_attach() opens
  118. * them; ngui_font_detach() frees the arena (the browser calls it before
  119. * heap_reset()). NULL means the 8x16 bitmap fallback. */
  120. struct ufont_face *font;
  121. struct ufont_face *font_bold;
  122. struct ufont_face *font_italic;
  123. struct ufont_face *font_bold_italic;
  124. struct ufont_face *font_mono;
  125. struct ufont_face *font_emoji; /* lazy fallback face */
  126. int emoji_loaded;
  127. int font_px;
  128. /* Current pointer state, content-relative. */
  129. int mouse_x, mouse_y;
  130. unsigned int buttons;
  131. int inside;
  132. int focused;
  133. /* Scroll accumulated since the previous frame, in pixels. */
  134. int scroll_x, scroll_y;
  135. /* Press/release edges collected by ngui_begin(). */
  136. unsigned int pressed;
  137. unsigned int released;
  138. int press_x, press_y;
  139. int release_x, release_y;
  140. int clicked;
  141. int focus_claimed;
  142. unsigned int hot;
  143. unsigned int active;
  144. unsigned int focus;
  145. struct ngui_rect clip[4];
  146. int nclip;
  147. int dirty;
  148. unsigned int last_blink;
  149. int caret_on;
  150. int keys[32];
  151. unsigned int key_head, key_tail;
  152. int last_mouse_x, last_mouse_y;
  153. int last_inside, last_focused;
  154. /* Pointer motion is throttled: a redraw is needed for hover feedback,
  155. * but not for every 200 Hz mouse report. A pending motion is flushed
  156. * once 33 ms have passed, so the final hover state still appears. */
  157. unsigned int motion_ms;
  158. int motion_pending;
  159. /* Mouse cursor (enum cursor_shape): widgets set `cursor` while they
  160. * draw a frame (a text field under the pointer asks for the I-beam);
  161. * ngui_begin() resets it to the arrow for every redrawn frame and
  162. * ngui_end_ex() sends it to the kernel when it changed. */
  163. int cursor;
  164. int cursor_sent;
  165. int cursor_frame; /* this frame was redrawn (begin returned 1) */
  166. };
  167. void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h);
  168. void ngui_font_attach(struct ngui *ui); /* opens /ramfs/fonts; no-op if absent */
  169. void ngui_font_detach(struct ngui *ui); /* frees the ufont arena */
  170. int ngui_begin(struct ngui *ui); /* 1 = app should redraw */
  171. void ngui_end(struct ngui *ui);
  172. /* Same bookkeeping, but only publish the canvas when `present` is nonzero.
  173. * Lets an app poll input without copying an unchanged frame every loop. */
  174. void ngui_end_ex(struct ngui *ui, int present);
  175. void ngui_invalidate(struct ngui *ui);
  176. void ngui_set_focus(struct ngui *ui, unsigned int id);
  177. unsigned int ngui_focus(struct ngui *ui);
  178. int ngui_key(struct ngui *ui);
  179. int ngui_key_peek(struct ngui *ui);
  180. /* Layout: cut a rectangle off one side of `r` and return the piece. */
  181. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h);
  182. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h);
  183. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w);
  184. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w);
  185. void ngui_inset(struct ngui_rect *r, int n);
  186. int ngui_rect_contains(const struct ngui_rect *r, int x, int y);
  187. /* Primitives. All drawing is clipped to the current clip stack and canvas. */
  188. void ngui_push_clip(struct ngui *ui, struct ngui_rect r);
  189. void ngui_pop_clip(struct ngui *ui);
  190. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color);
  191. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color);
  192. void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
  193. int tl, int tr, int br, int bl,
  194. unsigned short color);
  195. void ngui_frame_round(struct ngui *ui, struct ngui_rect r,
  196. int tl, int tr, int br, int bl, int thickness,
  197. unsigned short color);
  198. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  199. unsigned short color);
  200. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  201. unsigned short color);
  202. void ngui_text_n(struct ngui *ui, int x, int y, const char *s,
  203. unsigned int n, unsigned short color);
  204. /* UTF-8 variants with the primary face -> emoji fallback stack. */
  205. void ngui_text_utf8(struct ngui *ui, int x, int y, const char *s,
  206. unsigned short color);
  207. void ngui_text_n_utf8(struct ngui *ui, int x, int y, const char *s,
  208. unsigned int n, unsigned short color);
  209. unsigned int ngui_text_width_utf8(struct ngui *ui, const char *s);
  210. void ngui_text_ellipsis_utf8(struct ngui *ui, struct ngui_rect r,
  211. int x, int y, const char *s,
  212. unsigned short color);
  213. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  214. unsigned int n, unsigned short color, int scale, int bold);
  215. /* Browser-oriented style: arbitrary pixel size, optional italic and mono. */
  216. void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
  217. unsigned int n, unsigned short color, int px, int bold,
  218. int italic, int mono);
  219. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  220. unsigned int n, unsigned short color);
  221. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  222. const char *s, unsigned short color);
  223. unsigned int ngui_text_width(const char *s);
  224. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale);
  225. unsigned int ngui_text_width_ui(struct ngui *ui, const char *s);
  226. unsigned int ngui_text_width_px(struct ngui *ui, const char *s, unsigned int n,
  227. int px, int bold, int italic, int mono);
  228. int ngui_text_ascent(struct ngui *ui, int px, int mono);
  229. int ngui_text_line_height(struct ngui *ui, int px, int mono);
  230. /* Face-aware variants: a non-NULL `face` (an @font-face loaded by the
  231. * caller) overrides the built-in regular/bold/italic/mono selection. */
  232. unsigned int ngui_text_width_face(struct ngui *ui, struct ufont_face *face,
  233. const char *s, unsigned int n, int px,
  234. int bold, int italic, int mono);
  235. int ngui_text_ascent_face(struct ngui *ui, struct ufont_face *face, int px,
  236. int mono);
  237. int ngui_text_line_height_face(struct ngui *ui, struct ufont_face *face,
  238. int px, int mono);
  239. void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x,
  240. int y, const char *s, unsigned int n,
  241. unsigned short color, int px, int bold, int italic,
  242. int mono);
  243. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  244. unsigned short color);
  245. /* Widgets. IDs must be stable across frames. */
  246. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  247. const char *label);
  248. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  249. const char *label, const char *shortcut, int checked);
  250. int ngui_menu_item_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
  251. const char *label, const char *shortcut, int checked);
  252. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  253. enum ngui_icon icon);
  254. /* Menu panel component (see struct ngui_popover). */
  255. void ngui_popover_begin(struct ngui *ui, struct ngui_popover *p);
  256. void ngui_popover_footer(struct ngui *ui, const struct ngui_popover *p,
  257. const char *text, unsigned short color);
  258. /* Section caption with an optional right-aligned action button; returns 1
  259. * when the action fired. */
  260. int ngui_section(struct ngui *ui, struct ngui_rect r, const char *caption,
  261. unsigned int action_id, const char *action);
  262. int ngui_list_row(struct ngui *ui, unsigned int id, struct ngui_rect r,
  263. const struct ngui_row *row);
  264. int ngui_list_row_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
  265. const struct ngui_row *row);
  266. void ngui_signal_bars(struct ngui *ui, struct ngui_rect r, int bars,
  267. unsigned short color);
  268. /* A two-line quick-settings tile (rounded, active when `on`). */
  269. int ngui_tile(struct ngui *ui, unsigned int id, struct ngui_rect r,
  270. const char *title, const char *subtitle, int on);
  271. /* Horizontal slider; writes *value and returns 1 when it changed. */
  272. int ngui_slider(struct ngui *ui, unsigned int id, struct ngui_slider *s,
  273. struct ngui_rect r, int *value, int min, int max);
  274. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  275. struct ngui_input *in);
  276. /* Ask for `shape` (enum cursor_shape) while the pointer is in `r` (and in
  277. * the current clip); returns 1 when it is. ngui_set_cursor() sets it
  278. * unconditionally for the current frame. */
  279. int ngui_cursor_rect(struct ngui *ui, struct ngui_rect r, int shape);
  280. void ngui_set_cursor(struct ngui *ui, int shape);
  281. void ngui_input_set(struct ngui_input *in, const char *s);
  282. /* Copy a field's real value out (a secret field's '*' mask is bypassed). */
  283. void ngui_input_value(const struct ngui_input *in, char *out,
  284. unsigned int max);
  285. /* Scrolling pane: call begin, draw the content with scroll->offset, end. */
  286. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  287. struct ngui_scroll *s);
  288. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  289. struct ngui_scroll *s);
  290. /* Apply pending wheel/touchpad scroll to s when the pointer is over r and
  291. * consume it. Call after ngui_scroll_begin() set view_h/content_h. */
  292. int ngui_scroll_wheel(struct ngui *ui, struct ngui_rect r,
  293. struct ngui_scroll *s);
  294. void ngui_scroll_to(struct ngui_scroll *s, int offset);
  295. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h);
  296. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s);
  297. #endif