Changeset 96c6a00 in mainline for uspace/lib/ui/src


Ignore:
Timestamp:
2022-03-10T13:44:10Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c88d7f99
Parents:
ca2680d
git-author:
Jiri Svoboda <jiri@…> (2022-03-09 18:44:00)
git-committer:
Jiri Svoboda <jiri@…> (2022-03-10 13:44:10)
Message:

Menu accelerators

Open a menu by pressing F10, then accelerator key (e.g. 'F').

Location:
uspace/lib/ui/src
Files:
1 added
3 edited

Legend:

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

    rca2680d r96c6a00  
    4343#include <stdlib.h>
    4444#include <str.h>
     45#include <uchar.h>
     46#include <ui/accel.h>
    4547#include <ui/control.h>
    4648#include <ui/paint.h>
     
    251253        ui_menu_get_geom(menu, spos, &geom);
    252254        *rect = geom.outer_rect;
     255}
     256
     257/** Get menu accelerator character.
     258 *
     259 * @param menu Menu
     260 * @return Accelerator character (lowercase) or the null character if
     261 *         the menu has no accelerator.
     262 */
     263char32_t ui_menu_get_accel(ui_menu_t *menu)
     264{
     265        return ui_accel_get(menu->caption);
    253266}
    254267
  • uspace/lib/ui/src/menubar.c

    rca2680d r96c6a00  
    252252        old_menu = mbar->selected;
    253253
    254         if (mbar->selected != menu)
    255                 mbar->selected = menu;
    256         else
    257                 mbar->selected = NULL;
     254        mbar->selected = menu;
    258255
    259256        /* Close previously open menu */
     
    333330{
    334331        gfx_rect_t rect;
     332        ui_menu_t *menu;
     333        char32_t maccel;
    335334
    336335        if (event->key == KC_F10) {
     
    361360
    362361                return ui_claimed;
     362        }
     363
     364        if (event->c != '\0' && !ui_menu_is_open(mbar->selected)) {
     365                /* Check if it is an accelerator. */
     366
     367                menu = ui_menu_first(mbar);
     368                while (menu != NULL) {
     369                        maccel = ui_menu_get_accel(menu);
     370                        if (event->c == maccel) {
     371                                ui_menu_bar_select(mbar, menu, true);
     372                                return ui_claimed;
     373                        }
     374
     375                        menu = ui_menu_next(menu);
     376                }
    363377        }
    364378
     
    428442                    gfx_pix_inside_rect(&ppos, &rect)) {
    429443                        mbar->active = true;
    430                         ui_menu_bar_select(mbar, menu, true);
     444
     445                        /* Open the menu, close if already open. */
     446                        if (menu == mbar->selected)
     447                                ui_menu_bar_select(mbar, NULL, false);
     448                        else
     449                                ui_menu_bar_select(mbar, menu, true);
     450
    431451                        return ui_claimed;
    432452                }
  • uspace/lib/ui/src/paint.c

    rca2680d r96c6a00  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2022 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3939#include <gfx/render.h>
    4040#include <gfx/text.h>
     41#include <ui/accel.h>
    4142#include <ui/paint.h>
    4243#include <stdlib.h>
     
    566567}
    567568
    568 /** Process text with '~' markup.
    569  *
    570  * Parse text with tilde markup into a list of strings. @a *rbuf is set
    571  * to point to a newly allocated buffer containing consecutive null-terminated
    572  * strings.
    573  *
    574  * Each part between two '~' becomes one string. '~~' is translated into
    575  * a literal '~' character. @a *endptr is set to point to the first character
    576  * beyond the end of the list.
    577  *
    578  * @param str String with tilde markup
    579  * @param rbuf Place to store pointer to newly allocated buffer.
    580  * @param endptr Place to store end pointer (just after last character)
    581  * @return EOK on success or an error code
    582  */
    583 static int ui_text_process(const char *str, char **rbuf, char **endptr)
    584 {
    585         const char *sp;
    586         char *dp;
    587         char *buf;
    588 
    589         buf = malloc(str_size(str) + 1);
    590         if (buf == NULL)
    591                 return ENOMEM;
    592 
    593         /* Break down string into list of (non)highlighted parts */
    594         sp = str;
    595         dp = buf;
    596         while (*sp != '\0') {
    597                 if (*sp == '~') {
    598                         if (sp[1] == '~') {
    599                                 sp += 2;
    600                                 *dp++ = '~';
    601                         } else {
    602                                 ++sp;
    603                                 *dp++ = '\0';
    604                         }
    605                 } else {
    606                         *dp++ = *sp++;
    607                 }
    608         }
    609 
    610         *dp++ = '\0';
    611         *rbuf = buf;
    612         *endptr = dp;
    613 
    614         return EOK;
    615 }
    616 
    617569/** Compute UI text width.
    618570 *
     
    664616
    665617        /* Break down string into list of (non)highlighted parts */
    666         rc = ui_text_process(str, &buf, &endptr);
     618        rc = ui_accel_process(str, &buf, &endptr);
    667619        if (rc != EOK)
    668620                return rc;
Note: See TracChangeset for help on using the changeset viewer.