ngui.h 12 KB

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