So this is a bit of an xy problem. My overall goal is to have a widget that functions similar to the QTableWidget from Qt, so it can handle lots of rows (few thousand) and supports a little context menu to do some operations on it.
Anyways, I am trying to make the scrollable clickable (since it can handle infinite items, and I could probably hack and saw a solution together) and I was successful with that. Git cloned the repo, and in widgets/src/scrollable.rs added a on_press field and function to the widget. And in update
I added a small statement:
if event
== Event::Mouse(mouse::Event::ButtonPressed(
mouse::Button::Right,
))
{
if let Some(on_press) = on_press.clone() {
state.is_pressed = true;
}
}
if event
== Event::Mouse(mouse::Event::ButtonReleased(
mouse::Button::Right,
))
{
state.is_pressed = false;
Which is pretty much a direct rip from the button widget. And from mouse_interaction
I was able to do a check for when the cursor is over the bounds, and is clicked:
if cursor.is_over(bounds) {
println!("over bounds");
if state.is_pressed {
println!("omg spot found");
}
}
And it works perfectly.
The problem that I have, and that I really can’t seem to figure out, is how to send the message. The widget/src/button.rs
’s update function has this part Here:
if let Some(on_press) = on_press.clone() {
let state = state();
if state.is_pressed {
state.is_pressed = false;
let bounds = layout.bounds();
if cursor.is_over(bounds) {
shell.publish(on_press);
}
So I figure that I should shell.publish(on_press)
the message in my modified scrollable, but when I try that, it tells me that my scrollable is of &Message
type and doesn’t implement Clone
. And when I looked more carefully into the source code of button.rs
, it has all these trait checks for Clone
, and the scrollable doesn’t:
|
505 | pub fn update<Message>(
| ------- this type parameter
...
547 | shell.publish(on_press)
| ------- ^^^^^^^^ expected type parameter `Message`, found `&Message`
| |
| arguments to this method are incorrect
|
= note: expected type parameter `Message`
found reference `&Message`
Does the Message
type not come from the Message
that the user creates in the file where the gui is run? Because I derived clone for that, but still not working:
#[derive(Debug, Clone, Copy)]
enum Message {
New,
}
and my view function:
fn view(&self) -> Element<Message> {
let mut col = Column::new();
for x in 0..=1000 {
let txt = text(format!("hi: {}", x));
col = col.push(txt);
}
let scroller = scrollable::Scrollable::new(col).on_press(Message::New);
container(scroller).into()
}
Any help is great