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

Make TextStyle !Copy

This commit is contained in:
Emil Ernerfeldt
2022-01-22 18:02:02 +01:00
parent 0df77d9074
commit 4f98f26fda
21 changed files with 126 additions and 92 deletions

View File

@@ -71,7 +71,7 @@ impl super::View for CodeEditor {
ui.collapsing("Theme", |ui| {
ui.group(|ui| {
theme.ui(ui);
theme.store_in_memory(ui.ctx());
theme.clone().store_in_memory(ui.ctx());
});
});

View File

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

View File

@@ -55,7 +55,11 @@ impl super::View for FontBook {
.selected_text(format!("{:?}", self.text_style))
.show_ui(ui, |ui| {
for style in egui::TextStyle::all() {
ui.selectable_value(&mut self.text_style, style, format!("{:?}", style));
ui.selectable_value(
&mut self.text_style,
style.clone(),
format!("{:?}", style),
);
}
});
@@ -68,19 +72,22 @@ impl super::View for FontBook {
}
});
let text_style = self.text_style;
let text_style = self.text_style.clone();
let filter = &self.filter;
let named_chars = self.named_chars.entry(text_style).or_insert_with(|| {
ui.fonts()
.lock()
.fonts
.font_mut(text_style)
.characters()
.iter()
.filter(|chr| !chr.is_whitespace() && !chr.is_ascii_control())
.map(|&chr| (chr, char_name(chr)))
.collect()
});
let named_chars = self
.named_chars
.entry(text_style.clone())
.or_insert_with(|| {
ui.fonts()
.lock()
.fonts
.font_mut(&text_style)
.characters()
.iter()
.filter(|chr| !chr.is_whitespace() && !chr.is_ascii_control())
.map(|&chr| (chr, char_name(chr)))
.collect()
});
ui.separator();
@@ -91,12 +98,14 @@ impl super::View for FontBook {
for (&chr, name) in named_chars {
if filter.is_empty() || name.contains(filter) || *filter == chr.to_string() {
let button = egui::Button::new(
egui::RichText::new(chr.to_string()).text_style(text_style),
egui::RichText::new(chr.to_string()).text_style(text_style.clone()),
)
.frame(false);
let tooltip_ui = |ui: &mut egui::Ui| {
ui.label(egui::RichText::new(chr.to_string()).text_style(text_style));
ui.label(
egui::RichText::new(chr.to_string()).text_style(text_style.clone()),
);
ui.label(format!("{}\nU+{:X}\n\nClick to copy", name, chr as u32));
};

View File

@@ -140,7 +140,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, ' ');
let width = ui.fonts().glyph_width(&TextStyle::Body, ' ');
ui.spacing_mut().item_spacing.x = width;
ui.label(RichText::new("Text can have").color(Color32::from_rgb(110, 255, 110)));

View File

@@ -262,7 +262,11 @@ impl Widget for &mut LegendDemo {
ui.label("Text style:");
ui.horizontal(|ui| {
TextStyle::all().for_each(|style| {
ui.selectable_value(&mut config.text_style, style, format!("{:?}", style));
ui.selectable_value(
&mut config.text_style,
style.clone(),
format!("{:?}", style),
);
});
});
ui.end_row();
@@ -284,7 +288,9 @@ impl Widget for &mut LegendDemo {
ui.end_row();
});
let legend_plot = Plot::new("legend_demo").legend(*config).data_aspect(1.0);
let legend_plot = Plot::new("legend_demo")
.legend(config.clone())
.data_aspect(1.0);
legend_plot
.show(ui, |plot_ui| {
plot_ui.line(LegendDemo::line_with_slope(0.5).name("lines"));

View File

@@ -81,7 +81,7 @@ fn huge_content_lines(ui: &mut egui::Ui) {
ui.add_space(4.0);
let text_style = TextStyle::Body;
let row_height = ui.fonts().row_height(text_style);
let row_height = ui.fonts().row_height(&text_style);
let num_rows = 10_000;
ScrollArea::vertical().auto_shrink([false; 2]).show_rows(
ui,
@@ -102,7 +102,7 @@ fn huge_content_painter(ui: &mut egui::Ui) {
ui.add_space(4.0);
let text_style = TextStyle::Body;
let row_height = ui.fonts().row_height(text_style) + ui.spacing().item_spacing.y;
let row_height = ui.fonts().row_height(&text_style) + ui.spacing().item_spacing.y;
let num_rows = 10_000;
ScrollArea::vertical()
@@ -130,7 +130,7 @@ fn huge_content_painter(ui: &mut egui::Ui) {
pos2(x, y),
Align2::LEFT_TOP,
text,
text_style,
text_style.clone(),
ui.visuals().text_color(),
);
used_rect = used_rect.union(text_rect);
@@ -265,7 +265,7 @@ impl super::View for ScrollStickTo {
ui.add_space(4.0);
let text_style = TextStyle::Body;
let row_height = ui.fonts().row_height(text_style);
let row_height = ui.fonts().row_height(&text_style);
ScrollArea::vertical().stick_to_bottom().show_rows(
ui,
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.spacing_mut().item_spacing.x = 0.0;
let row_height = ui.fonts().row_height(TextStyle::Body);
let row_height = ui.fonts().row_height(&TextStyle::Body);
ui.set_row_height(row_height);
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<'_>) {
let row_height = ui.fonts().row_height(TextStyle::Body);
let row_height = ui.fonts().row_height(&TextStyle::Body);
let one_indent = row_height / 2.0;
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 {
let row_height = ui.fonts().row_height(TextStyle::Body);
let row_height = ui.fonts().row_height(&TextStyle::Body);
let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover());
ui.painter().circle_filled(
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 {
let row_height = ui.fonts().row_height(TextStyle::Body);
let row_height = ui.fonts().row_height(&TextStyle::Body);
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

@@ -116,7 +116,7 @@ impl SyntectTheme {
}
}
#[derive(Clone, Copy, Hash, PartialEq)]
#[derive(Clone, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct CodeTheme {
@@ -158,15 +158,15 @@ impl CodeTheme {
}
}
pub fn store_in_memory(&self, ctx: &egui::Context) {
pub fn store_in_memory(self, ctx: &egui::Context) {
if self.dark_mode {
ctx.memory()
.data
.insert_persisted(egui::Id::new("dark"), *self);
.insert_persisted(egui::Id::new("dark"), self);
} else {
ctx.memory()
.data
.insert_persisted(egui::Id::new("light"), *self);
.insert_persisted(egui::Id::new("light"), self);
}
}
}
@@ -206,12 +206,12 @@ impl CodeTheme {
Self {
dark_mode: true,
formats: enum_map::enum_map![
TokenType::Comment => TextFormat::simple(text_style, Color32::from_gray(120)),
TokenType::Keyword => TextFormat::simple(text_style, Color32::from_rgb(255, 100, 100)),
TokenType::Literal => TextFormat::simple(text_style, Color32::from_rgb(87, 165, 171)),
TokenType::StringLiteral => TextFormat::simple(text_style, Color32::from_rgb(109, 147, 226)),
TokenType::Punctuation => TextFormat::simple(text_style, Color32::LIGHT_GRAY),
TokenType::Whitespace => TextFormat::simple(text_style, Color32::TRANSPARENT),
TokenType::Comment => TextFormat::simple(text_style.clone(), Color32::from_gray(120)),
TokenType::Keyword => TextFormat::simple(text_style.clone(), Color32::from_rgb(255, 100, 100)),
TokenType::Literal => TextFormat::simple(text_style.clone(), Color32::from_rgb(87, 165, 171)),
TokenType::StringLiteral => TextFormat::simple(text_style.clone(), Color32::from_rgb(109, 147, 226)),
TokenType::Punctuation => TextFormat::simple(text_style.clone(), Color32::LIGHT_GRAY),
TokenType::Whitespace => TextFormat::simple(text_style.clone(), Color32::TRANSPARENT),
],
}
}
@@ -223,12 +223,12 @@ impl CodeTheme {
dark_mode: false,
#[cfg(not(feature = "syntect"))]
formats: enum_map::enum_map![
TokenType::Comment => TextFormat::simple(text_style, Color32::GRAY),
TokenType::Keyword => TextFormat::simple(text_style, Color32::from_rgb(235, 0, 0)),
TokenType::Literal => TextFormat::simple(text_style, Color32::from_rgb(153, 134, 255)),
TokenType::StringLiteral => TextFormat::simple(text_style, Color32::from_rgb(37, 203, 105)),
TokenType::Punctuation => TextFormat::simple(text_style, Color32::DARK_GRAY),
TokenType::Whitespace => TextFormat::simple(text_style, Color32::TRANSPARENT),
TokenType::Comment => TextFormat::simple(text_style.clone(), Color32::GRAY),
TokenType::Keyword => TextFormat::simple(text_style.clone(), Color32::from_rgb(235, 0, 0)),
TokenType::Literal => TextFormat::simple(text_style.clone(), Color32::from_rgb(153, 134, 255)),
TokenType::StringLiteral => TextFormat::simple(text_style.clone(), Color32::from_rgb(37, 203, 105)),
TokenType::Punctuation => TextFormat::simple(text_style.clone(), Color32::DARK_GRAY),
TokenType::Whitespace => TextFormat::simple(text_style.clone(), Color32::TRANSPARENT),
],
}
}
@@ -259,7 +259,7 @@ impl CodeTheme {
// (TokenType::Whitespace, "whitespace"),
] {
let format = &mut self.formats[tt];
ui.style_mut().override_text_style = Some(format.style);
ui.style_mut().override_text_style = Some(format.style.clone());
ui.visuals_mut().override_text_color = Some(format.color);
ui.radio_value(&mut selected_tt, tt, tt_name);
}
@@ -412,7 +412,7 @@ impl Highligher {
while !text.is_empty() {
if text.starts_with("//") {
let end = text.find('\n').unwrap_or_else(|| text.len());
job.append(&text[..end], 0.0, theme.formats[TokenType::Comment]);
job.append(&text[..end], 0.0, theme.formats[TokenType::Comment].clone());
text = &text[end..];
} else if text.starts_with('"') {
let end = text[1..]
@@ -420,7 +420,11 @@ impl Highligher {
.map(|i| i + 2)
.or_else(|| text.find('\n'))
.unwrap_or_else(|| text.len());
job.append(&text[..end], 0.0, theme.formats[TokenType::StringLiteral]);
job.append(
&text[..end],
0.0,
theme.formats[TokenType::StringLiteral].clone(),
);
text = &text[end..];
} else if text.starts_with(|c: char| c.is_ascii_alphanumeric()) {
let end = text[1..]
@@ -432,19 +436,27 @@ impl Highligher {
} else {
TokenType::Literal
};
job.append(word, 0.0, theme.formats[tt]);
job.append(word, 0.0, theme.formats[tt].clone());
text = &text[end..];
} else if text.starts_with(|c: char| c.is_ascii_whitespace()) {
let end = text[1..]
.find(|c: char| !c.is_ascii_whitespace())
.map_or_else(|| text.len(), |i| i + 1);
job.append(&text[..end], 0.0, theme.formats[TokenType::Whitespace]);
job.append(
&text[..end],
0.0,
theme.formats[TokenType::Whitespace].clone(),
);
text = &text[end..];
} else {
let mut it = text.char_indices();
it.next();
let end = it.next().map_or(text.len(), |(idx, _chr)| idx);
job.append(&text[..end], 0.0, theme.formats[TokenType::Punctuation]);
job.append(
&text[..end],
0.0,
theme.formats[TokenType::Punctuation].clone(),
);
text = &text[end..];
}
}