|
|
@@ -814,9 +814,19 @@ static void js_host_changed(void)
|
|
|
ngui_invalidate(&ui);
|
|
|
}
|
|
|
|
|
|
+static void js_force_layout(void);
|
|
|
+
|
|
|
static int js_host_rect(unsigned int node, int *x, int *y, int *w, int *h)
|
|
|
{
|
|
|
- if (node >= BR_MAX_NODES || !layout_boxes[node].valid)
|
|
|
+ if (node >= BR_MAX_NODES)
|
|
|
+ return 0;
|
|
|
+ /* A geometry read after a DOM change gets a synchronous layout, like
|
|
|
+ * a browser's forced reflow: Bootstrap's tooltip inserts itself,
|
|
|
+ * reads offsetWidth, then moves itself twice with $.offset(), which
|
|
|
+ * reads its own current position each time. */
|
|
|
+ if (!layout_valid)
|
|
|
+ js_force_layout();
|
|
|
+ if (!layout_boxes[node].valid)
|
|
|
return 0;
|
|
|
*x = layout_boxes[node].x - page_rect.x;
|
|
|
*y = layout_boxes[node].y - page_rect.y;
|
|
|
@@ -9228,6 +9238,9 @@ static void draw_image_block(struct lay *l, unsigned int node, int x, int y,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+static int intrinsic_width(struct ngui *ui, unsigned int node,
|
|
|
+ const struct br_style *pst, int depth, int base);
|
|
|
+
|
|
|
static void layout_block(struct lay *l, unsigned int node,
|
|
|
const struct br_style *st, int depth,
|
|
|
unsigned short link)
|
|
|
@@ -9316,6 +9329,40 @@ static void layout_block(struct lay *l, unsigned int node,
|
|
|
mp_px(st, 0, old_right - old_left) -
|
|
|
mp_px(st, 2, old_right - old_left);
|
|
|
}
|
|
|
+ /* An auto width placed by one horizontal offset is
|
|
|
+ * shrink-to-fit (CSS 2.1 10.3.7): a Bootstrap tooltip or a
|
|
|
+ * dropdown is as wide as its content, not its containing
|
|
|
+ * block. With neither offset the box keeps the containing
|
|
|
+ * width, since `inset: 0` overlays are not parsed and only
|
|
|
+ * stretch that way; an empty measure keeps it too. */
|
|
|
+ if (st->width_kind == 0 && st->left_set != st->right_set) {
|
|
|
+ int cb_l = st->pos == 2 ? page_rect.x : flow_left;
|
|
|
+ int cb_r = st->pos == 2 ? page_rect.x +
|
|
|
+ page_rect.w - 12 : flow_right;
|
|
|
+ int avail = cb_r - cb_l - (st->left_set ? st->left :
|
|
|
+ st->right);
|
|
|
+ int w = intrinsic_width(l->ui, node, st, 0,
|
|
|
+ cb_r - cb_l);
|
|
|
+
|
|
|
+ if (w > 0) {
|
|
|
+ w += mp_px(st, 7, cb_r - cb_l) +
|
|
|
+ mp_px(st, 5, cb_r - cb_l) +
|
|
|
+ mp_px(st, 3, cb_r - cb_l) +
|
|
|
+ mp_px(st, 1, cb_r - cb_l) +
|
|
|
+ st->border_w[1] + st->border_w[3];
|
|
|
+ if (st->max_width_kind == 1 &&
|
|
|
+ w > (int)st->max_width)
|
|
|
+ w = (int)st->max_width;
|
|
|
+ if (avail > 8 && w > avail)
|
|
|
+ w = avail;
|
|
|
+ if (st->right_set) {
|
|
|
+ l->right = cb_r - st->right;
|
|
|
+ l->left = l->right - w;
|
|
|
+ } else {
|
|
|
+ l->right = l->left + w;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
/* `left: 100%` parks a box (Docusaurus' "Skip to main
|
|
|
* content" link) past the containing block's right edge
|
|
|
* until it is focused. An auto width then has no room: it
|
|
|
@@ -9878,7 +9925,11 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
|
|
|
continue;
|
|
|
}
|
|
|
br_style_for(&doc, c, pst, &cs);
|
|
|
- if (cs.display == BR_DISPLAY_NONE)
|
|
|
+ /* Absolute/fixed children take no room in the parent's
|
|
|
+ * shrink-to-fit width: a shown Bootstrap tooltip widened the
|
|
|
+ * floated navbar list and slid the hovered item away. */
|
|
|
+ if (cs.display == BR_DISPLAY_NONE || cs.pos == 1 ||
|
|
|
+ cs.pos == 2)
|
|
|
continue;
|
|
|
if (nd->tag == BR_TAG_BR) {
|
|
|
if (line_w > max_w)
|
|
|
@@ -10043,6 +10094,19 @@ static unsigned int layout_float_group(struct lay *l, unsigned int first,
|
|
|
/* A float blockifies its box: Bootstrap floats inline
|
|
|
* anchors too (the navbar brand and its text links), and
|
|
|
* their padding must be honoured. */
|
|
|
+ /* An absolute/fixed sibling is out of flow and does not end
|
|
|
+ * the row: Bootstrap inserts its tooltip right after the
|
|
|
+ * hovered navbar <li>, and the row used to break there. */
|
|
|
+ if (!cs.float_left && (cs.pos == 1 || cs.pos == 2)) {
|
|
|
+ int sy = l->y, sl = l->left, sr = l->right;
|
|
|
+
|
|
|
+ layout_out_of_flow(l, n, &cs, depth, lk);
|
|
|
+ l->y = sy;
|
|
|
+ l->left = sl;
|
|
|
+ l->right = sr;
|
|
|
+ n = nd->next;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
if (!cs.float_left)
|
|
|
break;
|
|
|
if (nd->tag == BR_TAG_A && nd->href_off)
|
|
|
@@ -12389,11 +12453,16 @@ static void render_page(int left, int right, int y0)
|
|
|
|
|
|
for (i = 0; i < ndeferred; i++) {
|
|
|
struct br_style st;
|
|
|
+ int hide;
|
|
|
|
|
|
br_style_for(&doc, deferred[i].node, &base, &st);
|
|
|
if (st.display == BR_DISPLAY_NONE)
|
|
|
continue;
|
|
|
- l->draw = 1;
|
|
|
+ /* A transparent or visibility:hidden positioned box
|
|
|
+ * still gets its boxes (scripts measure it) but no
|
|
|
+ * paint and no hit rectangles. */
|
|
|
+ hide = st.opacity_hidden || st.vis_hidden;
|
|
|
+ l->draw = !hide;
|
|
|
l->left = deferred[i].flow_left;
|
|
|
l->right = deferred[i].flow_right;
|
|
|
l->y = deferred[i].flow_y;
|
|
|
@@ -12458,7 +12527,7 @@ static void render_page(int left, int right, int y0)
|
|
|
continue;
|
|
|
clip.h = ch;
|
|
|
ngui_push_clip(&ui, clip);
|
|
|
- if (st.has_bg)
|
|
|
+ if (st.has_bg && !hide)
|
|
|
ngui_fill_rect(&ui, clip, st.bg);
|
|
|
layout_block(l, deferred[i].node, &st, 0,
|
|
|
deferred[i].link);
|
|
|
@@ -13052,6 +13121,53 @@ static void draw_page(struct ngui_rect page)
|
|
|
img_stat_paint += u_ticks_ms() - t0;
|
|
|
}
|
|
|
|
|
|
+/* Lay the page out now, into the page back buffer, so scripts read real
|
|
|
+ * boxes for nodes they just inserted. Absolute/fixed boxes only get their
|
|
|
+ * boxes in the painting pass, so this is a full render; the next frame
|
|
|
+ * repaints the screen as usual. Skipped while the page has never been
|
|
|
+ * drawn (no viewport yet) or without the back buffer. */
|
|
|
+static void js_force_layout(void)
|
|
|
+{
|
|
|
+ static int busy;
|
|
|
+ unsigned short *saved_px = ui.px;
|
|
|
+ int saved_w = ui.w, saved_h = ui.h;
|
|
|
+ struct ngui_rect page = page_rect;
|
|
|
+
|
|
|
+ static unsigned int window_t0, spent;
|
|
|
+ unsigned int t0 = u_ticks_ms();
|
|
|
+
|
|
|
+ if (busy || !page_backbuf || pagebuf_w != ui.w ||
|
|
|
+ pagebuf_h != ui.h || page.w < 40 || page.h < 40 ||
|
|
|
+ layout_right <= layout_left)
|
|
|
+ return;
|
|
|
+ /* Budget: a script that alternates writes and reads (an animation
|
|
|
+ * loop) would otherwise re-render the page on every read. Past
|
|
|
+ * 40 ms of forced layouts in a 100 ms window it reads the last
|
|
|
+ * layout, as before. */
|
|
|
+ if (t0 - window_t0 >= 100) {
|
|
|
+ window_t0 = t0;
|
|
|
+ spent = 0;
|
|
|
+ }
|
|
|
+ if (spent >= 40)
|
|
|
+ return;
|
|
|
+ busy = 1;
|
|
|
+ ui.px = page_backbuf;
|
|
|
+ layout_view_h = page.h;
|
|
|
+ layout_content_h = measure_page(layout_left, layout_right);
|
|
|
+ layout_valid = 1;
|
|
|
+ layout_nnode = doc.nnode;
|
|
|
+ ngui_push_clip(&ui, page);
|
|
|
+ render_page(layout_left, layout_right, page.y - page_scroll.offset);
|
|
|
+ ngui_pop_clip(&ui);
|
|
|
+ ui.px = saved_px;
|
|
|
+ ui.w = saved_w;
|
|
|
+ ui.h = saved_h;
|
|
|
+ page_cache_valid = 0;
|
|
|
+ ngui_invalidate(&ui);
|
|
|
+ spent += u_ticks_ms() - t0;
|
|
|
+ busy = 0;
|
|
|
+}
|
|
|
+
|
|
|
static void draw_status(struct ngui_rect r)
|
|
|
{
|
|
|
static char msg[BR_URL_MAX + 32]; /* leaf: off the 128 KB stack */
|
|
|
@@ -13697,6 +13813,25 @@ static int link_fragment_nav(unsigned int node)
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
+/* A positioned box laid out while transparent or visibility:hidden (or
|
|
|
+ * inside one): it has a box for scripts but must not catch the pointer. */
|
|
|
+static int node_invisible(unsigned int n)
|
|
|
+{
|
|
|
+ unsigned int guard = 0;
|
|
|
+
|
|
|
+ while (n < doc.nnode && guard++ < 64) {
|
|
|
+ struct br_style st;
|
|
|
+
|
|
|
+ style_for_node(n, &st);
|
|
|
+ if (st.opacity_hidden || st.vis_hidden)
|
|
|
+ return 1;
|
|
|
+ if (n == 0)
|
|
|
+ break;
|
|
|
+ n = doc.nodes[n].parent;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
/* The innermost laid-out box under a screen point: the smallest valid box
|
|
|
* that contains it (the last one wins a tie, i.e. the later/deeper node).
|
|
|
* Used to deliver clicks to elements that are neither links nor controls
|
|
|
@@ -13714,7 +13849,7 @@ static unsigned int find_box_hit(int x, int y)
|
|
|
y >= b->y + b->h)
|
|
|
continue;
|
|
|
area = (unsigned int)b->w * (unsigned int)b->h;
|
|
|
- if (area <= best_area) {
|
|
|
+ if (area <= best_area && !node_invisible(i)) {
|
|
|
best = i;
|
|
|
best_area = area;
|
|
|
}
|
|
|
@@ -13722,6 +13857,96 @@ static unsigned int find_box_hit(int x, int y)
|
|
|
return best;
|
|
|
}
|
|
|
|
|
|
+/* The element under the pointer for hover events: the innermost laid-out
|
|
|
+ * box, or the link/control hit inside it (an inline <a> has no box of its
|
|
|
+ * own but a hit rectangle). -1 when none. */
|
|
|
+static int hover_node = -1;
|
|
|
+
|
|
|
+static int node_contains(unsigned int a, unsigned int n)
|
|
|
+{
|
|
|
+ unsigned int guard = 0;
|
|
|
+
|
|
|
+ while (n < doc.nnode && guard++ < BR_MAX_NODES) {
|
|
|
+ if (n == a)
|
|
|
+ return 1;
|
|
|
+ if (n == 0)
|
|
|
+ return 0;
|
|
|
+ n = doc.nodes[n].parent;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int hover_target(void)
|
|
|
+{
|
|
|
+ int h, x = ui.mouse_x, y = ui.mouse_y;
|
|
|
+ unsigned int b;
|
|
|
+
|
|
|
+ if (!ui.inside || ui.buttons || sug_visible() ||
|
|
|
+ x < page_rect.x || x >= page_rect.x + page_rect.w - 12 ||
|
|
|
+ y < page_rect.y || y >= page_rect.y + page_rect.h)
|
|
|
+ return -1;
|
|
|
+ h = find_hit(x, y);
|
|
|
+ b = find_box_hit(x, y);
|
|
|
+ if (h && (!b || node_contains(b, (unsigned int)h)))
|
|
|
+ return h;
|
|
|
+ if (b)
|
|
|
+ return (int)b;
|
|
|
+ return h ? h : -1;
|
|
|
+}
|
|
|
+
|
|
|
+/* mouseover/mouseout (bubbling, with relatedTarget) at the old and new
|
|
|
+ * element, and mouseleave/mouseenter on every ancestor that was left or
|
|
|
+ * entered (outermost first for enter), like a browser's boundary events.
|
|
|
+ * jQuery 1.11 builds its mouseenter (Bootstrap tooltips) from mouseover
|
|
|
+ * and relatedTarget. */
|
|
|
+static void page_hover_events(void)
|
|
|
+{
|
|
|
+ int now, old, cx, cy, path[64], np = 0, i;
|
|
|
+ unsigned int n;
|
|
|
+
|
|
|
+ if (!js_browser_active()) {
|
|
|
+ hover_node = -1;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ now = hover_target();
|
|
|
+ if (now >= (int)doc.nnode)
|
|
|
+ now = -1;
|
|
|
+ if (now == hover_node)
|
|
|
+ return;
|
|
|
+ old = hover_node;
|
|
|
+ if (old >= (int)doc.nnode)
|
|
|
+ old = -1;
|
|
|
+ hover_node = now;
|
|
|
+ cx = ui.mouse_x - page_rect.x;
|
|
|
+ cy = ui.mouse_y - page_rect.y;
|
|
|
+ if (old >= 0) {
|
|
|
+ js_browser_mouse_event((unsigned int)old, "mouseout", 1, now,
|
|
|
+ cx, cy);
|
|
|
+ for (n = (unsigned int)old, i = 0;
|
|
|
+ n < doc.nnode && i < 64 &&
|
|
|
+ !(now >= 0 && node_contains(n, (unsigned int)now)); i++) {
|
|
|
+ js_browser_mouse_event(n, "mouseleave", 0, now, cx, cy);
|
|
|
+ if (n == 0)
|
|
|
+ break;
|
|
|
+ n = doc.nodes[n].parent;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (now < 0 || now >= (int)doc.nnode)
|
|
|
+ return;
|
|
|
+ js_browser_mouse_event((unsigned int)now, "mouseover", 1, old, cx, cy);
|
|
|
+ for (n = (unsigned int)now;
|
|
|
+ n < doc.nnode && np < 64 &&
|
|
|
+ !(old >= 0 && node_contains(n, (unsigned int)old));) {
|
|
|
+ path[np++] = (int)n;
|
|
|
+ if (n == 0)
|
|
|
+ break;
|
|
|
+ n = doc.nodes[n].parent;
|
|
|
+ }
|
|
|
+ for (i = np - 1; i >= 0; i--)
|
|
|
+ js_browser_mouse_event((unsigned int)path[i], "mouseenter", 0,
|
|
|
+ old, cx, cy);
|
|
|
+}
|
|
|
+
|
|
|
static void handle_page_click(unsigned int *action)
|
|
|
{
|
|
|
int node;
|
|
|
@@ -13856,6 +14081,7 @@ void _start(const char *arg)
|
|
|
draw(&action);
|
|
|
handle_keys(&action);
|
|
|
handle_page_click(&action);
|
|
|
+ page_hover_events();
|
|
|
ngui_end(&ui);
|
|
|
}
|
|
|
if (action & ACT_NAV) {
|