Files
egui/examples/custom_style/src/main.rs
T
Emil Ernerfeldt 2f6fe9c572 eframe: Replace Frame::update with fn logic and fn ui (#7775)
* Part of https://github.com/emilk/egui/issues/5113
* Part of https://github.com/emilk/egui/issues/3524

## What
This deprecates `eframe::App::update` and replaces it with two new
functions:

```rs
pub trait App {
	/// Called just before `ui`, and in the future this will
    /// also be called for background apps when needed.
	fn logic(&mut self, ctx: &egui::Context, frame: &mut Frame) { }
	
    /// Show your user interface to the user.
	fn ui(&mut self, ui: &mut egui::Ui, frame: &mut Frame);

	…
}
```

Similarly, `Context::run` is deprecated in favor of `Context::run_ui`.

`Plugin`s are now handed a `Ui` instead of just a `Context` in
`on_begin/end_frame`.

## TODO
…either in this PR or a later one
* [x] Deprecate `App::update`
* [x] Deprecate `Context::run`
* [x] Change plugins to get a `Ui`
* [x] Update kittest
* [x] Change viewports to get UI:s (`show_viewport_immediate` etc)
  - https://github.com/emilk/egui/pull/7779

## Later PRs
* [ ] Deprecate `Panel::show`
* [ ] Deprecate `CentralPanel::show`
* [ ] Deprecate `CentralPanel` ?
2025-12-16 17:05:50 +01:00

70 lines
2.4 KiB
Rust

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's an example
use eframe::egui::{
self, Color32, Stroke, Style, Theme, global_theme_preference_buttons, style::Selection,
};
use egui_demo_lib::{View as _, WidgetGallery};
fn main() -> eframe::Result {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([350.0, 590.0]),
..Default::default()
};
eframe::run_native(
"egui example: custom style",
options,
Box::new(|cc| Ok(Box::new(MyApp::new(cc)))),
)
}
fn setup_custom_style(ctx: &egui::Context) {
ctx.style_mut_of(Theme::Light, use_light_green_accent);
ctx.style_mut_of(Theme::Dark, use_dark_purple_accent);
}
fn use_light_green_accent(style: &mut Style) {
style.visuals.hyperlink_color = Color32::from_rgb(18, 180, 85);
style.visuals.text_cursor.stroke.color = Color32::from_rgb(28, 92, 48);
style.visuals.selection = Selection {
bg_fill: Color32::from_rgb(157, 218, 169),
stroke: Stroke::new(1.0, Color32::from_rgb(28, 92, 48)),
};
}
fn use_dark_purple_accent(style: &mut Style) {
style.visuals.hyperlink_color = Color32::from_rgb(202, 135, 227);
style.visuals.text_cursor.stroke.color = Color32::from_rgb(234, 208, 244);
style.visuals.selection = Selection {
bg_fill: Color32::from_rgb(105, 67, 119),
stroke: Stroke::new(1.0, Color32::from_rgb(234, 208, 244)),
};
}
struct MyApp {
widget_gallery: WidgetGallery,
}
impl MyApp {
fn new(cc: &eframe::CreationContext<'_>) -> Self {
setup_custom_style(&cc.egui_ctx);
egui_extras::install_image_loaders(&cc.egui_ctx); // Needed for the "Widget Gallery" demo
Self {
widget_gallery: WidgetGallery::default(),
}
}
}
impl eframe::App for MyApp {
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show_inside(ui, |ui| {
ui.heading("egui using a customized style");
ui.label("Switch between dark and light mode to see the different styles in action.");
global_theme_preference_buttons(ui);
ui.separator();
self.widget_gallery.ui(ui);
});
}
}