modal: rwlock optimization

This commit is contained in:
ardocrat 2024-05-15 14:53:12 +03:00
parent 5f976e9166
commit a1721f978e

View file

@ -12,12 +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 lazy_static::lazy_static;
use std::sync::Arc;
use parking_lot::RwLock;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, RwLock};
use egui::{Align2, Rect, RichText, Rounding, Stroke, Vec2}; use egui::{Align2, Rect, RichText, Rounding, Stroke, Vec2};
use egui::epaint::{RectShape, Shadow}; use egui::epaint::{RectShape, Shadow};
use lazy_static::lazy_static;
use crate::gui::Colors; use crate::gui::Colors;
use crate::gui::views::{Root, View}; use crate::gui::views::{Root, View};
@ -65,7 +65,7 @@ impl Modal {
/// Mark [`Modal`] closed. /// Mark [`Modal`] closed.
pub fn close(&self) { pub fn close(&self) {
let mut w_nav = MODAL_STATE.write().unwrap(); let mut w_nav = MODAL_STATE.write();
w_nav.modal = None; w_nav.modal = None;
} }
@ -93,14 +93,14 @@ impl Modal {
/// Set [`Modal`] instance into state to show at ui. /// Set [`Modal`] instance into state to show at ui.
pub fn show(self) { pub fn show(self) {
let mut w_nav = MODAL_STATE.write().unwrap(); let mut w_nav = MODAL_STATE.write();
w_nav.modal = Some(self); w_nav.modal = Some(self);
} }
/// Remove [`Modal`] from [`ModalState`] if it's showing and can be closed. /// Remove [`Modal`] from [`ModalState`] if it's showing and can be closed.
/// Return `false` if Modal existed in [`ModalState`] before call. /// Return `false` if Modal existed in [`ModalState`] before call.
pub fn on_back() -> bool { pub fn on_back() -> bool {
let mut w_state = MODAL_STATE.write().unwrap(); let mut w_state = MODAL_STATE.write();
// If Modal is showing and closeable, remove it from state. // If Modal is showing and closeable, remove it from state.
if w_state.modal.is_some() { if w_state.modal.is_some() {
@ -117,13 +117,13 @@ impl Modal {
pub fn opened() -> Option<&'static str> { pub fn opened() -> Option<&'static str> {
// Check if modal is showing. // Check if modal is showing.
{ {
if MODAL_STATE.read().unwrap().modal.is_none() { if MODAL_STATE.read().modal.is_none() {
return None; return None;
} }
} }
// Get identifier of opened modal. // Get identifier of opened modal.
let r_state = MODAL_STATE.read().unwrap(); let r_state = MODAL_STATE.read();
let modal = r_state.modal.as_ref().unwrap(); let modal = r_state.modal.as_ref().unwrap();
Some(modal.id) Some(modal.id)
} }
@ -131,7 +131,7 @@ impl Modal {
/// Set title text for current opened [`Modal`]. /// Set title text for current opened [`Modal`].
pub fn set_title(title: String) { pub fn set_title(title: String) {
// Save state. // Save state.
let mut w_state = MODAL_STATE.write().unwrap(); let mut w_state = MODAL_STATE.write();
if w_state.modal.is_some() { if w_state.modal.is_some() {
let mut modal = w_state.modal.clone().unwrap(); let mut modal = w_state.modal.clone().unwrap();
modal.title = Some(title.to_uppercase()); modal.title = Some(title.to_uppercase());
@ -142,11 +142,11 @@ impl Modal {
/// Draw opened [`Modal`] content. /// Draw opened [`Modal`] content.
pub fn ui(ctx: &egui::Context, add_content: impl FnOnce(&mut egui::Ui, &Modal)) { pub fn ui(ctx: &egui::Context, add_content: impl FnOnce(&mut egui::Ui, &Modal)) {
let has_modal = { let has_modal = {
MODAL_STATE.read().unwrap().modal.is_some() MODAL_STATE.read().modal.is_some()
}; };
if has_modal { if has_modal {
let modal = { let modal = {
let r_state = MODAL_STATE.read().unwrap(); let r_state = MODAL_STATE.read();
r_state.modal.clone().unwrap() r_state.modal.clone().unwrap()
}; };
modal.window_ui(ctx, add_content); modal.window_ui(ctx, add_content);