From 85e1b141ce3a373928bf58d3c569db013562d646 Mon Sep 17 00:00:00 2001 From: Erlend Walstad <96946613+lampsitter@users.noreply.github.com> Date: Sun, 8 Sep 2024 12:12:40 +0200 Subject: [PATCH] Handle breaking changes --- crates/egui_demo_app/src/apps/http_app.rs | 6 +++- crates/egui_demo_lib/src/demo/code_editor.rs | 12 +++++-- crates/egui_demo_lib/src/demo/code_example.rs | 3 +- crates/egui_demo_lib/src/lib.rs | 2 +- crates/egui_extras/src/syntax_highlighting.rs | 33 ++++++++++++------- 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/crates/egui_demo_app/src/apps/http_app.rs b/crates/egui_demo_app/src/apps/http_app.rs index d6b57284267..abc728fd5b2 100644 --- a/crates/egui_demo_app/src/apps/http_app.rs +++ b/crates/egui_demo_app/src/apps/http_app.rs @@ -224,7 +224,11 @@ fn syntax_highlighting( let extension = extension_and_rest.first()?; let theme = egui_extras::syntax_highlighting::CodeTheme::from_style(&ctx.style()); Some(ColoredText(egui_extras::syntax_highlighting::highlight( - ctx, &theme, text, extension, + ctx, + &ctx.style(), + &theme, + text, + extension, ))) } diff --git a/crates/egui_demo_lib/src/demo/code_editor.rs b/crates/egui_demo_lib/src/demo/code_editor.rs index 4dad60d3cb4..fe39e1be8f1 100644 --- a/crates/egui_demo_lib/src/demo/code_editor.rs +++ b/crates/egui_demo_lib/src/demo/code_editor.rs @@ -67,7 +67,8 @@ impl crate::View for CodeEditor { }); } - let mut theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx()); + let mut theme = + egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx(), ui.style()); ui.collapsing("Theme", |ui| { ui.group(|ui| { theme.ui(ui); @@ -76,8 +77,13 @@ impl crate::View for CodeEditor { }); let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| { - let mut layout_job = - egui_extras::syntax_highlighting::highlight(ui.ctx(), &theme, string, language); + let mut layout_job = egui_extras::syntax_highlighting::highlight( + ui.ctx(), + ui.style(), + &theme, + string, + language, + ); layout_job.wrap.max_width = wrap_width; ui.fonts(|f| f.layout_job(layout_job)) }; diff --git a/crates/egui_demo_lib/src/demo/code_example.rs b/crates/egui_demo_lib/src/demo/code_example.rs index 18a251077b2..3db90ad3e34 100644 --- a/crates/egui_demo_lib/src/demo/code_example.rs +++ b/crates/egui_demo_lib/src/demo/code_example.rs @@ -130,7 +130,8 @@ impl crate::View for CodeExample { ui.separator(); - let mut theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx()); + let mut theme = + egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx(), ui.style()); ui.collapsing("Theme", |ui| { theme.ui(ui); theme.store_in_memory(ui.ctx()); diff --git a/crates/egui_demo_lib/src/lib.rs b/crates/egui_demo_lib/src/lib.rs index ce18e0910ee..23c28bcb65c 100644 --- a/crates/egui_demo_lib/src/lib.rs +++ b/crates/egui_demo_lib/src/lib.rs @@ -21,7 +21,7 @@ pub use rendering_test::ColorTest; /// View some Rust code with syntax highlighting and selection. pub(crate) fn rust_view_ui(ui: &mut egui::Ui, code: &str) { let language = "rs"; - let theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx()); + let theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx(), ui.style()); egui_extras::syntax_highlighting::code_view_ui(ui, &theme, code, language); } diff --git a/crates/egui_extras/src/syntax_highlighting.rs b/crates/egui_extras/src/syntax_highlighting.rs index 85f352591cd..e782677c95c 100644 --- a/crates/egui_extras/src/syntax_highlighting.rs +++ b/crates/egui_extras/src/syntax_highlighting.rs @@ -15,14 +15,24 @@ pub fn code_view_ui( code: &str, language: &str, ) -> egui::Response { - let layout_job = highlight(ui, theme, code, language); + let layout_job = highlight(ui.ctx(), ui.style(), theme, code, language); ui.add(egui::Label::new(layout_job).selectable(true)) } /// Add syntax highlighting to a code string. /// /// The results are memoized, so you can call this every frame without performance penalty. -pub fn highlight(ui: &egui::Ui, theme: &CodeTheme, code: &str, language: &str) -> LayoutJob { +pub fn highlight( + ctx: &egui::Context, + style: &egui::Style, + theme: &CodeTheme, + code: &str, + language: &str, +) -> LayoutJob { + // We take in both context and style so that in situations where ui is not available such as when + // performing it at a separate thread (ctx, ctx.style()) can be used and when ui is available + // (ui.ctx(), ui.style()) can be used + impl egui::util::cache::ComputerMut<(&egui::FontId, &CodeTheme, &str, &str), LayoutJob> for Highlighter { @@ -36,13 +46,12 @@ pub fn highlight(ui: &egui::Ui, theme: &CodeTheme, code: &str, language: &str) - type HighlightCache = egui::util::cache::FrameCache; - let font_id = ui - .style() + let font_id = style .override_font_id .clone() - .unwrap_or_else(|| TextStyle::Monospace.resolve(ui.style())); + .unwrap_or_else(|| TextStyle::Monospace.resolve(style)); - ui.ctx().memory_mut(|mem| { + ctx.memory_mut(|mem| { mem.caches .cache::() .get((&font_id, theme, code, language)) @@ -199,10 +208,10 @@ impl CodeTheme { /// Load code theme from egui memory. /// /// There is one dark and one light theme stored at any one time. - pub fn from_memory(ui: &egui::Ui) -> Self { + pub fn from_memory(ctx: &egui::Context, style: &egui::Style) -> Self { #![allow(clippy::needless_return)] - let (id, default) = if ui.style().visuals.dark_mode { + let (id, default) = if style.visuals.dark_mode { (egui::Id::new("dark"), Self::dark as fn(f32) -> Self) } else { (egui::Id::new("light"), Self::light as fn(f32) -> Self) @@ -210,17 +219,17 @@ impl CodeTheme { #[cfg(feature = "serde")] { - return ui.ctx().data_mut(|d| { + return ctx.data_mut(|d| { d.get_persisted(id) - .unwrap_or_else(|| default(monospace_font_size(ui.style()))) + .unwrap_or_else(|| default(monospace_font_size(style))) }); } #[cfg(not(feature = "serde"))] { - return ui.ctx().data_mut(|d| { + return ctx.data_mut(|d| { d.get_temp(id) - .unwrap_or_else(|| default(monospace_font_size(ui.style()))) + .unwrap_or_else(|| default(monospace_font_size(style))) }); } }