1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 05:40:03 -04:00

Switch to parking_lot::Mutex

This commit is contained in:
Emil Ernerfeldt
2020-04-17 23:30:01 +02:00
parent 481af55ce5
commit 407df94945
9 changed files with 63 additions and 43 deletions

View File

@@ -1,4 +1,6 @@
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use parking_lot::Mutex;
use crate::*;
@@ -18,8 +20,8 @@ impl Clone for Context {
style: Mutex::new(self.style()),
fonts: self.fonts.clone(),
input: self.input,
memory: Mutex::new(self.memory.lock().unwrap().clone()),
graphics: Mutex::new(self.graphics.lock().unwrap().clone()),
memory: Mutex::new(self.memory.lock().clone()),
graphics: Mutex::new(self.graphics.lock().clone()),
}
}
}
@@ -40,37 +42,33 @@ impl Context {
}
pub fn style(&self) -> Style {
*self.style.lock().unwrap()
*self.style.lock()
}
pub fn set_style(&self, style: Style) {
*self.style.lock().unwrap() = style;
*self.style.lock() = style;
}
// TODO: move
pub fn new_frame(&mut self, gui_input: GuiInput) {
self.input = gui_input;
if !gui_input.mouse_down || gui_input.mouse_pos.is_none() {
self.memory.lock().unwrap().active_id = None;
self.memory.lock().active_id = None;
}
}
pub fn drain_paint_lists(&self) -> Vec<PaintCmd> {
let memory = self.memory.lock().unwrap();
self.graphics
.lock()
.unwrap()
.drain(&memory.window_order)
.collect()
let memory = self.memory.lock();
self.graphics.lock().drain(&memory.window_order).collect()
}
/// Is the user interacting with anything?
pub fn any_active(&self) -> bool {
self.memory.lock().unwrap().active_id.is_some()
self.memory.lock().active_id.is_some()
}
pub fn interact(&self, layer: Layer, rect: Rect, interaction_id: Option<Id>) -> InteractInfo {
let mut memory = self.memory.lock().unwrap();
let mut memory = self.memory.lock();
let hovered = if let Some(mouse_pos) = self.input.mouse_pos {
if rect.contains(mouse_pos) {

View File

@@ -55,7 +55,7 @@ impl Emigui {
}
pub fn paint(&mut self) -> Mesh {
let paint_commands: Vec<PaintCmd> = self.ctx.graphics.lock().unwrap().drain().collect();
let paint_commands = self.ctx.drain_paint_lists();
let mut mesher = Mesher::new(self.last_input.pixels_per_point);
mesher.anti_alias = self.anti_alias;

View File

@@ -1,5 +1,6 @@
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use parking_lot::Mutex;
use rusttype::{point, Scale};
use crate::{
@@ -96,7 +97,7 @@ impl Font {
.collect();
let mut glyph_infos = vec![];
let mut atlas_lock = atlas.lock().unwrap();
let mut atlas_lock = atlas.lock();
for glyph in glyphs {
let uv = if let Some(bb) = glyph.pixel_bounding_box() {

View File

@@ -1,9 +1,11 @@
use std::{
collections::{hash_map::DefaultHasher, BTreeMap},
hash::{Hash, Hasher},
sync::{Arc, Mutex},
sync::Arc,
};
use parking_lot::Mutex;
use crate::{
font::Font,
texture_atlas::{Texture, TextureAtlas},
@@ -102,7 +104,7 @@ impl Fonts {
)
})
.collect();
self.texture = atlas.lock().unwrap().texture().clone();
self.texture = atlas.lock().texture().clone();
let mut hasher = DefaultHasher::new();
self.texture.pixels.hash(&mut hasher);

View File

@@ -98,7 +98,7 @@ where
F: FnOnce(&mut Region),
{
let layer = Layer::Popup;
let where_to_put_background = ctx.graphics.lock().unwrap().layer(layer).len();
let where_to_put_background = ctx.graphics.lock().layer(layer).len();
let style = ctx.style();
let window_padding = style.window_padding;
@@ -125,9 +125,8 @@ where
let rect = Rect::from_min_size(window_pos, outer_size);
let mut graphics = ctx.graphics.lock().unwrap();
let graphics = graphics.layer(layer);
graphics.insert(
let mut graphics = ctx.graphics.lock();
graphics.layer(layer).insert(
where_to_put_background,
PaintCmd::Rect {
corner_radius: 5.0,

View File

@@ -39,21 +39,11 @@ impl Region {
/// Can be used for free painting.
/// NOTE: all coordinates are screen coordinates!
pub fn add_paint_cmd(&mut self, paint_cmd: PaintCmd) {
self.ctx
.graphics
.lock()
.unwrap()
.layer(self.layer)
.push(paint_cmd)
self.ctx.graphics.lock().layer(self.layer).push(paint_cmd)
}
pub fn add_paint_cmds(&mut self, mut cmds: Vec<PaintCmd>) {
self.ctx
.graphics
.lock()
.unwrap()
.layer(self.layer)
.append(&mut cmds)
self.ctx.graphics.lock().layer(self.layer).append(&mut cmds)
}
/// Options for this region, and any child regions we may spawn.
@@ -124,7 +114,7 @@ impl Region {
);
let open = {
let mut memory = self.ctx.memory.lock().unwrap();
let mut memory = self.ctx.memory.lock();
if interact.clicked {
if memory.open_foldables.contains(&id) {
memory.open_foldables.remove(&id);

View File

@@ -30,7 +30,7 @@ impl Window {
{
let id = make_id(&self.title);
let mut state = ctx.memory.lock().unwrap().get_or_create_window(
let mut state = ctx.memory.lock().get_or_create_window(
id,
Rect::from_min_size(
vec2(400.0, 200.0), // TODO
@@ -39,7 +39,7 @@ impl Window {
);
let layer = Layer::Window(id);
let where_to_put_background = ctx.graphics.lock().unwrap().layer(layer).len();
let where_to_put_background = ctx.graphics.lock().layer(layer).len();
let style = ctx.style();
let window_padding = style.window_padding;
@@ -69,9 +69,8 @@ impl Window {
state.rect = Rect::from_min_size(state.rect.min(), outer_size);
let mut graphics = ctx.graphics.lock().unwrap();
let graphics = graphics.layer(layer);
graphics.insert(
let mut graphics = ctx.graphics.lock();
graphics.layer(layer).insert(
where_to_put_background,
PaintCmd::Rect {
corner_radius: 5.0,
@@ -89,7 +88,7 @@ impl Window {
state.rect = state.rect.translate(ctx.input().mouse_move);
}
let mut memory = ctx.memory.lock().unwrap();
let mut memory = ctx.memory.lock();
if interact.active || interact.clicked {
memory.move_window_to_top(id);
}