qr: rwlock optimization
This commit is contained in:
parent
7ee8fb2ff6
commit
18a1bc6734
1 changed files with 8 additions and 7 deletions
|
@ -12,11 +12,12 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::Arc;
|
||||||
|
use parking_lot::RwLock;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use egui::{SizeHint, TextureHandle, TextureOptions};
|
use egui::{SizeHint, TextureHandle, TextureOptions};
|
||||||
use egui::load::SizedTexture;
|
use egui::load::SizedTexture;
|
||||||
use egui_extras::image::{load_svg_bytes, load_svg_bytes_with_size};
|
use egui_extras::image::load_svg_bytes_with_size;
|
||||||
|
|
||||||
use crate::gui::views::types::QrCreationState;
|
use crate::gui::views::types::QrCreationState;
|
||||||
use crate::gui::views::View;
|
use crate::gui::views::View;
|
||||||
|
@ -53,7 +54,7 @@ impl QrCodeContent {
|
||||||
self.create_image(text);
|
self.create_image(text);
|
||||||
} else {
|
} else {
|
||||||
// Create image from SVG data.
|
// Create image from SVG data.
|
||||||
let r_create = self.qr_creation_state.read().unwrap();
|
let r_create = self.qr_creation_state.read();
|
||||||
let svg = r_create.svg.as_ref().unwrap();
|
let svg = r_create.svg.as_ref().unwrap();
|
||||||
let size = SizeHint::Size(ui.available_width() as u32, ui.available_width() as u32);
|
let size = SizeHint::Size(ui.available_width() as u32, ui.available_width() as u32);
|
||||||
let color_img = load_svg_bytes_with_size(svg, Some(size)).unwrap();
|
let color_img = load_svg_bytes_with_size(svg, Some(size)).unwrap();
|
||||||
|
@ -74,13 +75,13 @@ impl QrCodeContent {
|
||||||
|
|
||||||
/// Check if image is creating.
|
/// Check if image is creating.
|
||||||
fn creating(&self) -> bool {
|
fn creating(&self) -> bool {
|
||||||
let r_create = self.qr_creation_state.read().unwrap();
|
let r_create = self.qr_creation_state.read();
|
||||||
r_create.creating
|
r_create.creating
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if image was created.
|
/// Check if image was created.
|
||||||
fn has_image(&self) -> bool {
|
fn has_image(&self) -> bool {
|
||||||
let r_create = self.qr_creation_state.read().unwrap();
|
let r_create = self.qr_creation_state.read();
|
||||||
r_create.svg.is_some()
|
r_create.svg.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +93,7 @@ impl QrCodeContent {
|
||||||
let qr = qrcodegen::QrCode::encode_text(text.as_str(),
|
let qr = qrcodegen::QrCode::encode_text(text.as_str(),
|
||||||
qrcodegen::QrCodeEcc::Medium).unwrap();
|
qrcodegen::QrCodeEcc::Medium).unwrap();
|
||||||
let svg = Self::qr_to_svg(qr, 0);
|
let svg = Self::qr_to_svg(qr, 0);
|
||||||
let mut w_create = qr_creation_state.write().unwrap();
|
let mut w_create = qr_creation_state.write();
|
||||||
w_create.creating = false;
|
w_create.creating = false;
|
||||||
w_create.svg = Some(svg.into_bytes());
|
w_create.svg = Some(svg.into_bytes());
|
||||||
});
|
});
|
||||||
|
@ -126,7 +127,7 @@ impl QrCodeContent {
|
||||||
|
|
||||||
/// Reset QR code image content state to default.
|
/// Reset QR code image content state to default.
|
||||||
pub fn clear_state(&mut self) {
|
pub fn clear_state(&mut self) {
|
||||||
let mut w_create = self.qr_creation_state.write().unwrap();
|
let mut w_create = self.qr_creation_state.write();
|
||||||
*w_create = QrCreationState::default();
|
*w_create = QrCreationState::default();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue