1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 14:20:04 -04:00

Avoid deadlocks by using lambdas for context lock (#2625)

ctx.input().key_pressed(Key::A) -> ctx.input(|i| i.key_pressed(Key::A))
This commit is contained in:
Emil Ernerfeldt
2023-01-25 10:24:23 +01:00
committed by GitHub
parent eee4cf6a82
commit 8ce0e1c520
77 changed files with 1445 additions and 1123 deletions

View File

@@ -64,9 +64,7 @@ impl<'a> Widget for DatePickerButton<'a> {
fn ui(self, ui: &mut Ui) -> egui::Response {
let id = ui.make_persistent_id(self.id_source);
let mut button_state = ui
.memory()
.data
.get_persisted::<DatePickerButtonState>(id)
.memory_mut(|mem| mem.data.get_persisted::<DatePickerButtonState>(id))
.unwrap_or_default();
let mut text = RichText::new(format!("{} 📆", self.selection.format("%Y-%m-%d")));
@@ -81,7 +79,7 @@ impl<'a> Widget for DatePickerButton<'a> {
let mut button_response = ui.add(button);
if button_response.clicked() {
button_state.picker_visible = true;
ui.memory().data.insert_persisted(id, button_state.clone());
ui.memory_mut(|mem| mem.data.insert_persisted(id, button_state.clone()));
}
if button_state.picker_visible {
@@ -131,10 +129,10 @@ impl<'a> Widget for DatePickerButton<'a> {
}
if !button_response.clicked()
&& (ui.input().key_pressed(Key::Escape) || area_response.clicked_elsewhere())
&& (ui.input(|i| i.key_pressed(Key::Escape)) || area_response.clicked_elsewhere())
{
button_state.picker_visible = false;
ui.memory().data.insert_persisted(id, button_state);
ui.memory_mut(|mem| mem.data.insert_persisted(id, button_state));
}
}

View File

@@ -41,16 +41,14 @@ impl<'a> DatePickerPopup<'a> {
let id = ui.make_persistent_id("date_picker");
let today = chrono::offset::Utc::now().date_naive();
let mut popup_state = ui
.memory()
.data
.get_persisted::<DatePickerPopupState>(id)
.memory_mut(|mem| mem.data.get_persisted::<DatePickerPopupState>(id))
.unwrap_or_default();
if !popup_state.setup {
popup_state.year = self.selection.year();
popup_state.month = self.selection.month();
popup_state.day = self.selection.day();
popup_state.setup = true;
ui.memory().data.insert_persisted(id, popup_state.clone());
ui.memory_mut(|mem| mem.data.insert_persisted(id, popup_state.clone()));
}
let weeks = month_data(popup_state.year, popup_state.month);
@@ -93,9 +91,10 @@ impl<'a> DatePickerPopup<'a> {
popup_state.day = popup_state
.day
.min(popup_state.last_day_of_month());
ui.memory()
.data
.insert_persisted(id, popup_state.clone());
ui.memory_mut(|mem| {
mem.data
.insert_persisted(id, popup_state.clone());
});
}
}
});
@@ -116,9 +115,10 @@ impl<'a> DatePickerPopup<'a> {
popup_state.day = popup_state
.day
.min(popup_state.last_day_of_month());
ui.memory()
.data
.insert_persisted(id, popup_state.clone());
ui.memory_mut(|mem| {
mem.data
.insert_persisted(id, popup_state.clone());
});
}
}
});
@@ -136,9 +136,10 @@ impl<'a> DatePickerPopup<'a> {
)
.changed()
{
ui.memory()
.data
.insert_persisted(id, popup_state.clone());
ui.memory_mut(|mem| {
mem.data
.insert_persisted(id, popup_state.clone());
});
}
}
});
@@ -160,7 +161,9 @@ impl<'a> DatePickerPopup<'a> {
popup_state.year -= 1;
popup_state.day =
popup_state.day.min(popup_state.last_day_of_month());
ui.memory().data.insert_persisted(id, popup_state.clone());
ui.memory_mut(|mem| {
mem.data.insert_persisted(id, popup_state.clone());
});
}
});
});
@@ -178,7 +181,9 @@ impl<'a> DatePickerPopup<'a> {
}
popup_state.day =
popup_state.day.min(popup_state.last_day_of_month());
ui.memory().data.insert_persisted(id, popup_state.clone());
ui.memory_mut(|mem| {
mem.data.insert_persisted(id, popup_state.clone());
});
}
});
});
@@ -194,7 +199,9 @@ impl<'a> DatePickerPopup<'a> {
}
popup_state.day = popup_state.last_day_of_month();
}
ui.memory().data.insert_persisted(id, popup_state.clone());
ui.memory_mut(|mem| {
mem.data.insert_persisted(id, popup_state.clone());
});
}
});
});
@@ -210,7 +217,9 @@ impl<'a> DatePickerPopup<'a> {
popup_state.year += 1;
}
}
ui.memory().data.insert_persisted(id, popup_state.clone());
ui.memory_mut(|mem| {
mem.data.insert_persisted(id, popup_state.clone());
});
}
});
});
@@ -224,7 +233,9 @@ impl<'a> DatePickerPopup<'a> {
}
popup_state.day =
popup_state.day.min(popup_state.last_day_of_month());
ui.memory().data.insert_persisted(id, popup_state.clone());
ui.memory_mut(|mem| {
mem.data.insert_persisted(id, popup_state.clone());
});
}
});
});
@@ -234,7 +245,9 @@ impl<'a> DatePickerPopup<'a> {
popup_state.year += 1;
popup_state.day =
popup_state.day.min(popup_state.last_day_of_month());
ui.memory().data.insert_persisted(id, popup_state.clone());
ui.memory_mut(|mem| {
mem.data.insert_persisted(id, popup_state.clone());
});
}
});
});
@@ -342,10 +355,12 @@ impl<'a> DatePickerPopup<'a> {
popup_state.year = day.year();
popup_state.month = day.month();
popup_state.day = day.day();
ui.memory().data.insert_persisted(
id,
popup_state.clone(),
);
ui.memory_mut(|mem| {
mem.data.insert_persisted(
id,
popup_state.clone(),
);
});
}
},
);
@@ -387,12 +402,12 @@ impl<'a> DatePickerPopup<'a> {
if close {
popup_state.setup = false;
ui.memory().data.insert_persisted(id, popup_state);
ui.memory()
.data
.get_persisted_mut_or_default::<DatePickerButtonState>(self.button_id)
.picker_visible = false;
ui.memory_mut(|mem| {
mem.data.insert_persisted(id, popup_state);
mem.data
.get_persisted_mut_or_default::<DatePickerButtonState>(self.button_id)
.picker_visible = false;
});
}
saved && close

View File

@@ -479,7 +479,7 @@ impl TableState {
let rect = Rect::from_min_size(ui.available_rect_before_wrap().min, Vec2::ZERO);
ui.ctx().check_for_id_clash(state_id, rect, "Table");
if let Some(state) = ui.data().get_persisted::<Self>(state_id) {
if let Some(state) = ui.data_mut(|d| d.get_persisted::<Self>(state_id)) {
// make sure that the stored widths aren't out-dated
if state.column_widths.len() == default_widths.len() {
return (true, state);
@@ -495,7 +495,7 @@ impl TableState {
}
fn store(self, ui: &egui::Ui, state_id: egui::Id) {
ui.data().insert_persisted(state_id, self);
ui.data_mut(|d| d.insert_persisted(state_id, self));
}
}
@@ -680,14 +680,12 @@ impl<'a> Table<'a> {
}
}
let dragging_something_else = {
let pointer = &ui.input().pointer;
pointer.any_down() || pointer.any_pressed()
};
let dragging_something_else =
ui.input(|i| i.pointer.any_down() || i.pointer.any_pressed());
let resize_hover = resize_response.hovered() && !dragging_something_else;
if resize_hover || resize_response.dragged() {
ui.output().cursor_icon = egui::CursorIcon::ResizeColumn;
ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeColumn);
}
let stroke = if resize_response.dragged() {