pub struct EditHelper<'a, W: Widget<A::Ui>, A: Area, S> { /* private fields */ }Expand description
A struct used by Modes to edit Text
You will want to use this struct when editing Widgets
with Cursors. For example, let’s say you want to create an
mode for the File widget:
/// A very basic example Mode.
#[derive(Clone)]
struct PlacesCharactersAndMoves;
impl<U: Ui> Mode<U> for PlacesCharactersAndMoves {
type Widget = File;
/* ... */In order to modify the widget, you must implement the
Mode::send_key method. In it, you receive the following:
- The key.
- A
&mut Self::Widget. - An
Area, which you can resize and modify it in other ways. - The current
Cursorsof the widget, these should be modified by theEditHelper.
In a Mode without cursors, you’d probably want to run
Cursors::clear, in order to make sure there are no cursors.
impl<U: Ui> Mode<U> for PlacesCharactersAndMoves {
/* ... */
fn send_key(&mut self, key: KeyEvent, widget: &mut File, area: &U::Area) {
match key {
// actions based on the key pressed
key!(KeyCode::Char('c')) => {
/* Do something when the character 'c' is typed. */
}
/* Matching the rest of the keys */
}
}(You can use the key! macro in order to match KeyEvents).
With the EditHelper, you can modify Text in a simplified
way. This is done by two actions, editing and moving. You
can only do one of these on any number of cursors at the same
time.
impl<U: Ui> Mode<U> for PlacesCharactersAndMoves {
/* ... */
fn send_key(&mut self, key: KeyEvent, file: &mut File, area: &U::Area) {
let mut helper = EditHelper::new(file, area);
match key {
key!(KeyCode::Char(c)) => {
helper.edit_many(.., |e| e.insert('c'));
helper.move_many(.., |mut m| { m.move_hor(1); });
},
key!(KeyCode::Right, KeyMod::SHIFT) => {
helper.move_many(.., |mut m| {
if m.anchor().is_none() {
m.set_anchor()
}
m.move_hor(1);
})
}
key!(KeyCode::Right) => {
helper.move_many(.., |mut m| {
m.unset_anchor();
m.move_hor(1);
})
}
/* Predictable remaining implementations */
}
}Implementations§
Source§impl<'a, W: Widget<A::Ui>, A: Area> EditHelper<'a, W, A, ()>
impl<'a, W: Widget<A::Ui>, A: Area> EditHelper<'a, W, A, ()>
Sourcepub fn new(widget: &'a mut W, area: &'a A) -> Self
pub fn new(widget: &'a mut W, area: &'a A) -> Self
Returns a new instance of EditHelper
Source§impl<W: Widget<A::Ui>, A: Area, S> EditHelper<'_, W, A, S>
impl<W: Widget<A::Ui>, A: Area, S> EditHelper<'_, W, A, S>
Sourcepub fn edit_nth(&mut self, n: usize) -> Editor<'_, W, A, S>
pub fn edit_nth(&mut self, n: usize) -> Editor<'_, W, A, S>
Edits the nth Cursor in the Text
Once dropped, the Cursor in this Editor will be added
back to the list of Cursors, unless it is destroyed
If you want to edit on the main cursor, see edit_main, if
you want to edit on many Cursors, see edit_iter.
Just like all other edit methods, this one will populate the
Cursors, so if there are no Cursors, it will create
one at Point::default.
Sourcepub fn edit_main(&mut self) -> Editor<'_, W, A, S>
pub fn edit_main(&mut self) -> Editor<'_, W, A, S>
Edits the main Cursor in the Text
Once dropped, the Cursor in this Editor will be added
back to the list of Cursors, unless it is destroyed
If you want to edit on the nth cursor, see edit_nth,
same for edit_last, if you want to edit on many
Cursors, see edit_iter.
Just like all other edit methods, this one will populate the
Cursors, so if there are no Cursors, it will create
one at Point::default.
Sourcepub fn edit_last(&mut self) -> Editor<'_, W, A, S>
pub fn edit_last(&mut self) -> Editor<'_, W, A, S>
Edits the last Cursor in the Text
Once dropped, the Cursor in this Editor will be added
back to the list of Cursors, unless it is destroyed
If you want to edit on the nth cursor, see edit_nth,
same for edit_main, if you want to edit on many
Cursors, see edit_iter.
Just like all other edit methods, this one will populate the
Cursors, so if there are no Cursors, it will create
one at Point::default.
Sourcepub fn edit_iter<'b>(&'b mut self) -> EditIter<'b, W, A, S>
pub fn edit_iter<'b>(&'b mut self) -> EditIter<'b, W, A, S>
A Lender over all Editors of the Text
This lets you easily iterate over all Cursors, without
having to worry about insertion affecting the order at which
they are edited (like what repeated calls to edit_nth
would do)
Note however that you can’t use a Lender (also known as a
lending iterator) in a for loop, but you should be able
to just while let Some(e) = editors.next() {} or
helper.edit_iter().for_each(|_| {}) instead.
Just like all other edit methods, this one will populate the
Cursors, so if there are no Cursors, it will create
one at Point::default.
Sourcepub fn widget_mut(&mut self) -> &mut W
pub fn widget_mut(&mut self) -> &mut W
A mutable reference to the Widget
Sourcepub fn cursors_mut(&mut self) -> &mut Cursors
pub fn cursors_mut(&mut self) -> &mut Cursors
A mutable reference to the Widget
Sourcepub fn new_moment(&mut self)
pub fn new_moment(&mut self)
Finishes the current moment and adds a new one to the history