Changeset 28ca31ed in mainline for uspace/lib/ui
- Timestamp:
- 2024-02-13T20:13:48Z (20 months ago)
- Branches:
- master
- Children:
- 10657856
- Parents:
- 242e3c3
- Location:
- uspace/lib/ui
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ui/include/ui/list.h
r242e3c3 r28ca31ed 1 1 /* 2 * Copyright (c) 202 3Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 57 57 extern errno_t ui_list_entry_append(ui_list_t *, 58 58 ui_list_entry_attr_t *, ui_list_entry_t **); 59 extern void ui_list_entry_move_up(ui_list_entry_t *); 60 extern void ui_list_entry_move_down(ui_list_entry_t *); 59 61 extern void ui_list_entry_delete(ui_list_entry_t *); 60 62 extern void *ui_list_entry_get_arg(ui_list_entry_t *); -
uspace/lib/ui/src/list.c
r242e3c3 r28ca31ed 1 1 /* 2 * Copyright (c) 202 3Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 711 711 } 712 712 713 /** Move UI list entry one position up. 714 * 715 * @parm entry UI list entry 716 */ 717 void ui_list_entry_move_up(ui_list_entry_t *entry) 718 { 719 ui_list_t *list = entry->list; 720 ui_list_entry_t *prev; 721 722 prev = ui_list_prev(entry); 723 if (prev == NULL) { 724 /* Entry is already on first position, nothing to do. */ 725 return; 726 } 727 728 list_remove(&entry->lentries); 729 list_insert_before(&entry->lentries, &prev->lentries); 730 731 /* Make sure page stays on the same position/idx as it was before */ 732 if (list->page == entry) { 733 list->page = prev; 734 } else if (list->page == prev) { 735 list->page = entry; 736 } 737 738 /* 739 * Return cursor to the same position/idx as it was before, 740 * but then move it using ui_list_cursor_move() to the new 741 * position (this ensures scrolling when needed). 742 */ 743 if (list->cursor == entry) { 744 list->cursor = prev; 745 ui_list_cursor_move(list, entry, list->cursor_idx - 1); 746 } else if (list->cursor == prev) { 747 list->cursor = entry; 748 ui_list_cursor_move(list, prev, list->cursor_idx + 1); 749 } 750 } 751 752 /** Move UI list entry one position down. 753 * 754 * @parm entry UI list entry 755 */ 756 void ui_list_entry_move_down(ui_list_entry_t *entry) 757 { 758 ui_list_t *list = entry->list; 759 ui_list_entry_t *next; 760 761 next = ui_list_next(entry); 762 if (next == NULL) { 763 /* Entry is already on last position, nothing to do. */ 764 return; 765 } 766 767 list_remove(&entry->lentries); 768 list_insert_after(&entry->lentries, &next->lentries); 769 770 /* Make sure page stays on the same position/idx as it was before */ 771 if (list->page == entry) { 772 list->page = next; 773 } else if (list->page == next) { 774 list->page = entry; 775 } 776 777 /* 778 * Return cursor to the same position/idx as it was before, 779 * but then move it using ui_list_cursor_move() to the new 780 * position (this ensures scrolling when needed). 781 */ 782 if (list->cursor == entry) { 783 list->cursor = next; 784 ui_list_cursor_move(list, entry, list->cursor_idx + 1); 785 } else if (list->cursor == next) { 786 list->cursor = entry; 787 ui_list_cursor_move(list, next, list->cursor_idx - 1); 788 } 789 } 790 713 791 /** Destroy UI list entry. 714 792 * -
uspace/lib/ui/test/list.c
r242e3c3 r28ca31ed 1 1 /* 2 * Copyright (c) 202 3Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 904 904 } 905 905 906 /** ui_list_entry_move_up() moves entry up */ 907 PCUT_TEST(entry_move_up) 908 { 909 ui_t *ui; 910 ui_window_t *window; 911 ui_wnd_params_t params; 912 ui_list_t *list; 913 ui_list_entry_attr_t attr; 914 ui_list_entry_t *e1, *e2, *e3; 915 ui_list_entry_t *e; 916 errno_t rc; 917 918 rc = ui_create_disp(NULL, &ui); 919 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 920 921 ui_wnd_params_init(¶ms); 922 params.caption = "Test"; 923 924 rc = ui_window_create(ui, ¶ms, &window); 925 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 926 927 rc = ui_list_create(window, true, &list); 928 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 929 930 ui_list_entry_attr_init(&attr); 931 932 /* Create entries */ 933 934 attr.caption = "a"; 935 attr.arg = (void *)1; 936 rc = ui_list_entry_append(list, &attr, &e1); 937 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 938 939 attr.caption = "b"; 940 attr.arg = (void *)2; 941 rc = ui_list_entry_append(list, &attr, &e2); 942 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 943 944 attr.caption = "c"; 945 attr.arg = (void *)3; 946 rc = ui_list_entry_append(list, &attr, &e3); 947 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 948 949 e = ui_list_first(list); 950 PCUT_ASSERT_EQUALS(e1, e); 951 952 /* Moving first entry up should have no effect */ 953 ui_list_entry_move_up(e1); 954 955 e = ui_list_first(list); 956 PCUT_ASSERT_EQUALS(e1, e); 957 958 e = ui_list_next(e); 959 PCUT_ASSERT_EQUALS(e2, e); 960 961 e = ui_list_next(e); 962 PCUT_ASSERT_EQUALS(e3, e); 963 964 e = ui_list_next(e); 965 PCUT_ASSERT_NULL(e); 966 967 /* Move second entry up */ 968 ui_list_entry_move_up(e2); 969 970 e = ui_list_first(list); 971 PCUT_ASSERT_EQUALS(e2, e); 972 973 e = ui_list_next(e); 974 PCUT_ASSERT_EQUALS(e1, e); 975 976 e = ui_list_next(e); 977 PCUT_ASSERT_EQUALS(e3, e); 978 979 e = ui_list_next(e); 980 PCUT_ASSERT_NULL(e); 981 982 ui_list_destroy(list); 983 ui_window_destroy(window); 984 ui_destroy(ui); 985 } 986 987 /** ui_list_entry_move_down() moves entry down */ 988 PCUT_TEST(entry_move_down) 989 { 990 ui_t *ui; 991 ui_window_t *window; 992 ui_wnd_params_t params; 993 ui_list_t *list; 994 ui_list_entry_attr_t attr; 995 ui_list_entry_t *e1, *e2, *e3; 996 ui_list_entry_t *e; 997 errno_t rc; 998 999 rc = ui_create_disp(NULL, &ui); 1000 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 1001 1002 ui_wnd_params_init(¶ms); 1003 params.caption = "Test"; 1004 1005 rc = ui_window_create(ui, ¶ms, &window); 1006 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 1007 1008 rc = ui_list_create(window, true, &list); 1009 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 1010 1011 ui_list_entry_attr_init(&attr); 1012 1013 /* Create entries */ 1014 1015 attr.caption = "a"; 1016 attr.arg = (void *)1; 1017 rc = ui_list_entry_append(list, &attr, &e1); 1018 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 1019 1020 attr.caption = "b"; 1021 attr.arg = (void *)2; 1022 rc = ui_list_entry_append(list, &attr, &e2); 1023 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 1024 1025 attr.caption = "c"; 1026 attr.arg = (void *)3; 1027 rc = ui_list_entry_append(list, &attr, &e3); 1028 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 1029 1030 e = ui_list_first(list); 1031 PCUT_ASSERT_EQUALS(e1, e); 1032 1033 /* Moving last entry down should have no effect */ 1034 ui_list_entry_move_down(e3); 1035 1036 e = ui_list_first(list); 1037 PCUT_ASSERT_EQUALS(e1, e); 1038 1039 e = ui_list_next(e); 1040 PCUT_ASSERT_EQUALS(e2, e); 1041 1042 e = ui_list_next(e); 1043 PCUT_ASSERT_EQUALS(e3, e); 1044 1045 e = ui_list_next(e); 1046 PCUT_ASSERT_NULL(e); 1047 1048 /* Move second-to-last entry down */ 1049 ui_list_entry_move_down(e2); 1050 1051 e = ui_list_first(list); 1052 PCUT_ASSERT_EQUALS(e1, e); 1053 1054 e = ui_list_next(e); 1055 PCUT_ASSERT_EQUALS(e3, e); 1056 1057 e = ui_list_next(e); 1058 PCUT_ASSERT_EQUALS(e2, e); 1059 1060 e = ui_list_next(e); 1061 PCUT_ASSERT_NULL(e); 1062 1063 ui_list_destroy(list); 1064 ui_window_destroy(window); 1065 ui_destroy(ui); 1066 } 1067 906 1068 /** ui_list_entry_delete() deletes entry */ 907 1069 PCUT_TEST(entry_delete)
Note:
See TracChangeset
for help on using the changeset viewer.