Improve mutex usage
This commit is contained in:
parent
3f0023e7c5
commit
61562a1588
|
@ -4,11 +4,14 @@ use super::{GUIElementsRef, MainWindowRef};
|
||||||
use slint::Model;
|
use slint::Model;
|
||||||
use std::{rc::Rc, sync::{Arc, Mutex}};
|
use std::{rc::Rc, sync::{Arc, Mutex}};
|
||||||
|
|
||||||
|
struct VRAM {
|
||||||
|
pub file_list: Rc<slint::VecModel<slint::StandardListViewItem>>,
|
||||||
|
pub image_list: Rc<slint::VecModel<VRAMImage>>
|
||||||
|
}
|
||||||
|
|
||||||
pub struct MainTab {
|
pub struct MainTab {
|
||||||
main_window: MainWindowRef,
|
main_window: MainWindowRef,
|
||||||
mtx: Arc<Mutex<i32>>,
|
vram: Arc<Mutex<VRAM>>,
|
||||||
vram_file_list: Rc<slint::VecModel<slint::StandardListViewItem>>,
|
|
||||||
vram_image_list: Rc<slint::VecModel<VRAMImage>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MainTab {
|
impl MainTab {
|
||||||
|
@ -20,10 +23,11 @@ impl MainTab {
|
||||||
main_window.borrow().set_main_tab_vram_file_list(vram_file_list.clone().into());
|
main_window.borrow().set_main_tab_vram_file_list(vram_file_list.clone().into());
|
||||||
main_window.borrow().set_main_tab_vram_images(vram_image_list.clone().into());
|
main_window.borrow().set_main_tab_vram_images(vram_image_list.clone().into());
|
||||||
|
|
||||||
MainTab{main_window, mtx: Arc::new(Mutex::new(0)), vram_file_list, vram_image_list}
|
MainTab{main_window, vram: Arc::new(Mutex::new(VRAM{file_list: vram_file_list, image_list: vram_image_list}))}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image, image_palette: Option<slint::Image>) {
|
pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image, image_palette: Option<slint::Image>) {
|
||||||
|
let vram_data = self.vram.lock().expect("VRAM already locked");
|
||||||
let add_new_image = |file_name: &String, image: slint::Image| {
|
let add_new_image = |file_name: &String, image: slint::Image| {
|
||||||
let vram_image = VRAMImage{
|
let vram_image = VRAMImage{
|
||||||
img: image,
|
img: image,
|
||||||
|
@ -33,30 +37,28 @@ impl MainTab {
|
||||||
is_palette: false,
|
is_palette: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
self.vram_file_list.push(slint::StandardListViewItem::from(file_name.as_str()));
|
vram_data.file_list.push(slint::StandardListViewItem::from(file_name.as_str()));
|
||||||
self.vram_image_list.push(vram_image);
|
vram_data.image_list.push(vram_image);
|
||||||
};
|
};
|
||||||
|
|
||||||
let _lock = self.mtx.lock().unwrap();
|
|
||||||
add_new_image(file_name, image);
|
add_new_image(file_name, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove_vram_file(&mut self, idx: usize) -> bool {
|
pub fn remove_vram_file(&mut self, idx: usize) -> bool {
|
||||||
let _lock = self.mtx.lock().unwrap();
|
let vram_data = self.vram.lock().expect("VRAM already locked");
|
||||||
|
if let Some(element) = vram_data.image_list.iter().skip(idx).next() {
|
||||||
if let Some(element) = self.vram_image_list.iter().skip(idx).next() {
|
|
||||||
if element.is_palette {
|
if element.is_palette {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.vram_file_list.remove(idx);
|
vram_data.file_list.remove(idx);
|
||||||
self.vram_image_list.remove(idx);
|
vram_data.image_list.remove(idx);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn move_vram_image(&mut self, idx: usize, dx: i32, dy: i32) {
|
pub fn move_vram_image(&mut self, idx: usize, dx: i32, dy: i32) {
|
||||||
if let Some(mut vram_info) = self.vram_image_list.row_data(idx) {
|
let vram_data = self.vram.lock().expect("VRAM already locked");
|
||||||
|
if let Some(mut vram_info) = vram_data.image_list.row_data(idx) {
|
||||||
vram_info.x += dx;
|
vram_info.x += dx;
|
||||||
vram_info.y += dy;
|
vram_info.y += dy;
|
||||||
|
|
||||||
|
@ -77,7 +79,7 @@ impl MainTab {
|
||||||
vram_info.y = VRAM_HEIGHT as i32 - vram_img_height;
|
vram_info.y = VRAM_HEIGHT as i32 - vram_img_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.vram_image_list.set_row_data(idx, vram_info);
|
vram_data.image_list.set_row_data(idx, vram_info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue