jscene: add a stronger focus function

This commit is contained in:
Lephenixnoir 2022-06-19 22:45:05 +01:00
parent d0856d100b
commit 4e01a99b0b
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 52 additions and 0 deletions

View File

@ -87,6 +87,14 @@ void *jscene_focused_widget(jscene *scene);
The selected widget, obviously, must be a descendant of the scene. */
void jscene_set_focused_widget(jscene *scene, void *widget);
/* jscene_show_and_focus(): Make a widget visible and focus it
This function does three things:
1. Set the visibility of the target widget to [true]
2. Configure any parent with a stacked layout to show the child that
contains the target widget
3. Focus the target widget */
void jscene_show_and_focus(jscene *scene, void *widget);
/* jscene_process_key_event(): Send a key event to the focused widget
Returns true if the event was accepted, false if it was ignored. */
bool jscene_process_key_event(jscene *scene, key_event_t event);

View File

@ -204,6 +204,9 @@ void jwidget_insert_child(void *w, void *child, int position);
(w) must be the parent of (child). The child is left without a parent. */
void jwidget_remove_child(void *w, void *child);
/* jwidget_child_position(): Find the position of a widget in the child list */
int jwidget_child_position(void *w, void *child);
//---
// Sizing and stretching
//---

View File

@ -138,6 +138,35 @@ void jscene_set_focused_widget(jscene *s, void *w0)
(jevent){ .type = JWIDGET_FOCUS_IN, .source = w });
}
void jscene_show_and_focus(jscene *scene, void *w0)
{
J_CAST(w)
/* Ensure the widget is visible */
jwidget_set_visible(w, true);
/* Force stacked layouts all the wat up to [scene] to show [w] */
jwidget *current = w;
jwidget *parent = w->parent;
while(parent != (jwidget *)scene) {
jlayout_stack *stack = jlayout_get_stack(parent);
if(stack) {
int pos = jwidget_child_position(parent, current);
if(stack->active != pos) {
stack->active = pos;
parent->update = true;
}
}
current = parent;
parent = current->parent;
}
/* Give the widget focus */
jscene_set_focused_widget(scene, w);
}
bool jscene_process_key_event(jscene *scene, key_event_t event)
{
jwidget *candidate = scene->focus;

View File

@ -249,6 +249,18 @@ void jwidget_remove_child(void *w0, void *child0)
w->dirty = 1;
}
int jwidget_child_position(void *w0, void *child0)
{
J_CAST(w, child)
for(int i = 0; i < w->child_count; i++) {
if(w->children[i] == child)
return i;
}
return -1;
}
//---
// Sizing and stretching
//---