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

[Editor] Event functions are not called on the editor #396

Open
dominikwilkowski opened this issue Mar 24, 2024 · 2 comments
Open

[Editor] Event functions are not called on the editor #396

dominikwilkowski opened this issue Mar 24, 2024 · 2 comments

Comments

@dominikwilkowski
Copy link
Contributor

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)
}
@jrmoulton
Copy link
Contributor

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

@dominikwilkowski
Copy link
Contributor Author

That's what I would expect here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants