Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added get_focused_item() to request the current nav focus #2258

Merged
merged 2 commits into from
Jan 31, 2024

Conversation

v-ein
Copy link
Contributor

@v-ein v-ein commented Jan 19, 2024


name: Pull Request
about: Create a pull request to help us improve
title: Added get_focused_item() to request the current nav focus
assignees: @hoffstadt


Description:

Currently there's no easy way to determine which widget is focused. Yes, there's is_item_focused, but it only gives a True/False test on a particular widget. You can't ask DPG, "what UUID is currently holding the focus?" - you have to either loop through all widgets checking the state of each one individually, or bind a focus handler to every widget so that you can store the focused ID somewhere.

This PR adds a function to DPG to request current focus.

Here's the test program I used:

import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport(title="Test", width=750, height=700)

with dpg.window(width=300) as w:
    def update_data(s, a):
        dpg.set_value("focus-display", f"Focused UUID: {dpg.get_focused_item()}")

    with dpg.item_handler_registry() as handlers:
        dpg.add_item_visible_handler(callback=update_data)

    dpg.add_button(label="Lorem ipsum")
    dpg.add_selectable(label="dolor sit")
    dpg.add_input_text()
    dpg.add_drag_int()
    dpg.add_text(tag="focus-display")
    dpg.bind_item_handler_registry(dpg.last_item(), handlers)

dpg.show_viewport()
dpg.show_item_registry()
dpg.setup_dearpygui()
dpg.start_dearpygui()
dpg.destroy_context()

Concerning Areas:

These changes are implemented much like get_active_window is. As a result, the focused item ID is stored in GContext->itemRegistry, which adds a dependency on mvItemRegistry to mvAppItemState.

Technically, mvAppItemState doesn't need to know about mvItemRegistry. I'd say both focusedItem and activeWindow should reside in GContext rather than itemRegistry. Please let me know what you think on this.

I can modify the fix so that focusedItem (or both focusedItem and activeWindow) are in GContext and there are no unnecessary dependencies.

@hoffstadt
Copy link
Owner

Great work! I agree they should be added to GContext!

@v-ein
Copy link
Contributor Author

v-ein commented Jan 25, 2024

Moved them to GContext. Here's an updated test program that I used to test both get_focused_item and get_active_window:

import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport(title="Test", width=750, height=700)

with dpg.window(width=300, height=400) as w:
    def update_data(s, a):
        dpg.set_value("focus-display", f"Focused: {dpg.get_focused_item()} | Wnd: {dpg.get_active_window()}")

    with dpg.item_handler_registry() as handlers:
        dpg.add_item_visible_handler(callback=update_data)

    dpg.add_text(tag="focus-display")
    dpg.bind_item_handler_registry(dpg.last_item(), handlers)

    dpg.add_button(label="Lorem ipsum")
    dpg.add_selectable(label="Dolor sit")
    dpg.add_input_text()
    dpg.add_drag_int()

    with dpg.child_window():
        dpg.add_button(label="Lorem ipsum")
        dpg.add_selectable(label="Dolor sit")
        dpg.add_input_text()
        dpg.add_drag_int()

with dpg.window(pos=(320, 0), width=300) as w:
    dpg.add_input_double()

dpg.show_viewport()
dpg.show_item_registry()
dpg.setup_dearpygui()
dpg.start_dearpygui()
dpg.destroy_context()

@hoffstadt hoffstadt merged commit 003efa9 into hoffstadt:master Jan 31, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants