floem icon indicating copy to clipboard operation
floem copied to clipboard

[Editor] Event functions are not called on the editor

Open dominikwilkowski opened this issue 11 months ago • 2 comments

Floems event functions like on_event, on_key_down, on_resize etc are not being called. Taking the editor example below:

use floem::{
    keyboard::{Key, ModifiersState, NamedKey},
    view::View,
    views::{
        editor::{
            command::{Command, CommandExecuted},
            core::{command::EditCommand, editor::EditType, selection::Selection},
            text::SimpleStyling,
        },
        stack, text_editor, Decorators,
    },
    widgets::button,
};

fn app_view() -> impl View {
    let text = std::env::args()
        .nth(1)
        .map(|s| std::fs::read_to_string(s).unwrap());
    let text = text.as_deref().unwrap_or("Hello world");

    let editor_a = text_editor(text).styling(SimpleStyling::dark());
    let editor_b = editor_a
        .shared_editor()
        .pre_command(|ev| {
            if matches!(ev.cmd, Command::Edit(EditCommand::Undo)) {
                println!("Undo command executed on editor B, ignoring!");
                return CommandExecuted::Yes;
            }
            CommandExecuted::No
        })
        .gutter(false)
        .update(|_| {
            // This hooks up to both editors!
            println!("Editor changed");
        })
        .placeholder("Some placeholder text");
    let doc = editor_a.doc();
    let gutter_a = editor_a.editor().gutter;
    let gutter_b = editor_b.editor().gutter;

    let view = stack((
+         editor_a.on_event_cont(floem::event::EventListener::KeyDown, |event| {
+             println!("{:?}", event);
+         }),
        editor_b,
        stack((
            button(|| "Clear").on_click_stop(move |_| {
                doc.edit_single(
                    Selection::region(0, doc.text().len()),
                    "",
                    EditType::DeleteSelection,
                );
            }),
            button(|| "Flip Gutter").on_click_stop(move |_| {
                let a = !gutter_a.get_untracked();
                let b = !gutter_b.get_untracked();
                gutter_a.set(a);
                gutter_b.set(b);
            }),
        ))
        .style(|s| s.width_full().flex_row().items_center().justify_center()),
    ))
    .style(|s| s.size_full().flex_col().items_center().justify_center());

    let id = view.id();
    view.on_key_up(
        Key::Named(NamedKey::F11),
        ModifiersState::empty(),
        move |_| id.inspect(),
    )
}

fn main() {
    floem::launch(app_view)
}

dominikwilkowski avatar Mar 24 '24 21:03 dominikwilkowski

We could just have the editor let the events keep propagating. Then they could be handled up at the TextEditor View.

jrmoulton avatar Mar 27 '24 08:03 jrmoulton

That's what I would expect here

dominikwilkowski avatar Mar 27 '24 10:03 dominikwilkowski