How do I focus on widget programatically? #5545
-
I'm trying to implement developer console for my game and I want the text field to be focused right when user opens console. and still retain that focus when user presses enter key. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
In case anyone is curious, this is is the code for console. It's incomplete and can't actually process user commands and it should have proper message type distinction. use std::sync::RwLock;
use egui::{Align, Button, Color32, Context, Label, Layout, RichText, Rounding, ScrollArea, Shape, Stroke, TextBuffer, TextEdit, Widget, Window};
use egui::epaint::RectShape;
use egui::scroll_area::ScrollBarVisibility;
pub static CONSOLE: RwLock<Console> = RwLock::new(Console::new());
pub struct Console {
output: String,
input: String,
}
impl Console {
pub const fn new() -> Self {
Self {
output: String::new(),
input: String::new(),
}
}
pub fn show(&mut self, ctx: Context) {
Window::new("Console")
.collapsible(false)
.default_open(true)
.resizable(true)
.movable(true)
.title_bar(false)
.show(&ctx, |ui| {
let title = RichText::new("Console")
.small_raised()
.color(Color32::WHITE)
.line_height(Some(25.0));
ui.label(title);
ui.with_layout(Layout::bottom_up(Align::Min), |ui| {
ui.with_layout(Layout::right_to_left(Align::Max), |ui| {
ui.horizontal(|ui| {
let mut should_submit = false;
if Button::new(RichText::new("Submit ").color(Color32::BLACK))
.fill(ctx.style().visuals.window_fill)
.stroke(Stroke::new(1.0, Color32::WHITE))
.ui(ui)
.clicked()
{
should_submit = true;
}
let input_field = TextEdit::singleline(&mut self.input)
.code_editor()
.desired_width(f32::INFINITY)
.lock_focus(true)
.show(ui);
if input_field.response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
should_submit = true;
}
if should_submit {
let input = self.input.take();
self.submit(input.trim());
}
})
});
let window_height = ui.available_height();
ScrollArea::vertical()
.enable_scrolling(true)
.auto_shrink(false)
.max_height(window_height)
.stick_to_bottom(true)
.scroll_bar_visibility(ScrollBarVisibility::AlwaysVisible)
.show(ui, |ui| {
ui.with_layout(Layout::top_down(Align::Min), |ui| {
let rect = ui.clip_rect();
let bg = RectShape::new(
rect,
Rounding::ZERO,
ui.visuals().extreme_bg_color,
ui.visuals().selection.stroke,
);
let where_to_put_background = ui.painter().add(Shape::Noop);
ui.painter().set(where_to_put_background, bg);
self.output.lines().for_each(|line| {
let mut text = RichText::new(line);
text = text.size(10.0);
if line.contains("invalid")
|| line.contains("Error")
|| line.contains("fail")
{
text = text.color(Color32::RED)
} else {
text = text.color(Color32::WHITE)
}
Label::new(text).ui(ui);
});
});
});
});
})
.unwrap();
}
pub fn println(&mut self, text: &str) {
self.output.push_str("\n");
self.output.push_str(text);
}
pub fn submit(&mut self, text: &str) {
self.println(&format!("Unknown command: {}", text));
}
}
#[macro_export]
macro_rules! console_println {
($($arg:tt)*) => {
crate::beam::ui::console::CONSOLE.write().unwrap().println(&format!($($arg)*))
};
} |
Beta Was this translation helpful? Give feedback.
-
Hey,
where response is the response returned by your TextField |
Beta Was this translation helpful? Give feedback.
Yep, or directly call
Response::request_focus