How to Add a Widget Event Filter - mchpgfx/legato.docs GitHub Wiki

Microchip Technology How to Add A Widget Event Filter

Graphics does not exist in a vaccum. The whole point of Graphics User Interface is to provide human-machine interaction.

The previous guide How to Add a Widget Event Callback teaches how to create an widget event callback to widget. This guide teaches widget event filter and how to set one up.

Event Filters - Custom Event Handling

Event filters can be used to add or 'override' default widget event handling.

The allow events like Touch Down/Up/Move, Change Language, Widget Paint, Widget Move, Widget Resized, Widget Focus to be handled in Application Code.

Event filters can also be used to 'mute' specific events from being handled.

Custom Event Handling With Event Filters

Example: Adding a Touch Move Event Filter to a Label Widget

Here is an example of adding a move event filter to the label "Fast" in Legato Quickstart.

Legato Quickstart

  1. Open app.c in MPLABX

Open app.c

  1. Include definitions.h header file in app.c

Include definition.h

  1. Define the event filter function. This will definte the actions that will be used when a touch event is received by the target widget

Define Event Filter function

Here is the code for reference:


static leBool app_filterEvent(leWidget* target, leWidgetEvent* evt, void* data)
{
    switch(evt->id)
    {
        case LE_EVENT_TOUCH_DOWN:
        {
            evt->owner = target;
            evt->accepted = LE_TRUE;
            break;
        }
        case LE_EVENT_TOUCH_MOVE:            
        {
            int x, y;
            leWidgetEvent_TouchMove * move = (leWidgetEvent_TouchMove *) evt;
            
            x = move->x;
            y = move->y;
            
            target->fn->setPosition(target, x, y);
            evt->accepted = LE_TRUE;
            
            break;
        }
        default:
            break;
    }
    
    return LE_FALSE;
}
  1. Also in app.c, define the event filter structure.

Define Event Structure

Code for reference:


static leWidgetEventFilter app_eventFilter =
{
    app_filterEvent,
    NULL
};
  1. Define the Screen0_OnShow event handler function (See the How to Add a Screen Event guide for details). In the handler, the event filter will be registered for the ‘Fast.’ label widget on the screen. This will enable event handling for the widget, and will move the widget when touched and dragged on the screen.

Register the Event Filter

Code for reference:


void Screen0_OnShow(void)
{
    Screen0_fastLabel->fn->installEventFilter(Screen0_fastLabel, app_eventFilter);   
}


Conclusion

At this point, you’re ready to generate, build and run your application. Touch and drag the ‘Fast.’ label to move it around the screen. Observe how its movement slows down when it’s moved over the MPLAB® Harmony Graphics Suite image. Can you tell why it slows down?


If you are new to MPLAB® Harmony, you should probably start with these tutorials:


Is this page helpful? Send feedback

⚠️ **GitHub.com Fallback** ⚠️