mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 14:50:03 -04:00
Lock focus on multiline text edit
This commit is contained in:
@@ -128,8 +128,12 @@ pub(crate) struct Focus {
|
|||||||
/// The last widget interested in focus.
|
/// The last widget interested in focus.
|
||||||
last_interested: Option<Id>,
|
last_interested: Option<Id>,
|
||||||
|
|
||||||
|
/// Set at the beginning of the frame, set to `false` when "used".
|
||||||
|
is_focus_locked: bool,
|
||||||
|
|
||||||
/// Set at the beginning of the frame, set to `false` when "used".
|
/// Set at the beginning of the frame, set to `false` when "used".
|
||||||
pressed_tab: bool,
|
pressed_tab: bool,
|
||||||
|
|
||||||
/// Set at the beginning of the frame, set to `false` when "used".
|
/// Set at the beginning of the frame, set to `false` when "used".
|
||||||
pressed_shift_tab: bool,
|
pressed_shift_tab: bool,
|
||||||
}
|
}
|
||||||
@@ -195,6 +199,7 @@ impl Focus {
|
|||||||
modifiers,
|
modifiers,
|
||||||
} = event
|
} = event
|
||||||
{
|
{
|
||||||
|
if !self.is_focus_locked {
|
||||||
if modifiers.shift {
|
if modifiers.shift {
|
||||||
self.pressed_shift_tab = true;
|
self.pressed_shift_tab = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -203,6 +208,7 @@ impl Focus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn end_frame(&mut self, used_ids: &epaint::ahash::AHashMap<Id, Pos2>) {
|
pub(crate) fn end_frame(&mut self, used_ids: &epaint::ahash::AHashMap<Id, Pos2>) {
|
||||||
if let Some(id) = self.id {
|
if let Some(id) = self.id {
|
||||||
@@ -225,11 +231,11 @@ impl Focus {
|
|||||||
self.id = Some(id);
|
self.id = Some(id);
|
||||||
self.give_to_next = false;
|
self.give_to_next = false;
|
||||||
} else if self.id == Some(id) {
|
} else if self.id == Some(id) {
|
||||||
if self.pressed_tab {
|
if self.pressed_tab && !self.is_focus_locked {
|
||||||
self.id = None;
|
self.id = None;
|
||||||
self.give_to_next = true;
|
self.give_to_next = true;
|
||||||
self.pressed_tab = false;
|
self.pressed_tab = false;
|
||||||
} else if self.pressed_shift_tab {
|
} else if self.pressed_shift_tab && !self.is_focus_locked {
|
||||||
self.id_next_frame = self.last_interested; // frame-delay so gained_focus works
|
self.id_next_frame = self.last_interested; // frame-delay so gained_focus works
|
||||||
self.pressed_shift_tab = false;
|
self.pressed_shift_tab = false;
|
||||||
}
|
}
|
||||||
@@ -287,14 +293,22 @@ impl Memory {
|
|||||||
self.interaction.focus.id == Some(id)
|
self.interaction.focus.id == Some(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn lock_focus(&mut self, id: Id) {
|
||||||
|
if self.had_focus_last_frame(id) && self.has_focus(id) {
|
||||||
|
self.interaction.focus.is_focus_locked = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Give keyboard focus to a specific widget
|
/// Give keyboard focus to a specific widget
|
||||||
pub fn request_focus(&mut self, id: Id) {
|
pub fn request_focus(&mut self, id: Id) {
|
||||||
self.interaction.focus.id = Some(id);
|
self.interaction.focus.id = Some(id);
|
||||||
|
self.interaction.focus.is_focus_locked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn surrender_focus(&mut self, id: Id) {
|
pub fn surrender_focus(&mut self, id: Id) {
|
||||||
if self.interaction.focus.id == Some(id) {
|
if self.interaction.focus.id == Some(id) {
|
||||||
self.interaction.focus.id = None;
|
self.interaction.focus.id = None;
|
||||||
|
self.interaction.focus.is_focus_locked = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -383,6 +383,10 @@ impl<'t> TextEdit<'t> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ui.memory().has_focus(id) && enabled {
|
if ui.memory().has_focus(id) && enabled {
|
||||||
|
if multiline {
|
||||||
|
ui.memory().lock_focus(id);
|
||||||
|
}
|
||||||
|
|
||||||
let mut cursorp = state
|
let mut cursorp = state
|
||||||
.cursorp
|
.cursorp
|
||||||
.map(|cursorp| {
|
.map(|cursorp| {
|
||||||
@@ -438,6 +442,19 @@ impl<'t> TextEdit<'t> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Event::Key {
|
||||||
|
key: Key::Tab,
|
||||||
|
pressed: true,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if multiline {
|
||||||
|
let mut ccursor = delete_selected(text, &cursorp);
|
||||||
|
insert_text(&mut ccursor, text, "\t");
|
||||||
|
Some(CCursorPair::one(ccursor))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
Event::Key {
|
Event::Key {
|
||||||
key: Key::Enter,
|
key: Key::Enter,
|
||||||
pressed: true,
|
pressed: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user