From bce239291446a8f8ebdf38c83daf7ecde61f5210 Mon Sep 17 00:00:00 2001 From: Philipp Kaeser Date: Tue, 14 Jan 2025 20:10:10 +0000 Subject: [PATCH] Adds method to create an action-triggering menu item from a descriptor. --- src/action_item.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++ src/action_item.h | 16 +++++++++++++ src/wlmaker_test.c | 2 ++ 3 files changed, 78 insertions(+) diff --git a/src/action_item.c b/src/action_item.c index cfb3e306..21bdf10c 100644 --- a/src/action_item.c +++ b/src/action_item.c @@ -51,6 +51,38 @@ static const wlmtk_element_vmt_t _wlmaker_action_item_element_vmt = { /* == Exported methods ===================================================== */ +/* ------------------------------------------------------------------------- */ +/** + * Creates a menu item triggering an action item from a descriptor. + * + * @param desc_ptr + * @param dest_ptr + * @param style_ptr + * @param server_ptr + * @param env_ptr + * + * @return true on success. + */ +bool wlmaker_action_item_create_from_desc( + wlmaker_action_item_desc_t *desc_ptr, + void *dest_ptr, + const wlmtk_menu_item_style_t *style_ptr, + wlmaker_server_t *server_ptr, + wlmtk_env_t *env_ptr) +{ + wlmaker_action_item_t *action_item_ptr = wlmaker_action_item_create( + desc_ptr->text_ptr, + style_ptr, + desc_ptr->action, + server_ptr, + env_ptr); + if (NULL == action_item_ptr) return false; + + *(wlmaker_action_item_t**)( + (uint8_t*)dest_ptr + desc_ptr->destination_ofs) = action_item_ptr; + return true; +} + /* ------------------------------------------------------------------------- */ wlmaker_action_item_t *wlmaker_action_item_create( const char *text_ptr, @@ -134,4 +166,32 @@ void _wlmaker_action_item_clicked(wlmtk_menu_item_t *menu_item_ptr) } } +/* == Unit tests =========================================================== */ + +static void _wlmaker_action_item_test_create(bs_test_t *test_ptr); + +/** Test cases for action items. */ +const bs_test_case_t wlmaker_action_item_test_cases[] = { + { 1, "create", _wlmaker_action_item_test_create }, + { 0, NULL, NULL }, +}; + +/* ------------------------------------------------------------------------- */ +/** Tests creation the menu item. */ +void _wlmaker_action_item_test_create(bs_test_t *test_ptr) +{ + wlmaker_action_item_t *ai_ptr = NULL; + wlmaker_action_item_desc_t desc = { "text", 42, 0 }; + wlmtk_menu_item_style_t style = {}; + wlmaker_server_t server = {}; + + BS_TEST_VERIFY_TRUE( + test_ptr, + wlmaker_action_item_create_from_desc( + &desc, &ai_ptr, &style, &server, NULL)); + + BS_TEST_VERIFY_NEQ(test_ptr, NULL, ai_ptr); + wlmaker_action_item_destroy(ai_ptr); +} + /* == End of action_item.c ================================================== */ diff --git a/src/action_item.h b/src/action_item.h index 20076010..2425d91f 100644 --- a/src/action_item.h +++ b/src/action_item.h @@ -31,6 +31,19 @@ typedef struct _wlmaker_action_item_t wlmaker_action_item_t; extern "C" { #endif // __cplusplus +/** Descriptor for creating a menu item triggering an action. */ +typedef struct { + /** Text for the menu item. */ + const char *text_ptr; + /** The action to trigger. */ + wlmaker_action_t action; + /** + * Where to store the @ref wlmaker_action_item_t, relative to the + * `dest_ptr` argument of @ref wlmaker_action_item_create_from_desc. + */ + size_t destination_ofs; +} wlmaker_action_item_desc_t; + /** * Creates a menu item that triggers a @ref wlmaker_action_t. * @@ -60,6 +73,9 @@ void wlmaker_action_item_destroy(wlmaker_action_item_t *action_item_ptr); wlmtk_menu_item_t *wlmaker_action_item_menu_item( wlmaker_action_item_t *action_item_ptr); +/** Unit test cases. */ +extern const bs_test_case_t wlmaker_action_item_test_cases[]; + #ifdef __cplusplus } // extern "C" #endif // __cplusplus diff --git a/src/wlmaker_test.c b/src/wlmaker_test.c index 375ce824..cb576b6d 100644 --- a/src/wlmaker_test.c +++ b/src/wlmaker_test.c @@ -19,6 +19,7 @@ */ #include "action.h" +#include "action_item.h" #include "clip.h" #include "config.h" #include "corner.h" @@ -31,6 +32,7 @@ /** WLMaker unit tests. */ const bs_test_set_t wlmaker_tests[] = { { 1, "action", wlmaker_action_test_cases }, + { 1, "action_item", wlmaker_action_item_test_cases }, { 1, "clip", wlmaker_clip_test_cases }, { 1, "config", wlmaker_config_test_cases }, { 1, "corner", wlmaker_corner_test_cases },