Skip to content

Commit d567341

Browse files
emilkdanielkeller
andauthored
Put everything in Context behind the same Mutex (#1050)
* Move all interior mutability from Context to CtxRef and make it a handle * Rename `CtxRef` to `Context` * The old `Context` is now `ContextImpl` and is non-pub * Add benchmark Painter::rect Co-authored-by: Daniel Keller <dklr433@gmail.com>
1 parent 225d2b5 commit d567341

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+549
-494
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
1111
* Added `Ui::add_visible` and `Ui::add_visible_ui`.
1212

1313
### Changed 🔧
14+
* ⚠️ `Context::input` and `Ui::input` now locks a mutex. This can lead to a dead-lock is used in an `if let` binding!
15+
* `if let Some(pos) = ui.input().pointer.latest_pos()` and similar must now be rewritten on two lines, or with added `{}` around the righ-hand-side.
16+
* Search for this problem in your code using the regex `if let .*input`.
17+
* Renamed `CtxRef` to `Context` ([#1050](https://github.com/emilk/egui/pull/1050)).
18+
* `Context` can now be cloned and stored between frames ([#1050](https://github.com/emilk/egui/pull/1050)).
1419
* Renamed `Ui::visible` to `Ui::is_visible`.
1520
* Split `Event::Text` into `Event::Text` and `Event::Paste` ([#1058](https://github.com/emilk/egui/pull/1058)).
1621

1722
### Fixed 🐛
1823
* Context menu now respects the theme ([#1043](https://github.com/emilk/egui/pull/1043))
1924

25+
### Contributors 🙏
26+
* [danielkeller](https://github.com/danielkeller): [#1050](https://github.com/emilk/egui/pull/1050).
27+
28+
2029
## 0.16.1 - 2021-12-31 - Add back `CtxRef::begin_frame,end_frame`
2130

2231
### Added ⭐

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ Notable contributions by:
365365
* [@AlexApps99](https://github.com/AlexApps99): [`egui_glow`](https://github.com/emilk/egui/pull/685).
366366
* [@mankinskin](https://github.com/mankinskin): [Context menus](https://github.com/emilk/egui/pull/543).
367367
* [@t18b219k](https://github.com/t18b219k): [Port glow painter to web](https://github.com/emilk/egui/pull/868).
368+
* [@danielkeller](https://github.com/danielkeller): [`Context` refactor](https://github.com/emilk/egui/pull/1050).
368369
* And [many more](https://github.com/emilk/egui/graphs/contributors?type=a).
369370

370371
egui is licensed under [MIT](LICENSE-MIT) OR [Apache-2.0](LICENSE-APACHE).

eframe/examples/custom_font.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl epi::App for MyApp {
1919

2020
fn setup(
2121
&mut self,
22-
ctx: &egui::CtxRef,
22+
ctx: &egui::Context,
2323
_frame: &epi::Frame,
2424
_storage: Option<&dyn epi::Storage>,
2525
) {
@@ -51,7 +51,7 @@ impl epi::App for MyApp {
5151
ctx.set_fonts(fonts);
5252
}
5353

54-
fn update(&mut self, ctx: &egui::CtxRef, _frame: &epi::Frame) {
54+
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {
5555
egui::CentralPanel::default().show(ctx, |ui| {
5656
ui.heading("egui using custom fonts");
5757
ui.text_edit_multiline(&mut self.text);

eframe/examples/file_dialog.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl epi::App for MyApp {
1313
"Native file dialogs and drag-and-drop files"
1414
}
1515

16-
fn update(&mut self, ctx: &egui::CtxRef, _frame: &epi::Frame) {
16+
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {
1717
egui::CentralPanel::default().show(ctx, |ui| {
1818
ui.label("Drag-and-drop files onto the window!");
1919

@@ -57,7 +57,7 @@ impl epi::App for MyApp {
5757
}
5858

5959
impl MyApp {
60-
fn detect_files_being_dropped(&mut self, ctx: &egui::CtxRef) {
60+
fn detect_files_being_dropped(&mut self, ctx: &egui::Context) {
6161
use egui::*;
6262

6363
// Preview hovering files:

eframe/examples/hello_world.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl epi::App for MyApp {
2121
"My egui App"
2222
}
2323

24-
fn update(&mut self, ctx: &egui::CtxRef, frame: &epi::Frame) {
24+
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
2525
let Self { name, age } = self;
2626

2727
egui::CentralPanel::default().show(ctx, |ui| {

eframe/examples/image.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl epi::App for MyApp {
1212
"Show an image with eframe/egui"
1313
}
1414

15-
fn update(&mut self, ctx: &egui::CtxRef, frame: &epi::Frame) {
15+
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
1616
if self.texture.is_none() {
1717
// Load the image:
1818
let image_data = include_bytes!("rust-logo-256x256.png");

eframe/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! "My egui App"
2727
//! }
2828
//!
29-
//! fn update(&mut self, ctx: &egui::CtxRef, frame: &epi::Frame) {
29+
//! fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
3030
//! egui::CentralPanel::default().show(ctx, |ui| {
3131
//! ui.heading("Hello World!");
3232
//! });
@@ -129,7 +129,7 @@ pub fn start_web(canvas_id: &str, app: Box<dyn epi::App>) -> Result<(), wasm_bin
129129
/// "My egui App"
130130
/// }
131131
///
132-
/// fn update(&mut self, ctx: &egui::CtxRef, frame: &epi::Frame) {
132+
/// fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
133133
/// egui::CentralPanel::default().show(ctx, |ui| {
134134
/// ui.heading("Hello World!");
135135
/// });
@@ -160,7 +160,7 @@ pub fn run_native(app: Box<dyn epi::App>, native_options: epi::NativeOptions) ->
160160
/// "My egui App"
161161
/// }
162162
///
163-
/// fn update(&mut self, ctx: &egui::CtxRef, frame: &epi::Frame) {
163+
/// fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
164164
/// egui::CentralPanel::default().show(ctx, |ui| {
165165
/// ui.heading("Hello World!");
166166
/// });

egui-winit/src/epi.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl Persistence {
191191
pub struct EpiIntegration {
192192
frame: epi::Frame,
193193
persistence: crate::epi::Persistence,
194-
pub egui_ctx: egui::CtxRef,
194+
pub egui_ctx: egui::Context,
195195
egui_winit: crate::State,
196196
pub app: Box<dyn epi::App>,
197197
/// When set, it is time to quit
@@ -206,7 +206,7 @@ impl EpiIntegration {
206206
persistence: crate::epi::Persistence,
207207
app: Box<dyn epi::App>,
208208
) -> Self {
209-
let egui_ctx = egui::CtxRef::default();
209+
let egui_ctx = egui::Context::default();
210210

211211
*egui_ctx.memory() = persistence.load_memory().unwrap_or_default();
212212

@@ -250,7 +250,7 @@ impl EpiIntegration {
250250
}
251251

252252
fn warm_up(&mut self, window: &winit::window::Window) {
253-
let saved_memory = self.egui_ctx.memory().clone();
253+
let saved_memory: egui::Memory = self.egui_ctx.memory().clone();
254254
self.egui_ctx.memory().set_everything_is_visible(true);
255255
let (_, tex_alloc_data, _) = self.update(window);
256256
self.frame.lock().output.tex_allocation_data = tex_alloc_data; // handle it next frame

egui/src/containers/area.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub(crate) struct Prepared {
176176
impl Area {
177177
pub fn show<R>(
178178
self,
179-
ctx: &CtxRef,
179+
ctx: &Context,
180180
add_contents: impl FnOnce(&mut Ui) -> R,
181181
) -> InnerResponse<R> {
182182
let prepared = self.begin(ctx);
@@ -186,7 +186,7 @@ impl Area {
186186
InnerResponse { inner, response }
187187
}
188188

189-
pub(crate) fn begin(self, ctx: &CtxRef) -> Prepared {
189+
pub(crate) fn begin(self, ctx: &Context) -> Prepared {
190190
let Area {
191191
id,
192192
movable,
@@ -234,7 +234,7 @@ impl Area {
234234
}
235235
}
236236

237-
pub fn show_open_close_animation(&self, ctx: &CtxRef, frame: &Frame, is_open: bool) {
237+
pub fn show_open_close_animation(&self, ctx: &Context, frame: &Frame, is_open: bool) {
238238
// must be called first so animation managers know the latest state
239239
let visibility_factor = ctx.animate_bool(self.id.with("close_animation"), is_open);
240240

@@ -276,7 +276,7 @@ impl Prepared {
276276
self.drag_bounds
277277
}
278278

279-
pub(crate) fn content_ui(&self, ctx: &CtxRef) -> Ui {
279+
pub(crate) fn content_ui(&self, ctx: &Context) -> Ui {
280280
let screen_rect = ctx.input().screen_rect();
281281

282282
let bounds = if let Some(bounds) = self.drag_bounds {
@@ -317,7 +317,7 @@ impl Prepared {
317317
}
318318

319319
#[allow(clippy::needless_pass_by_value)] // intentional to swallow up `content_ui`.
320-
pub(crate) fn end(self, ctx: &CtxRef, content_ui: Ui) -> Response {
320+
pub(crate) fn end(self, ctx: &Context, content_ui: Ui) -> Response {
321321
let Prepared {
322322
layer_id,
323323
mut state,
@@ -370,8 +370,9 @@ impl Prepared {
370370
}
371371

372372
fn pointer_pressed_on_area(ctx: &Context, layer_id: LayerId) -> bool {
373-
if let Some(pointer_pos) = ctx.input().pointer.interact_pos() {
374-
ctx.input().pointer.any_pressed() && ctx.layer_id_at(pointer_pos) == Some(layer_id)
373+
if let Some(pointer_pos) = ctx.pointer_interact_pos() {
374+
let any_pressed = ctx.input().pointer.any_pressed();
375+
any_pressed && ctx.layer_id_at(pointer_pos) == Some(layer_id)
375376
} else {
376377
false
377378
}

egui/src/containers/panel.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl SidePanel {
199199
let mut is_resizing = false;
200200
if resizable {
201201
let resize_id = id.with("__resize");
202-
if let Some(pointer) = ui.input().pointer.latest_pos() {
202+
if let Some(pointer) = ui.ctx().latest_pointer_pos() {
203203
let we_are_on_top = ui
204204
.ctx()
205205
.layer_id_at(pointer)
@@ -284,7 +284,7 @@ impl SidePanel {
284284
/// Show the panel at the top level.
285285
pub fn show<R>(
286286
self,
287-
ctx: &CtxRef,
287+
ctx: &Context,
288288
add_contents: impl FnOnce(&mut Ui) -> R,
289289
) -> InnerResponse<R> {
290290
self.show_dyn(ctx, Box::new(add_contents))
@@ -293,7 +293,7 @@ impl SidePanel {
293293
/// Show the panel at the top level.
294294
fn show_dyn<'c, R>(
295295
self,
296-
ctx: &CtxRef,
296+
ctx: &Context,
297297
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
298298
) -> InnerResponse<R> {
299299
let layer_id = LayerId::background();
@@ -485,7 +485,8 @@ impl TopBottomPanel {
485485
let mut is_resizing = false;
486486
if resizable {
487487
let resize_id = id.with("__resize");
488-
if let Some(pointer) = ui.input().pointer.latest_pos() {
488+
let latest_pos = ui.input().pointer.latest_pos();
489+
if let Some(pointer) = latest_pos {
489490
let we_are_on_top = ui
490491
.ctx()
491492
.layer_id_at(pointer)
@@ -570,7 +571,7 @@ impl TopBottomPanel {
570571
/// Show the panel at the top level.
571572
pub fn show<R>(
572573
self,
573-
ctx: &CtxRef,
574+
ctx: &Context,
574575
add_contents: impl FnOnce(&mut Ui) -> R,
575576
) -> InnerResponse<R> {
576577
self.show_dyn(ctx, Box::new(add_contents))
@@ -579,7 +580,7 @@ impl TopBottomPanel {
579580
/// Show the panel at the top level.
580581
fn show_dyn<'c, R>(
581582
self,
582-
ctx: &CtxRef,
583+
ctx: &Context,
583584
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
584585
) -> InnerResponse<R> {
585586
let layer_id = LayerId::background();
@@ -670,7 +671,7 @@ impl CentralPanel {
670671
/// Show the panel at the top level.
671672
pub fn show<R>(
672673
self,
673-
ctx: &CtxRef,
674+
ctx: &Context,
674675
add_contents: impl FnOnce(&mut Ui) -> R,
675676
) -> InnerResponse<R> {
676677
self.show_dyn(ctx, Box::new(add_contents))
@@ -679,7 +680,7 @@ impl CentralPanel {
679680
/// Show the panel at the top level.
680681
fn show_dyn<'c, R>(
681682
self,
682-
ctx: &CtxRef,
683+
ctx: &Context,
683684
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
684685
) -> InnerResponse<R> {
685686
let available_rect = ctx.available_rect();

egui/src/containers/popup.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ impl MonoState {
6666
/// }
6767
/// # });
6868
/// ```
69-
pub fn show_tooltip<R>(ctx: &CtxRef, id: Id, add_contents: impl FnOnce(&mut Ui) -> R) -> Option<R> {
69+
pub fn show_tooltip<R>(
70+
ctx: &Context,
71+
id: Id,
72+
add_contents: impl FnOnce(&mut Ui) -> R,
73+
) -> Option<R> {
7074
show_tooltip_at_pointer(ctx, id, add_contents)
7175
}
7276

@@ -88,7 +92,7 @@ pub fn show_tooltip<R>(ctx: &CtxRef, id: Id, add_contents: impl FnOnce(&mut Ui)
8892
/// # });
8993
/// ```
9094
pub fn show_tooltip_at_pointer<R>(
91-
ctx: &CtxRef,
95+
ctx: &Context,
9296
id: Id,
9397
add_contents: impl FnOnce(&mut Ui) -> R,
9498
) -> Option<R> {
@@ -104,7 +108,7 @@ pub fn show_tooltip_at_pointer<R>(
104108
///
105109
/// If the tooltip does not fit under the area, it tries to place it above it instead.
106110
pub fn show_tooltip_for<R>(
107-
ctx: &CtxRef,
111+
ctx: &Context,
108112
id: Id,
109113
rect: &Rect,
110114
add_contents: impl FnOnce(&mut Ui) -> R,
@@ -129,7 +133,7 @@ pub fn show_tooltip_for<R>(
129133
///
130134
/// Returns `None` if the tooltip could not be placed.
131135
pub fn show_tooltip_at<R>(
132-
ctx: &CtxRef,
136+
ctx: &Context,
133137
id: Id,
134138
suggested_position: Option<Pos2>,
135139
add_contents: impl FnOnce(&mut Ui) -> R,
@@ -146,7 +150,7 @@ pub fn show_tooltip_at<R>(
146150
}
147151

148152
fn show_tooltip_at_avoid_dyn<'c, R>(
149-
ctx: &CtxRef,
153+
ctx: &Context,
150154
mut id: Id,
151155
suggested_position: Option<Pos2>,
152156
above: bool,
@@ -229,15 +233,15 @@ fn show_tooltip_at_avoid_dyn<'c, R>(
229233
/// }
230234
/// # });
231235
/// ```
232-
pub fn show_tooltip_text(ctx: &CtxRef, id: Id, text: impl Into<WidgetText>) -> Option<()> {
236+
pub fn show_tooltip_text(ctx: &Context, id: Id, text: impl Into<WidgetText>) -> Option<()> {
233237
show_tooltip(ctx, id, |ui| {
234238
crate::widgets::Label::new(text).ui(ui);
235239
})
236240
}
237241

238242
/// Show a pop-over window.
239243
fn show_tooltip_area_dyn<'c, R>(
240-
ctx: &CtxRef,
244+
ctx: &Context,
241245
id: Id,
242246
window_pos: Pos2,
243247
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,

egui/src/containers/scroll_area.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,11 @@ impl Prepared {
523523
};
524524
let content_response = ui.interact(inner_rect, id.with("area"), sense);
525525

526-
let input = ui.input();
527526
if content_response.dragged() {
528527
for d in 0..2 {
529528
if has_bar[d] {
530-
state.offset[d] -= input.pointer.delta()[d];
531-
state.vel[d] = input.pointer.velocity()[d];
529+
state.offset[d] -= ui.input().pointer.delta()[d];
530+
state.vel[d] = ui.input().pointer.velocity()[d];
532531
state.scroll_stuck_to_end[d] = false;
533532
} else {
534533
state.vel[d] = 0.0;
@@ -537,7 +536,7 @@ impl Prepared {
537536
} else {
538537
let stop_speed = 20.0; // Pixels per second.
539538
let friction_coeff = 1000.0; // Pixels per second squared.
540-
let dt = input.unstable_dt;
539+
let dt = ui.input().unstable_dt;
541540

542541
let friction = friction_coeff * dt;
543542
if friction > state.vel.length() || state.vel.length() < stop_speed {

egui/src/containers/window.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,15 @@ impl<'open> Window<'open> {
235235
#[inline]
236236
pub fn show<R>(
237237
self,
238-
ctx: &CtxRef,
238+
ctx: &Context,
239239
add_contents: impl FnOnce(&mut Ui) -> R,
240240
) -> Option<InnerResponse<Option<R>>> {
241241
self.show_dyn(ctx, Box::new(add_contents))
242242
}
243243

244244
fn show_dyn<'c, R>(
245245
self,
246-
ctx: &CtxRef,
246+
ctx: &Context,
247247
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
248248
) -> Option<InnerResponse<Option<R>>> {
249249
let Window {
@@ -296,7 +296,7 @@ impl<'open> Window<'open> {
296296
.and_then(|window_interaction| {
297297
// Calculate roughly how much larger the window size is compared to the inner rect
298298
let title_bar_height = if with_title_bar {
299-
title.font_height(ctx.fonts(), &ctx.style()) + title_content_spacing
299+
title.font_height(ctx) + title_content_spacing
300300
} else {
301301
0.0
302302
};
@@ -757,7 +757,7 @@ fn show_title_bar(
757757
) -> TitleBar {
758758
let inner_response = ui.horizontal(|ui| {
759759
let height = title
760-
.font_height(ui.fonts(), ui.style())
760+
.font_height(ui.ctx())
761761
.max(ui.spacing().interact_size.y);
762762
ui.set_min_height(height);
763763

0 commit comments

Comments
 (0)