Changeset 8ef48ece in mainline for uspace/lib/ui/src/pbutton.c


Ignore:
Timestamp:
2020-10-15T22:12:22Z (5 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
de9992c
Parents:
faca61b8
Message:

Generating button activation event

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/src/pbutton.c

    rfaca61b8 r8ef48ece  
    9191}
    9292
     93/** Set push button callbacks.
     94 *
     95 * @param pbutton Push button
     96 * @param cb Push button callbacks
     97 * @param arg Callback argument
     98 */
     99void ui_pbutton_set_cb(ui_pbutton_t *pbutton, ui_pbutton_cb_t *cb, void *arg)
     100{
     101        pbutton->cb = cb;
     102        pbutton->arg = arg;
     103}
     104
    93105/** Set button rectangle.
    94106 *
     
    314326        gfx_rect_t rect;
    315327        gfx_coord_t thickness;
     328        bool depressed;
    316329        errno_t rc;
    317330
    318331        thickness = pbutton->isdefault ? 2 : 1;
     332        depressed = pbutton->held && pbutton->inside;
    319333
    320334        rect.p0.x = pbutton->rect.p0.x + thickness;
     
    350364        pos.y = (rect.p0.y + rect.p1.y) / 2;
    351365
    352         if (pbutton->held) {
     366        if (depressed) {
    353367                pos.x += ui_pb_press_dx;
    354368                pos.y += ui_pb_press_dy;
     
    370384                goto error;
    371385
    372         if (pbutton->held) {
     386        if (depressed) {
    373387                rc = ui_pbutton_paint_inset(pbutton, &rect);
    374388                if (rc != EOK)
     
    389403/** Press down button.
    390404 *
    391  * This does not automatically repaint the button.
    392  *
    393405 * @param pbutton Push button
    394406 */
    395407void ui_pbutton_press(ui_pbutton_t *pbutton)
    396408{
     409        if (pbutton->held)
     410                return;
     411
     412        pbutton->inside = true;
    397413        pbutton->held = true;
     414        (void) ui_pbutton_paint(pbutton);
    398415}
    399416
    400417/** Release button.
    401418 *
    402  * This does not automatically repaint the button.
    403  *
    404419 * @param pbutton Push button
    405420 */
    406421void ui_pbutton_release(ui_pbutton_t *pbutton)
    407422{
     423        if (!pbutton->held)
     424                return;
     425
    408426        pbutton->held = false;
     427
     428        if (pbutton->inside) {
     429                (void) ui_pbutton_paint(pbutton);
     430                ui_pbutton_clicked(pbutton);
     431        }
     432}
     433
     434/** Pointer entered button.
     435 *
     436 * @param pbutton Push button
     437 */
     438void ui_pbutton_enter(ui_pbutton_t *pbutton)
     439{
     440        if (pbutton->inside)
     441                return;
     442
     443        pbutton->inside = true;
     444        if (pbutton->held)
     445                (void) ui_pbutton_paint(pbutton);
     446}
     447
     448/** Pointer left button.
     449 *
     450 * @param pbutton Push button
     451 */
     452void ui_pbutton_leave(ui_pbutton_t *pbutton)
     453{
     454        if (!pbutton->inside)
     455                return;
     456
     457        pbutton->inside = false;
     458        if (pbutton->held)
     459                (void) ui_pbutton_paint(pbutton);
     460}
     461
     462/** Button was clicked.
     463 *
     464 * @param pbutton Push button
     465 */
     466void ui_pbutton_clicked(ui_pbutton_t *pbutton)
     467{
     468        if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
     469                pbutton->cb->clicked(pbutton, pbutton->arg);
    409470}
    410471
     
    417478{
    418479        gfx_coord2_t pos;
     480        bool inside;
    419481
    420482        pos.x = event->hpos;
    421483        pos.y = event->vpos;
    422484
    423         if (gfx_pix_inside_rect(&pos, &pbutton->rect)) {
    424                 if (event->type == POS_PRESS) {
     485        inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
     486
     487        switch (event->type) {
     488        case POS_PRESS:
     489                if (inside)
    425490                        ui_pbutton_press(pbutton);
    426                         (void) ui_pbutton_paint(pbutton);
     491                break;
     492        case POS_RELEASE:
     493                ui_pbutton_release(pbutton);
     494                break;
     495        case POS_UPDATE:
     496                if (inside && !pbutton->inside) {
     497                        ui_pbutton_enter(pbutton);
     498                } else if (!inside && pbutton->inside) {
     499                        ui_pbutton_leave(pbutton);
    427500                }
    428         }
    429 
    430         if (event->type == POS_RELEASE && pbutton->held) {
    431                 ui_pbutton_release(pbutton);
    432                 (void) ui_pbutton_paint(pbutton);
     501                break;
    433502        }
    434503}
Note: See TracChangeset for help on using the changeset viewer.