Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions crates/egui_demo_lib/src/demo/code_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,25 @@ impl crate::View for CodeEditor {
};

egui::ScrollArea::vertical().show(ui, |ui| {
ui.add(
egui::TextEdit::multiline(code)
.font(egui::TextStyle::Monospace) // for cursor height
.code_editor()
.desired_rows(10)
.lock_focus(true)
.desired_width(f32::INFINITY)
.layouter(&mut layouter),
);
let editor = egui::TextEdit::multiline(code)
.font(egui::TextStyle::Monospace) // for cursor height
.code_editor()
.desired_rows(10)
.lock_focus(true)
.desired_width(f32::INFINITY)
.layouter(&mut layouter);
let editor = if !cfg!(feature = "syntect") {
use egui::Color32;
let background_color = if theme.is_dark() {
Color32::BLACK
} else {
Color32::WHITE
};
editor.background_color(background_color)
} else {
editor
};
ui.add(editor);
});
}
}
45 changes: 31 additions & 14 deletions crates/egui_extras/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ impl Default for CodeTheme {
}

impl CodeTheme {
pub fn is_dark(&self) -> bool {
self.dark_mode
}

/// Selects either dark or light theme based on the given style.
pub fn from_style(style: &egui::Style) -> Self {
let font_id = style
Expand Down Expand Up @@ -317,6 +321,24 @@ impl CodeTheme {

#[cfg(feature = "syntect")]
impl CodeTheme {
/// Change the font size
pub fn with_font_size(&self, font_size: f32) -> Self {
Self {
dark_mode: self.dark_mode,
syntect_theme: self.syntect_theme,
font_id: egui::FontId::monospace(font_size),
}
}

/// Change the `font_id` of the theme
pub fn with_font_id(&self, font_id: egui::FontId) -> Self {
Self {
dark_mode: self.dark_mode,
syntect_theme: self.syntect_theme,
font_id,
}
}

fn dark_with_font_id(font_id: egui::FontId) -> Self {
Self {
dark_mode: true,
Expand All @@ -333,10 +355,6 @@ impl CodeTheme {
}
}

pub fn is_dark(&self) -> bool {
self.dark_mode
}

/// Show UI for changing the color theme.
pub fn ui(&mut self, ui: &mut egui::Ui) {
ui.horizontal(|ui| {
Expand All @@ -346,11 +364,9 @@ impl CodeTheme {
ui.selectable_value(&mut self.dark_mode, false, "☀ Light theme")
.on_hover_text("Use the light mode theme");
});

for theme in SyntectTheme::all() {
if theme.is_dark() == self.dark_mode {
ui.radio_value(&mut self.syntect_theme, theme, theme.name());
}
let current_theme_is_dark = self.is_dark();
for theme in SyntectTheme::all().filter(|t| t.is_dark() == current_theme_is_dark) {
ui.radio_value(&mut self.syntect_theme, theme, theme.name());
}
}
}
Expand Down Expand Up @@ -408,12 +424,13 @@ impl CodeTheme {

ui.vertical(|ui| {
ui.set_width(150.0);
egui::widgets::global_theme_preference_buttons(ui);

ui.add_space(8.0);
ui.separator();
ui.add_space(8.0);
ui.horizontal(|ui| {
ui.selectable_value(&mut self.dark_mode, true, "🌙 Dark theme")
.on_hover_text("Use the dark mode theme");

ui.selectable_value(&mut self.dark_mode, false, "☀ Light theme")
.on_hover_text("Use the light mode theme");
});
ui.scope(|ui| {
for (tt, tt_name) in [
(TokenType::Comment, "// comment"),
Expand Down
Loading