pub struct MenuBuilder { /* private fields */ }Expand description
A fluent builder for a Menu.
Start a builder via Menu::builder() or MenuBuilder::default(), chain
item-appending methods, and finish with .build():
use tvision_rs::menu::{Menu, alt};
use tvision_rs::command::Command;
let menu = Menu::builder()
.submenu("~F~ile", alt('f'), |m| {
m.command_key("E~x~it", Command::QUIT, alt('x'), "Alt-X")
})
.build();Each method appends exactly one MenuItem and returns self for
chaining. The first item appended sets Menu::default to Some(0),
making the first item pre-selected when the menu opens. The builder
always uses Some(0) as the default — there is no builder method to
choose a different pre-selected item. If you need a non-first default
(e.g. to match the Turbo Vision two-argument TMenu(itemList, theDefault)
constructor), build the item list with the builder, then override the field:
let mut menu = builder.build(); menu.default = Some(n);, or construct a
Menu struct literal directly (both fields are pub).
For items that need full field control (custom help_ctx, pre-set
disabled, etc.) use MenuBuilder::item with a MenuItem literal.
§Turbo Vision heritage
Replaces the operator+ overloads (menu.cpp) that chained TSubMenu and
TMenuItem nodes together, and the NewItem/NewLine/NewSubMenu
constructor functions from the Pascal-era API.
Implementations§
Source§impl MenuBuilder
impl MenuBuilder
Sourcepub fn item(self, item: MenuItem) -> Self
pub fn item(self, item: MenuItem) -> Self
Append an already-built MenuItem, giving full control over every field.
Use this when the convenience methods (command,
command_key, submenu)
don’t expose what you need — for example, a custom help_ctx, a
pre-disabled item, or a submenu with a non-NO_CONTEXT help id:
use tvision_rs::menu::{Menu, MenuItem};
use tvision_rs::command::Command;
use tvision_rs::help::HelpCtx;
let menu = Menu::builder()
.item(MenuItem::Command {
name: "~S~ave".into(),
command: Command::SAVE,
key_code: None,
param: None,
help_ctx: HelpCtx::custom("myapp.save"),
disabled: false,
})
.build();Sourcepub fn separator(self) -> Self
pub fn separator(self) -> Self
Append a MenuItem::Separator — a horizontal divider line.
Corresponds to the classic Turbo Vision NewLine() helper (newLine() in
magiblot C++).
Sourcepub fn command(self, name: impl Into<String>, command: Command) -> Self
pub fn command(self, name: impl Into<String>, command: Command) -> Self
Append a MenuItem::Command with no accelerator and no shortcut text.
Use this for items that are triggered only by navigating with arrow keys
or by their hotkey letter, not by a global accelerator. For items with an
accelerator use command_key instead.
All convenience builder methods use HelpCtx::NO_CONTEXT and
disabled: false. To set a custom help_ctx, pre-disable the item, or
control any other non-default field, build the item explicitly with
MenuBuilder::item and a MenuItem::Command literal.
Corresponds to NewItem(name, "", 0, cmd, hcNoContext, 0) in the classic
(Pascal-era) Turbo Vision API; magiblot C++ builds the equivalent
TMenuItem via operator+.
Sourcepub fn command_key(
self,
name: impl Into<String>,
command: Command,
key_code: impl Into<Option<KeyEvent>>,
param: impl Into<String>,
) -> Self
pub fn command_key( self, name: impl Into<String>, command: Command, key_code: impl Into<Option<KeyEvent>>, param: impl Into<String>, ) -> Self
Append a MenuItem::Command with an accelerator key and shortcut display text.
key_code is the global accelerator (use alt() for Alt+<char>
combinations, or pass a KeyEvent directly for others). param is the
display hint shown right-aligned next to the label (e.g. "Alt-X",
"F3"); an empty string is stored as None (no hint shown).
use tvision_rs::menu::{Menu, alt};
use tvision_rs::command::Command;
use tvision_rs::event::{Key, KeyEvent};
let menu = Menu::builder()
.command_key("~O~pen", Command::OPEN, KeyEvent::from(Key::F(3)), "F3")
.command_key("E~x~it", Command::QUIT, alt('x'), "Alt-X")
.build();See MenuBuilder::command for the HelpCtx/disabled escape hatch.
Corresponds to NewItem(name, param, key, cmd, hcNoContext, 0) in the
classic (Pascal-era) Turbo Vision API.
Append a MenuItem::SubMenu whose contents are built by the closure.
name is the label (tilde-hotkey syntax applies). key_code is the
optional global accelerator (use alt() or None). The closure
receives a fresh MenuBuilder for the nested menu and must return it
after appending items:
use tvision_rs::menu::{Menu, alt};
use tvision_rs::command::Command;
let menu = Menu::builder()
.submenu("~F~ile", alt('f'), |m| {
m.command("~N~ew", Command::NEW)
.command("~O~pen", Command::OPEN)
})
.build();help_ctx limitation: this method always uses HelpCtx::NO_CONTEXT
for the submenu item itself. To attach a custom help_ctx, construct the
MenuItem::SubMenu variant directly and pass it to MenuBuilder::item.
Corresponds to NewSubMenu(name, key, subMenu, hcNoContext, 0) in the
classic (Pascal-era) Turbo Vision API.
Trait Implementations§
Source§impl Default for MenuBuilder
impl Default for MenuBuilder
Source§fn default() -> MenuBuilder
fn default() -> MenuBuilder
Auto Trait Implementations§
impl Freeze for MenuBuilder
impl RefUnwindSafe for MenuBuilder
impl Send for MenuBuilder
impl Sync for MenuBuilder
impl Unpin for MenuBuilder
impl UnsafeUnpin for MenuBuilder
impl UnwindSafe for MenuBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.