
* code hotkey to N, move superscript hotkey to Y ctrl A S D F G H are all taken, CTRL Q is traditionally to remove formatting and should be reserved for that. CTRL W E R T are also all taken. CTRL Z X C V are taken so all of the first 4/5 keys of each row except Q are inaccessible. * strike through conflict, update text * fixed underline command * added ALTSHIFT, browser documentation * underline ALTSHIFT Q it leaves the Q character which is considered a bug but before this pull underline was not working entirely so this is progress * update text * ALTSHIFT is treated as a command * added eighth command, ALTSHIFT+W adds two spaces * CTRL+Y to toggle case on text_edit demo * better code * Revised Menu * fix dead link * Update lib.rs * Update easy_mark_editor.rs * Update egui/src/data/input.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * update * reverted variables used for debugging * fixed labels hotkey conflict * comments * fmt * cargo fmt * Nice hotkey menu Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
111 lines
3.8 KiB
Rust
111 lines
3.8 KiB
Rust
/// Showcase [`TextEdit`].
|
|
#[derive(PartialEq)]
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
|
#[cfg_attr(feature = "serde", serde(default))]
|
|
pub struct TextEdit {
|
|
pub text: String,
|
|
}
|
|
|
|
impl Default for TextEdit {
|
|
fn default() -> Self {
|
|
Self {
|
|
text: "Edit this text".to_owned(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl super::Demo for TextEdit {
|
|
fn name(&self) -> &'static str {
|
|
"🖹 TextEdit"
|
|
}
|
|
|
|
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
|
|
egui::Window::new(self.name())
|
|
.open(open)
|
|
.resizable(false)
|
|
.show(ctx, |ui| {
|
|
use super::View as _;
|
|
self.ui(ui);
|
|
});
|
|
}
|
|
}
|
|
|
|
impl super::View for TextEdit {
|
|
fn ui(&mut self, ui: &mut egui::Ui) {
|
|
let Self { text } = self;
|
|
|
|
ui.horizontal(|ui| {
|
|
ui.spacing_mut().item_spacing.x = 0.0;
|
|
ui.label("Advanced usage of ");
|
|
ui.code("TextEdit");
|
|
ui.label(".");
|
|
});
|
|
|
|
let output = egui::TextEdit::multiline(text)
|
|
.hint_text("Type something!")
|
|
.show(ui);
|
|
|
|
ui.horizontal(|ui| {
|
|
ui.spacing_mut().item_spacing.x = 0.0;
|
|
ui.label("Selected text: ");
|
|
if let Some(text_cursor_range) = output.cursor_range {
|
|
use egui::TextBuffer as _;
|
|
let selected_chars = text_cursor_range.as_sorted_char_range();
|
|
let selected_text = text.char_range(selected_chars);
|
|
ui.code(selected_text);
|
|
}
|
|
});
|
|
|
|
let anything_selected = output
|
|
.cursor_range
|
|
.map_or(false, |cursor| !cursor.is_empty());
|
|
|
|
ui.add_enabled(
|
|
anything_selected,
|
|
egui::Label::new("Press ctrl+Y to toggle the case of selected text (cmd+Y on Mac)"),
|
|
);
|
|
|
|
if ui
|
|
.input_mut()
|
|
.consume_key(egui::Modifiers::COMMAND, egui::Key::Y)
|
|
{
|
|
if let Some(text_cursor_range) = output.cursor_range {
|
|
use egui::TextBuffer as _;
|
|
let selected_chars = text_cursor_range.as_sorted_char_range();
|
|
let selected_text = text.char_range(selected_chars.clone());
|
|
let upper_case = selected_text.to_uppercase();
|
|
let new_text = if selected_text == upper_case {
|
|
selected_text.to_lowercase()
|
|
} else {
|
|
upper_case
|
|
};
|
|
text.delete_char_range(selected_chars.clone());
|
|
text.insert_text(&new_text, selected_chars.start);
|
|
}
|
|
}
|
|
|
|
ui.horizontal(|ui| {
|
|
ui.label("Move cursor to the:");
|
|
|
|
if ui.button("start").clicked() {
|
|
let text_edit_id = output.response.id;
|
|
if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), text_edit_id) {
|
|
let ccursor = egui::text::CCursor::new(0);
|
|
state.set_ccursor_range(Some(egui::text::CCursorRange::one(ccursor)));
|
|
state.store(ui.ctx(), text_edit_id);
|
|
ui.ctx().memory().request_focus(text_edit_id); // give focus back to the [`TextEdit`].
|
|
}
|
|
}
|
|
|
|
if ui.button("end").clicked() {
|
|
let text_edit_id = output.response.id;
|
|
if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), text_edit_id) {
|
|
let ccursor = egui::text::CCursor::new(text.chars().count());
|
|
state.set_ccursor_range(Some(egui::text::CCursorRange::one(ccursor)));
|
|
state.store(ui.ctx(), text_edit_id);
|
|
ui.ctx().memory().request_focus(text_edit_id); // give focus back to the [`TextEdit`].
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|