/* ngui: tiny immediate-mode GUI toolkit for ring-3 canvas windows. * * The toolkit owns no memory: the app passes its framebuffer and keeps the * state structs for text fields and scroll panes. Everything is RGB565 and * ASCII, using the kernel's 8x16 font. See ucmd_files.c for a user. */ #ifndef NGUI_H #define NGUI_H #include "syscall.h" #include "wm_abi.h" #include "icon_abi.h" #define NGUI_INPUT_MAX 512 #define NGUI_MOTION_REDRAW_MS 33 /* Corner radius shared by cards, tiles, list rows and dialogs. */ #define NGUI_RADIUS 6 #define NGUI_INPUT_CHANGED 1 #define NGUI_INPUT_SUBMIT 2 #define NGUI_INPUT_CANCEL 4 #define NGUI_INPUT_NEXT 8 /* Tab: move to the next field (with CANCEL) */ /* Key codes from io.c. Arrows are 0x100..; the page/home/end keys are * produced by the extended PS/2 path. */ #define NGUI_KEY_ESC 0x1B #define NGUI_KEY_BACKSPACE 0x08 #define NGUI_KEY_TAB 0x09 #define NGUI_KEY_LEFT 0x100 #define NGUI_KEY_RIGHT 0x101 #define NGUI_KEY_UP 0x102 #define NGUI_KEY_DOWN 0x103 #define NGUI_KEY_PAGE_UP 0x104 #define NGUI_KEY_PAGE_DOWN 0x105 #define NGUI_KEY_HOME 0x106 #define NGUI_KEY_END 0x107 #define NGUI_KEY_DELETE 0x108 #define NGUI_KEY_CTRL_LEFT 0x109 #define NGUI_KEY_CTRL_RIGHT 0x10A #define NGUI_KEY_CTRL_DELETE 0x10B #define NGUI_KEY_CTRL_BACKSPACE 0x10C #define NGUI_RGB(r, g, b) \ ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))) struct ufont_face; struct ngui_rect { int x, y, w, h; }; struct ngui_theme { unsigned short bg; unsigned short panel; unsigned short panel_alt; unsigned short border; unsigned short text; unsigned short text_dim; unsigned short accent; unsigned short selection; unsigned short button; unsigned short button_hot; unsigned short button_down; unsigned short icon_folder; unsigned short icon_file; unsigned short icon_disk; unsigned short error; unsigned short success; unsigned short warning; }; extern const struct ngui_theme ngui_theme_dark; /* Icons come from the Adwaita symbolic set (include/icon_abi.h): ngui loads * /ramfs/icons/adwaita16.icn on first use and tints the 16 px masks with the * colour passed to ngui_icon(). Both packs carry the bundle; without it * ngui_icon() draws nothing. */ #define NGUI_ICON_ENUM_(e, name, file) NGUI_ICON_##e, enum ngui_icon { ICON_LIST(NGUI_ICON_ENUM_) NGUI_ICON_COUNT }; #undef NGUI_ICON_ENUM_ #define NGUI_ICON_PATH ICON_BUNDLE_PATH /* Short display name of an icon ("folder", "back", ...). */ const char *ngui_icon_name(enum ngui_icon icon); struct ngui_input { char text[NGUI_INPUT_MAX]; unsigned int len; unsigned int cursor; 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 */ int secret; /* paint '*' per character (passwords) */ const char *placeholder; unsigned int last_click_ms; /* previous press, for double-click select-all */ }; struct ngui_scroll { int offset; /* pixels scrolled from the top */ int content_h; int view_h; int dragging; int drag_dy; int hide_bar; /* wheel/keys only; draw no scrollbar */ /* The bar is an overlay: it reserves no layout width, stays hidden * while the pointer is outside the pane and slides in on hover. */ int bar_vis; /* 0 hidden .. animation target shown */ int bar_target; /* requested visibility (0 or full) */ int bar_hot; /* pointer is over the scrollbar strip */ unsigned int bar_ms; /* last animation step (u_ticks_ms) */ }; struct ngui_slider { int dragging; }; /* A flat menu panel: the surface, a body rectangle for the caller's widgets * and a one-line footer for status text. There is no title bar or close * button; the app is dismissed by clicking outside it or pressing Esc. Use * on one chromeless canvas window created with u_win_create_popover(). */ struct ngui_popover { struct ngui_rect body; struct ngui_rect footer; }; /* A two-line list row with optional signal bars and right-aligned text. */ struct ngui_row { const char *title; const char *subtitle; const char *trailing; int bars; /* -1 none, 0..4 signal strength bars */ int selected; }; struct ngui { unsigned short *px; int w, h; struct ngui_theme theme; /* Fonts are owned by ufont.c's heap arena. ngui_font_attach() opens * them; ngui_font_detach() frees the arena (the browser calls it before * heap_reset()). NULL means the 8x16 bitmap fallback. */ struct ufont_face *font; struct ufont_face *font_bold; struct ufont_face *font_italic; struct ufont_face *font_bold_italic; struct ufont_face *font_mono; struct ufont_face *font_emoji; /* lazy fallback face */ int emoji_loaded; int font_px; /* Current pointer state, content-relative. */ int mouse_x, mouse_y; unsigned int buttons; int inside; int focused; /* Scroll accumulated since the previous frame, in pixels. */ int scroll_x, scroll_y; /* Press/release edges collected by ngui_begin(). `pressed` and * `released` are the left button; the right-button edges let an app * open a context menu without taking the normal click path. */ unsigned int pressed; unsigned int released; unsigned int right_pressed; unsigned int right_released; int press_x, press_y; int release_x, release_y; int right_press_x, right_press_y; int right_release_x, right_release_y; int clicked; int focus_claimed; unsigned int hot; unsigned int active; unsigned int focus; struct ngui_rect clip[4]; int nclip; int dirty; unsigned int last_blink; int caret_on; int keys[32]; unsigned int key_head, key_tail; int last_mouse_x, last_mouse_y; int last_inside, last_focused; /* Pointer motion is throttled: a redraw is needed for hover feedback, * but not for every 200 Hz mouse report. A pending motion is flushed * once 33 ms have passed, so the final hover state still appears. */ unsigned int motion_ms; int motion_pending; /* Mouse cursor (enum cursor_shape): widgets set `cursor` while they * draw a frame (a text field under the pointer asks for the I-beam); * ngui_begin() resets it to the arrow for every redrawn frame and * ngui_end_ex() sends it to the kernel when it changed. */ int cursor; int cursor_sent; int cursor_frame; /* this frame was redrawn (begin returned 1) */ }; void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h); void ngui_font_attach(struct ngui *ui); /* opens /ramfs/fonts; no-op if absent */ void ngui_font_detach(struct ngui *ui); /* frees the ufont arena */ int ngui_begin(struct ngui *ui); /* 1 = app should redraw */ void ngui_end(struct ngui *ui); /* Same bookkeeping, but only publish the canvas when `present` is nonzero. * Lets an app poll input without copying an unchanged frame every loop. */ void ngui_end_ex(struct ngui *ui, int present); void ngui_invalidate(struct ngui *ui); void ngui_set_focus(struct ngui *ui, unsigned int id); unsigned int ngui_focus(struct ngui *ui); int ngui_key(struct ngui *ui); int ngui_key_peek(struct ngui *ui); /* Layout: cut a rectangle off one side of `r` and return the piece. */ struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h); struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h); struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w); struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w); void ngui_inset(struct ngui_rect *r, int n); int ngui_rect_contains(const struct ngui_rect *r, int x, int y); /* Primitives. All drawing is clipped to the current clip stack and canvas. */ void ngui_push_clip(struct ngui *ui, struct ngui_rect r); void ngui_pop_clip(struct ngui *ui); void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color); void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color); void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r, int tl, int tr, int br, int bl, unsigned short color); void ngui_frame_round(struct ngui *ui, struct ngui_rect r, int tl, int tr, int br, int bl, int thickness, unsigned short color); void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1, unsigned short color); void ngui_text(struct ngui *ui, int x, int y, const char *s, unsigned short color); void ngui_text_n(struct ngui *ui, int x, int y, const char *s, unsigned int n, unsigned short color); /* UTF-8 variants with the primary face -> emoji fallback stack. */ void ngui_text_utf8(struct ngui *ui, int x, int y, const char *s, unsigned short color); void ngui_text_n_utf8(struct ngui *ui, int x, int y, const char *s, unsigned int n, unsigned short color); unsigned int ngui_text_width_utf8(struct ngui *ui, const char *s); void ngui_text_ellipsis_utf8(struct ngui *ui, struct ngui_rect r, int x, int y, const char *s, unsigned short color); void ngui_text_styled(struct ngui *ui, int x, int y, const char *s, unsigned int n, unsigned short color, int scale, int bold); /* Browser-oriented style: arbitrary pixel size, optional italic and mono. */ void ngui_text_px(struct ngui *ui, int x, int y, const char *s, unsigned int n, unsigned short color, int px, int bold, int italic, int mono); void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s, unsigned int n, unsigned short color); void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y, const char *s, unsigned short color); unsigned int ngui_text_width(const char *s); unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale); unsigned int ngui_text_width_ui(struct ngui *ui, const char *s); unsigned int ngui_text_width_px(struct ngui *ui, const char *s, unsigned int n, int px, int bold, int italic, int mono); int ngui_text_ascent(struct ngui *ui, int px, int mono); int ngui_text_line_height(struct ngui *ui, int px, int mono); /* Face-aware variants: a non-NULL `face` (an @font-face loaded by the * caller) overrides the built-in regular/bold/italic/mono selection. */ unsigned int ngui_text_width_face(struct ngui *ui, struct ufont_face *face, const char *s, unsigned int n, int px, int bold, int italic, int mono); int ngui_text_ascent_face(struct ngui *ui, struct ufont_face *face, int px, int mono); int ngui_text_line_height_face(struct ngui *ui, struct ufont_face *face, int px, int mono); void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x, int y, const char *s, unsigned int n, unsigned short color, int px, int bold, int italic, int mono); void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon, unsigned short color); /* Widgets. IDs must be stable across frames. */ int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r, const char *label); int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r, const char *label, const char *shortcut, int checked); int ngui_menu_item_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r, const char *label, const char *shortcut, int checked); int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r, enum ngui_icon icon); /* Menu panel component (see struct ngui_popover). */ void ngui_popover_begin(struct ngui *ui, struct ngui_popover *p); void ngui_popover_footer(struct ngui *ui, const struct ngui_popover *p, const char *text, unsigned short color); /* Section caption with an optional right-aligned action button; returns 1 * when the action fired. */ int ngui_section(struct ngui *ui, struct ngui_rect r, const char *caption, unsigned int action_id, const char *action); int ngui_list_row(struct ngui *ui, unsigned int id, struct ngui_rect r, const struct ngui_row *row); int ngui_list_row_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r, const struct ngui_row *row); void ngui_signal_bars(struct ngui *ui, struct ngui_rect r, int bars, unsigned short color); /* A two-line quick-settings tile (rounded, active when `on`). */ int ngui_tile(struct ngui *ui, unsigned int id, struct ngui_rect r, const char *title, const char *subtitle, int on); /* Horizontal slider; writes *value and returns 1 when it changed. */ int ngui_slider(struct ngui *ui, unsigned int id, struct ngui_slider *s, struct ngui_rect r, int *value, int min, int max); int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r, struct ngui_input *in); /* Ask for `shape` (enum cursor_shape) while the pointer is in `r` (and in * the current clip); returns 1 when it is. ngui_set_cursor() sets it * unconditionally for the current frame. */ int ngui_cursor_rect(struct ngui *ui, struct ngui_rect r, int shape); void ngui_set_cursor(struct ngui *ui, int shape); void ngui_input_set(struct ngui_input *in, const char *s); /* Copy a field's real value out (a secret field's '*' mask is bypassed). */ void ngui_input_value(const struct ngui_input *in, char *out, unsigned int max); /* Scrolling pane: call begin, draw the content with scroll->offset, end. */ void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r, struct ngui_scroll *s); void ngui_scroll_end(struct ngui *ui, struct ngui_rect r, struct ngui_scroll *s); /* Apply pending wheel/touchpad scroll to s when the pointer is over r and * consume it. Call after ngui_scroll_begin() set view_h/content_h. */ int ngui_scroll_wheel(struct ngui *ui, struct ngui_rect r, struct ngui_scroll *s); void ngui_scroll_to(struct ngui_scroll *s, int offset); void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h); int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s); #endif