Hi!
I've struggling with this second day and cannot figure this out. I am using nwg Windows Native API to let user choose to which database connect to. The problem here is I still get conflicts about references, whenever variable gets out of scope, borrowing etc.. It creates a small window with combobox, button and selected property which is going to be then provided to Tauri framework to setup a window object property.
#[derive(Default)]
pub struct Selector {
window: nwg::Window,
combobox: nwg::ComboBox<&'static str>,
button: nwg::Button,
svg: nwg::ImageFrame,
selected: Option<String>
}
impl Selector {
fn ui(dialog: Rc<RefCell<Self>>) {
nwg::init().expect("Error while creating Windows Native UI");
let clone = dialog.clone();
{
let mut borrow = RefCell::borrow_mut(&dialog);
nwg::Window::builder()
.size((300, 200))
.position((300, 300))
.title("Wybór firmy")
.build(&mut borrow.window)
.unwrap();
let svg_data = include_bytes!("./assets/logo.svg");
let tree = Tree::from_data(svg_data, &Options::default()).unwrap();
let mut pixmap = Pixmap::new(142, 17).unwrap();
resvg::render(&tree, Transform::from_scale(0.4, 0.4), &mut pixmap.as_mut());
let image = DynamicImage::ImageRgba8(ImageBuffer::<Rgba<u8>, _>::from_raw(142, 17, pixmap.take()).unwrap());
let mut image_data: Vec<u8> = Vec::new();
{
let mut cursor = Cursor::new(&mut image_data);
image.write_to(&mut cursor, image::ImageFormat::Bmp).unwrap();
}
let nwg_image = nwg::Bitmap::from_bin(&image_data).unwrap();
nwg::ImageFrame::builder()
.parent(&borrow.window)
.position((79, 15))
.size((142, 17))
.bitmap(Some(&nwg_image))
.build(&mut borrow.svg)
.unwrap();
nwg::ComboBox::builder()
.collection(vec!["DB 1", "DB 2", "DB 3"])
.parent(&borrow.window)
.position((10, 50))
.size((280, 25))
.build(&mut borrow.combobox)
.unwrap();
nwg::Button::builder()
.text("Wybierz")
.parent(&borrow.window)
.position((10, 150))
.size((280, 30))
.build(&mut borrow.button)
.unwrap();
}
let mut borrow = RefCell::get_mut(&mut clone);
nwg::bind_event_handler(&borrow.button.handle, &borrow.window.handle, |event, _data, handle| {
if event == nwg::Event::OnButtonClick {
borrow.selected = Some(borrow.combobox.selection_string().unwrap());
}
});
nwg::dispatch_thread_events();
}
pub fn new() -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
window: Default::default(),
combobox: Default::default(),
button: Default::default(),
svg: Default::default(),
selected: None,
}))
}
}
I would be really grateful for any help cause I'm loosing my mind with it. I think this is not quite complicated, but on daily basis I don't use Rust and it is kinda catchy for me.
It always throws errors nearby this:
let mut borrow = RefCell::get_mut(&mut clone);
nwg::bind_event_handler(&borrow.button.handle, &borrow.window.handle, |event, _data, handle| {
if event == nwg::Event::OnButtonClick {
borrow.selected = Some(borrow.combobox.selection_string().unwrap());
}
Like "clone" is freed while borrowed etc.., and how do I modify the Option inside the structure upon content in combobox, whenever button is clicked?