Skip to content

Commit

Permalink
Handle breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lampsitter committed Sep 8, 2024
1 parent c085bf7 commit 85e1b14
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
6 changes: 5 additions & 1 deletion crates/egui_demo_app/src/apps/http_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)))
}

Expand Down
12 changes: 9 additions & 3 deletions crates/egui_demo_lib/src/demo/code_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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))
};
Expand Down
3 changes: 2 additions & 1 deletion crates/egui_demo_lib/src/demo/code_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
33 changes: 21 additions & 12 deletions crates/egui_extras/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -36,13 +46,12 @@ pub fn highlight(ui: &egui::Ui, theme: &CodeTheme, code: &str, language: &str) -

type HighlightCache = egui::util::cache::FrameCache<LayoutJob, Highlighter>;

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::<HighlightCache>()
.get((&font_id, theme, code, language))
Expand Down Expand Up @@ -199,28 +208,28 @@ 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)
};

#[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)))
});
}
}
Expand Down

0 comments on commit 85e1b14

Please sign in to comment.