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

Merge branch 'master' into zlayer

# Conflicts:
#	crates/egui/src/containers/area.rs
#	crates/egui/src/containers/popup.rs
#	crates/egui/src/context.rs
#	crates/egui/src/painter.rs
#	crates/egui/src/ui.rs
This commit is contained in:
Emil Ernerfeldt
2023-01-26 14:52:56 +01:00
96 changed files with 2107 additions and 1374 deletions

View File

@@ -79,7 +79,7 @@ impl super::View for CodeEditor {
let mut layout_job =
crate::syntax_highlighting::highlight(ui.ctx(), &theme, string, language);
layout_job.wrap.max_width = wrap_width;
ui.fonts().layout_job(layout_job)
ui.fonts(|f| f.layout_job(layout_job))
};
egui::ScrollArea::vertical().show(ui, |ui| {

View File

@@ -103,7 +103,7 @@ impl CodeExample {
ui.horizontal(|ui| {
let font_id = egui::TextStyle::Monospace.resolve(ui.style());
let indentation = 8.0 * ui.fonts().glyph_width(&font_id, ' ');
let indentation = 8.0 * ui.fonts(|f| f.glyph_width(&font_id, ' '));
let item_spacing = ui.spacing_mut().item_spacing;
ui.add_space(indentation - item_spacing.x);

View File

@@ -30,7 +30,7 @@ impl super::View for DancingStrings {
Frame::canvas(ui.style()).show(ui, |ui| {
ui.ctx().request_repaint();
let time = ui.input().time;
let time = ui.input(|i| i.time);
let desired_size = ui.available_width() * vec2(1.0, 0.35);
let (_id, rect) = ui.allocate_space(desired_size);

View File

@@ -177,7 +177,7 @@ impl DemoWindows {
fn mobile_ui(&mut self, ctx: &Context) {
if self.about_is_open {
let screen_size = ctx.input().screen_rect.size();
let screen_size = ctx.input(|i| i.screen_rect.size());
let default_width = (screen_size.x - 20.0).min(400.0);
let mut close = false;
@@ -216,7 +216,7 @@ impl DemoWindows {
ui.menu_button(egui::RichText::new("⏷ demos").size(font_size), |ui| {
ui.set_style(ui.ctx().style()); // ignore the "menu" style set by `menu_button`.
self.demo_list_ui(ui);
if ui.ui_contains_pointer() && ui.input().pointer.any_click() {
if ui.ui_contains_pointer() && ui.input(|i| i.pointer.any_click()) {
ui.close_menu();
}
});
@@ -291,7 +291,7 @@ impl DemoWindows {
ui.separator();
if ui.button("Organize windows").clicked() {
ui.ctx().memory().reset_areas();
ui.ctx().memory_mut(|mem| mem.reset_areas());
}
});
});
@@ -309,12 +309,12 @@ fn file_menu_button(ui: &mut Ui) {
// NOTE: we must check the shortcuts OUTSIDE of the actual "File" menu,
// or else they would only be checked if the "File" menu was actually open!
if ui.input_mut().consume_shortcut(&organize_shortcut) {
ui.ctx().memory().reset_areas();
if ui.input_mut(|i| i.consume_shortcut(&organize_shortcut)) {
ui.ctx().memory_mut(|mem| mem.reset_areas());
}
if ui.input_mut().consume_shortcut(&reset_shortcut) {
*ui.ctx().memory() = Default::default();
if ui.input_mut(|i| i.consume_shortcut(&reset_shortcut)) {
ui.ctx().memory_mut(|mem| *mem = Default::default());
}
ui.menu_button("File", |ui| {
@@ -335,7 +335,7 @@ fn file_menu_button(ui: &mut Ui) {
)
.clicked()
{
ui.ctx().memory().reset_areas();
ui.ctx().memory_mut(|mem| mem.reset_areas());
ui.close_menu();
}
@@ -347,7 +347,7 @@ fn file_menu_button(ui: &mut Ui) {
.on_hover_text("Forget scroll, positions, sizes etc")
.clicked()
{
*ui.ctx().memory() = Default::default();
ui.ctx().memory_mut(|mem| *mem = Default::default());
ui.close_menu();
}
});

View File

@@ -1,7 +1,7 @@
use egui::*;
pub fn drag_source(ui: &mut Ui, id: Id, body: impl FnOnce(&mut Ui)) {
let is_being_dragged = ui.memory().is_being_dragged(id);
let is_being_dragged = ui.memory(|mem| mem.is_being_dragged(id));
if !is_being_dragged {
let response = ui.scope(body).response;
@@ -9,10 +9,10 @@ pub fn drag_source(ui: &mut Ui, id: Id, body: impl FnOnce(&mut Ui)) {
// Check for drags:
let response = ui.interact(response.rect, id, Sense::drag());
if response.hovered() {
ui.output().cursor_icon = CursorIcon::Grab;
ui.ctx().set_cursor_icon(CursorIcon::Grab);
}
} else {
ui.output().cursor_icon = CursorIcon::Grabbing;
ui.ctx().set_cursor_icon(CursorIcon::Grabbing);
// Paint the body to a new layer:
let layer_id = AreaLayerId::new(Order::Tooltip, id);
@@ -37,7 +37,7 @@ pub fn drop_target<R>(
can_accept_what_is_being_dragged: bool,
body: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
let is_being_dragged = ui.memory().is_anything_being_dragged();
let is_being_dragged = ui.memory(|mem| mem.is_anything_being_dragged());
let margin = Vec2::splat(4.0);
@@ -139,7 +139,7 @@ impl super::View for DragAndDropDemo {
});
});
if ui.memory().is_being_dragged(item_id) {
if ui.memory(|mem| mem.is_being_dragged(item_id)) {
source_col_row = Some((col_idx, row_idx));
}
}
@@ -153,7 +153,7 @@ impl super::View for DragAndDropDemo {
}
});
let is_being_dragged = ui.memory().is_anything_being_dragged();
let is_being_dragged = ui.memory(|mem| mem.is_anything_being_dragged());
if is_being_dragged && can_accept_what_is_being_dragged && response.hovered() {
drop_col = Some(col_idx);
}
@@ -162,7 +162,7 @@ impl super::View for DragAndDropDemo {
if let Some((source_col, source_row)) = source_col_row {
if let Some(drop_col) = drop_col {
if ui.input().pointer.any_released() {
if ui.input(|i| i.pointer.any_released()) {
// do the drop:
let item = self.columns[source_col].remove(source_row);
self.columns[drop_col].push(item);

View File

@@ -93,7 +93,7 @@ impl super::View for FontBook {
};
if ui.add(button).on_hover_ui(tooltip_ui).clicked() {
ui.output().copied_text = chr.to_string();
ui.output_mut(|o| o.copied_text = chr.to_string());
}
}
}
@@ -103,15 +103,16 @@ impl super::View for FontBook {
}
fn available_characters(ui: &egui::Ui, family: egui::FontFamily) -> BTreeMap<char, String> {
ui.fonts()
.lock()
.fonts
.font(&egui::FontId::new(10.0, family)) // size is arbitrary for getting the characters
.characters()
.iter()
.filter(|chr| !chr.is_whitespace() && !chr.is_ascii_control())
.map(|&chr| (chr, char_name(chr)))
.collect()
ui.fonts(|f| {
f.lock()
.fonts
.font(&egui::FontId::new(10.0, family)) // size is arbitrary for getting the characters
.characters()
.iter()
.filter(|chr| !chr.is_whitespace() && !chr.is_ascii_control())
.map(|&chr| (chr, char_name(chr)))
.collect()
})
}
fn char_name(chr: char) -> String {

View File

@@ -202,7 +202,7 @@ impl Widgets {
ui.horizontal_wrapped(|ui| {
// Trick so we don't have to add spaces in the text below:
let width = ui.fonts().glyph_width(&TextStyle::Body.resolve(ui.style()), ' ');
let width = ui.fonts(|f|f.glyph_width(&TextStyle::Body.resolve(ui.style()), ' '));
ui.spacing_mut().item_spacing.x = width;
ui.label(RichText::new("Text can have").color(Color32::from_rgb(110, 255, 110)));

View File

@@ -49,7 +49,7 @@ impl super::View for MultiTouch {
ui.separator();
ui.label("Try touch gestures Pinch/Stretch, Rotation, and Pressure with 2+ fingers.");
let num_touches = ui.input().multi_touch().map_or(0, |mt| mt.num_touches);
let num_touches = ui.input(|i| i.multi_touch().map_or(0, |mt| mt.num_touches));
ui.label(format!("Current touches: {}", num_touches));
let color = if ui.visuals().dark_mode {
@@ -93,7 +93,7 @@ impl super::View for MultiTouch {
// touch pressure will make the arrow thicker (not all touch devices support this):
stroke_width += 10. * multi_touch.force;
self.last_touch_time = ui.input().time;
self.last_touch_time = ui.input(|i| i.time);
} else {
self.slowly_reset(ui);
}
@@ -118,7 +118,7 @@ impl MultiTouch {
// This has nothing to do with the touch gesture. It just smoothly brings the
// painted arrow back into its original position, for a nice visual effect:
let time_since_last_touch = (ui.input().time - self.last_touch_time) as f32;
let time_since_last_touch = (ui.input(|i| i.time) - self.last_touch_time) as f32;
let delay = 0.5;
if time_since_last_touch < delay {
@@ -133,7 +133,7 @@ impl MultiTouch {
self.rotation = 0.0;
self.translation = Vec2::ZERO;
} else {
let dt = ui.input().unstable_dt;
let dt = ui.input(|i| i.unstable_dt);
let half_life_factor = (-(2_f32.ln()) / half_life * dt).exp();
self.zoom = 1. + ((self.zoom - 1.) * half_life_factor);
self.rotation *= half_life_factor;

View File

@@ -53,11 +53,10 @@ impl PaintBezier {
});
ui.collapsing("Global tessellation options", |ui| {
let mut tessellation_options = *(ui.ctx().tessellation_options());
let tessellation_options = &mut tessellation_options;
let mut tessellation_options = ui.ctx().tessellation_options(|to| *to);
tessellation_options.ui(ui);
let mut new_tessellation_options = ui.ctx().tessellation_options();
*new_tessellation_options = *tessellation_options;
ui.ctx()
.tessellation_options_mut(|to| *to = tessellation_options);
});
ui.radio_value(&mut self.degree, 3, "Quadratic Bézier");

View File

@@ -20,7 +20,7 @@ pub fn password_ui(ui: &mut egui::Ui, password: &mut String) -> egui::Response {
// Get state for this widget.
// You should get state by value, not by reference to avoid borrowing of [`Memory`].
let mut show_plaintext = ui.data().get_temp::<bool>(state_id).unwrap_or(false);
let mut show_plaintext = ui.data_mut(|d| d.get_temp::<bool>(state_id).unwrap_or(false));
// Process ui, change a local copy of the state
// We want TextEdit to fill entire space, and have button after that, so in that case we can
@@ -43,7 +43,7 @@ pub fn password_ui(ui: &mut egui::Ui, password: &mut String) -> egui::Response {
});
// Store the (possibly changed) state:
ui.data().insert_temp(state_id, show_plaintext);
ui.data_mut(|d| d.insert_temp(state_id, show_plaintext));
// All done! Return the interaction response so the user can check what happened
// (hovered, clicked, …) and maybe show a tooltip:

View File

@@ -11,6 +11,124 @@ use plot::{
// ----------------------------------------------------------------------------
#[derive(PartialEq, Eq)]
enum Panel {
Lines,
Markers,
Legend,
Charts,
Items,
Interaction,
CustomAxes,
LinkedAxes,
}
impl Default for Panel {
fn default() -> Self {
Self::Lines
}
}
// ----------------------------------------------------------------------------
#[derive(PartialEq, Default)]
pub struct PlotDemo {
line_demo: LineDemo,
marker_demo: MarkerDemo,
legend_demo: LegendDemo,
charts_demo: ChartsDemo,
items_demo: ItemsDemo,
interaction_demo: InteractionDemo,
custom_axes_demo: CustomAxisDemo,
linked_axes_demo: LinkedAxisDemo,
open_panel: Panel,
}
impl super::Demo for PlotDemo {
fn name(&self) -> &'static str {
"🗠 Plot"
}
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)
.default_size(vec2(400.0, 400.0))
.vscroll(false)
.show(ctx, |ui| self.ui(ui));
}
}
impl super::View for PlotDemo {
fn ui(&mut self, ui: &mut Ui) {
ui.horizontal(|ui| {
egui::reset_button(ui, self);
ui.collapsing("Instructions", |ui| {
ui.label("Pan by dragging, or scroll (+ shift = horizontal).");
ui.label("Box zooming: Right click to zoom in and zoom out using a selection.");
if cfg!(target_arch = "wasm32") {
ui.label("Zoom with ctrl / ⌘ + pointer wheel, or with pinch gesture.");
} else if cfg!(target_os = "macos") {
ui.label("Zoom with ctrl / ⌘ + scroll.");
} else {
ui.label("Zoom with ctrl + scroll.");
}
ui.label("Reset view with double-click.");
ui.add(crate::egui_github_link_file!());
});
});
ui.separator();
ui.horizontal(|ui| {
ui.selectable_value(&mut self.open_panel, Panel::Lines, "Lines");
ui.selectable_value(&mut self.open_panel, Panel::Markers, "Markers");
ui.selectable_value(&mut self.open_panel, Panel::Legend, "Legend");
ui.selectable_value(&mut self.open_panel, Panel::Charts, "Charts");
ui.selectable_value(&mut self.open_panel, Panel::Items, "Items");
ui.selectable_value(&mut self.open_panel, Panel::Interaction, "Interaction");
ui.selectable_value(&mut self.open_panel, Panel::CustomAxes, "Custom Axes");
ui.selectable_value(&mut self.open_panel, Panel::LinkedAxes, "Linked Axes");
});
ui.separator();
match self.open_panel {
Panel::Lines => {
self.line_demo.ui(ui);
}
Panel::Markers => {
self.marker_demo.ui(ui);
}
Panel::Legend => {
self.legend_demo.ui(ui);
}
Panel::Charts => {
self.charts_demo.ui(ui);
}
Panel::Items => {
self.items_demo.ui(ui);
}
Panel::Interaction => {
self.interaction_demo.ui(ui);
}
Panel::CustomAxes => {
self.custom_axes_demo.ui(ui);
}
Panel::LinkedAxes => {
self.linked_axes_demo.ui(ui);
}
}
}
}
fn is_approx_zero(val: f64) -> bool {
val.abs() < 1e-6
}
fn is_approx_integer(val: f64) -> bool {
val.fract().abs() < 1e-6
}
// ----------------------------------------------------------------------------
#[derive(PartialEq)]
struct LineDemo {
animate: bool,
@@ -152,7 +270,7 @@ impl LineDemo {
self.options_ui(ui);
if self.animate {
ui.ctx().request_repaint();
self.time += ui.input().unstable_dt.at_most(1.0 / 30.0) as f64;
self.time += ui.input(|i| i.unstable_dt).at_most(1.0 / 30.0) as f64;
};
let mut plot = Plot::new("lines_demo").legend(Legend::default());
if self.square {
@@ -754,7 +872,7 @@ impl ChartsDemo {
Plot::new("Normal Distribution Demo")
.legend(Legend::default())
.data_aspect(1.0)
.clamp_grid(true)
.show(ui, |plot_ui| plot_ui.bar_chart(chart))
.response
}
@@ -864,121 +982,3 @@ impl ChartsDemo {
.response
}
}
// ----------------------------------------------------------------------------
#[derive(PartialEq, Eq)]
enum Panel {
Lines,
Markers,
Legend,
Charts,
Items,
Interaction,
CustomAxes,
LinkedAxes,
}
impl Default for Panel {
fn default() -> Self {
Self::Lines
}
}
// ----------------------------------------------------------------------------
#[derive(PartialEq, Default)]
pub struct PlotDemo {
line_demo: LineDemo,
marker_demo: MarkerDemo,
legend_demo: LegendDemo,
charts_demo: ChartsDemo,
items_demo: ItemsDemo,
interaction_demo: InteractionDemo,
custom_axes_demo: CustomAxisDemo,
linked_axes_demo: LinkedAxisDemo,
open_panel: Panel,
}
impl super::Demo for PlotDemo {
fn name(&self) -> &'static str {
"🗠 Plot"
}
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)
.default_size(vec2(400.0, 400.0))
.vscroll(false)
.show(ctx, |ui| self.ui(ui));
}
}
impl super::View for PlotDemo {
fn ui(&mut self, ui: &mut Ui) {
ui.horizontal(|ui| {
egui::reset_button(ui, self);
ui.collapsing("Instructions", |ui| {
ui.label("Pan by dragging, or scroll (+ shift = horizontal).");
ui.label("Box zooming: Right click to zoom in and zoom out using a selection.");
if cfg!(target_arch = "wasm32") {
ui.label("Zoom with ctrl / ⌘ + pointer wheel, or with pinch gesture.");
} else if cfg!(target_os = "macos") {
ui.label("Zoom with ctrl / ⌘ + scroll.");
} else {
ui.label("Zoom with ctrl + scroll.");
}
ui.label("Reset view with double-click.");
ui.add(crate::egui_github_link_file!());
});
});
ui.separator();
ui.horizontal(|ui| {
ui.selectable_value(&mut self.open_panel, Panel::Lines, "Lines");
ui.selectable_value(&mut self.open_panel, Panel::Markers, "Markers");
ui.selectable_value(&mut self.open_panel, Panel::Legend, "Legend");
ui.selectable_value(&mut self.open_panel, Panel::Charts, "Charts");
ui.selectable_value(&mut self.open_panel, Panel::Items, "Items");
ui.selectable_value(&mut self.open_panel, Panel::Interaction, "Interaction");
ui.selectable_value(&mut self.open_panel, Panel::CustomAxes, "Custom Axes");
ui.selectable_value(&mut self.open_panel, Panel::LinkedAxes, "Linked Axes");
});
ui.separator();
match self.open_panel {
Panel::Lines => {
self.line_demo.ui(ui);
}
Panel::Markers => {
self.marker_demo.ui(ui);
}
Panel::Legend => {
self.legend_demo.ui(ui);
}
Panel::Charts => {
self.charts_demo.ui(ui);
}
Panel::Items => {
self.items_demo.ui(ui);
}
Panel::Interaction => {
self.interaction_demo.ui(ui);
}
Panel::CustomAxes => {
self.custom_axes_demo.ui(ui);
}
Panel::LinkedAxes => {
self.linked_axes_demo.ui(ui);
}
}
}
}
fn is_approx_zero(val: f64) -> bool {
val.abs() < 1e-6
}
fn is_approx_integer(val: f64) -> bool {
val.fract().abs() < 1e-6
}

View File

@@ -112,7 +112,7 @@ fn huge_content_painter(ui: &mut egui::Ui) {
ui.add_space(4.0);
let font_id = TextStyle::Body.resolve(ui.style());
let row_height = ui.fonts().row_height(&font_id) + ui.spacing().item_spacing.y;
let row_height = ui.fonts(|f| f.row_height(&font_id)) + ui.spacing().item_spacing.y;
let num_rows = 10_000;
ScrollArea::vertical()

View File

@@ -65,10 +65,7 @@ impl super::View for TextEdit {
egui::Label::new("Press ctrl+Y to toggle the case of selected text (cmd+Y on Mac)"),
);
if ui
.input_mut()
.consume_key(egui::Modifiers::COMMAND, egui::Key::Y)
{
if ui.input_mut(|i| i.consume_key(egui::Modifiers::COMMAND, egui::Key::Y)) {
if let Some(text_cursor_range) = output.cursor_range {
use egui::TextBuffer as _;
let selected_chars = text_cursor_range.as_sorted_char_range();
@@ -93,7 +90,7 @@ impl super::View for TextEdit {
let ccursor = egui::text::CCursor::new(0);
state.set_ccursor_range(Some(egui::text::CCursorRange::one(ccursor)));
state.store(ui.ctx(), text_edit_id);
ui.ctx().memory().request_focus(text_edit_id); // give focus back to the [`TextEdit`].
ui.ctx().memory_mut(|mem| mem.request_focus(text_edit_id)); // give focus back to the [`TextEdit`].
}
}
@@ -103,7 +100,7 @@ impl super::View for TextEdit {
let ccursor = egui::text::CCursor::new(text.chars().count());
state.set_ccursor_range(Some(egui::text::CCursorRange::one(ccursor)));
state.store(ui.ctx(), text_edit_id);
ui.ctx().memory().request_focus(text_edit_id); // give focus back to the [`TextEdit`].
ui.ctx().memory_mut(|mem| mem.request_focus(text_edit_id)); // give focus back to the [`TextEdit`].
}
}
});

View File

@@ -175,6 +175,8 @@ impl WidgetGallery {
egui::ComboBox::from_label("Take your pick")
.selected_text(format!("{:?}", radio))
.show_ui(ui, |ui| {
ui.style_mut().wrap = Some(false);
ui.set_min_width(60.0);
ui.selectable_value(radio, Enum::First, "First");
ui.selectable_value(radio, Enum::Second, "Second");
ui.selectable_value(radio, Enum::Third, "Third");

View File

@@ -50,7 +50,7 @@ impl super::Demo for WindowOptions {
anchor_offset,
} = self.clone();
let enabled = ctx.input().time - disabled_time > 2.0;
let enabled = ctx.input(|i| i.time) - disabled_time > 2.0;
if !enabled {
ctx.request_repaint();
}
@@ -132,7 +132,7 @@ impl super::View for WindowOptions {
ui.horizontal(|ui| {
if ui.button("Disable for 2 seconds").clicked() {
self.disabled_time = ui.input().time;
self.disabled_time = ui.input(|i| i.time);
}
egui::reset_button(ui, self);
ui.add(crate::egui_github_link_file!());

View File

@@ -81,7 +81,7 @@ impl EasyMarkEditor {
let mut layouter = |ui: &egui::Ui, easymark: &str, wrap_width: f32| {
let mut layout_job = highlighter.highlight(ui.style(), easymark);
layout_job.wrap.max_width = wrap_width;
ui.fonts().layout_job(layout_job)
ui.fonts(|f| f.layout_job(layout_job))
};
ui.add(
@@ -141,7 +141,7 @@ fn nested_hotkeys_ui(ui: &mut egui::Ui) {
fn shortcuts(ui: &Ui, code: &mut dyn TextBuffer, ccursor_range: &mut CCursorRange) -> bool {
let mut any_change = false;
if ui.input_mut().consume_shortcut(&SHORTCUT_INDENT) {
if ui.input_mut(|i| i.consume_shortcut(&SHORTCUT_INDENT)) {
// This is a placeholder till we can indent the active line
any_change = true;
let [primary, _secondary] = ccursor_range.sorted();
@@ -160,7 +160,7 @@ fn shortcuts(ui: &Ui, code: &mut dyn TextBuffer, ccursor_range: &mut CCursorRang
(SHORTCUT_STRIKETHROUGH, "~"),
(SHORTCUT_UNDERLINE, "_"),
] {
if ui.input_mut().consume_shortcut(&shortcut) {
if ui.input_mut(|i| i.consume_shortcut(&shortcut)) {
any_change = true;
toggle_surrounding(code, ccursor_range, surrounding);
};

View File

@@ -144,7 +144,7 @@ fn bullet_point(ui: &mut Ui, width: f32) -> Response {
fn numbered_point(ui: &mut Ui, width: f32, number: &str) -> Response {
let font_id = TextStyle::Body.resolve(ui.style());
let row_height = ui.fonts().row_height(&font_id);
let row_height = ui.fonts(|f| f.row_height(&font_id));
let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover());
let text = format!("{}.", number);
let text_color = ui.visuals().strong_text_color();

View File

@@ -102,6 +102,6 @@ fn test_egui_zero_window_size() {
/// Detect narrow screens. This is used to show a simpler UI on mobile devices,
/// especially for the web demo at <https://egui.rs>.
pub fn is_mobile(ctx: &egui::Context) -> bool {
let screen_size = ctx.input().screen_rect().size();
let screen_size = ctx.screen_rect().size();
screen_size.x < 550.0
}

View File

@@ -8,7 +8,7 @@ pub fn code_view_ui(ui: &mut egui::Ui, mut code: &str) {
let mut layouter = |ui: &egui::Ui, string: &str, _wrap_width: f32| {
let layout_job = highlight(ui.ctx(), &theme, string, language);
// layout_job.wrap.max_width = wrap_width; // no wrapping
ui.fonts().layout_job(layout_job)
ui.fonts(|f| f.layout_job(layout_job))
};
ui.add(
@@ -31,9 +31,11 @@ pub fn highlight(ctx: &egui::Context, theme: &CodeTheme, code: &str, language: &
type HighlightCache = egui::util::cache::FrameCache<LayoutJob, Highlighter>;
let mut memory = ctx.memory();
let highlight_cache = memory.caches.cache::<HighlightCache>();
highlight_cache.get((theme, code, language))
ctx.memory_mut(|mem| {
mem.caches
.cache::<HighlightCache>()
.get((theme, code, language))
})
}
// ----------------------------------------------------------------------------
@@ -146,21 +148,23 @@ impl CodeTheme {
pub fn from_memory(ctx: &egui::Context) -> Self {
if ctx.style().visuals.dark_mode {
ctx.data()
.get_persisted(egui::Id::new("dark"))
.unwrap_or_else(CodeTheme::dark)
ctx.data_mut(|d| {
d.get_persisted(egui::Id::new("dark"))
.unwrap_or_else(CodeTheme::dark)
})
} else {
ctx.data()
.get_persisted(egui::Id::new("light"))
.unwrap_or_else(CodeTheme::light)
ctx.data_mut(|d| {
d.get_persisted(egui::Id::new("light"))
.unwrap_or_else(CodeTheme::light)
})
}
}
pub fn store_in_memory(self, ctx: &egui::Context) {
if self.dark_mode {
ctx.data().insert_persisted(egui::Id::new("dark"), self);
ctx.data_mut(|d| d.insert_persisted(egui::Id::new("dark"), self));
} else {
ctx.data().insert_persisted(egui::Id::new("light"), self);
ctx.data_mut(|d| d.insert_persisted(egui::Id::new("light"), self));
}
}
}
@@ -230,9 +234,8 @@ impl CodeTheme {
pub fn ui(&mut self, ui: &mut egui::Ui) {
ui.horizontal_top(|ui| {
let selected_id = egui::Id::null();
let mut selected_tt: TokenType = *ui
.data()
.get_persisted_mut_or(selected_id, TokenType::Comment);
let mut selected_tt: TokenType =
ui.data_mut(|d| *d.get_persisted_mut_or(selected_id, TokenType::Comment));
ui.vertical(|ui| {
ui.set_width(150.0);
@@ -274,7 +277,7 @@ impl CodeTheme {
ui.add_space(16.0);
ui.data().insert_persisted(selected_id, selected_tt);
ui.data_mut(|d| d.insert_persisted(selected_id, selected_tt));
egui::Frame::group(ui.style())
.inner_margin(egui::Vec2::splat(2.0))