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

bind_item_handler_registry tests for handler applicability incorrectly #2196

Closed
v-ein opened this issue Oct 4, 2023 · 1 comment · Fixed by #2223
Closed

bind_item_handler_registry tests for handler applicability incorrectly #2196

v-ein opened this issue Oct 4, 2023 · 1 comment · Fixed by #2223
Labels
state: pending not addressed yet type: bug bug

Comments

@v-ein
Copy link
Contributor

v-ein commented Oct 4, 2023

Version of Dear PyGui

Version: 1.9.1
Operating System: Windows 10

My Issue/Question

While bind_item_handler_registry tests whether each handler can be bound to the target widget, it does that in a weird way: for some handlers this test doesn't work. This makes it possible to bind handlers that are not applicable, or sometimes makes it impossible to bind a handler that the widget actually does support.

The handlers affected are:

add_item_deactivated_handler
add_item_edited_handler
add_item_focus_handler
add_item_hover_handler
add_item_resize_handler
add_item_toggled_open_handler

Here are a couple of examples:

  1. dpg.table_row() only supports the 'visible' handler, but DPG has no objections when one of those handlers above is bound to the row.
  2. dpg.add_node_link() supports the hover handler, but DPG won't allow to bind it. On a side note, binding any handler to add_node_link() will lead to a segfault due to another bug; that bug will go in a separate ticket.

To Reproduce

Steps to reproduce the behavior:

  1. Run the first example.
  2. The code must fail with an exception saying a handler is not applicable. If it doesn't fail, then all handlers have been bound to the table row row, even though it doesn't support any of them.
  3. Run the second example.
  4. The code is expected to fail with exactly one exception, which it will catch and print, prepended with "Expected failure". This is because it attempts to bind a deactivated_handler, which node_link does not support. If there's no such message, then DPG allowed deactivated_handler to be bound, though it should not.
  5. It might then segfault due to a bug in node_link.
  6. If the code prints "Unexpected failure" and an exception, then it was unable to bind the hover handler, even though it's supported by node_link.

Expected behavior

Handlers having "xxx_handler_applicable": True in get_item_info must get bound without any issues.

Handlers having "xxx_handler_applicable": False must cause bind_item_handler_registry to fail.

Screenshots/Video

None.

Standalone, minimal, complete and verifiable example

Example 1: binding unsupported handlers to a table row.

import dearpygui.dearpygui as dpg

dpg.create_context()

with dpg.window(label="Test", pos=(10, 10), width=180, height=250) as wnd:
    print(dpg.get_dearpygui_version())
    with dpg.table():
        dpg.add_table_column()
        with dpg.table_row() as row:
            dpg.add_text("Lorem ipsum")

    for k, v in dpg.get_item_info(row).items():
        if k.endswith("handler_applicable"):
            print(f"{k}:\t{v}")

    with dpg.item_handler_registry() as handlers:
        dpg.add_item_deactivated_handler(callback=lambda: print("Gotcha!"))
        dpg.add_item_edited_handler(callback=lambda: print("Gotcha!"))
        dpg.add_item_focus_handler(callback=lambda: print("Gotcha!"))
        dpg.add_item_hover_handler(callback=lambda: print("Gotcha!"))
        dpg.add_item_resize_handler(callback=lambda: print("Gotcha!"))
        dpg.add_item_toggled_open_handler(callback=lambda: print("Gotcha!"))

    dpg.bind_item_handler_registry(row, handlers)
    print("Must have crashed by this point - something's wrong.")

dpg.create_viewport(title='Test', width=900, height=900)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

Example 2: binding supported and unsupported handlers to a node link.

import traceback
import dearpygui.dearpygui as dpg


dpg.create_context()
with dpg.window(label="Example Window") as main:
    with dpg.node_editor() as editor:
        with dpg.node(label="Node 1", pos=(20, 20)):
            with dpg.node_attribute(label="Attr", attribute_type=dpg.mvNode_Attr_Output) as attr1:
                dpg.add_text("Text")

        with dpg.node(label="Node 2", pos=(220, 20)):
            with dpg.node_attribute(label="Attr", attribute_type=dpg.mvNode_Attr_Input) as attr2:
                dpg.add_text("Text")

        node_link = dpg.add_node_link(attr1, attr2, parent=editor)

        try:
            with dpg.item_handler_registry() as handlers:
                dpg.add_item_deactivated_handler(callback=lambda: print("Node attr deactivated"))
            dpg.bind_item_handler_registry(node_link, handlers)
        except Exception as e:
            print("... Expected failure ...")
            print(traceback.format_exc())

        try:
            with dpg.item_handler_registry() as handlers:
                dpg.add_item_hover_handler(callback=lambda: print("Node attr hovered"))
            dpg.bind_item_handler_registry(node_link, handlers)
        except Exception as e:
            print("!!! Unexpected failure !!!")
            print(traceback.format_exc())


dpg.create_viewport()
dpg.setup_dearpygui()

dpg.set_primary_window(main, True)

dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
@v-ein v-ein added state: pending not addressed yet type: bug bug labels Oct 4, 2023
@v-ein
Copy link
Contributor Author

v-ein commented Oct 4, 2023

The issue is caused by tests like this in mvItemHandlerRegistry::onBind:

if (!(applicableState & ~MV_STATE_DEACTIVATED))

Notice the ~ operator, which is wrong here. I've got a fix and going to eventually open a PR.

@v-ein v-ein changed the title bind_item_handler_registry tests for handlers applicability incorrectly bind_item_handler_registry tests for handler applicability incorrectly Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: pending not addressed yet type: bug bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant