Replies: 1 comment
-
|
Hey, i tried working on your code in egui 0.31.0. Note that the latest version is 0.34.0 and all the existing code is no longer supported. The best I was able to come up with was with Here is a video of it working CleanShot.2026-04-27.at.18.04.06.mp4This is the entire project within a single file. You can copy and paste this into a brand new repo. just set the Cargo.toml to the following [package]
name = "saithe6-question"
version = "0.1.0"
edition = "2024"
[dependencies]
eframe = "0.31.0"
#eframe = "0.34.1"
use eframe::egui::{self, Color32, Ui};
fn main() -> eframe::Result<()> {
let options = eframe::NativeOptions::default();
eframe::run_native(
"egui side panel scaffold",
options,
Box::new(|_cc| Ok(Box::<MyApp>::default())),
)
}
#[derive(Default)]
struct MyApp {
insert_mode: bool,
skills: NamedList,
resources: NamedList,
character_resource: CharacterResource,
minor_harm_0: String,
minor_harm_1: String,
major_harm: String,
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
self.left_panel(ui);
ui.heading("Main content area");
ui.label("This central panel is just here so the side panel has something to live beside.");
ui.checkbox(&mut self.insert_mode, "Insert mode");
});
}
}
impl MyApp {
fn left_panel(&mut self, ui: &mut Ui) {
egui::SidePanel::left("left_panel")
.resizable(false)
// .resizable(true)
.exact_width(300.0)
// .exact_width(ui.available_width())
// .default_width(ui.available_width())
.show_inside(ui, |ui| {
let ui_builder = egui::UiBuilder::new();
ui.scope_builder(ui_builder, |ui| show_stats(ui, self));
ui.separator();
self.skills.list(ui, &mut self.insert_mode);
ui.separator();
ui.horizontal(|ui| {
egui::TextEdit::singleline(&mut self.character_resource.name)
.clip_text(false)
.background_color(Color32::TRANSPARENT)
.frame(false)
.hint_text("...")
.desired_width(20.0)
.insertable(ui, &mut self.insert_mode);
ui.with_layout(
egui::Layout::default().with_cross_align(egui::Align::Max),
|ui| {
egui::DragValue::new(&mut self.character_resource.count)
.speed(0.05)
.range(0..=5)
.suffix("/5")
.insertable(ui, &mut self.insert_mode);
},
);
});
self.resources.list(ui, &mut self.insert_mode);
ui.separator();
let mut harm = |label: &str, harm: &mut String| {
ui.horizontal(|ui| {
ui.label(label);
egui::TextEdit::singleline(harm)
.clip_text(false)
.background_color(Color32::TRANSPARENT)
.frame(false)
.hint_text("...")
.desired_width(20.0)
.insertable(ui, &mut self.insert_mode);
});
};
harm("Minor:", &mut self.minor_harm_0);
harm("Minor:", &mut self.minor_harm_1);
harm("Major:", &mut self.major_harm);
ui.separator();
if ui.button("Action Roll").clicked() {
println!("Action Roll clicked");
};
});
}
}
fn show_stats(ui: &mut Ui, app: &mut MyApp) {
ui.heading("Stats");
ui.checkbox(&mut app.insert_mode, "Insert mode");
ui.label(format!("Skills: {}", app.skills.items.len()));
ui.label(format!("Resources: {}", app.resources.items.len()));
}
#[derive(Clone)]
struct NamedItem {
name: String,
value: i32,
}
impl NamedItem {
fn new(name: impl Into<String>, value: i32) -> Self {
Self {
name: name.into(),
value,
}
}
}
#[derive(Clone)]
struct NamedList {
items: Vec<NamedItem>,
}
impl Default for NamedList {
fn default() -> Self {
Self {
items: vec![
NamedItem::new("Hunt", 1),
NamedItem::new("Study", 2),
NamedItem::new("Tinker", 0),
],
}
}
}
impl NamedList {
fn list(&mut self, ui: &mut Ui, insert_mode: &mut bool) {
for item in &mut self.items {
ui.horizontal(|ui| {
egui::TextEdit::singleline(&mut item.name)
.clip_text(false)
.background_color(Color32::TRANSPARENT)
.frame(false)
.desired_width(80.0)
.insertable(ui, insert_mode);
egui::DragValue::new(&mut item.value)
.speed(0.1)
.range(0..=5)
.insertable(ui, insert_mode);
});
}
if ui.small_button("+ add row").clicked() {
self.items.push(NamedItem::new("New", 0));
}
}
}
struct CharacterResource {
name: String,
count: i32,
}
impl Default for CharacterResource {
fn default() -> Self {
Self {
name: "Stress".to_owned(),
count: 0,
}
}
}
trait InsertableWidgetExt {
type Output;
fn insertable(self, ui: &mut Ui, insert_mode: &mut bool) -> Self::Output;
}
impl InsertableWidgetExt for egui::TextEdit<'_> {
type Output = egui::Response;
fn insertable(self, ui: &mut Ui, insert_mode: &mut bool) -> Self::Output {
ui.add_enabled(*insert_mode, self)
}
}
impl<'a> InsertableWidgetExt for egui::DragValue<'a> {
type Output = egui::Response;
fn insertable(self, ui: &mut Ui, insert_mode: &mut bool) -> Self::Output {
ui.add_enabled(*insert_mode, self)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to make a left panel grow with the size of textedits inside it and shrink back down to minimum size when possible.



this is what it looks like before making it grow
I can make it grow by typing
but then it stays big
I can't find anything that would tell me how to do this.
here's the code for the panel
Beta Was this translation helpful? Give feedback.
All reactions