Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

focus text edit and fix system ordering #22

Merged
merged 2 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ impl Plugin for DevConsolePlugin {
Update,
(
ui::read_logs,
ui::open_close_ui,
ui::render_ui.run_if(|s: Res<ConsoleUiState>| s.open),
(
ui::open_close_ui,
ui::render_ui.run_if(|s: Res<ConsoleUiState>| s.open),
)
.chain(),
),
);
}
Expand Down
15 changes: 15 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub const COMMAND_RESULT_NAME: &str = "console_result";
pub(crate) struct ConsoleUiState {
/// Wherever the console is open or not.
pub(crate) open: bool,
/// Whether we have set focus this open or not.
pub(crate) text_focus: bool,
/// A list of all log messages received plus an
/// indicator indicating if the message is new.
pub(crate) log: Vec<(LogMessage, bool)>,
Expand Down Expand Up @@ -54,6 +56,7 @@ pub(crate) fn open_close_ui(
) {
if key.just_pressed(config.open_key) {
state.open = !state.open;
state.text_focus = false;
}
}

Expand Down Expand Up @@ -94,20 +97,32 @@ pub(crate) fn render_ui(
bottom: 5.0,
}))
.show_inside(ui, |ui| {
let text_edit_id = egui::Id::new("text_edit");

//We can use a right to left layout, so we can place the text input last and tell it to fill all remaining space
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if ui.button("Submit").clicked() {
submit_command(&mut state.command);

// Return keyboard focus to the text edit control.
ui.ctx().memory_mut(|mem| mem.request_focus(text_edit_id));
}
// ui.button is a shorthand command, a similar command exists for text edits, but this is how to manually construct a widget.
// doing this also allows access to more options of the widget, rather than being stuck with the default the shorthand picks.
let text_edit = egui::TextEdit::singleline(&mut state.command)
.id(text_edit_id)
.desired_width(ui.available_width())
.margin(egui::Vec2::splat(4.0))
.font(config.theme.font.clone())
.lock_focus(true);

ui.add(text_edit);

// Each time we open the console, we want to set focus to the text edit control.
if !state.text_focus {
state.text_focus = true;
ui.ctx().memory_mut(|mem| mem.request_focus(text_edit_id));
}
});
});
// Now we can fill the remaining minutespace with a scrollarea, which has only the vertical scrollbar enabled and expands to be as big as possible.
Expand Down