Changeset 3e6c51f in mainline


Ignore:
Timestamp:
2021-10-13T07:41:33Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Children:
b9d689b
Parents:
af2ea83
git-author:
Jiri Svoboda <jiri@…> (2021-10-12 19:41:17)
git-committer:
Jiri Svoboda <jiri@…> (2021-10-13 07:41:33)
Message:

Page up and page down

Location:
uspace/app/nav
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/nav/panel.c

    raf2ea83 r3e6c51f  
    137137
    138138        gfx_text_fmt_init(&fmt);
    139 
    140         rows = panel->rect.p1.y - panel->rect.p0.y - 2;
     139        rows = panel_page_size(panel);
    141140
    142141        /* Do not display entry outside of current page */
     
    201200                return rc;
    202201
    203         lines = panel->rect.p1.y - panel->rect.p0.y - 2;
     202        lines = panel_page_size(panel);
    204203        i = 0;
    205204
     
    244243                                panel_cursor_bottom(panel);
    245244                                break;
     245                        case KC_PAGE_UP:
     246                                panel_page_up(panel);
     247                                break;
     248                        case KC_PAGE_DOWN:
     249                                panel_page_down(panel);
     250                                break;
    246251                        default:
    247252                                break;
     
    282287{
    283288        panel->rect = *rect;
     289}
     290
     291/** Get panel page size.
     292 *
     293 * @param panel Panel
     294 * @return Number of entries that fit in panel at the same time.
     295 */
     296unsigned panel_page_size(panel_t *panel)
     297{
     298        return panel->rect.p1.y - panel->rect.p0.y - 2;
    284299}
    285300
     
    494509}
    495510
     511/** Move cursor to a new position, possibly scrolling.
     512 *
     513 * @param panel Panel
     514 * @param entry New entry under cursor
     515 * @param entry_idx Index of new entry under cursor
     516 */
    496517void panel_cursor_move(panel_t *panel, panel_entry_t *entry, size_t entry_idx)
    497518{
     
    503524        size_t i;
    504525
    505         rows = panel->rect.p1.y - panel->rect.p0.y - 2;
     526        rows = panel_page_size(panel);
    506527
    507528        old_cursor = panel->cursor;
     
    604625}
    605626
     627/** Move one page up.
     628 *
     629 * @param panel Panel
     630 */
     631void panel_page_up(panel_t *panel)
     632{
     633        gfx_context_t *gc = ui_window_get_gc(panel->window);
     634        panel_entry_t *old_page;
     635        panel_entry_t *old_cursor;
     636        size_t old_idx;
     637        size_t rows;
     638        panel_entry_t *entry;
     639        size_t i;
     640
     641        rows = panel_page_size(panel);
     642
     643        old_page = panel->page;
     644        old_cursor = panel->cursor;
     645        old_idx = panel->cursor_idx;
     646
     647        /* Move page by rows entries up (if possible) */
     648        for (i = 0; i < rows; i++) {
     649                entry = panel_prev(panel->page);
     650                if (entry != NULL) {
     651                        panel->page = entry;
     652                        --panel->page_idx;
     653                }
     654        }
     655
     656        /* Move cursor by rows entries up (if possible) */
     657
     658        for (i = 0; i < rows; i++) {
     659                entry = panel_prev(panel->cursor);
     660                if (entry != NULL) {
     661                        panel->cursor = entry;
     662                        --panel->cursor_idx;
     663                }
     664        }
     665
     666        if (panel->page != old_page) {
     667                /* We have scrolled. Need to repaint all entries */
     668                (void) panel_paint(panel);
     669        } else if (panel->cursor != old_cursor) {
     670                /* No scrolling, but cursor has moved */
     671                panel_entry_paint(old_cursor, old_idx);
     672                panel_entry_paint(panel->cursor, panel->cursor_idx);
     673
     674                (void) gfx_update(gc);
     675        }
     676}
     677
     678/** Move one page down.
     679 *
     680 * @param panel Panel
     681 */
     682void panel_page_down(panel_t *panel)
     683{
     684        gfx_context_t *gc = ui_window_get_gc(panel->window);
     685        panel_entry_t *old_page;
     686        panel_entry_t *old_cursor;
     687        size_t old_idx;
     688        size_t max_idx;
     689        size_t rows;
     690        panel_entry_t *entry;
     691        size_t i;
     692
     693        rows = panel_page_size(panel);
     694
     695        old_page = panel->page;
     696        old_cursor = panel->cursor;
     697        old_idx = panel->cursor_idx;
     698        max_idx = panel->entries_cnt - rows;
     699
     700        /* Move page by rows entries down (if possible) */
     701        for (i = 0; i < rows; i++) {
     702                entry = panel_next(panel->page);
     703                /* Do not scroll that results in a short page */
     704                if (entry != NULL && panel->page_idx < max_idx) {
     705                        panel->page = entry;
     706                        ++panel->page_idx;
     707                }
     708        }
     709
     710        /* Move cursor by rows entries down (if possible) */
     711
     712        for (i = 0; i < rows; i++) {
     713                entry = panel_next(panel->cursor);
     714                if (entry != NULL) {
     715                        panel->cursor = entry;
     716                        ++panel->cursor_idx;
     717                }
     718        }
     719
     720        if (panel->page != old_page) {
     721                /* We have scrolled. Need to repaint all entries */
     722                (void) panel_paint(panel);
     723        } else if (panel->cursor != old_cursor) {
     724                /* No scrolling, but cursor has moved */
     725                panel_entry_paint(old_cursor, old_idx);
     726                panel_entry_paint(panel->cursor, panel->cursor_idx);
     727
     728                (void) gfx_update(gc);
     729        }
     730}
     731
    606732/** @}
    607733 */
  • uspace/app/nav/panel.h

    raf2ea83 r3e6c51f  
    108108extern ui_control_t *panel_ctl(panel_t *);
    109109extern void panel_set_rect(panel_t *, gfx_rect_t *);
     110extern unsigned panel_page_size(panel_t *);
    110111extern errno_t panel_entry_append(panel_t *, const char *, uint64_t);
    111112extern void panel_entry_delete(panel_entry_t *);
     
    121122extern void panel_cursor_top(panel_t *);
    122123extern void panel_cursor_bottom(panel_t *);
     124extern void panel_page_up(panel_t *);
     125extern void panel_page_down(panel_t *);
    123126
    124127#endif
  • uspace/app/nav/test/panel.c

    raf2ea83 r3e6c51f  
    209209}
    210210
     211/** panel_page_size() returns correct size */
     212PCUT_TEST(page_size)
     213{
     214        panel_t *panel;
     215        ui_control_t *control;
     216        gfx_rect_t rect;
     217        errno_t rc;
     218
     219        rc = panel_create(NULL, &panel);
     220        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     221
     222        control = panel_ctl(panel);
     223        PCUT_ASSERT_NOT_NULL(control);
     224
     225        rect.p0.x = 10;
     226        rect.p0.y = 20;
     227        rect.p1.x = 30;
     228        rect.p1.y = 40;
     229
     230        panel_set_rect(panel, &rect);
     231
     232        /* NOTE If page size changes, we have problems elsewhere in the tests */
     233        PCUT_ASSERT_INT_EQUALS(18, panel_page_size(panel));
     234
     235        panel_destroy(panel);
     236}
     237
    211238/** panel_entry_append() appends new entry */
    212239PCUT_TEST(entry_append)
     
    518545        rect.p1.y = 4; // XXX Assuming this makes page size 2
    519546        panel_set_rect(panel, &rect);
     547
     548        PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
    520549
    521550        /* Add tree entries (more than page size, which is 2) */
     
    597626        rect.p1.y = 4; // XXX Assuming this makes page size 2
    598627        panel_set_rect(panel, &rect);
     628
     629        PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
    599630
    600631        /* Add tree entries (more than page size, which is 2) */
     
    679710        panel_set_rect(panel, &rect);
    680711
     712        PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
     713
    681714        /* Add tree entries (more than page size, which is 2) */
    682715        rc = panel_entry_append(panel, "a", 1);
     
    737770        rect.p1.y = 4; // XXX Assuming this makes page size 2
    738771        panel_set_rect(panel, &rect);
     772
     773        PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
    739774
    740775        /* Add tree entries (more than page size, which is 2) */
     
    770805}
    771806
     807/** panel_page_up() moves one page up */
     808PCUT_TEST(page_up)
     809{
     810        ui_t *ui;
     811        ui_window_t *window;
     812        ui_wnd_params_t params;
     813        panel_t *panel;
     814        gfx_rect_t rect;
     815        errno_t rc;
     816
     817        rc = ui_create_disp(NULL, &ui);
     818        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     819
     820        ui_wnd_params_init(&params);
     821        params.caption = "Test";
     822
     823        rc = ui_window_create(ui, &params, &window);
     824        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     825
     826        rc = panel_create(window, &panel);
     827        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     828
     829        rect.p0.x = 0;
     830        rect.p0.y = 0;
     831        rect.p1.x = 10;
     832        rect.p1.y = 4; // XXX Assuming this makes page size 2
     833        panel_set_rect(panel, &rect);
     834
     835        PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
     836
     837        /* Add five entries (2 full pages, one partial) */
     838        rc = panel_entry_append(panel, "a", 1);
     839        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     840
     841        rc = panel_entry_append(panel, "b", 2);
     842        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     843
     844        rc = panel_entry_append(panel, "c", 3);
     845        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     846
     847        rc = panel_entry_append(panel, "d", 4);
     848        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     849
     850        rc = panel_entry_append(panel, "e", 5);
     851        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     852
     853        /* Cursor to the last entry and page start to the next-to-last entry */
     854        panel->cursor = panel_last(panel);
     855        panel->cursor_idx = 4;
     856        panel->page = panel_prev(panel->cursor);
     857        panel->page_idx = 3;
     858
     859        /* Move one page up */
     860        panel_page_up(panel);
     861
     862        /* Page should now start at second entry and cursor at third */
     863        PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
     864        PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
     865        PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
     866        PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
     867        PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
     868        PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
     869
     870        /* Move one page up again. */
     871        panel_page_up(panel);
     872
     873        /* Cursor and page start should now both be at the first entry */
     874        PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
     875        PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
     876        PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
     877        PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
     878        PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
     879
     880        /* Moving further up should do nothing (we are at the top). */
     881        panel_page_up(panel);
     882
     883        /* Cursor and page start should still be at the first entry */
     884        PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
     885        PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
     886        PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
     887        PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
     888        PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
     889
     890        panel_destroy(panel);
     891        ui_window_destroy(window);
     892        ui_destroy(ui);
     893}
     894
     895/** panel_page_up() moves one page down */
     896PCUT_TEST(page_down)
     897{
     898        ui_t *ui;
     899        ui_window_t *window;
     900        ui_wnd_params_t params;
     901        panel_t *panel;
     902        gfx_rect_t rect;
     903        errno_t rc;
     904
     905        rc = ui_create_disp(NULL, &ui);
     906        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     907
     908        ui_wnd_params_init(&params);
     909        params.caption = "Test";
     910
     911        rc = ui_window_create(ui, &params, &window);
     912        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     913
     914        rc = panel_create(window, &panel);
     915        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     916
     917        rect.p0.x = 0;
     918        rect.p0.y = 0;
     919        rect.p1.x = 10;
     920        rect.p1.y = 4; // XXX Assuming this makes page size 2
     921        panel_set_rect(panel, &rect);
     922
     923        PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
     924
     925        /* Add five entries (2 full pages, one partial) */
     926        rc = panel_entry_append(panel, "a", 1);
     927        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     928
     929        rc = panel_entry_append(panel, "b", 2);
     930        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     931
     932        rc = panel_entry_append(panel, "c", 3);
     933        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     934
     935        rc = panel_entry_append(panel, "d", 4);
     936        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     937
     938        rc = panel_entry_append(panel, "e", 5);
     939        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     940
     941        /* Cursor and page to the first entry */
     942        panel->cursor = panel_first(panel);
     943        panel->cursor_idx = 0;
     944        panel->page = panel->cursor;
     945        panel->page_idx = 0;
     946
     947        /* Move one page down */
     948        panel_page_down(panel);
     949
     950        /* Page and cursor should point to the third entry */
     951        PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
     952        PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
     953        PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
     954        PCUT_ASSERT_STR_EQUALS("c", panel->page->name);
     955        PCUT_ASSERT_INT_EQUALS(3, panel->page->size);
     956        PCUT_ASSERT_INT_EQUALS(2, panel->page_idx);
     957
     958        /* Move one page down again. */
     959        panel_page_down(panel);
     960
     961        /* Cursor should point to last and page to next-to-last entry */
     962        PCUT_ASSERT_STR_EQUALS("e", panel->cursor->name);
     963        PCUT_ASSERT_INT_EQUALS(5, panel->cursor->size);
     964        PCUT_ASSERT_INT_EQUALS(4, panel->cursor_idx);
     965        PCUT_ASSERT_STR_EQUALS("d", panel->page->name);
     966        PCUT_ASSERT_INT_EQUALS(4, panel->page->size);
     967        PCUT_ASSERT_INT_EQUALS(3, panel->page_idx);
     968
     969        /* Moving further down should do nothing (we are at the bottom). */
     970        panel_page_down(panel);
     971
     972        /* Cursor should still point to last and page to next-to-last entry */
     973        PCUT_ASSERT_STR_EQUALS("e", panel->cursor->name);
     974        PCUT_ASSERT_INT_EQUALS(5, panel->cursor->size);
     975        PCUT_ASSERT_INT_EQUALS(4, panel->cursor_idx);
     976        PCUT_ASSERT_STR_EQUALS("d", panel->page->name);
     977        PCUT_ASSERT_INT_EQUALS(4, panel->page->size);
     978        PCUT_ASSERT_INT_EQUALS(3, panel->page_idx);
     979
     980        panel_destroy(panel);
     981        ui_window_destroy(window);
     982        ui_destroy(ui);
     983}
     984
    772985PCUT_EXPORT(panel);
Note: See TracChangeset for help on using the changeset viewer.