Skip to content

Add custom hyperlink callback for rich text hyperlinks #3335

Closed
@jasmaa

Description

Checklist

  • I have searched the issue tracker for open issues that relate to the same feature, before opening a new one.
  • This issue only relates to a single feature. I will open new issues for any other features.

Is your feature request related to a problem?

I am working on a graphical browser for the Gemini protocol, and I am using the rich text widget to display page content. When I click a hyperlink in the rich text, I want to load the page within my application. However current behavior opens URL in the web browser by default with no way to customize the behavior for rich text hyperlinks AFAIK.

Is it possible to construct a solution with the existing API?

No response

Describe the solution you'd like to see.

I think it would be useful to have custom callbacks within rich text hyperlinks. Currently, while the Hyperlink widget supports a custom callback (#2979), this is not supported for HyperlinkSegments in the RichText widget.

One solution would be to add an OnTapped field to HyperlinkSegment (following the design for Hyperlink) and pass it to the Hyperlink that gets generated in HyperlinkSegment.Visual():

type HyperlinkSegment struct {
	Alignment fyne.TextAlign
	Text      string
	URL       *url.URL

        // add
	OnTapped  func()
}

Example usage:

content := widget.NewRichTextFromMarkdown(`
[link 1](example.com)
[link 2](example.com)
`)

// Set callback for all top-level hyperlink segments
for _, s := range content.Segments {
  if link, ok := s.(*HyperlinkSegment); ok {
    link.OnTapped = func() {
      fmt.Println(link.URL)
    }
  } 
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    HacktoberfestIssues that count towards Hacktoberfest scores.enhancementNew feature or requestgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions