实现目标:在输入框中输入文本,通过指定热键,弹出菜单。
use fltk::{
enums::{Event, Key},
prelude::*,
*,
};
fn main() {
let app = app::App::default();
let mut window = window::DoubleWindow::new(100, 100, 300, 200, "Menu in text editor");
let mut menu = menu::MenuButton::default().with_type(menu::MenuButtonType::Popup3);
menu.add_choice("Hello|Hi|You");
let mut editor = text::TextEditor::new(20, 20, 260, 160, "");
let buf = text::TextBuffer::default();
editor.set_buffer(buf.clone());
editor.handle(move |te, ev| match ev {
Event::KeyDown => {
if app::event_key_down(Key::AltL) {
if let Some(item) = menu.popup() {
if let Some(label) = item.label() {
te.insert(&label);
}
}
println!("{}", buf.line_text(te.insert_position()));
}
true
}
_ => false,
});
window.end();
window.show();
while app.wait() {}
}