use crate::density;
use crate::theme::{self, spacing, surface, FontScale};
use crate::widgets::{divider, meta_text, scaled_text};
use iced::widget::{button, column, container, mouse_area, opaque, pin, row, stack, Space};
use iced::{Element, Length};
#[derive(Debug, Clone, PartialEq)]
pub enum MenuRow<Message> {
Item { label: String, hint: Option<String>, on: Option<Message> },
Separator,
}
impl<Message> MenuRow<Message> {
pub fn item(label: impl Into<String>, on: Message) -> Self {
MenuRow::Item { label: label.into(), hint: None, on: Some(on) }
}
pub fn disabled(label: impl Into<String>) -> Self {
MenuRow::Item { label: label.into(), hint: None, on: None }
}
pub fn hint(self, note: impl Into<String>) -> Self {
match self {
MenuRow::Item { label, on, .. } => MenuRow::Item { label, hint: Some(note.into()), on },
separator => separator,
}
}
}
pub const MENU_WIDTH: f32 = 260.0;
pub const MENU_SEPARATOR: f32 = 9.0;
pub fn menu_padding(scale: FontScale) -> f32 {
scale.apply(spacing::XS)
}
pub fn menu_separator_height(scale: FontScale) -> f32 {
scale.apply(MENU_SEPARATOR)
}
pub fn menu_size(separators: impl IntoIterator<Item = bool>, scale: FontScale) -> (f32, f32) {
let rows: f32 = separators.into_iter().map(|sep| if sep { menu_separator_height(scale) } else { density::row_height(scale) }).sum();
let chrome = 2.0 * menu_padding(scale) + 2.0;
(scale.apply(MENU_WIDTH), rows + chrome)
}
pub fn place_menu(at: (f32, f32), menu: (f32, f32), window: (f32, f32)) -> (f32, f32) {
fn axis(at: f32, size: f32, limit: f32) -> f32 {
if at + size <= limit {
at
} else if at - size >= 0.0 {
at - size
} else {
(limit - size).max(0.0)
}
}
(axis(at.0, menu.0, window.0), axis(at.1, menu.1, window.1))
}
pub fn context_menu<'a, Message: Clone + 'a>(rows: Vec<MenuRow<Message>>, highlighted: Option<usize>, scale: FontScale, width: f32) -> Element<'a, Message> {
let row_h = density::row_height(scale);
let mut list = column![].width(Length::Fill);
for (index, item) in rows.into_iter().enumerate() {
match item {
MenuRow::Separator => {
let slot = menu_separator_height(scale);
list = list.push(container(divider()).height(Length::Fixed(slot)).center_y(Length::Fixed(slot)).padding([0, spacing::SM as u16]));
}
MenuRow::Item { label, hint, on } => {
let enabled = on.is_some();
let lit = highlighted == Some(index);
let colour = if enabled { theme::text() } else { theme::text_dim() };
let mut content = row![scaled_text(label, density::ROW_TEXT_BASE, scale)
.color(colour)
.wrapping(iced::widget::text::Wrapping::None)
.width(Length::Fill)]
.align_y(iced::Alignment::Center)
.spacing(spacing::MD);
if let Some(hint) = hint {
content = content.push(meta_text(hint, density::META_TEXT_BASE, scale));
}
list = list.push(
button(content)
.on_press_maybe(on)
.width(Length::Fill)
.height(Length::Fixed(row_h))
.padding([0, spacing::SM as u16])
.style(move |t: &iced::Theme, status| menu_item_style(t, status, lit, enabled)),
);
}
}
}
container(list)
.padding(menu_padding(scale))
.width(Length::Fixed(width))
.style(|_t: &iced::Theme| container::Style {
background: Some(iced::Background::Color(surface::sidebar())),
border: iced::Border { color: surface::card_border(), width: 1.0, radius: density::inner_radius().into() },
..container::Style::default()
})
.into()
}
pub fn menu_item_style(theme: &iced::Theme, status: button::Status, highlighted: bool, enabled: bool) -> button::Style {
let hovered = matches!(status, button::Status::Hovered | button::Status::Pressed);
let lit = enabled && (highlighted || hovered);
button::Style {
background: lit.then(|| iced::Background::Color(theme.extended_palette().primary.weak.color)),
text_color: theme::text(),
border: iced::Border { radius: density::nested_radius().into(), ..iced::Border::default() },
..button::Style::default()
}
}
pub fn menu_overlay<'a, Message: Clone + 'a>(rows: Vec<MenuRow<Message>>, at: (f32, f32), window: (f32, f32), close: Message, scale: FontScale) -> Element<'a, Message> {
let size = menu_size(rows.iter().map(|r| matches!(r, MenuRow::Separator)), scale);
let (x, y) = place_menu(at, size, window);
let away = mouse_area(Space::new().width(Length::Fill).height(Length::Fill)).on_press(close.clone()).on_right_press(close);
let placed = pin(opaque(context_menu(rows, None, scale, size.0))).x(x).y(y).width(Length::Fill).height(Length::Fill);
stack![away, placed].width(Length::Fill).height(Length::Fill).into()
}
pub mod pointer {
use iced::{event, mouse, Event, Subscription};
use std::sync::atomic::{AtomicU32, Ordering};
static X: AtomicU32 = AtomicU32::new(0);
static Y: AtomicU32 = AtomicU32::new(0);
pub fn track<Message: 'static + Send>() -> Subscription<Message> {
event::listen_with(|event, _status, _window| {
if let Event::Mouse(mouse::Event::CursorMoved { position }) = event {
X.store(position.x.to_bits(), Ordering::Relaxed);
Y.store(position.y.to_bits(), Ordering::Relaxed);
}
None
})
}
pub fn last() -> (f32, f32) {
(f32::from_bits(X.load(Ordering::Relaxed)), f32::from_bits(Y.load(Ordering::Relaxed)))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_menu_near_an_edge_flips_to_open_the_other_way() {
let window = (800.0, 600.0);
let menu = (260.0, 200.0);
assert_eq!(place_menu((100.0, 100.0), menu, window), (100.0, 100.0), "room: down and right");
assert_eq!(place_menu((700.0, 100.0), menu, window), (440.0, 100.0), "the right edge: opens left");
assert_eq!(place_menu((100.0, 550.0), menu, window), (100.0, 350.0), "the bottom: opens up");
}
#[test]
fn a_menu_too_big_for_either_side_keeps_its_first_rows_on_screen() {
assert_eq!(place_menu((50.0, 50.0), (260.0, 900.0), (800.0, 600.0)), (50.0, 0.0));
}
#[test]
fn the_size_placed_counts_each_row_as_it_is_drawn() {
let scale = FontScale::default();
let (w, h) = menu_size([false, true, false], scale);
assert_eq!(w, MENU_WIDTH);
assert_eq!(h, 2.0 * density::row_height(scale) + MENU_SEPARATOR + 2.0 * spacing::XS + 2.0);
}
#[test]
fn a_row_without_a_message_is_shown_but_not_choosable() {
let row: MenuRow<u8> = MenuRow::disabled("End as administrator").hint("needs Show everything");
assert_eq!(row, MenuRow::Item { label: "End as administrator".into(), hint: Some("needs Show everything".into()), on: None });
}
}