iced-rs / iced

A cross-platform GUI library for Rust, inspired by Elm
https://iced.rs
MIT License
24.84k stars 1.17k forks source link

Rich text appears to capture mouse interactions #2630

Open kiedtl opened 1 month ago

kiedtl commented 1 month ago

Is your issue REALLY a bug?

Is there an existing issue for this?

Is this issue related to iced?

What happened?

I've noticed that clicking a rich-text element embedded inside a button does not trigger the on_press.

MRE (forgive unnecessary use of daemon):

use iced::window;
use iced::{Element, Task};
use iced::widget::{text, button, span, rich_text, column};

fn main() -> iced::Result {
    iced::daemon(Example::title, Example::update, Example::view)
        .run_with(Example::new)
}

struct Example {
    // Nuh uh
}

#[derive(Debug, Clone)]
enum Message {
    Nuhuh,
    Pressed,
}

impl Example {
    fn new() -> (Self, Task<Message>) {
        let (_id, open) = window::open(window::Settings::default());

        (Self {}, open.map(|_| Message::Nuhuh))
    }

    fn title(&self, _: window::Id) -> String {
        "Test".to_owned()
    }

    fn update(&mut self, message: Message) -> Task<Message> {
        match message {
            Message::Nuhuh => { }
            Message::Pressed => {
                println!("hi");
            }
        }

        Task::none()
    }

    fn view(&self, _: window::Id) -> Element<Message> {
        button(column![
            text("clicking this yields Message::Pressed!"),
            rich_text([
                span("but clicking this doesn't do anything!"),
                span("\nand neither does this!"),
            ])
        ])
            .on_press(Message::Pressed)
            .into()
    }
}

In the above example, clicking on the first text element causes the button to be clicked, but clicking the spans does not.

What is the expected behavior?

Clicking any area of the button should yield the appropriate event.

Version

crates.io release

Operating System

Both Windows and Linux (X11) were tested.

Do you have any log output?

No response

kiedtl commented 1 month ago

Alright, I've done some digging and this appears to be... intentional?

Surely this is leftover code, right? Captured this and tracking which span is pressed doesn't seem to be used at all. It's not even available to the user. Maybe I'm missing something?

Koranir commented 1 month ago

Rich text can include links, which is handled as part of the widget (you missed this, the selected span is used just a few blocks beneath your link). Indeed, a better solution would be to only intercept the input if the selected span is a link.

kiedtl commented 1 month ago

Ah, my bad, I see the issue. I’ll see if I can whip up a PR at some point.