use crate::color::Color;
use crate::event::{Cursor, Key, Mods};
use crate::id::Id;
use crate::layout::{Align, Length};
use crate::menus::{Lead, Row};
use crate::told::Role;
use crate::ui::{Icon, Measure, Response, Ui};
const LINE: f32 = 9.0;
const GAP: f32 = 2.0;
const MORE: Icon = Icon::solid("M3.5 6.8a1.2 1.2 0 1 1 0 2.4 1.2 1.2 0 0 1 0-2.4zM8 6.8a1.2 1.2 0 1 1 0 2.4 1.2 1.2 0 0 1 0-2.4zM12.5 6.8a1.2 1.2 0 1 1 0 2.4 1.2 1.2 0 0 1 0-2.4z");
pub(crate) struct Bar {
id: Id,
down: bool,
room: f32,
need: f32,
taken: f32,
all: f32,
count: u32,
out: bool,
line: bool,
seen: bool,
more: Id,
}
pub(crate) struct Left {
bar: Id,
tool: Id,
icon: Icon,
hint: String,
on: Option<bool>,
line: bool,
}
impl Ui {
#[cfg_attr(debug_assertions, track_caller)]
pub fn tool_bar(&mut self) -> Response {
self.bar_of_tools(false)
}
pub fn tool_column(&mut self) -> Response {
self.bar_of_tools(true)
}
#[cfg_attr(debug_assertions, track_caller)]
fn bar_of_tools(&mut self, down: bool) -> Response {
let (t, id, at) = (self.theme, self.next_id(), self.tree.specs.len() as u32);
let read = self.rooms.get(&id).copied();
let along = if down { self.column().h(Length::Grow(1.0)).align(Align::Start, Align::Center) } else { self.row().w(Length::Grow(1.0)) };
let bar = along.pad(4.0).gap(GAP).fill(t.panel).clip().open();
self.measures.push(Measure { at, within: at, id, read: read.unwrap_or([f32::NAN; 3]) });
self.tell(&bar, Role::ToolBar, None, "");
self.frame.later[0] = Some(Ui::show_left);
let room = read.map_or(f32::INFINITY, |read| read[down as usize]);
let more = self.id("more");
let fresh = Bar { id, down, room, need: 0.0, taken: 0.0, all: 0.0, count: 0, out: false, line: false, seen: true, more };
match self.frame.bars.iter_mut().find(|bar| bar.id == id) {
Some(bar) => *bar = Bar { need: bar.need, ..fresh },
None => self.frame.bars.push(fresh),
}
bar
}
fn this_bar(&self) -> Option<usize> {
let around = self.tree.specs[self.open.last()?.at as usize].id;
self.frame.bars.iter().position(|bar| bar.id == around)
}
pub fn tool(&mut self, icon: Icon, hint: &str) -> Response {
self.tool_like(icon, hint, None)
}
pub fn tool_toggle(&mut self, on: &mut bool, icon: Icon, hint: &str) -> Response {
let mut state = self.tool_like(icon, hint, Some(*on));
if state.clicked {
(*on, state.changed) = (!*on, true);
}
state
}
pub fn tool_group(&mut self) {
if let Some(at) = self.this_bar() {
self.frame.bars[at].line = true;
}
}
fn tool_like(&mut self, icon: Icon, hint: &str, on: Option<bool>) -> Response {
let Some(at) = self.this_bar() else { return self.tool_button(None, icon, hint, on) };
let (control, bar) = (self.theme.control, &mut self.frame.bars[at]);
let (nth, line) = (bar.count, std::mem::take(&mut bar.line) && bar.count > 0);
let wide = control + GAP + if line { LINE + GAP } else { 0.0 };
let more = if bar.need > bar.room { control + GAP } else { 0.0 };
let fits = !bar.out && bar.taken + wide + more <= bar.room + GAP;
(bar.count, bar.all, bar.taken) = (nth + 1, bar.all + wide, bar.taken + if fits { wide } else { 0.0 });
let id = self.id(("tool", nth));
if fits {
if line {
self.tool_line(at);
}
return self.tool_button(Some(nth), icon, hint, on);
}
if !std::mem::replace(&mut self.frame.bars[at].out, true) {
self.more_button(at);
}
self.leave_out(at, id, icon, hint, on, line);
let clicked = self.frame.picked == id;
self.frame.picked = if clicked { 0 } else { self.frame.picked };
Response { id, clicked, ..Default::default() }
}
fn tool_button(&mut self, nth: Option<u32>, icon: Icon, hint: &str, on: Option<bool>) -> Response {
let t = self.theme;
let state = self.press(nth.map_or_else(|| self.next_id(), |nth| self.id(("tool", nth))));
if state.hovered {
self.set_cursor(Cursor::Hand);
}
let down = on == Some(true) || state.pressed;
let (fill, ink) = match (down, state.hovered) {
(true, _) => (t.selected, t.text),
(false, true) => (t.hover, t.text),
(false, false) => (Color::NONE, t.text_dim),
};
let edge = if on == Some(true) { t.accent } else { Color::NONE };
let frame = self.row().size(t.control, t.control).align(Align::Center, Align::Center).fill(fill).border(edge, 1.0).radius(t.radius);
match nth {
Some(nth) => frame.key(("tool", nth)).sense().focusable().open(),
None => frame.sense().focusable().open(),
};
self.el().icon(icon, 16.0, ink).show();
self.close();
let (name, keys) = hint.split_once('\t').unwrap_or((hint, ""));
self.tell(&state, Role::Button, on, name);
self.tell_keys(keys);
self.tooltip(&state, hint);
state
}
fn tool_line(&mut self, at: usize) {
let (t, down) = (self.theme, self.frame.bars[at].down);
let [wide, high] = if down { [t.control, LINE] } else { [LINE, t.control] };
self.row().size(wide, high).align(Align::Center, Align::Center).open();
self.el().size(if down { t.control - 10.0 } else { 1.0 }, if down { 1.0 } else { t.control - 10.0 }).fill(t.line).show();
self.close();
}
fn more_button(&mut self, at: usize) {
let (t, more, down) = (self.theme, self.frame.bars[at].more, self.frame.bars[at].down);
let state = self.press(more);
let open = self.popups.iter().any(|popup| popup.id == more);
if !open && (state.clicked || state.focused && self.key(if down { Key::Right } else { Key::Down }, Mods::NONE)) {
self.pop_up(more, state.rect, down);
}
let fill = if open || state.hovered { t.hover } else { Color::NONE };
self.row().key("more").size(t.control, t.control).align(Align::Center, Align::Center).fill(fill).radius(t.radius).sense().focusable().open();
self.el().icon(MORE, 16.0, if open || state.hovered { t.text } else { t.text_dim }).show();
self.close();
let words = self.frame_words.more;
self.tell(&state, Role::Fold, Some(open), words);
self.tooltip(&state, words);
}
fn leave_out(&mut self, at: usize, tool: Id, icon: Icon, hint: &str, on: Option<bool>, line: bool) {
let (bar, count) = (self.frame.bars[at].id, self.frame.left_count);
let line = line && self.frame.left[..count].last().is_some_and(|left| left.bar == bar);
self.frame.left_count += 1;
if let Some(left) = self.frame.left.get_mut(count) {
(left.bar, left.tool, left.icon, left.on, left.line) = (bar, tool, icon, on, line);
if left.hint != hint {
left.hint.clear();
left.hint.push_str(hint);
}
return;
}
self.frame.left.push(Left { bar, tool, icon, hint: hint.to_string(), on, line });
}
pub(crate) fn show_left(&mut self) {
let left = std::mem::take(&mut self.frame.left);
let count = std::mem::replace(&mut self.frame.left_count, 0);
self.frame.bars.retain(|bar| bar.seen);
for at in 0..self.frame.bars.len() {
let bar = &mut self.frame.bars[at];
(self.again, bar.need, bar.seen) = (self.again || bar.need != bar.all, bar.all, false);
let (id, more) = (bar.id, bar.more);
if !self.menu(more) {
continue;
}
for tool in left[..count].iter().filter(|tool| tool.bar == id) {
self.left_row(tool);
}
self.close();
}
self.frame.left = left;
self.frame.left.truncate(count);
}
fn left_row(&mut self, tool: &Left) {
if tool.line {
self.separator();
}
let row = Row { lead: Lead::Icon(tool.icon, tool.on == Some(true)), on: tool.on, ..Row::PLAIN };
if self.menu_row(&tool.hint, row).clicked {
(self.frame.picked, self.again) = (tool.tool, true);
self.close_popup();
}
}
}