Just starting to get my head around GUI programming in Rust. I've decided to go with FLTK but my problem is not really FLTK.
This is basically a set of digits where individual digits can be scrolled up/down. For each digit I need to handle mouse events.
This is the digit type.
pub struct VFODigit{
id : i32,
pub frame : Frame,
i_cc : Arc<Mutex<protocol::cc_out::CCDataMutex>>,
}
// Implementation methods on UDPRData
impl VFODigit {
// Create a new instance and initialise the default arrays
pub fn new( id : i32, label : &String, font : Font, size : i32, color : Color, i_cc : Arc<Mutex<protocol::cc_out::CCDataMutex>>) -> VFODigit {
let mut frame = Frame::default().with_label(label);
frame.set_label_color(color);
frame.set_label_font(font);
frame.set_label_size(size);
// Object state
VFODigit {
id : id,
frame : frame,
i_cc : i_cc,
}
}
pub fn get_id(&self) -> i32 {
return self.id;
}
pub fn set_label(&mut self, label : &String) {
self.frame.set_label(label);
}
}
The function that creates the digits (which is their parent) adds them to a grid and also to a HashMap works fine and I can retrieve the widgets from the map and update them. However when I try to add the handler I can't seem to get round the error.
fn create_digits(&mut self) {
let mut index = 0;
for i in 0..11 {
if (i == 3) || (i == 7) {
// Add a separator
let mut sep = self.new_sep();
self.grid.insert(&mut sep, 0, i);
} else {
// Add the next digit
let mut digit = VFODigit::new(index, &String::from("0"), Font::Times, 20, Color::DarkCyan, self.i_cc.clone());
self.grid.insert(&mut digit.frame, 0, i);
self.digit_map.insert(index as i32, digit);
digit.frame.handle(move |f, e| self.digit_handler(f, e));
index += 1;
}
}
}
and the test handler
fn digit_handler(&mut self, f: &mut Frame, e: Event) -> bool {
if e == Event::Enter {
println!("Enter");
} else if e == Event::Leave {
println!("Leave");
} else if e == Event::MouseWheel {
println!("Wheel");
}
return true;
}
The error trace.
error[E0521]: borrowed data escapes outside of associated function
--> src\app\ui\components\vfo.rs:124:22
|
112 | fn create_digits(&mut self) {
| ---------
| |
| self
is a reference that is only valid in the associated function body
| let's call the lifetime of this reference '1
...
124 | &mut digit.frame.handle(move |f, e| self.digit_handler(f, e));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| self
escapes the associated function body here
| argument requires that '1
must outlive 'static
error[E0382]: borrow of moved value: self
--> src\app\ui\components\vfo.rs:125:17
|
112 | fn create_digits(&mut self) {
| --------- move occurs because self
has type &mut VFOState
, which does not implement the Copy
trait
...
124 | &mut digit.frame.handle(move |f, e| self.digit_handler(f, e));
| ----------- ---- variable moved due to use in closure
| |
| value moved into closure here
125 | self.digit_map.insert(index as i32, digit);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value bor