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

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>
This commit is contained in:
Emil Ernerfeldt
2022-01-10 23:13:10 +01:00
committed by GitHub
parent 225d2b506d
commit d5673412dd
75 changed files with 550 additions and 495 deletions

View File

@@ -34,7 +34,7 @@ impl epi::App for ColorTest {
"🎨 Color test"
}
fn update(&mut self, ctx: &egui::CtxRef, frame: &epi::Frame) {
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
if frame.is_web() {
ui.label(

View File

@@ -16,7 +16,7 @@ impl epi::App for DemoApp {
fn setup(
&mut self,
_ctx: &egui::CtxRef,
_ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
) {
@@ -31,7 +31,7 @@ impl epi::App for DemoApp {
epi::set_value(storage, epi::APP_KEY, self);
}
fn update(&mut self, ctx: &egui::CtxRef, _frame: &epi::Frame) {
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {
self.demo_windows.ui(ctx);
}
}

View File

@@ -26,7 +26,7 @@ impl super::Demo for CodeEditor {
"🖮 Code Editor"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
use super::View as _;
egui::Window::new(self.name())
.open(open)

View File

@@ -68,7 +68,7 @@ impl super::Demo for CodeExample {
"🖮 Code Example"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
use super::View;
egui::Window::new(self.name())
.open(open)

View File

@@ -47,7 +47,7 @@ impl super::Demo for ContextMenus {
"☰ Context Menus"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
use super::View;
egui::Window::new(self.name())
.vscroll(false)

View File

@@ -10,7 +10,7 @@ impl super::Demo for DancingStrings {
"♫ Dancing Strings"
}
fn show(&mut self, ctx: &CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)

View File

@@ -1,5 +1,5 @@
use super::Demo;
use egui::{CtxRef, ScrollArea, Ui};
use egui::{Context, ScrollArea, Ui};
use std::collections::BTreeSet;
// ----------------------------------------------------------------------------
@@ -58,7 +58,7 @@ impl Demos {
}
}
pub fn windows(&mut self, ctx: &CtxRef) {
pub fn windows(&mut self, ctx: &Context) {
let Self { demos, open } = self;
for demo in demos {
let mut is_open = open.contains(demo.name());
@@ -113,7 +113,7 @@ impl Tests {
}
}
pub fn windows(&mut self, ctx: &CtxRef) {
pub fn windows(&mut self, ctx: &Context) {
let Self { demos, open } = self;
for demo in demos {
let mut is_open = open.contains(demo.name());
@@ -149,7 +149,7 @@ pub struct DemoWindows {
impl DemoWindows {
/// Show the app ui (menu bar and windows).
/// `sidebar_ui` can be used to optionally show some things in the sidebar
pub fn ui(&mut self, ctx: &CtxRef) {
pub fn ui(&mut self, ctx: &Context) {
let Self { demos, tests } = self;
egui::SidePanel::right("egui_demo_panel")
@@ -214,7 +214,7 @@ impl DemoWindows {
}
/// Show the open windows.
fn windows(&mut self, ctx: &CtxRef) {
fn windows(&mut self, ctx: &Context) {
let Self { demos, tests } = self;
demos.windows(ctx);

View File

@@ -25,7 +25,7 @@ pub fn drag_source(ui: &mut Ui, id: Id, body: impl FnOnce(&mut Ui)) {
// (anything with `Order::Tooltip` always gets an empty `Response`)
// So this is fine!
if let Some(pointer_pos) = ui.input().pointer.interact_pos() {
if let Some(pointer_pos) = ui.ctx().pointer_interact_pos() {
let delta = pointer_pos - response.rect.center();
ui.ctx().translate_layer(layer_id, delta);
}
@@ -101,7 +101,7 @@ impl super::Demo for DragAndDropDemo {
"✋ Drag and Drop"
}
fn show(&mut self, ctx: &CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)

View File

@@ -21,7 +21,7 @@ impl super::Demo for FontBook {
"🔤 Font Book"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| {
use super::View as _;
self.ui(ui);

View File

@@ -76,7 +76,7 @@ impl super::Demo for LayoutTest {
"Layout Test"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.resizable(false)

View File

@@ -31,7 +31,7 @@ impl Demo for MiscDemoWindow {
"✨ Misc Demos"
}
fn show(&mut self, ctx: &CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &Context, open: &mut bool) {
Window::new(self.name())
.open(open)
.vscroll(true)
@@ -140,7 +140,8 @@ impl Widgets {
ui.horizontal_wrapped(|ui| {
// Trick so we don't have to add spaces in the text below:
ui.spacing_mut().item_spacing.x = ui.fonts()[TextStyle::Body].glyph_width(' ');
let width = ui.fonts()[TextStyle::Body].glyph_width(' ');
ui.spacing_mut().item_spacing.x = width;
ui.label(RichText::new("Text can have").color(Color32::from_rgb(110, 255, 110)));
ui.colored_label(Color32::from_rgb(128, 140, 255), "color"); // Shortcut version

View File

@@ -45,5 +45,5 @@ pub trait Demo {
fn name(&self) -> &'static str;
/// Show windows, etc
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool);
fn show(&mut self, ctx: &egui::Context, open: &mut bool);
}

View File

@@ -26,7 +26,7 @@ impl super::Demo for MultiTouch {
"👌 Multi Touch"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.default_size(vec2(512.0, 512.0))
@@ -77,7 +77,7 @@ impl super::View for MultiTouch {
// color and width:
let mut stroke_width = 1.;
let color = Color32::GRAY;
if let Some(multi_touch) = ui.input().multi_touch() {
if let Some(multi_touch) = ui.ctx().multi_touch() {
// This adjusts the current zoom factor and rotation angle according to the dynamic
// change (for the current frame) of the touch gesture:
self.zoom *= multi_touch.zoom_delta;

View File

@@ -74,7 +74,7 @@ impl super::Demo for Painting {
"🖊 Painting"
}
fn show(&mut self, ctx: &CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)

View File

@@ -647,7 +647,7 @@ impl super::Demo for PlotDemo {
"🗠 Plot"
}
fn show(&mut self, ctx: &CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)

View File

@@ -29,7 +29,7 @@ impl super::Demo for Scrolling {
"↕ Scrolling"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.resizable(false)

View File

@@ -36,7 +36,7 @@ impl super::Demo for Sliders {
"⬌ Sliders"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.resizable(false)

View File

@@ -6,7 +6,7 @@ impl super::Demo for CursorTest {
"Cursor Test"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| {
use super::View as _;
self.ui(ui);
@@ -38,7 +38,7 @@ impl super::Demo for IdTest {
"ID Test"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| {
use super::View as _;
self.ui(ui);
@@ -115,7 +115,7 @@ impl super::Demo for ManualLayoutTest {
"Manual Layout Test"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.resizable(false)
.open(open)
@@ -202,7 +202,7 @@ impl super::Demo for TableTest {
"Table Test"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| {
use super::View as _;
self.ui(ui);
@@ -314,7 +314,7 @@ impl super::Demo for InputTest {
"Input Test"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.resizable(false)
@@ -383,7 +383,7 @@ impl super::Demo for WindowResizeTest {
"↔ Window Resize"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
use egui::*;
Window::new("↔ auto-sized")

View File

@@ -19,7 +19,7 @@ impl super::Demo for TextEdit {
"🖹 TextEdit"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.resizable(false)

View File

@@ -39,7 +39,7 @@ impl super::Demo for WidgetGallery {
"🗄 Widget Gallery"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.resizable(true)

View File

@@ -36,7 +36,7 @@ impl super::Demo for WindowOptions {
"🗖 Window Options"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
let Self {
title,
title_bar,

View File

@@ -7,7 +7,7 @@ impl super::Demo for WindowWithPanels {
"🗖 Window With Panels"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
use super::View as _;
let window = egui::Window::new("Window with Panels")
.default_width(600.0)

View File

@@ -37,7 +37,7 @@ impl epi::App for FractalClock {
"🕑 Fractal Clock"
}
fn update(&mut self, ctx: &egui::CtxRef, _frame: &epi::Frame) {
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {
egui::CentralPanel::default()
.frame(Frame::dark_canvas(&ctx.style()))
.show(ctx, |ui| self.ui(ui, crate::seconds_since_midnight()));

View File

@@ -67,7 +67,7 @@ impl epi::App for HttpApp {
"⬇ HTTP"
}
fn update(&mut self, ctx: &egui::CtxRef, frame: &epi::Frame) {
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
if let Some(receiver) = &mut self.in_progress {
// Are we there yet?
if let Ok(result) = receiver.try_recv() {

View File

@@ -78,7 +78,7 @@ impl Default for BackendPanel {
}
impl BackendPanel {
pub fn update(&mut self, ctx: &egui::CtxRef, frame: &epi::Frame) {
pub fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
self.frame_history
.on_new_frame(ctx.input().time, frame.info().cpu_usage);
@@ -88,7 +88,7 @@ impl BackendPanel {
}
}
pub fn end_of_frame(&mut self, ctx: &egui::CtxRef) {
pub fn end_of_frame(&mut self, ctx: &egui::Context) {
self.egui_windows.windows(ctx);
}
@@ -325,7 +325,7 @@ impl EguiWindows {
ui.checkbox(output_events, "📤 Output Events");
}
fn windows(&mut self, ctx: &egui::CtxRef) {
fn windows(&mut self, ctx: &egui::Context) {
let Self {
settings,
inspection,

View File

@@ -34,7 +34,7 @@ impl epi::App for EasyMarkEditor {
"🖹 EasyMark editor"
}
fn update(&mut self, ctx: &egui::CtxRef, _frame: &epi::Frame) {
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {
egui::TopBottomPanel::bottom("easy_mark_bottom").show(ctx, |ui| {
let layout = egui::Layout::top_down(egui::Align::Center).with_main_justify(true);
ui.allocate_ui_with_layout(ui.available_size(), layout, |ui| {

View File

@@ -18,7 +18,8 @@ pub fn easy_mark_it<'em>(ui: &mut Ui, items: impl Iterator<Item = easy_mark::Ite
ui.allocate_ui_with_layout(initial_size, layout, |ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.set_row_height(ui.fonts()[TextStyle::Body].row_height());
let row_height = (*ui.fonts())[TextStyle::Body].row_height();
ui.set_row_height(row_height);
for item in items {
item_ui(ui, item);

View File

@@ -94,7 +94,7 @@ impl FrameHistory {
let cpu_usage = to_screen.inverse().transform_pos(pointer_pos).y;
let text = format!("{:.1} ms", 1e3 * cpu_usage);
shapes.push(Shape::text(
ui.fonts(),
&*ui.fonts(),
pos2(rect.left(), y),
egui::Align2::LEFT_BOTTOM,
text,

View File

@@ -140,7 +140,7 @@ Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, tur
#[test]
fn test_egui_e2e() {
let mut demo_windows = crate::DemoWindows::default();
let mut ctx = egui::CtxRef::default();
let ctx = egui::Context::default();
let raw_input = egui::RawInput::default();
const NUM_FRAMES: usize = 5;
@@ -156,7 +156,7 @@ fn test_egui_e2e() {
#[test]
fn test_egui_zero_window_size() {
let mut demo_windows = crate::DemoWindows::default();
let mut ctx = egui::CtxRef::default();
let ctx = egui::Context::default();
let raw_input = egui::RawInput {
screen_rect: Some(egui::Rect::from_min_max(egui::Pos2::ZERO, egui::Pos2::ZERO)),
..Default::default()

View File

@@ -44,7 +44,7 @@ impl epi::App for WrapApp {
fn setup(
&mut self,
_ctx: &egui::CtxRef,
_ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
) {
@@ -72,7 +72,7 @@ impl epi::App for WrapApp {
cfg!(not(debug_assertions))
}
fn update(&mut self, ctx: &egui::CtxRef, frame: &epi::Frame) {
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
if let Some(web_info) = frame.info().web_info.as_ref() {
if let Some(anchor) = web_info.web_location_hash.strip_prefix('#') {
self.selected_anchor = anchor.to_owned();
@@ -165,7 +165,7 @@ impl WrapApp {
});
}
fn ui_file_drag_and_drop(&mut self, ctx: &egui::CtxRef) {
fn ui_file_drag_and_drop(&mut self, ctx: &egui::Context) {
use egui::*;
// Preview hovering files: