v21 addContextMenuItems and RunPlugin
#1
Hello,

I'm trying to achieve a Related videos custom menu action from a list item with addContextMenuItems and RunPlugin but I cannot make it work and I don't understand the issue.

Basicaly I display a list of videos in listitems and for each I add a custom menu for related videos with addContextMenuItems and RunPlugin.
The context menu appears correctly but when I select it nothing happens, I expect to display another list of videos related on the current listitem.

For testing purpose I added a related list item that displays the related videos using the same plugin url and it works, so the issue should be in RunPlugin or something I missed but I cannot find what

python:
def list_videos(genre_index):
    """
    Create the list of playable videos in the Kodi interface.
    :param genre_index: the index of genre in the list of movie genres
    :type genre_index: int
    """
    genre_info = get_videos(genre_index)
    # Set plugin category. It is displayed in some skins as the name
    # of the current section.
    xbmcplugin.setPluginCategory(HANDLE, genre_info['genre'])
    # Set plugin content. It allows Kodi to select appropriate views
    # for this type of content.
    xbmcplugin.setContent(HANDLE, 'movies')
    # Get the list of videos in the category.
    videos = genre_info['movies']
    # Iterate through videos.
    for video in videos:
        # Create a list item with a text label
        list_item = xbmcgui.ListItem(label=video['title'])
        # Set graphics (thumbnail, fanart, banner, poster, landscape etc.) for the list item.
        # Here we use only poster for simplicity's sake.
        # In a real-life plugin you may need to set multiple image types.
        list_item.setArt({'poster': video['poster']})
        # Set additional info for the list item via InfoTag.
        # 'mediatype' is needed for skin to display info for this ListItem correctly.
        info_tag = list_item.getVideoInfoTag()
        info_tag.setMediaType('movie')
        info_tag.setTitle(video['title'])
        # Set 'IsPlayable' property to 'true'. This is mandatory for playable items!
        list_item.setProperty('IsPlayable', 'true')

        # add a related ContextMenu with the URL
        list_item.addContextMenuItems([ ("Show related...", "RunPlugin({})".format(get_url(action='related', genre_index=genre_index))) ])

        # Create a URL for a plugin recursive call.
        # Example: plugin://plugin.video.example/?action=play&video=https%3A%2F%2Fia600702.us.archive.org%2F3%2Fitems%2Firon_mask%2Firon_mask_512kb.mp4
        url = get_url(action='play', video=video['url'])
        # Add the list item to a virtual Kodi folder.
        # is_folder = False means that this item won't open any sub-list.
        is_folder = False
        # Add our item to the Kodi virtual folder listing.
        xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)

    # Add a Related list item with the same url for debug
    list_item = xbmcgui.ListItem(label="Related")
    rel_url = get_url(action='related', genre_index=genre_index)
    xbmcplugin.addDirectoryItem(HANDLE, rel_url, list_item, False)

    # Add sort methods for the virtual folder items
    xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
    xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_VIDEO_YEAR)

    # Finish creating a virtual folder.
    xbmcplugin.endOfDirectory(HANDLE)
Reply
#2
hello,

in my experience you cannot use "RunPlugin" to display the new list.  try "Container.Update" instead.

Code:
list_item.addContextMenuItems([ ("Show related...", "Container.Update({})".format(get_url(action='related', genre_index=genre_index))) ])
Reply
#3
(2024-09-07, 03:31)dickfitzwell Wrote: in my experience you cannot use "RunPlugin" to display the new list.  try "Container.Update" instead.

Yep, can't do it directly using just RunPlugin, but rather than using Container.Update I would suggest using

Code:
"ActivateWindow(Videos, {}, return)".format(get_url(action='related', genre_index=genre_index)))

You never know where your listitem may end up (it may not always be the plugin listing that you are generating) and refreshing some builtin containers (e.g. in playlist windows) can be problematic. It can also impact on skin layout when using widgets.
Reply
#4
(2024-09-07, 03:59)MoojMidge Wrote: Yep, can't do it directly using just RunPlugin, but rather than using Container.Update I would suggest using

Code:
"ActivateWindow(Videos, {}, return)".format(get_url(action='related', genre_index=genre_index)))

You never know where your listitem may end up (it may not always be the plugin listing that you are generating) and refreshing some builtin containers (e.g. in playlist windows) can be problematic. It can also impact on skin layout when using widgets.

usually I try to detect if it is a widget and use "ActivateWindow" but I did not see that in their sample.  so there are no drawbacks or unintended consequences of always using ActivateWindow vs Container.Update?  in what case is Container.Update preferred then?
Reply
#5
(2024-09-07, 04:08)dickfitzwell Wrote: so there are no drawbacks or unintended consequences of always using ActivateWindow vs Container.Update?

The main drawback that I am aware of is that if the plugin call fails then the use of the ActivateWindow will open the default video window and you will lose your current window history, while Container.Update will just leave you where you were if I recall correctly.

Can work around this by doing everything the plugin call would normally do in the first run, check and cache the listing, and only then use ActivateWindow with the same plugin url if there is a listing to use. The second plugin call with ActivateWindow can then fetch and use the previously cached output rather than doing everything again. It is cumbersome, but if you are caching things for use with reuselanguageinvoker then it can actually speed things up.
 
(2024-09-07, 04:08)dickfitzwell Wrote: in what case is Container.Update preferred then?

Can't say, haven't looked into the code to see. I'm guessing it would be simpler and/or slightly faster.
Reply
#6
Thanks for your answers,

I tested only Container.Update for now and it works well, I'll do some tests with ActivateWindow later

I used
python:
list_item.addContextMenuItems([ ("Show related...", "Container.Update({})".format(get_url(action='related', genre_index=genre_index))) ])
Reply

Logout Mark Read Team Forum Stats Members Help
addContextMenuItems and RunPlugin0