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

Add custom hyperlink callback for rich text hyperlinks #3335

Closed
2 tasks done
jasmaa opened this issue Oct 16, 2022 · 4 comments
Closed
2 tasks done

Add custom hyperlink callback for rich text hyperlinks #3335

jasmaa opened this issue Oct 16, 2022 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers Hacktoberfest Issues that count towards Hacktoberfest scores.

Comments

@jasmaa
Copy link

jasmaa commented Oct 16, 2022

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)
    }
  } 
}
@andydotxyz andydotxyz added enhancement New feature or request good first issue Good for newcomers Hacktoberfest Issues that count towards Hacktoberfest scores. labels Oct 16, 2022
@Jacalz
Copy link
Member

Jacalz commented Oct 17, 2022

One thing I can think of with this is to have a different scheme for the link and register a handler in a similar way to the custom storage repository. Instead of hyperlinks taking a net.URL, we can use a fyne.URI and register a handler for opening. The default for https can then be to open the web browser and other schemes can call out to some OS lookup.

@andydotxyz
Copy link
Member

If we were to take that approach it should have (or can still?) go into the Hyperlink widget.
This ticket should probably mimic the approach for the main widget I think.

@Jacalz
Copy link
Member

Jacalz commented Oct 17, 2022

Yes, that was what I was thinking. I totally agree with you 👍

@andydotxyz
Copy link
Member

Added on develop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers Hacktoberfest Issues that count towards Hacktoberfest scores.
Projects
None yet
Development

No branches or pull requests

3 participants