Skip to content

Commit

Permalink
Makes wlmtk_menu_item_t elements private.
Browse files Browse the repository at this point in the history
  • Loading branch information
phkaeser committed Feb 22, 2025
1 parent 2730acc commit 602b19d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 56 deletions.
2 changes: 0 additions & 2 deletions src/action_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ wlmaker_action_item_t *wlmaker_action_item_create(
&wlmtk_menu_item_events(action_item_ptr->menu_item_ptr)->triggered,
&action_item_ptr->triggered_listener,
_wlmaker_action_item_handle_triggered);
// TODO([email protected]): Should not be required!
action_item_ptr->menu_item_ptr->width = style_ptr->width;

if (!wlmtk_menu_item_set_text(action_item_ptr->menu_item_ptr, text_ptr)) {
wlmaker_action_item_destroy(action_item_ptr);
Expand Down
30 changes: 24 additions & 6 deletions src/toolkit/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,39 @@ void test_set_mode(bs_test_t *test_ptr)
wlmtk_menu_add_item(&menu, item1_ptr);

// Setting the mode must propagate.
BS_TEST_VERIFY_EQ(test_ptr, WLMTK_MENU_MODE_NORMAL, item1_ptr->mode);
BS_TEST_VERIFY_EQ(
test_ptr,
WLMTK_MENU_MODE_NORMAL,
wlmtk_menu_item_get_mode(item1_ptr));
wlmtk_menu_set_mode(&menu, WLMTK_MENU_MODE_RIGHTCLICK);
BS_TEST_VERIFY_EQ(test_ptr, WLMTK_MENU_MODE_RIGHTCLICK, item1_ptr->mode);
BS_TEST_VERIFY_EQ(
test_ptr,
WLMTK_MENU_MODE_RIGHTCLICK,
wlmtk_menu_item_get_mode(item1_ptr));

// A new item must get the mode applied.
wlmtk_menu_item_t *item2_ptr = wlmtk_menu_item_create(&s.item, NULL);
BS_TEST_VERIFY_NEQ_OR_RETURN(test_ptr, NULL, item2_ptr);
BS_TEST_VERIFY_EQ(test_ptr, WLMTK_MENU_MODE_NORMAL, item2_ptr->mode);
BS_TEST_VERIFY_EQ(
test_ptr,
WLMTK_MENU_MODE_NORMAL,
wlmtk_menu_item_get_mode(item2_ptr));
wlmtk_menu_add_item(&menu, item2_ptr);
BS_TEST_VERIFY_EQ(test_ptr, WLMTK_MENU_MODE_RIGHTCLICK, item2_ptr->mode);
BS_TEST_VERIFY_EQ(
test_ptr,
WLMTK_MENU_MODE_RIGHTCLICK,
wlmtk_menu_item_get_mode(item2_ptr));

// Setting the mode must propagate to all.
wlmtk_menu_set_mode(&menu, WLMTK_MENU_MODE_NORMAL);
BS_TEST_VERIFY_EQ(test_ptr, WLMTK_MENU_MODE_NORMAL, item1_ptr->mode);
BS_TEST_VERIFY_EQ(test_ptr, WLMTK_MENU_MODE_NORMAL, item2_ptr->mode);
BS_TEST_VERIFY_EQ(
test_ptr,
WLMTK_MENU_MODE_NORMAL,
wlmtk_menu_item_get_mode(item1_ptr));
BS_TEST_VERIFY_EQ(
test_ptr,
WLMTK_MENU_MODE_NORMAL,
wlmtk_menu_item_get_mode(item2_ptr));

wlmtk_menu_fini(&menu);
}
Expand Down
55 changes: 55 additions & 0 deletions src/toolkit/menu_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,43 @@

/* == Declarations ========================================================= */

/** State of a menu item. */
struct _wlmtk_menu_item_t {
/** A menu item is a buffer. */
wlmtk_buffer_t super_buffer;
/** The superclass' @ref wlmtk_element_t virtual method table. */
wlmtk_element_vmt_t orig_super_element_vmt;

/** Event listeners. @see wlmtk_menu_item_events. */
wlmtk_menu_item_events_t events;

/** List node, within @ref wlmtk_menu_t::items. */
bs_dllist_node_t dlnode;

/** Text to be shown for the menu item. */
char *text_ptr;
/** Width of the item element, in pixels. */
int width;
/** Mode of the menu (and the item). */
wlmtk_menu_mode_t mode;

/** Texture buffer holding the item in enabled state. */
struct wlr_buffer *enabled_wlr_buffer_ptr;
/** Texture buffer holding the item in highlighted state. */
struct wlr_buffer *highlighted_wlr_buffer_ptr;
/** Texture buffer holding the item in disabled state. */
struct wlr_buffer *disabled_wlr_buffer_ptr;

/** Whether the item is enabled. */
bool enabled;

/** State of the menu item. */
wlmtk_menu_item_state_t state;

/** Style of the menu item. */
wlmtk_menu_item_style_t style;
};

static bool _wlmtk_menu_item_redraw(
wlmtk_menu_item_t *menu_item_ptr);
static void _wlmtk_menu_item_set_state(
Expand Down Expand Up @@ -94,6 +131,8 @@ wlmtk_menu_item_t *wlmtk_menu_item_create(
&_wlmtk_menu_item_element_vmt);

menu_item_ptr->style = *style_ptr;
// TODO([email protected]): Should not be required!
menu_item_ptr->width = style_ptr->width;
wl_signal_init(&menu_item_ptr->events.triggered);
menu_item_ptr->enabled = true;
_wlmtk_menu_item_set_state(menu_item_ptr, WLMTK_MENU_ITEM_ENABLED);
Expand Down Expand Up @@ -134,6 +173,13 @@ void wlmtk_menu_item_set_mode(
menu_item_ptr->mode = mode;
}

/* ------------------------------------------------------------------------- */
wlmtk_menu_mode_t wlmtk_menu_item_get_mode(
wlmtk_menu_item_t *menu_item_ptr)
{
return menu_item_ptr->mode;
}

/* ------------------------------------------------------------------------- */
bool wlmtk_menu_item_set_text(
wlmtk_menu_item_t *menu_item_ptr,
Expand Down Expand Up @@ -599,6 +645,11 @@ void test_right_click(bs_test_t *test_ptr)
wlmtk_button_event_t b = { .button = BTN_LEFT, .type = WLMTK_BUTTON_CLICK };
wlmtk_button_event_t bup = { .button = BTN_RIGHT, .type = WLMTK_BUTTON_UP };

BS_TEST_VERIFY_EQ(
test_ptr,
WLMTK_MENU_MODE_NORMAL,
wlmtk_menu_item_get_mode(item_ptr));

// Pointer enters to highlight, click triggers.
BS_TEST_VERIFY_TRUE(test_ptr, wlmtk_element_pointer_motion(e, 20, 10, 1));
BS_TEST_VERIFY_TRUE(test_ptr, wlmtk_element_pointer_button(e, &b));
Expand All @@ -611,6 +662,10 @@ void test_right_click(bs_test_t *test_ptr)

// Switch mode to right-click.
wlmtk_menu_item_set_mode(item_ptr, WLMTK_MENU_MODE_RIGHTCLICK);
BS_TEST_VERIFY_EQ(
test_ptr,
WLMTK_MENU_MODE_RIGHTCLICK,
wlmtk_menu_item_get_mode(item_ptr));

// Pointer remains inside, click is ignored.
BS_TEST_VERIFY_TRUE(test_ptr, wlmtk_element_pointer_motion(e, 20, 10, 1));
Expand Down
52 changes: 4 additions & 48 deletions src/toolkit/menu_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,43 +84,6 @@ struct _wlmtk_menu_item_style_t {
uint64_t width;
};

/** State of a menu item. */
struct _wlmtk_menu_item_t {
/** A menu item is a buffer. */
wlmtk_buffer_t super_buffer;
/** The superclass' @ref wlmtk_element_t virtual method table. */
wlmtk_element_vmt_t orig_super_element_vmt;

/** Event listeners. @see wlmtk_menu_item_events. */
wlmtk_menu_item_events_t events;

/** List node, within @ref wlmtk_menu_t::items. */
bs_dllist_node_t dlnode;

/** Text to be shown for the menu item. */
char *text_ptr;
/** Width of the item element, in pixels. */
int width;
/** Mode of the menu (and the item). */
wlmtk_menu_mode_t mode;

/** Texture buffer holding the item in enabled state. */
struct wlr_buffer *enabled_wlr_buffer_ptr;
/** Texture buffer holding the item in highlighted state. */
struct wlr_buffer *highlighted_wlr_buffer_ptr;
/** Texture buffer holding the item in disabled state. */
struct wlr_buffer *disabled_wlr_buffer_ptr;

/** Whether the item is enabled. */
bool enabled;

/** State of the menu item. */
wlmtk_menu_item_state_t state;

/** Style of the menu item. */
wlmtk_menu_item_style_t style;
};

/**
* Creates a menu item.
*
Expand Down Expand Up @@ -156,6 +119,10 @@ void wlmtk_menu_item_set_mode(
wlmtk_menu_item_t *menu_item_ptr,
wlmtk_menu_mode_t mode);

/** @return the mode of this item. */
wlmtk_menu_mode_t wlmtk_menu_item_get_mode(
wlmtk_menu_item_t *menu_item_ptr);

/**
* Sets or updates the text for the menu item.
*
Expand Down Expand Up @@ -186,17 +153,6 @@ wlmtk_menu_item_t *wlmtk_menu_item_from_dlnode(bs_dllist_node_t *dlnode_ptr);
/** Returns a pointer to the superclass @ref wlmtk_element_t. */
wlmtk_element_t *wlmtk_menu_item_element(wlmtk_menu_item_t *menu_item_ptr);

/** Fake menu item, useful for unit tests. */
typedef struct {
/** State of the menu item. */
wlmtk_menu_item_t menu_item;
} wlmtk_fake_menu_item_t;

/** Ctor for the fake menu item. */
wlmtk_fake_menu_item_t *wlmtk_fake_menu_item_create(void);
/** Dtor for the fake menu item. */
void wlmtk_fake_menu_item_destroy(wlmtk_fake_menu_item_t *fake_menu_item_ptr);

/** Unit test cases. */
extern const bs_test_case_t wlmtk_menu_item_test_cases[];

Expand Down

0 comments on commit 602b19d

Please sign in to comment.