|
@@ -2290,81 +2290,188 @@ void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
|
|
|
scroll_clamp(s);
|
|
scroll_clamp(s);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+#define SCROLLBAR_W 8
|
|
|
|
|
+#define SCROLLBAR_INSET 2
|
|
|
|
|
+#define SCROLLBAR_HIT_W 14
|
|
|
|
|
+#define SCROLLBAR_STEPS 8
|
|
|
|
|
+#define SCROLLBAR_STEP_MS 14
|
|
|
|
|
+
|
|
|
|
|
+static struct ngui_rect scroll_track_rect(const struct ngui_rect *r)
|
|
|
|
|
+{
|
|
|
|
|
+ struct ngui_rect t;
|
|
|
|
|
+ int inset = SCROLLBAR_INSET;
|
|
|
|
|
+
|
|
|
|
|
+ if (r->w < SCROLLBAR_W + 2 * inset || r->h <= 2 * inset)
|
|
|
|
|
+ inset = 0;
|
|
|
|
|
+ t.x = r->x + r->w - SCROLLBAR_W - inset;
|
|
|
|
|
+ t.y = r->y + inset;
|
|
|
|
|
+ t.w = SCROLLBAR_W;
|
|
|
|
|
+ t.h = r->h - 2 * inset;
|
|
|
|
|
+ return t;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static struct ngui_rect scroll_hit_rect(const struct ngui_rect *r)
|
|
|
|
|
+{
|
|
|
|
|
+ struct ngui_rect h = *r;
|
|
|
|
|
+ int w = SCROLLBAR_HIT_W;
|
|
|
|
|
+
|
|
|
|
|
+ if (w > r->w)
|
|
|
|
|
+ w = r->w;
|
|
|
|
|
+ h.x = r->x + r->w - w;
|
|
|
|
|
+ h.w = w;
|
|
|
|
|
+ return h;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int pointer_in_rect(struct ngui *ui, const struct ngui_rect *r)
|
|
|
|
|
+{
|
|
|
|
|
+ if (!ui->inside || !ngui_rect_contains(r, ui->mouse_x, ui->mouse_y))
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ if (ui->nclip > 0 &&
|
|
|
|
|
+ !ngui_rect_contains(&ui->clip[ui->nclip - 1], ui->mouse_x,
|
|
|
|
|
+ ui->mouse_y))
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ return 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Width of the pill for one animation step. The bar grows into the pane
|
|
|
|
|
+ * from its right edge, so the animation needs no alpha compositing. */
|
|
|
|
|
+static int scroll_bar_w(int vis)
|
|
|
|
|
+{
|
|
|
|
|
+ if (vis <= 0)
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ if (vis >= SCROLLBAR_STEPS)
|
|
|
|
|
+ return SCROLLBAR_W;
|
|
|
|
|
+ return 1 + (vis - 1) * (SCROLLBAR_W - 1) / (SCROLLBAR_STEPS - 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Advance the overlay bar animation. The bar stays hidden while the pointer
|
|
|
|
|
+ * is outside the pane and slides in on hover; a drag pins it open. Setting
|
|
|
|
|
+ * ui->dirty while steps remain makes the app draw the next frame. */
|
|
|
|
|
+static void scroll_bar_update(struct ngui *ui, const struct ngui_rect *r,
|
|
|
|
|
+ struct ngui_scroll *s)
|
|
|
|
|
+{
|
|
|
|
|
+ int hover, target, steps;
|
|
|
|
|
+ unsigned int now, due;
|
|
|
|
|
+
|
|
|
|
|
+ if (s->hide_bar || !scrollable(s)) {
|
|
|
|
|
+ s->dragging = 0;
|
|
|
|
|
+ s->bar_vis = 0;
|
|
|
|
|
+ s->bar_target = 0;
|
|
|
|
|
+ s->bar_hot = 0;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ {
|
|
|
|
|
+ struct ngui_rect hit = scroll_hit_rect(r);
|
|
|
|
|
+
|
|
|
|
|
+ hover = pointer_in_rect(ui, r);
|
|
|
|
|
+ s->bar_hot = hover &&
|
|
|
|
|
+ ngui_rect_contains(&hit, ui->mouse_x, ui->mouse_y);
|
|
|
|
|
+ }
|
|
|
|
|
+ target = (hover || s->dragging) ? SCROLLBAR_STEPS : 0;
|
|
|
|
|
+ now = u_ticks_ms();
|
|
|
|
|
+ if (target != s->bar_target) {
|
|
|
|
|
+ s->bar_target = target;
|
|
|
|
|
+ s->bar_ms = now;
|
|
|
|
|
+ if (s->bar_vis != target)
|
|
|
|
|
+ ui->dirty = 1;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (s->bar_vis == target)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ due = now - s->bar_ms;
|
|
|
|
|
+ steps = (int)(due / SCROLLBAR_STEP_MS);
|
|
|
|
|
+ if (steps > 0) {
|
|
|
|
|
+ if (target > s->bar_vis) {
|
|
|
|
|
+ s->bar_vis += steps;
|
|
|
|
|
+ if (s->bar_vis > target)
|
|
|
|
|
+ s->bar_vis = target;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ s->bar_vis -= steps;
|
|
|
|
|
+ if (s->bar_vis < target)
|
|
|
|
|
+ s->bar_vis = target;
|
|
|
|
|
+ }
|
|
|
|
|
+ s->bar_ms += (unsigned int)steps * SCROLLBAR_STEP_MS;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (s->bar_vis != target)
|
|
|
|
|
+ ui->dirty = 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void scroll_bar_press(struct ngui_scroll *s,
|
|
|
|
|
+ const struct ngui_rect *track, int th, int y)
|
|
|
|
|
+{
|
|
|
|
|
+ int ty = scroll_thumb_y(track, s);
|
|
|
|
|
+
|
|
|
|
|
+ if (y >= ty && y < ty + th) {
|
|
|
|
|
+ s->dragging = 1;
|
|
|
|
|
+ s->drag_dy = y - ty;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ int rel = y - track->y - th / 2;
|
|
|
|
|
+ int avail = track->h - th;
|
|
|
|
|
+ int max = s->content_h - s->view_h;
|
|
|
|
|
+
|
|
|
|
|
+ if (rel < 0)
|
|
|
|
|
+ rel = 0;
|
|
|
|
|
+ if (rel > avail)
|
|
|
|
|
+ rel = avail;
|
|
|
|
|
+ if (avail > 0)
|
|
|
|
|
+ s->offset = rel * max / avail;
|
|
|
|
|
+ s->dragging = 1;
|
|
|
|
|
+ s->drag_dy = th / 2;
|
|
|
|
|
+ scroll_clamp(s);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void scroll_bar_drag(struct ngui_scroll *s,
|
|
|
|
|
+ const struct ngui_rect *track, int th, int y)
|
|
|
|
|
+{
|
|
|
|
|
+ int rel = y - track->y - s->drag_dy;
|
|
|
|
|
+ int avail = track->h - th;
|
|
|
|
|
+ int max = s->content_h - s->view_h;
|
|
|
|
|
+
|
|
|
|
|
+ if (rel < 0)
|
|
|
|
|
+ rel = 0;
|
|
|
|
|
+ if (rel > avail)
|
|
|
|
|
+ rel = avail;
|
|
|
|
|
+ if (avail > 0)
|
|
|
|
|
+ s->offset = rel * max / avail;
|
|
|
|
|
+ scroll_clamp(s);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
|
|
void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
|
|
|
struct ngui_scroll *s)
|
|
struct ngui_scroll *s)
|
|
|
{
|
|
{
|
|
|
- struct ngui_rect bar;
|
|
|
|
|
- int scrolls = scrollable(s);
|
|
|
|
|
|
|
+ struct ngui_rect track, hit;
|
|
|
|
|
+ int th;
|
|
|
|
|
|
|
|
s->view_h = r.h;
|
|
s->view_h = r.h;
|
|
|
scroll_clamp(s);
|
|
scroll_clamp(s);
|
|
|
if (!ui->buttons)
|
|
if (!ui->buttons)
|
|
|
s->dragging = 0;
|
|
s->dragging = 0;
|
|
|
- if (s->hide_bar)
|
|
|
|
|
- s->dragging = 0;
|
|
|
|
|
- if (!s->hide_bar && scrolls) {
|
|
|
|
|
- int th = scroll_thumb_h(&r, s);
|
|
|
|
|
- int ty = scroll_thumb_y(&r, s);
|
|
|
|
|
|
|
+ scroll_bar_update(ui, &r, s);
|
|
|
|
|
|
|
|
- bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
|
|
|
|
|
|
|
+ if (!s->hide_bar && scrollable(s)) {
|
|
|
|
|
+ track = scroll_track_rect(&r);
|
|
|
|
|
+ hit = scroll_hit_rect(&r);
|
|
|
|
|
+ th = scroll_thumb_h(&track, s);
|
|
|
if (ui->pressed &&
|
|
if (ui->pressed &&
|
|
|
- ngui_rect_contains(&bar, ui->press_x, ui->press_y)) {
|
|
|
|
|
- if (ui->press_y >= ty && ui->press_y < ty + th) {
|
|
|
|
|
- s->dragging = 1;
|
|
|
|
|
- s->drag_dy = ui->press_y - ty;
|
|
|
|
|
- } else {
|
|
|
|
|
- int rel = ui->press_y - r.y - th / 2;
|
|
|
|
|
- int avail = r.h - th;
|
|
|
|
|
- int max = s->content_h - s->view_h;
|
|
|
|
|
-
|
|
|
|
|
- if (rel < 0)
|
|
|
|
|
- rel = 0;
|
|
|
|
|
- if (rel > avail)
|
|
|
|
|
- rel = avail;
|
|
|
|
|
- if (avail > 0)
|
|
|
|
|
- s->offset = rel * max / avail;
|
|
|
|
|
- s->dragging = 1;
|
|
|
|
|
- s->drag_dy = th / 2;
|
|
|
|
|
- scroll_clamp(s);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ ngui_rect_contains(&hit, ui->press_x, ui->press_y)) {
|
|
|
|
|
+ scroll_bar_press(s, &track, th, ui->press_y);
|
|
|
|
|
+ /* The bar is on top of the content: a drag must not
|
|
|
|
|
+ * also activate a row or button below it. */
|
|
|
|
|
+ ui->pressed = 0;
|
|
|
|
|
+ ui->clicked = 0;
|
|
|
}
|
|
}
|
|
|
- /* Sampled buttons are authoritative while held: some event paths
|
|
|
|
|
- * deliver the press late or coalesce it with the release. Start
|
|
|
|
|
- * the drag from the current pointer position as well. */
|
|
|
|
|
|
|
+ /* Sampled buttons are authoritative while held: some event
|
|
|
|
|
+ * paths deliver the press late or coalesce it with the
|
|
|
|
|
+ * release. Start the drag from the current pointer position
|
|
|
|
|
+ * as well, but only once the bar is fully visible. */
|
|
|
if (!s->dragging && (ui->buttons & 1) &&
|
|
if (!s->dragging && (ui->buttons & 1) &&
|
|
|
- ngui_rect_contains(&bar, ui->mouse_x, ui->mouse_y)) {
|
|
|
|
|
- if (ui->mouse_y >= ty && ui->mouse_y < ty + th) {
|
|
|
|
|
- s->dragging = 1;
|
|
|
|
|
- s->drag_dy = ui->mouse_y - ty;
|
|
|
|
|
- } else {
|
|
|
|
|
- int rel = ui->mouse_y - r.y - th / 2;
|
|
|
|
|
- int avail = r.h - th;
|
|
|
|
|
- int max = s->content_h - s->view_h;
|
|
|
|
|
-
|
|
|
|
|
- if (rel < 0)
|
|
|
|
|
- rel = 0;
|
|
|
|
|
- if (rel > avail)
|
|
|
|
|
- rel = avail;
|
|
|
|
|
- if (avail > 0)
|
|
|
|
|
- s->offset = rel * max / avail;
|
|
|
|
|
- s->dragging = 1;
|
|
|
|
|
- s->drag_dy = th / 2;
|
|
|
|
|
- scroll_clamp(s);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if (s->dragging && (ui->buttons & 1)) {
|
|
|
|
|
- int rel = ui->mouse_y - r.y - s->drag_dy;
|
|
|
|
|
- int avail = r.h - th;
|
|
|
|
|
- int max = s->content_h - s->view_h;
|
|
|
|
|
-
|
|
|
|
|
- if (rel < 0)
|
|
|
|
|
- rel = 0;
|
|
|
|
|
- if (rel > avail)
|
|
|
|
|
- rel = avail;
|
|
|
|
|
- if (avail > 0)
|
|
|
|
|
- s->offset = rel * max / avail;
|
|
|
|
|
- scroll_clamp(s);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ s->bar_hot && s->bar_vis == SCROLLBAR_STEPS)
|
|
|
|
|
+ scroll_bar_press(s, &track, th, ui->mouse_y);
|
|
|
|
|
+ if (s->dragging && (ui->buttons & 1))
|
|
|
|
|
+ scroll_bar_drag(s, &track, th, ui->mouse_y);
|
|
|
if (ui->released)
|
|
if (ui->released)
|
|
|
s->dragging = 0;
|
|
s->dragging = 0;
|
|
|
}
|
|
}
|
|
@@ -2374,19 +2481,30 @@ void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
|
|
|
void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
|
|
void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
|
|
|
struct ngui_scroll *s)
|
|
struct ngui_scroll *s)
|
|
|
{
|
|
{
|
|
|
|
|
+ struct ngui_rect track, thumb;
|
|
|
|
|
+ int bw, th, ty;
|
|
|
|
|
+ unsigned short color;
|
|
|
|
|
+
|
|
|
ngui_pop_clip(ui);
|
|
ngui_pop_clip(ui);
|
|
|
- if (s->hide_bar || !scrollable(s))
|
|
|
|
|
|
|
+ /* content_h is often known only after drawing, so advance the state
|
|
|
|
|
+ * here as well; a new target requests a follow-up frame. */
|
|
|
|
|
+ scroll_bar_update(ui, &r, s);
|
|
|
|
|
+ if (s->hide_bar || !scrollable(s) || s->bar_vis <= 0)
|
|
|
return;
|
|
return;
|
|
|
- {
|
|
|
|
|
- struct ngui_rect bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
|
|
|
|
|
- int th = scroll_thumb_h(&r, s);
|
|
|
|
|
- int ty = scroll_thumb_y(&r, s);
|
|
|
|
|
- struct ngui_rect thumb = rect_make(bar.x, ty, 14, th);
|
|
|
|
|
|
|
|
|
|
- ngui_fill_rect(ui, bar, ui->theme.panel_alt);
|
|
|
|
|
- ngui_fill_rect(ui, thumb, s->dragging ?
|
|
|
|
|
- ui->theme.accent : ui->theme.text_dim);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ track = scroll_track_rect(&r);
|
|
|
|
|
+ bw = scroll_bar_w(s->bar_vis);
|
|
|
|
|
+ if (bw <= 0)
|
|
|
|
|
+ return;
|
|
|
|
|
+ th = scroll_thumb_h(&track, s);
|
|
|
|
|
+ ty = scroll_thumb_y(&track, s);
|
|
|
|
|
+ thumb = rect_make(r.x + r.w - SCROLLBAR_INSET - bw, ty, bw, th);
|
|
|
|
|
+ color = s->dragging ? ui->theme.accent :
|
|
|
|
|
+ s->bar_hot ? ui->theme.text_dim : ui->theme.border;
|
|
|
|
|
+ ngui_fill_round_rect(ui, thumb, bw / 2, bw / 2, bw / 2, bw / 2,
|
|
|
|
|
+ color);
|
|
|
|
|
+ if (s->bar_hot)
|
|
|
|
|
+ ngui_set_cursor(ui, CURSOR_DEFAULT);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int ngui_scroll_wheel(struct ngui *ui, struct ngui_rect r,
|
|
int ngui_scroll_wheel(struct ngui *ui, struct ngui_rect r,
|