The menu bar
Os::menus puts menus of MenuItems in the menu bar, whose clicks reach the code that holds each item.
Putting menus in the bar
impl Os {
pub fn menus(&self, menus: impl IntoIterator<Item = Menu>) -> Result<(), Error>;
}
impl Menu {
pub fn new(title: impl Into<String>) -> Self;
pub fn item(self, item: &MenuItem) -> Self;
pub fn separator(self) -> Self;
}A Menu is a title and its items, and a MenuItem is a value the application keeps, as a tray's item is: clicks returns its clicks from then on. No item is named by a string the application matches, so a click reaches only the code that holds the item. Os::menus puts the menus in the bar, in place of the ones put there before:
use hypersig_desktop::{Menu, MenuItem, Os};
/// Puts a File menu in the menu bar, and opens a note each time New is chosen.
fn file_menu(os: &Os) -> Result<(), hypersig_desktop::Error> {
let new = MenuItem::new("New note");
let mut clicks = new.clicks();
os.menus([Menu::new("File").item(&new).separator().item(&MenuItem::new("Export"))])?;
tokio::spawn(async move {
while clicks.next().await {
// Open a window for a new note here.
}
});
Ok(())
}A tray's items are MenuItems too (TrayItem names the same type), so one item can sit in both. In a test, FakeOs::menus returns the menus put in the bar last, and FakeOs::click_menu_item clicks an item.
On each system
- On macOS the menus go between the Edit and Window menus of the application's menu bar, which keeps the application, Edit, and Window menus that copying, pasting, and quitting need. A run saw a menu appear there and System Events click its item through to the code that held it.
- On Linux a bar holding the menus is put at the top of every window, opened then or later, above the page. No run has looked at it yet.
- Windows shows no menu bar: muda puts one in a window only through an
unsafecall, and this crate forbidsunsafe.Os::menusreturnsOk(())there and shows nothing. - A context menu of the system's is not offered, for the same reason on Windows and macOS. A menu shown inside the page, as Discord's are, needs a
contextmenuevent that hypersig does not have yet.