Skip to content

Commit

Permalink
chore(example): add live theme switcher in todo demo (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
grantshandy authored Apr 28, 2023
1 parent 630b5bd commit b951aba
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions examples/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use catppuccin_egui::{FRAPPE, LATTE, MACCHIATO, MOCHA};
use eframe::egui;

fn main() {
Expand All @@ -18,6 +19,15 @@ struct App {
todos: Vec<Todo>,
new_todo: String,
mode: Mode,
theme: CatppuccinTheme,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum CatppuccinTheme {
Frappe,
Latte,
Macchiato,
Mocha,
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -76,13 +86,22 @@ impl Default for App {
.collect(),
new_todo: String::new(),
mode: Mode::default(),
theme: CatppuccinTheme::Mocha,
}
}
}

impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
catppuccin_egui::set_theme(ctx, catppuccin_egui::MOCHA);
catppuccin_egui::set_theme(
ctx,
match self.theme {
CatppuccinTheme::Frappe => FRAPPE,
CatppuccinTheme::Latte => LATTE,
CatppuccinTheme::Macchiato => MACCHIATO,
CatppuccinTheme::Mocha => MOCHA,
},
);
ctx.set_pixels_per_point(1.25);

let mut fonts = egui::FontDefinitions::default();
Expand All @@ -98,7 +117,24 @@ impl eframe::App for App {
ctx.set_fonts(fonts);

egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Todos");
ui.columns(2, |columns| {
columns[0].heading("Todos");
columns[1].with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
egui::ComboBox::from_label("")
.selected_text(format!("{:?}", self.theme))
.show_ui(ui, |ui| {
ui.selectable_value(&mut self.theme, CatppuccinTheme::Latte, "Latte");
ui.selectable_value(&mut self.theme, CatppuccinTheme::Frappe, "Frappe");
ui.selectable_value(
&mut self.theme,
CatppuccinTheme::Macchiato,
"Macchiato",
);
ui.selectable_value(&mut self.theme, CatppuccinTheme::Mocha, "Mocha");
});
});
});

ui.horizontal(|ui| {
if ui.text_edit_singleline(&mut self.new_todo).lost_focus()
&& !self.new_todo.is_empty()
Expand All @@ -111,6 +147,7 @@ impl eframe::App for App {
self.new_todo.clear();
}
});

ui.separator();

egui::ScrollArea::vertical()
Expand Down

0 comments on commit b951aba

Please sign in to comment.