summaryrefslogtreecommitdiff
path: root/src/ui/scrollwidget.c
blob: b6f73b6c26ce0533ac6be86ea104e2fa9c8875ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* Copyright 2020 Jaakko Keränen <jaakko.keranen@iki.fi>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

#include "scrollwidget.h"
#include "paint.h"
#include "util.h"
#include "periodic.h"
#include "app.h"

#include <SDL_timer.h>

iDefineObjectConstruction(ScrollWidget)

static float minOpacity_(void) {
#if !defined (iPlatformApple)
    if (deviceType_App() == desktop_AppDeviceType) {
        /* Don't fade the scrollbars completely. */
        return 0.333f;
    }
#endif
    return 0.0f;
}

struct Impl_ScrollWidget {
    iWidget widget;
    iRangei range;
    int thumb;
    int thumbSize;
    iClick click;
    int startThumb;
    iAnim opacity;
    iBool fadeEnabled;
    uint32_t fadeStart;
    iBool willCheckFade;
};

static void updateMetrics_ScrollWidget_(iScrollWidget *d) {
    iWidget *w = as_Widget(d);
    w->rect.size.x = gap_UI * 3;
}

static void animateOpacity_ScrollWidget_(void *ptr) {
    iScrollWidget *d = ptr;
    if (!isFinished_Anim(&d->opacity)) {
        addTicker_App(animateOpacity_ScrollWidget_, ptr);
    }
    refresh_Widget(ptr);
}

void init_ScrollWidget(iScrollWidget *d) {
    iWidget *w = as_Widget(d);
    init_Widget(w);
    setId_Widget(w, "scroll");
    setFlags_Widget(w,
                    fixedWidth_WidgetFlag | resizeToParentHeight_WidgetFlag |
                        moveToParentRightEdge_WidgetFlag | touchDrag_WidgetFlag,
                    iTrue);
    updateMetrics_ScrollWidget_(d);
    init_Click(&d->click, d, SDL_BUTTON_LEFT);
    init_Anim(&d->opacity, minOpacity_());
    d->willCheckFade = iFalse;
    d->fadeEnabled = iTrue;
}

void deinit_ScrollWidget(iScrollWidget *d) {
    remove_Periodic(periodic_App(), d);
    removeTicker_App(animateOpacity_ScrollWidget_, d);
}

static int thumbSize_ScrollWidget_(const iScrollWidget *d) {
    return iMax(gap_UI * 6, d->thumbSize);
}

static iRect bounds_ScrollWidget_(const iScrollWidget *d) {
    const iWidget *w = constAs_Widget(d);
    iRect bounds = bounds_Widget(w);
    if (deviceType_App() == phone_AppDeviceType && isPortrait_App()) {
        /* Account for the hidable toolbar. */
        int toolbarHeight = lineHeight_Text(uiLabelLarge_FontId) + 3 * gap_UI;
        int excess = bottom_Rect(bounds) - (bottom_Rect(safeRect_Root(w->root)) - toolbarHeight);
        if (excess > 0) {
            adjustEdges_Rect(&bounds, 0, 0, -excess, 0);
        }
    }
    return bounds;
}

static iRect thumbRect_ScrollWidget_(const iScrollWidget *d) {
    const iRect bounds = bounds_ScrollWidget_(d);
    iRect rect = init_Rect(bounds.pos.x, bounds.pos.y, bounds.size.x, 0);
    const int total = size_Range(&d->range);
    if (total > 0) {
        const int tsize = thumbSize_ScrollWidget_(d);
//        iAssert(tsize <= height_Rect(bounds));
        const int tpos =
            iClamp((float) d->thumb / (float) total, 0, 1) * (height_Rect(bounds) - tsize);
        rect.pos.y  = bounds.pos.y + tpos;
        rect.size.y = tsize;
    }
    return rect;
}

static void unfade_ScrollWidget_(iScrollWidget *d, float opacity) {
    d->fadeStart = SDL_GetTicks() + 1000;
    if (targetValue_Anim(&d->opacity) < opacity) {
        setValue_Anim(&d->opacity, opacity, 66);
        addTickerRoot_App(animateOpacity_ScrollWidget_, as_Widget(d)->root, d);
    }
    if (!d->willCheckFade && d->fadeEnabled) {
        d->willCheckFade = iTrue;
        /* TODO: This causes an inexplicable refresh issue on macOS: the drawing of one frame
           takes 100ms for some reason (not the current frame but some time after). */
        add_Periodic(periodic_App(), d, "scrollbar.fade");
    }
    refresh_Widget(d);
}

static void checkVisible_ScrollWidget_(iScrollWidget *d) {
    const iBool wasHidden = !isVisible_Widget(d);
    const iBool isHidden = d->thumbSize != 0 ? height_Rect(thumbRect_ScrollWidget_(d)) == 0 : iTrue;
    setFlags_Widget(as_Widget(d), hidden_WidgetFlag, isHidden);
    if (wasHidden && !isHidden) {
        unfade_ScrollWidget_(d, 1.0f);
    }
}

void setRange_ScrollWidget(iScrollWidget *d, iRangei range) {
    range.end = iMax(range.start, range.end);
    d->range  = range;
    checkVisible_ScrollWidget_(d);
}

void setThumb_ScrollWidget(iScrollWidget *d, int thumb, int thumbSize) {
    const int oldThumb = d->thumb;
    d->thumb     = thumb;
    d->thumbSize = thumbSize;
    checkVisible_ScrollWidget_(d);
    if (oldThumb != d->thumb && thumbSize) {
        unfade_ScrollWidget_(d, 1.0f);
    }
}

void setFadeEnabled_ScrollWidget(iScrollWidget *d, iBool fadeEnabled) {
    d->fadeEnabled = fadeEnabled;
    unfade_ScrollWidget_(d, 1.0f);
}

static iBool processEvent_ScrollWidget_(iScrollWidget *d, const SDL_Event *ev) {
    iWidget *w = as_Widget(d);
    if (isMetricsChange_UserEvent(ev)) {
        updateMetrics_ScrollWidget_(d);
    }
    if (ev->type == SDL_MOUSEMOTION) {
        const iInt2 mouse = init_I2(ev->motion.x, ev->motion.y);
        const iBool isNearby = containsExpanded_Widget(&d->widget, mouse, 4 * gap_UI);
        const iBool isOver = isNearby && contains_Rect(thumbRect_ScrollWidget_(d), mouse);
        if (isNearby) {
            unfade_ScrollWidget_(d, isOver ? 1.0f : 0.4f);
        }
    }
    if (isCommand_UserEvent(ev, "scrollbar.fade")) {
        if (d->fadeEnabled && d->willCheckFade && SDL_GetTicks() > d->fadeStart) {
            setValue_Anim(&d->opacity, minOpacity_(), 200);
            remove_Periodic(periodic_App(), d);
            d->willCheckFade = iFalse;
            if (!isFinished_Anim(&d->opacity)) {
                addTicker_App(animateOpacity_ScrollWidget_, d);
            }
        }
        return iFalse;
    }
    switch (processEvent_Click(&d->click, ev)) {
        case started_ClickResult:
            setFlags_Widget(w, pressed_WidgetFlag, iTrue);
            d->startThumb = d->thumb;
            refresh_Widget(w);
            return iTrue;
        case drag_ClickResult: {
            const iRect bounds = bounds_ScrollWidget_(d);
            const int offset = delta_Click(&d->click).y;
            const int total = size_Range(&d->range);
            int dpos = (float) offset / (float) (height_Rect(bounds) - thumbSize_ScrollWidget_(d)) * total;
            d->thumb = iClamp(d->startThumb + dpos, d->range.start, d->range.end);
            postCommand_Widget(w, "scroll.moved arg:%d", d->thumb);
            refresh_Widget(w);
            return iTrue;
        }
        case finished_ClickResult:
        case aborted_ClickResult:
            if (!isMoved_Click(&d->click)) {
                /* Page up/down. */
                const iRect tr = thumbRect_ScrollWidget_(d);
                const int y = pos_Click(&d->click).y;
                int pgDir = 0;
                if (y < top_Rect(tr)) {
                    pgDir = -1;
                }
                else if (y > bottom_Rect(tr)) {
                    pgDir = +1;
                }
                if (pgDir) {
                    postCommand_Widget(w, "scroll.page arg:%d", pgDir);
                }
            }
            setFlags_Widget(w, pressed_WidgetFlag, iFalse);
            refresh_Widget(w);
            return iTrue;
        default:
            break;
    }
    return processEvent_Widget(w, ev);
}

static void draw_ScrollWidget_(const iScrollWidget *d) {
    const iWidget *w         = constAs_Widget(d);
    const iRect    bounds    = bounds_ScrollWidget_(d);
    const iBool    isPressed = (flags_Widget(w) & pressed_WidgetFlag) != 0;
    if (bounds.size.x > 0) {
        iPaint p;
        init_Paint(&p);
        /* Blend if opacity is not at maximum. */
        p.alpha = 255 * value_Anim(&d->opacity);
        SDL_Renderer *render = renderer_Window(get_Window());
        if (p.alpha < 255) {
            SDL_SetRenderDrawBlendMode(render, SDL_BLENDMODE_BLEND);
        }
        const iRect thumbRect = shrunk_Rect(
            thumbRect_ScrollWidget_(d), init_I2(isPressed ? gap_UI : (gap_UI * 4 / 3), gap_UI / 2));
        fillRect_Paint(&p, thumbRect, isPressed ? uiBackgroundPressed_ColorId : tmQuote_ColorId);
        if (p.alpha < 255) {
            SDL_SetRenderDrawBlendMode(render, SDL_BLENDMODE_NONE);
        }
    }
}

iBeginDefineSubclass(ScrollWidget, Widget)
    .processEvent = (iAny *) processEvent_ScrollWidget_,
    .draw         = (iAny *) draw_ScrollWidget_,
iEndDefineSubclass(ScrollWidget)