Viewport drag drop function

import omni.ui as ui

def _on_accept_drop(url):
    # only accept .png files
    if ".png" in url:
        return True
    else:
        return False

def _on_value(field):
    field.model.get_value_as_string()

def _on_drag(image_path):
    ui.Image(image_path, height=50, width=50)
    return image_path


window = ui.Window(title="Drag and Drop", width= 500, height=500)

with window.frame:
    with ui.VStack():
        image_path = "/img/path/im.png"
        image = ui.Image(image_path, height=100, width=100, drag_fn=lambda: _on_drag(image_path))

        path_widget = path_widget = ui.StringField(height=30)
        path_widget.model.add_value_changed_fn(lambda _: _on_value(path_widget))

        drop_area = ui.Workspace.get_window("Viewport").frame
        drop_area.set_accept_drop_fn(lambda url: _on_accept_drop(url))
        
        def _on_drop(e: ui.WidgetMouseDropEvent):
            path = e.mime_data
            path_widget.model.set_value(path)
        
        drop_area.set_drop_fn(_on_drop)  # internal drop


external_drag_drop = None
def _on_ext_drag_drop(e, payload, drop_area, path_widget, ):
    path = payload[0]
    path_widget.model.set_value(path)

def setup_external_drag_drop(window_name :str, drop_area, path_widget):
    global external_drag_drop
    if external_drag_drop:
        external_drag_drop.destroy()
    try:
        from omni.kit.window.drop_support import ExternalDragDrop
        external_drag_drop = ExternalDragDrop(window_name=window_name,
            drag_drop_fn=lambda e, p, drop_area=drop_area, path_widget=path_widget: _on_ext_drag_drop(e, p, drop_area, path_widget))
    except ImportError:
        pass

# if the image lives outside of the omniverse app, e.g filr explorer
setup_external_drag_drop("Viewport", drop_area, path_widget) # external drop