Skip to content

Commit

Permalink
add multi-line edit with "syntax highlighting"
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristófer R committed Mar 22, 2024
1 parent 2e95f43 commit efe381f
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use egui::{text::LayoutJob, FontId, TextFormat};

/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct TemplateApp {
// Example stuff:
label: String,

#[serde(skip)] // This how you opt-out of serialization of a field
value: f32,
text: String,
}

impl Default for TemplateApp {
fn default() -> Self {
Self {
// Example stuff:
label: "Hello World!".to_owned(),
value: 2.7,
text: "Test".to_owned(),
}
}
}
Expand Down Expand Up @@ -61,25 +58,60 @@ impl eframe::App for TemplateApp {
});
});

egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's
ui.heading("eframe template");

ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(&mut self.label);
});

ui.add(egui::Slider::new(&mut self.value, 0.0..=10.0).text("value"));
if ui.button("Increment").clicked() {
self.value += 1.0;
}
egui::TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| {
// The bottom panel is often a good place for toolbars and status bars:

ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
powered_by_egui_and_eframe(ui);
egui::warn_if_debug_build(ui);
});
});

egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's

ui.with_layout(
egui::Layout::centered_and_justified(egui::Direction::TopDown),
|ui| {
ui.add(
egui::TextEdit::multiline(&mut self.text)
.desired_width(f32::INFINITY)
.font(egui::TextStyle::Monospace)
.layouter(&mut |ui: &egui::Ui, text, wrap_width| {
let mut job = LayoutJob::default();

for (i, word) in text.split(' ').enumerate() {
job.append(
word,
0.0,
TextFormat {
font_id: FontId::new(14.0, egui::FontFamily::Monospace),
color: if i % 2 == 0 {
egui::Color32::BLUE
} else {
egui::Color32::RED
},
..Default::default()
},
);

job.append(
" ",
0.0,
TextFormat {
font_id: FontId::new(14.0, egui::FontFamily::Monospace),
..Default::default()
},
);
}

job.wrap.max_width = wrap_width;
ui.fonts(|f| f.layout_job(job))
}),
);
},
);
});
}
}

Expand Down

0 comments on commit efe381f

Please sign in to comment.