Replace indexing in Fonts with member function

This commit is contained in:
Emil Ernerfeldt 2022-01-22 14:03:29 +01:00
parent 0a15163fab
commit d70c80569f
7 changed files with 17 additions and 20 deletions

View file

@ -98,7 +98,7 @@ impl CodeExample {
); );
ui.horizontal(|ui| { ui.horizontal(|ui| {
let indentation = 8.0 * ui.fonts()[egui::TextStyle::Monospace].glyph_width(' '); let indentation = 8.0 * ui.fonts().glyph_width(egui::TextStyle::Monospace, ' ');
let item_spacing = ui.spacing_mut().item_spacing; let item_spacing = ui.spacing_mut().item_spacing;
ui.add_space(indentation - item_spacing.x); ui.add_space(indentation - item_spacing.x);

View file

@ -71,7 +71,8 @@ impl super::View for FontBook {
let text_style = self.text_style; let text_style = self.text_style;
let filter = &self.filter; let filter = &self.filter;
let named_chars = self.named_chars.entry(text_style).or_insert_with(|| { let named_chars = self.named_chars.entry(text_style).or_insert_with(|| {
ui.fonts()[text_style] ui.fonts()
.font(text_style)
.characters() .characters()
.iter() .iter()
.filter(|chr| !chr.is_whitespace() && !chr.is_ascii_control()) .filter(|chr| !chr.is_whitespace() && !chr.is_ascii_control())

View file

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

View file

@ -81,7 +81,7 @@ fn huge_content_lines(ui: &mut egui::Ui) {
ui.add_space(4.0); ui.add_space(4.0);
let text_style = TextStyle::Body; let text_style = TextStyle::Body;
let row_height = ui.fonts()[text_style].row_height(); let row_height = ui.fonts().row_height(text_style);
let num_rows = 10_000; let num_rows = 10_000;
ScrollArea::vertical().auto_shrink([false; 2]).show_rows( ScrollArea::vertical().auto_shrink([false; 2]).show_rows(
ui, ui,
@ -102,7 +102,7 @@ fn huge_content_painter(ui: &mut egui::Ui) {
ui.add_space(4.0); ui.add_space(4.0);
let text_style = TextStyle::Body; let text_style = TextStyle::Body;
let row_height = ui.fonts()[text_style].row_height() + ui.spacing().item_spacing.y; let row_height = ui.fonts().row_height(text_style) + ui.spacing().item_spacing.y;
let num_rows = 10_000; let num_rows = 10_000;
ScrollArea::vertical() ScrollArea::vertical()
@ -265,7 +265,7 @@ impl super::View for ScrollStickTo {
ui.add_space(4.0); ui.add_space(4.0);
let text_style = TextStyle::Body; let text_style = TextStyle::Body;
let row_height = ui.fonts()[text_style].row_height(); let row_height = ui.fonts().row_height(text_style);
ScrollArea::vertical().stick_to_bottom().show_rows( ScrollArea::vertical().stick_to_bottom().show_rows(
ui, ui,
row_height, row_height,

View file

@ -18,7 +18,7 @@ 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.allocate_ui_with_layout(initial_size, layout, |ui| {
ui.spacing_mut().item_spacing.x = 0.0; ui.spacing_mut().item_spacing.x = 0.0;
let row_height = (*ui.fonts())[TextStyle::Body].row_height(); let row_height = ui.fonts().row_height(TextStyle::Body);
ui.set_row_height(row_height); ui.set_row_height(row_height);
for item in items { for item in items {
@ -28,7 +28,7 @@ pub fn easy_mark_it<'em>(ui: &mut Ui, items: impl Iterator<Item = easy_mark::Ite
} }
pub fn item_ui(ui: &mut Ui, item: easy_mark::Item<'_>) { pub fn item_ui(ui: &mut Ui, item: easy_mark::Item<'_>) {
let row_height = ui.fonts()[TextStyle::Body].row_height(); let row_height = ui.fonts().row_height(TextStyle::Body);
let one_indent = row_height / 2.0; let one_indent = row_height / 2.0;
match item { match item {
@ -134,7 +134,7 @@ fn rich_text_from_style(text: &str, style: &easy_mark::Style) -> RichText {
} }
fn bullet_point(ui: &mut Ui, width: f32) -> Response { fn bullet_point(ui: &mut Ui, width: f32) -> Response {
let row_height = ui.fonts()[TextStyle::Body].row_height(); let row_height = ui.fonts().row_height(TextStyle::Body);
let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover()); let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover());
ui.painter().circle_filled( ui.painter().circle_filled(
rect.center(), rect.center(),
@ -145,7 +145,7 @@ fn bullet_point(ui: &mut Ui, width: f32) -> Response {
} }
fn numbered_point(ui: &mut Ui, width: f32, number: &str) -> Response { fn numbered_point(ui: &mut Ui, width: f32, number: &str) -> Response {
let row_height = ui.fonts()[TextStyle::Body].row_height(); let row_height = ui.fonts().row_height(TextStyle::Body);
let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover()); let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover());
let text = format!("{}.", number); let text = format!("{}.", number);
let text_color = ui.visuals().strong_text_color(); let text_color = ui.visuals().strong_text_color();

View file

@ -298,6 +298,11 @@ impl Fonts {
&self.definitions &self.definitions
} }
#[inline]
pub fn font(&self, text_style: TextStyle) -> &Font {
&self.fonts[&text_style]
}
/// Call each frame to get the change to the font texture since last call. /// Call each frame to get the change to the font texture since last call.
pub fn font_image_delta(&self) -> Option<crate::ImageDelta> { pub fn font_image_delta(&self) -> Option<crate::ImageDelta> {
self.atlas.lock().take_delta() self.atlas.lock().take_delta()
@ -382,15 +387,6 @@ impl Fonts {
} }
} }
impl std::ops::Index<TextStyle> for Fonts {
type Output = Font;
#[inline(always)]
fn index(&self, text_style: TextStyle) -> &Font {
&self.fonts[&text_style]
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
struct CachedGalley { struct CachedGalley {

View file

@ -86,7 +86,7 @@ fn layout_section(
byte_range, byte_range,
format, format,
} = section; } = section;
let font = &fonts[format.style]; let font = fonts.font(format.style);
let font_height = font.row_height(); let font_height = font.row_height();
let mut paragraph = out_paragraphs.last_mut().unwrap(); let mut paragraph = out_paragraphs.last_mut().unwrap();