Guide API
Skip to main content

MenuBuilder

Struct MenuBuilder 

Source
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

Source

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();
Source

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++).

Source

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+.

Source

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.

Source

pub fn submenu( self, name: impl Into<String>, key_code: impl Into<Option<KeyEvent>>, build: impl FnOnce(MenuBuilder) -> MenuBuilder, ) -> Self

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.

Source

pub fn build(self) -> Menu

Finish building and return the completed Menu.

Trait Implementations§

Source§

impl Default for MenuBuilder

Source§

fn default() -> MenuBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.