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

egui_extras: always depend on log crate (#3336)

* egui_extras: always depend on `log` crate

* syntax_highlighting fix: "serde" is always on

* Add "serde" flag to egui when using egui_extras
This commit is contained in:
Emil Ernerfeldt
2023-09-14 11:20:34 +02:00
committed by GitHub
parent 5e785ae00a
commit e367c20779
11 changed files with 26 additions and 121 deletions

View File

@@ -50,9 +50,7 @@ pub fn highlight(ctx: &egui::Context, theme: &CodeTheme, code: &str, language: &
// ----------------------------------------------------------------------------
#[cfg(not(feature = "syntect"))]
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(enum_map::Enum)]
#[derive(Clone, Copy, PartialEq, serde::Deserialize, serde::Serialize, enum_map::Enum)]
enum TokenType {
Comment,
Keyword,
@@ -63,8 +61,7 @@ enum TokenType {
}
#[cfg(feature = "syntect")]
#[derive(Clone, Copy, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Copy, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
enum SyntectTheme {
Base16EightiesDark,
Base16MochaDark,
@@ -128,9 +125,8 @@ impl SyntectTheme {
}
/// A selected color theme.
#[derive(Clone, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
#[derive(Clone, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct CodeTheme {
dark_mode: bool,
@@ -160,7 +156,6 @@ impl CodeTheme {
/// Load code theme from egui memory.
///
/// There is one dark and one light theme stored at any one time.
#[cfg(feature = "serde")]
pub fn from_memory(ctx: &egui::Context) -> Self {
if ctx.style().visuals.dark_mode {
ctx.data_mut(|d| {
@@ -178,7 +173,6 @@ impl CodeTheme {
/// Store theme to egui memory.
///
/// There is one dark and one light theme stored at any one time.
#[cfg(feature = "serde")]
pub fn store_in_memory(self, ctx: &egui::Context) {
if self.dark_mode {
ctx.data_mut(|d| d.insert_persisted(egui::Id::new("dark"), self));
@@ -186,36 +180,6 @@ impl CodeTheme {
ctx.data_mut(|d| d.insert_persisted(egui::Id::new("light"), self));
}
}
/// Load code theme from egui memory.
///
/// There is one dark and one light theme stored at any one time.
#[cfg(not(feature = "serde"))]
pub fn from_memory(ctx: &egui::Context) -> Self {
if ctx.style().visuals.dark_mode {
ctx.data_mut(|d| {
d.get_temp(egui::Id::new("dark"))
.unwrap_or_else(CodeTheme::dark)
})
} else {
ctx.data_mut(|d| {
d.get_temp(egui::Id::new("light"))
.unwrap_or_else(CodeTheme::light)
})
}
}
/// Store theme to egui memory.
///
/// There is one dark and one light theme stored at any one time.
#[cfg(not(feature = "serde"))]
pub fn store_in_memory(self, ctx: &egui::Context) {
if self.dark_mode {
ctx.data_mut(|d| d.insert_temp(egui::Id::new("dark"), self));
} else {
ctx.data_mut(|d| d.insert_temp(egui::Id::new("light"), self));
}
}
}
#[cfg(feature = "syntect")]
@@ -285,12 +249,8 @@ impl CodeTheme {
pub fn ui(&mut self, ui: &mut egui::Ui) {
ui.horizontal_top(|ui| {
let selected_id = egui::Id::null();
#[cfg(feature = "serde")]
let mut selected_tt: TokenType =
ui.data_mut(|d| *d.get_persisted_mut_or(selected_id, TokenType::Comment));
#[cfg(not(feature = "serde"))]
let mut selected_tt: TokenType =
ui.data_mut(|d| *d.get_temp_mut_or(selected_id, TokenType::Comment));
ui.vertical(|ui| {
ui.set_width(150.0);
@@ -332,10 +292,7 @@ impl CodeTheme {
ui.add_space(16.0);
#[cfg(feature = "serde")]
ui.data_mut(|d| d.insert_persisted(selected_id, selected_tt));
#[cfg(not(feature = "serde"))]
ui.data_mut(|d| d.insert_temp(selected_id, selected_tt));
egui::Frame::group(ui.style())
.inner_margin(egui::Vec2::splat(2.0))