egui_commonmark icon indicating copy to clipboard operation
egui_commonmark copied to clipboard

handling my own url

Open sbechet opened this issue 1 year ago • 1 comments

I would like your opinion: I'm not sure what method to use to manage your own url manager. Currently when I click on a URL it attempts to open on my browser. I would like to modify my egui interface based on this url.

I thought it was handled by egui itself, I tried that, but it doesn't return anything:

    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {

        ctx.output_mut(|o| {
            if let Some(openurl) = &o.open_url {
                println!("{}",openurl.url);
            }
        });
...

How to do ? Thank you!

sbechet avatar Jul 25 '24 20:07 sbechet

Chatgpt hallucinated an idea like

CommonMarkViewer::new()
        .link_action(|url| {
            handle_link(url);
            LinkAction::None // or Open, or OpenInTab, or...
        })
        .show(ctx, markdown_text);

sbechet avatar Jul 26 '24 06:07 sbechet

I also need to do this. Did you ever figure it out?

ptliddle avatar Oct 21 '24 04:10 ptliddle

There are two methods.

// egui_commonmark stuff here
ctx.output_mut(|o| {
   // open_url must be converted to None so that egui does not open the url afterwards
   if let Some(openurl) = &o.open_url.take() {
          println!("{}", openurl.url);
    }
});

It should work, but it has to be done after the egui_commonmark calls. This is the easiest method, but it has an annoyance when you do the following:

// This used to be the default, and is the most sane option IMO. You should always be able
// to see where an external link takes you.
ui.style_mut().url_in_tooltip = true;

This will show the url on hover. For normal urls this is good, but it is annoying when the link is supposed to be handled by the app. To avoid this egui_commonmark can be informed about the link before hand so that it can create it as a link instead of a hyperlink.

For this purpose link hooks can be used. In the example they are processed before the egui_commonmark call, but that is just due to that it is directly linked to what the example renders. It should be possible to call it afterwards too.

lampsitter avatar Oct 22 '24 15:10 lampsitter