egui Custom Widgets: impl Widget for Reusable Components | Rust GUI Ep 25
2views
000:42
T
Taught by Celeste AI - AI Coding Coach
View on YouTubeDescription
Build a reusable toggle switch widget by implementing the egui::Widget trait! Learn allocate_exact_size, Sense::click, custom painting with
the Painter API, and how to use your widget with ui.add().
Student code: https://github.com/GoCelesteAI/egui_custom_widgets/tree/main
What You'll Learn:
- impl egui::Widget trait for custom widgets
- fn ui(self, ui: &mut Ui) - Response method signature
- allocate_exact_size for reserving widget space
- Sense::click() for click interaction
- response.clicked() to toggle state
- Custom painting with ui.painter()
- rect_filled for rounded background
- circle_filled for the sliding knob
- Lifetime parameters for borrowed data
- ui.add(Toggle::new(...)) usage pattern
- Reusing the same widget for multiple settings
- Dark/light theme toggle with set_visuals
Timestamps:
0:00 Intro
0:12 App Preview
0:22 What We're Building
0:54 Create Project & Cargo.toml
1:26 main.rs — mod app & mod toggle
2:01 toggle.rs — Toggle Struct with Lifetime
2:24 impl new() Constructor
2:44 impl egui::Widget — fn ui()
3:14 allocate_exact_size & Sense::click
3:44 Custom Painting — Background & Circle
4:42 Return Response
5:08 app.rs — use crate::toggle::Toggle
5:28 MyApp Struct & Default
5:54 impl App — Dark/Light Visuals
6:20 Grid with Four Toggle Widgets
7:20 Status Label & Close
7:44 cargo build & Neo-tree Browse
8:10 App Demo
8:51 Recap
9:31 Next Episode
Series: Learn egui in Neovim
Part of a 36-episode series teaching Rust GUI development with egui, coded entirely in Neovim with Neo-tree.
Tags: egui, rust, gui, custom widget, toggle switch, impl widget, tutorial, neovim, painter, allocate_exact_size, sense click, reusable
component
Tags
eguirustguicustom widgettoggle switchimpl widgettutorialneovimpainterallocate_exact_sizesense clickreusable component