Skip to content

Commit

Permalink
Adds method to create an action-triggering menu item from a descriptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
phkaeser committed Jan 14, 2025
1 parent 856ba4a commit bce2392
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/action_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ================================================== */
16 changes: 16 additions & 0 deletions src/action_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/wlmaker_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include "action.h"
#include "action_item.h"
#include "clip.h"
#include "config.h"
#include "corner.h"
Expand All @@ -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 },
Expand Down

0 comments on commit bce2392

Please sign in to comment.