Sublime Forum

Clickable popup menus

#1

@OdatNurd @FichteFoll

for displaying Html popups we use view.show_popup(str, <on_navigate>)
My question is whether it is possible to pass multiple functions to a single popup?

My use case is that I want to show some list of clickable options and each one need to perform different functions

<body id="minihtml-preview">
  <h1>minihtml Example</h1>
  <p><a href="">option one</a></p>
  <p><a href="https://www.sublimetext.com/">option two</a></p>
  <p><a href="">option three</a></p>
</body>

Is it possible or not? Or we can pass only one function to each HTML popup instance?

0 Likes

#2

Yes, it is possible. The on_navigate() handler receives the href value. It can be used to run any kind of function based on the href.

    def navigate(href):
        # allow navigate() to manipulate the outer variables
        nonlocal highlight_diff

        if href == 'hide':
            view.hide_popup()
        elif href == 'copy':
            del_text = '\n'.join(del_lines)
            sublime.set_clipboard(del_text)
            sublime.status_message(
                'Copied: {0} characters'.format(len(del_text)))
        elif href == 'revert':
            # hide the popup and update the view
            view.hide_popup()
            revert.revert_change_impl(view, diff_info)
        elif href == 'disable_hl_diff':
            # show a diff popup with the same diff info (previous revision)
            highlight_diff = False
            _show_diff_popup_impl(
                git_gutter, line, highlight_diff, flags, diff_info)
        elif href == 'enable_hl_diff':
            # show a diff popup with the same diff info (highlight diff)
            highlight_diff = True
            _show_diff_popup_impl(
                git_gutter, line, highlight_diff, flags, diff_info)
        elif href in ('first_change', 'next_change', 'prev_change'):
            next_line = meta.get(href, line)
            point = view.text_point(next_line - 1, 0)

            def show_new_popup():
                # wait until scrolling has completed
                if not view.visible_region().contains(point):
                    return sublime.set_timeout_async(show_new_popup, 20)
                # show a diff popup with new diff info
                _show_diff_popup_impl(
                    git_gutter, next_line, highlight_diff, 0,
                    git_gutter.git_handler.diff_line_change(next_line))
            view.hide_popup()
            view.show_at_center(point)
            show_new_popup()
2 Likes

#3

According to this code snippet we have to write our custom function such as navigate() with href as parameter and pass to view.show_popup() like

view.show_popup(<content>, on_navigate=navigate())
0 Likes

#4

Very correct, that’s the way it works

1 Like

#5

Actually you should only pass in a pointer to your function and not execute it directly within the show_popup method. Ig it wouldn’t work otherwise.

view.show_popup(<content>, on_navigate=navigate)
1 Like

#6

Thanks @deathaxe and @UltraInstinct05

0 Likes