How to show mbox with button?

Hi me again… :blush:

using dev-6.0
What’s the proper way to create button activated mbox?
Also, how to call function with the mbox button?

The example shows mbox that’s already there created on setup.
I have one physical button and a clickable container (it’s nice that not only button can be used).

I’d like to open an mbox. Right now I create an mbox when I pressed the button and the box
disappear when I pressed again which is expected.
But I think everytime I pressed the button to show mbox I created a new mbox in memory.
Is mbox get deleted when it’s closed?

TIA

I think you should delete the message box when one of its buttons is pressed (e.g. the OK button).

I haven’t figured out yet how to use the mbox button.
Maybe I should make the whole mbox generate event when I pressed the button?

Is this correct to delete the mbox?

static void mbox_event_cb(lv_obj_t * mbox, lv_event_t event)
{
	if(lv_obj_del(mbox) != LV_RES_INV) {
		printf("Object not deleted\n");
	}
}

I’m hoping to find some attributes maybe I could just show and hide it.

The hardware button blog entry maybe helpful,

Thanks I already have my button working.

static int check_button_state(void) {
	int res;
	/* returns 0 or 1 on success */
	res = gpiod_ctxless_get_value(GPIO_CHIP_DEVICE, BUTTON_PIN_OFFSET, 0, "Button Consumer");
	if (res < 0) {
		printf("Unable to read button state\n");
	}
	return res;
}

static bool button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) {
		int btn_status = check_button_state();
				
		if (btn_status == 0) {	
			data->state = LV_INDEV_STATE_PR;
		} else if (btn_status == 1) {
			data->state = LV_INDEV_STATE_REL;
		}						
    data->btn_id = 0;
    return false;
}

I’m looking for a correct way to pop up a message box with a button press.

You can hide the message box like any other LittlevGL object.

lv_obj_set_hidden(mbox, true);

You also probably want this line from the example in your event handler so that it doesn’t get hidden by unrelated events.

if(event != LV_EVENT_CLICKED) return;

Thanks I got it. I think.

static void mbox_event_cb(lv_obj_t * obj, lv_event_t event)
{
	(void)obj;
	if(event != LV_EVENT_CLICKED) return;
	lv_obj_set_hidden(mbox, true);
}

I created the mbox once and hid it at setup. Then a button event on the parent pops it up.

lv_obj_set_hidden(mbox, false);

but I had to have mbox_event_cb hide it again otherwise when it closes it doesn’t pop right back.

That sounds correct.