chore: Menu::run
This commit is contained in:
parent
b179e581f5
commit
623b141767
1 changed files with 11 additions and 2 deletions
13
src/lib.rs
13
src/lib.rs
|
@ -4,7 +4,7 @@ pub mod font;
|
|||
use color::ColorPair;
|
||||
use font::FontExt;
|
||||
use pango::{prelude::FontExt as _, Font, Language, SCALE};
|
||||
use wmenu_rs::Menu as WMenu;
|
||||
use wmenu_rs::{Menu as WMenu, MenuError};
|
||||
|
||||
struct Config {
|
||||
pub bottom: bool,
|
||||
|
@ -18,17 +18,20 @@ struct Config {
|
|||
}
|
||||
|
||||
impl Config {
|
||||
// TODO: add font struct to handle these methods
|
||||
fn font_string(&self) -> String {
|
||||
format!("{} {}", self.font, self.font_size)
|
||||
}
|
||||
|
||||
fn font_height(&self, font: &str) -> i32 {
|
||||
let font = Font::load_from_string(font).unwrap();
|
||||
// TODO: let user pick language
|
||||
let metrics = font.metrics(Some(&Language::default()));
|
||||
|
||||
metrics.height() / SCALE
|
||||
}
|
||||
|
||||
// TODO: add ability to override height/line_height
|
||||
fn apply(self, wmenu: &mut WMenu) {
|
||||
let font_string = self.font_string();
|
||||
let font_height = self.font_height(&font_string);
|
||||
|
@ -36,6 +39,7 @@ impl Config {
|
|||
|
||||
let mut height = line_height;
|
||||
|
||||
// FIXME: ugly
|
||||
if self.lines > 0 {
|
||||
height = height + height * self.lines;
|
||||
}
|
||||
|
@ -99,6 +103,11 @@ impl Menu {
|
|||
self.inner_mut().add_items(items);
|
||||
self
|
||||
}
|
||||
|
||||
// TODO: wrap MenuError with new error type
|
||||
pub fn run(&self) -> Result<String, MenuError> {
|
||||
self.inner().run()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Menu {
|
||||
|
@ -122,7 +131,7 @@ mod tests {
|
|||
let items = ["rowern", "sybil", "vex"];
|
||||
let mut menu = Menu::new(Config::default());
|
||||
menu.add_items(&items);
|
||||
let result = menu.inner_mut().run();
|
||||
let result = menu.run();
|
||||
assert!(items.contains(&result.unwrap().as_str()));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue