feat: add page surfaces and collapsible sidebar
This commit is contained in:
+269
-618
File diff suppressed because it is too large
Load Diff
@@ -5,6 +5,7 @@ pub(crate) mod clipboard;
|
||||
pub(crate) mod developer_tools;
|
||||
#[cfg(target_os = "macos")]
|
||||
pub(crate) mod native_chrome;
|
||||
pub(crate) mod sidebar;
|
||||
pub(crate) mod sources;
|
||||
pub(crate) mod system_ui;
|
||||
pub(crate) mod tabs;
|
||||
|
||||
@@ -68,7 +68,7 @@ pub fn align_controls(window: &Window) {
|
||||
return;
|
||||
}
|
||||
let bounds = view.bounds();
|
||||
let center = f64::from(crate::tabs::HEIGHT) / 2.0;
|
||||
let center = f64::from(crate::sidebar::HEADER) / 2.0;
|
||||
let content_y = if view.isFlipped() {
|
||||
bounds.origin.y + center
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
//! The only persistent host toolbar. A collapsed sidebar owns just its toggle
|
||||
//! and the native traffic-light safe area; the page owns the rest of the window.
|
||||
use crate::{
|
||||
display_list::{DisplayList, DrawCommand},
|
||||
geometry::{Point, Rect},
|
||||
system_ui::{
|
||||
Button, Interaction, Target, Ui,
|
||||
theme::{self, Theme},
|
||||
},
|
||||
tabs::Tab,
|
||||
};
|
||||
|
||||
pub const WIDTH: f32 = 256.0;
|
||||
pub const HEADER: f32 = 40.0;
|
||||
pub const CONTROLS: f32 = if cfg!(target_os = "macos") { 86.0 } else { 0.0 };
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum Action {
|
||||
Toggle,
|
||||
NewPage,
|
||||
Tool(&'static str),
|
||||
Activate(u64),
|
||||
Close(u64),
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
pub open: bool,
|
||||
pub interaction: Interaction,
|
||||
pub targets: Vec<Target<Action>>,
|
||||
pub scroll: f32,
|
||||
}
|
||||
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
open: true,
|
||||
interaction: Interaction::default(),
|
||||
targets: Vec::new(),
|
||||
scroll: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn left(&self) -> f32 {
|
||||
if self.open { WIDTH } else { 0.0 }
|
||||
}
|
||||
|
||||
pub fn covers(&self, point: Point) -> bool {
|
||||
if self.open {
|
||||
point.x >= 0.0 && point.x < WIDTH
|
||||
} else {
|
||||
Rect::new(0.0, 0.0, CONTROLS + 44.0, HEADER).contains(point)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(
|
||||
&mut self,
|
||||
tabs: &[Tab],
|
||||
height: f32,
|
||||
accent: crate::display_list::Color,
|
||||
) -> DisplayList {
|
||||
let mut ui = Ui::new(Theme::new(accent), self.interaction.clone());
|
||||
if self.open {
|
||||
ui.fill(Rect::new(0.0, 0.0, WIDTH, height), theme::BAR);
|
||||
}
|
||||
let toggle_x = if self.open {
|
||||
WIDTH - 40.0
|
||||
} else {
|
||||
CONTROLS + 4.0
|
||||
};
|
||||
// Draw a sidebar icon with primitives so its shape does not depend on fonts.
|
||||
ui.button(
|
||||
Rect::new(toggle_x, 6.0, 30.0, 28.0),
|
||||
Action::Toggle,
|
||||
Button::new(""),
|
||||
);
|
||||
ui.list.push(DrawCommand::StrokeRoundedRect {
|
||||
rect: Rect::new(toggle_x + 7.0, 13.0, 16.0, 13.0),
|
||||
radius: 2.0,
|
||||
width: 1.0,
|
||||
color: theme::MUTED,
|
||||
});
|
||||
ui.fill(Rect::new(toggle_x + 12.0, 14.0, 1.0, 11.0), theme::MUTED);
|
||||
if self.open {
|
||||
ui.label(20.0, 61.0, "ENDORPHIN", theme::TEXT, false);
|
||||
ui.button(
|
||||
Rect::new(12.0, 95.0, WIDTH - 24.0, 34.0),
|
||||
Action::NewPage,
|
||||
Button::new("+ 新建页面"),
|
||||
);
|
||||
for (index, (name, label)) in [
|
||||
("Console", "Console"),
|
||||
("Sources", "Sources"),
|
||||
("Network", "Network"),
|
||||
("Settings", "Settings"),
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let selected = tabs
|
||||
.iter()
|
||||
.any(|tab| tab.active && tab.developer_tool.as_deref() == Some(name));
|
||||
ui.button(
|
||||
Rect::new(12.0, 147.0 + index as f32 * 34.0, WIDTH - 24.0, 30.0),
|
||||
Action::Tool(name),
|
||||
Button::new(label).selected(selected),
|
||||
);
|
||||
}
|
||||
ui.label(20.0, 303.0, "页面", theme::MUTED, false);
|
||||
let area = Rect::new(8.0, 333.0, WIDTH - 16.0, (height - 383.0).max(0.0));
|
||||
let pages: Vec<_> = tabs
|
||||
.iter()
|
||||
.filter(|tab| tab.developer_tool.is_none())
|
||||
.collect();
|
||||
self.scroll = self
|
||||
.scroll
|
||||
.clamp(0.0, (pages.len() as f32 * 36.0 - area.height).max(0.0));
|
||||
ui.list.push(DrawCommand::PushClip(area));
|
||||
for (index, tab) in pages.into_iter().enumerate() {
|
||||
let y = area.y + index as f32 * 36.0 - self.scroll;
|
||||
// Register only fully visible rows: paint clipping must match input.
|
||||
if y < area.y || y + 32.0 > area.y + area.height {
|
||||
continue;
|
||||
}
|
||||
ui.button(
|
||||
Rect::new(12.0, y, WIDTH - 56.0, 32.0),
|
||||
Action::Activate(tab.id),
|
||||
Button::new(&tab.title).selected(tab.active),
|
||||
);
|
||||
ui.button(
|
||||
Rect::new(WIDTH - 40.0, y, 28.0, 32.0),
|
||||
Action::Close(tab.id),
|
||||
Button::new("×"),
|
||||
);
|
||||
}
|
||||
ui.list.push(DrawCommand::PopClip);
|
||||
ui.label(20.0, height - 32.0, "本地工作区", theme::MUTED, false);
|
||||
}
|
||||
self.targets = ui.targets;
|
||||
self.interaction.reconcile(&self.targets);
|
||||
ui.list
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn collapsing_removes_every_host_target_except_the_toggle() {
|
||||
let mut sidebar = State::default();
|
||||
sidebar.draw(&[], 800.0, theme::PALETTE[0]);
|
||||
assert!(sidebar.targets.len() > 1);
|
||||
sidebar.open = false;
|
||||
sidebar.draw(&[], 800.0, theme::PALETTE[0]);
|
||||
assert_eq!(sidebar.targets.len(), 1);
|
||||
assert_eq!(sidebar.targets[0].action, Some(Action::Toggle));
|
||||
assert_eq!(sidebar.left(), 0.0);
|
||||
assert!(!sidebar.covers(Point::new(400.0, 10.0)));
|
||||
assert!(!sidebar.covers(Point::new(10.0, 80.0)));
|
||||
}
|
||||
}
|
||||
+2
-669
@@ -1,30 +1,7 @@
|
||||
//! Immutable tab chrome. IDs are session-local projections, never heap handles.
|
||||
use crate::{
|
||||
display_list::{Color, DisplayList, DrawCommand},
|
||||
geometry::{Point, Rect},
|
||||
text::TextStyle,
|
||||
};
|
||||
//! Immutable page registry projections. Host navigation is rendered in sidebar.
|
||||
//! IDs are session-local data and never own managed VM objects.
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
mod hover;
|
||||
mod motion;
|
||||
pub use motion::{Motion, Visual};
|
||||
|
||||
// Native traffic lights and all tab controls share this row's center line.
|
||||
pub const HEIGHT: f32 = 40.0;
|
||||
pub const INSET: f32 = 8.0;
|
||||
const NEW_TAB_SPACE: f32 = 44.0;
|
||||
const MIN_WIDTH: f32 = 88.0;
|
||||
pub const RAIL: f32 = 56.0;
|
||||
pub const CONTROLS: f32 = if cfg!(target_os = "macos") { 86.0 } else { 0.0 };
|
||||
pub const LEADING: f32 = CONTROLS + 12.0;
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Chrome {
|
||||
pub offset: Option<f32>,
|
||||
pub insertion: Option<usize>,
|
||||
pub pointer: Option<Point>,
|
||||
pub visual: Visual,
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Tab {
|
||||
pub id: u64,
|
||||
@@ -33,647 +10,3 @@ pub struct Tab {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub developer_tool: Option<String>,
|
||||
}
|
||||
|
||||
pub fn height(tabs: &[Tab]) -> f32 {
|
||||
let _ = tabs;
|
||||
HEIGHT
|
||||
}
|
||||
|
||||
/// Scroll offsets are logical pixels, never a discrete page index.
|
||||
fn viewport(width: f32) -> Rect {
|
||||
Rect::new(
|
||||
LEADING,
|
||||
0.0,
|
||||
(width - NEW_TAB_SPACE - LEADING).max(0.0),
|
||||
HEIGHT,
|
||||
)
|
||||
}
|
||||
fn tab_width(tabs: &[Tab], width: f32) -> f32 {
|
||||
(viewport(width).width / tabs.len().max(1) as f32)
|
||||
.clamp(MIN_WIDTH, 208.0)
|
||||
.min(viewport(width).width)
|
||||
}
|
||||
fn scroll_offset(tabs: &[Tab], width: f32, start: Option<f32>) -> f32 {
|
||||
let tab_width = tab_width(tabs, width);
|
||||
let available = viewport(width).width;
|
||||
let maximum = (tabs.len() as f32 * tab_width - available).max(0.0);
|
||||
let active = tabs.iter().position(|tab| tab.active).unwrap_or(0);
|
||||
start
|
||||
.unwrap_or(((active + 1) as f32 * tab_width - available).max(0.0))
|
||||
.clamp(0.0, maximum)
|
||||
}
|
||||
#[cfg(test)]
|
||||
fn visible_range(tabs: &[Tab], width: f32, start: Option<f32>) -> std::ops::Range<usize> {
|
||||
let tab_width = tab_width(tabs, width);
|
||||
if tab_width <= 0.0 {
|
||||
return 0..0;
|
||||
}
|
||||
let offset = scroll_offset(tabs, width, start);
|
||||
(offset / tab_width).floor() as usize
|
||||
..(((offset + viewport(width).width) / tab_width).ceil() as usize).min(tabs.len())
|
||||
}
|
||||
|
||||
/// Invisible drag edge zones only; ordinary clicks never scroll or activate another tab.
|
||||
pub fn edge_direction(tabs: &[Tab], width: f32, start: Option<f32>, point: Point) -> i8 {
|
||||
if !viewport(width).contains(point) || point.y >= HEIGHT {
|
||||
return 0;
|
||||
}
|
||||
let offset = scroll_offset(tabs, width, start);
|
||||
let maximum = (tabs.len() as f32 * tab_width(tabs, width) - viewport(width).width).max(0.0);
|
||||
if point.x < LEADING + 24.0 && offset > 0.0 {
|
||||
-1
|
||||
} else if point.x > width - NEW_TAB_SPACE - 24.0 && offset < maximum {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
pub fn wheel_offset(tabs: &[Tab], width: f32, start: Option<f32>, delta: f32) -> f32 {
|
||||
scroll_offset(tabs, width, Some(scroll_offset(tabs, width, start) + delta))
|
||||
}
|
||||
pub fn scroll_start(tabs: &[Tab], width: f32, start: Option<f32>, direction: i8) -> Option<f32> {
|
||||
let current = scroll_offset(tabs, width, start);
|
||||
let next = wheel_offset(tabs, width, Some(current), direction as f32 * MIN_WIDTH);
|
||||
(next != current).then_some(next)
|
||||
}
|
||||
struct Item<'a> {
|
||||
tab: &'a Tab,
|
||||
rect: Rect,
|
||||
}
|
||||
/// Retain all IDs, including clipped tabs, for continuous movement through the viewport.
|
||||
fn tab_rects(start: Option<f32>, tabs: &[Tab], width: f32) -> Vec<(u64, Rect)> {
|
||||
let tab_width = tab_width(tabs, width);
|
||||
let offset = scroll_offset(tabs, width, start);
|
||||
tabs.iter()
|
||||
.enumerate()
|
||||
.map(|(index, tab)| {
|
||||
(
|
||||
tab.id,
|
||||
Rect::new(
|
||||
LEADING + index as f32 * tab_width - offset,
|
||||
0.0,
|
||||
tab_width,
|
||||
HEIGHT,
|
||||
),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
fn items(start: Option<f32>, tabs: &[Tab], width: f32) -> Vec<Item<'_>> {
|
||||
let viewport = viewport(width);
|
||||
tabs.iter()
|
||||
.zip(tab_rects(start, tabs, width))
|
||||
.filter(|(_, (_, rect))| {
|
||||
rect.x + rect.width > viewport.x && rect.x < viewport.x + viewport.width
|
||||
})
|
||||
.map(|(tab, (_, rect))| Item { tab, rect })
|
||||
.collect()
|
||||
}
|
||||
fn pill_rect(rect: Rect) -> Rect {
|
||||
Rect::new(rect.x + 2.0, 4.0, (rect.width - 4.0).max(0.0), HEIGHT - 8.0)
|
||||
}
|
||||
fn close_rect(rect: Rect) -> Rect {
|
||||
Rect::new(
|
||||
rect.x + rect.width - 25.0,
|
||||
(HEIGHT - 18.0) / 2.0,
|
||||
18.0,
|
||||
18.0,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn hit(start: Option<f32>, tabs: &[Tab], width: f32, point: Point) -> Option<(u64, bool)> {
|
||||
if point.x < LEADING
|
||||
|| point.x >= width - NEW_TAB_SPACE
|
||||
|| point.y < 0.0
|
||||
|| point.y >= height(tabs)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
items(start, tabs, width)
|
||||
.into_iter()
|
||||
.find(|item| pill_rect(item.rect).contains(point))
|
||||
.map(|item| (item.tab.id, close_rect(item.rect).contains(point)))
|
||||
}
|
||||
|
||||
/// Gap index in the complete published order, including overflow-hidden tabs.
|
||||
pub fn insertion_index(
|
||||
start: Option<f32>,
|
||||
tabs: &[Tab],
|
||||
width: f32,
|
||||
point: Point,
|
||||
) -> Option<usize> {
|
||||
if point.x < LEADING || point.x >= width - NEW_TAB_SPACE || point.y < 0.0 || point.y >= HEIGHT {
|
||||
return None;
|
||||
}
|
||||
if tabs.is_empty() {
|
||||
return Some(0);
|
||||
}
|
||||
let visible: Vec<_> = items(start, tabs, width).into_iter().collect();
|
||||
for item in &visible {
|
||||
if point.x < item.rect.x + item.rect.width / 2.0 {
|
||||
return tabs.iter().position(|tab| tab.id == item.tab.id);
|
||||
}
|
||||
}
|
||||
visible
|
||||
.last()
|
||||
.and_then(|last| tabs.iter().position(|tab| tab.id == last.tab.id))
|
||||
.map(|index| index + 1)
|
||||
}
|
||||
|
||||
pub fn drag_anchor(
|
||||
start: Option<f32>,
|
||||
tabs: &[Tab],
|
||||
width: f32,
|
||||
id: u64,
|
||||
point: Point,
|
||||
) -> Option<Point> {
|
||||
if !viewport(width).contains(point) {
|
||||
return None;
|
||||
}
|
||||
let item = items(start, tabs, width)
|
||||
.into_iter()
|
||||
.find(|item| item.tab.id == id && pill_rect(item.rect).contains(point))?;
|
||||
(!close_rect(item.rect).contains(point)).then(|| Point::new(point.x - item.rect.x, point.y))
|
||||
}
|
||||
|
||||
pub fn draw_insertion(
|
||||
start: Option<f32>,
|
||||
list: &mut DisplayList,
|
||||
tabs: &[Tab],
|
||||
width: f32,
|
||||
index: usize,
|
||||
scale: f32,
|
||||
) {
|
||||
let visible: Vec<_> = items(start, tabs, width).into_iter().collect();
|
||||
let x = if tabs.is_empty() {
|
||||
Some(LEADING + 8.0)
|
||||
} else {
|
||||
visible
|
||||
.iter()
|
||||
.find(|item| tabs.get(index).is_some_and(|tab| tab.id == item.tab.id))
|
||||
.map(|item| item.rect.x)
|
||||
.or_else(|| {
|
||||
visible
|
||||
.last()
|
||||
.filter(|item| {
|
||||
tabs.iter().position(|tab| tab.id == item.tab.id) == index.checked_sub(1)
|
||||
})
|
||||
.map(|item| item.rect.x + item.rect.width)
|
||||
})
|
||||
};
|
||||
if let Some(x) = x {
|
||||
list.push(DrawCommand::FillRoundedRect {
|
||||
rect: Rect::new(
|
||||
x.clamp(1.5, (width - 1.5).max(1.5)) * scale - 1.5 * scale,
|
||||
3.0 * scale,
|
||||
3.0 * scale,
|
||||
(HEIGHT - 6.0) * scale,
|
||||
),
|
||||
radius: scale,
|
||||
color: Color::rgb(56, 95, 220),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn plus_rect(start: Option<f32>, tabs: &[Tab], width: f32) -> Rect {
|
||||
if tabs.len() as f32 * tab_width(tabs, width) > viewport(width).width {
|
||||
return Rect::new(width - 34.0, (HEIGHT - 28.0) / 2.0, 24.0, 28.0);
|
||||
}
|
||||
let end = items(start, tabs, width)
|
||||
.iter()
|
||||
.map(|item| item.rect.x + item.rect.width)
|
||||
.fold(LEADING, f32::max);
|
||||
Rect::new(
|
||||
(end + 4.0).min(width - 34.0),
|
||||
(HEIGHT - 28.0) / 2.0,
|
||||
24.0,
|
||||
28.0,
|
||||
)
|
||||
}
|
||||
|
||||
fn tab_style() -> TextStyle {
|
||||
TextStyle {
|
||||
font_size: 13.0,
|
||||
line_height: 18.0,
|
||||
..TextStyle::ui(1)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(
|
||||
_start: Option<f32>,
|
||||
list: &mut DisplayList,
|
||||
tabs: &[Tab],
|
||||
width: f32,
|
||||
scale: f32,
|
||||
_pointer: Option<Point>,
|
||||
visual: &Visual,
|
||||
) {
|
||||
let scaled = |rect: Rect| {
|
||||
Rect::new(
|
||||
rect.x * scale,
|
||||
rect.y * scale,
|
||||
rect.width * scale,
|
||||
rect.height * scale,
|
||||
)
|
||||
};
|
||||
list.push(DrawCommand::FillRoundedRect {
|
||||
rect: scaled(Rect::new(0.0, 0.0, width, HEIGHT)),
|
||||
radius: 0.0,
|
||||
color: crate::system_ui::theme::BAR,
|
||||
});
|
||||
let plus = visual.plus;
|
||||
if visual.plus_hover > 0.0 {
|
||||
list.push(DrawCommand::FillRoundedRect {
|
||||
rect: scaled(plus),
|
||||
radius: 6.0 * scale,
|
||||
color: crate::system_ui::theme::mix(
|
||||
crate::system_ui::theme::BAR,
|
||||
crate::system_ui::theme::TEXT,
|
||||
0.06 * visual.plus_hover,
|
||||
),
|
||||
});
|
||||
}
|
||||
list.push(DrawCommand::Text {
|
||||
origin: Point::new(
|
||||
(plus.x + (plus.width - crate::text::measure("+", tab_style()).width) / 2.0) * scale,
|
||||
((HEIGHT - tab_style().line_height) / 2.0) * scale,
|
||||
),
|
||||
value: "+".into(),
|
||||
style: tab_style().scaled(scale),
|
||||
color: crate::system_ui::theme::MUTED,
|
||||
});
|
||||
let mut paint_items: Vec<_> = tabs
|
||||
.iter()
|
||||
.map(|tab| Item {
|
||||
tab,
|
||||
rect: visual.rect(tab.id, Rect::default()),
|
||||
})
|
||||
.collect();
|
||||
paint_items.extend(
|
||||
visual
|
||||
.exiting
|
||||
.iter()
|
||||
.map(|(tab, rect)| Item { tab, rect: *rect }),
|
||||
);
|
||||
for item in paint_items {
|
||||
{
|
||||
if item.rect.width <= 0.0
|
||||
|| item.rect.x + item.rect.width <= visual.viewport.x
|
||||
|| item.rect.x >= visual.viewport.x + visual.viewport.width
|
||||
{
|
||||
continue;
|
||||
}
|
||||
list.push(DrawCommand::PushClip(scaled(visual.viewport)));
|
||||
list.push(DrawCommand::PushClip(scaled(pill_rect(item.rect))));
|
||||
}
|
||||
let pill = scaled(pill_rect(item.rect));
|
||||
let active = item.tab.active;
|
||||
let hover = visual.hover(item.tab.id);
|
||||
let background = if active {
|
||||
crate::system_ui::theme::BG
|
||||
} else {
|
||||
crate::system_ui::theme::mix(
|
||||
crate::system_ui::theme::BAR,
|
||||
crate::system_ui::theme::TEXT,
|
||||
0.06 * hover,
|
||||
)
|
||||
};
|
||||
if active || hover > 0.0 {
|
||||
list.push(DrawCommand::FillRoundedRect {
|
||||
rect: pill,
|
||||
radius: 6.0 * scale,
|
||||
color: background,
|
||||
});
|
||||
}
|
||||
{
|
||||
// Match the close button with an 18px slot and 7px edge inset.
|
||||
let icon = "◇";
|
||||
list.push(DrawCommand::Text {
|
||||
origin: Point::new(
|
||||
(item.rect.x
|
||||
+ 7.0
|
||||
+ (18.0 - crate::text::measure(icon, tab_style()).width) / 2.0)
|
||||
* scale,
|
||||
((HEIGHT - tab_style().line_height) / 2.0) * scale,
|
||||
),
|
||||
value: icon.into(),
|
||||
style: tab_style().scaled(scale),
|
||||
color: crate::system_ui::theme::MUTED,
|
||||
});
|
||||
}
|
||||
let title = &item.tab.title;
|
||||
let text_rect = Rect::new(
|
||||
item.rect.x + 30.0,
|
||||
(HEIGHT - tab_style().line_height) / 2.0,
|
||||
(item.rect.width - 58.0).max(0.0),
|
||||
tab_style().line_height,
|
||||
);
|
||||
list.push(DrawCommand::PushClip(scaled(text_rect)));
|
||||
list.push(DrawCommand::Text {
|
||||
origin: Point::new(
|
||||
(item.rect.x + 30.0) * scale,
|
||||
((HEIGHT - tab_style().line_height) / 2.0) * scale,
|
||||
),
|
||||
value: title.into(),
|
||||
style: tab_style().scaled(scale),
|
||||
color: if active {
|
||||
crate::system_ui::theme::TEXT
|
||||
} else {
|
||||
crate::system_ui::theme::MUTED
|
||||
},
|
||||
});
|
||||
list.push(DrawCommand::PopClip);
|
||||
if item.rect.width >= 44.0 {
|
||||
let close_hover = visual.close_hover(item.tab.id);
|
||||
if close_hover > 0.0 {
|
||||
list.push(DrawCommand::FillRoundedRect {
|
||||
rect: scaled(close_rect(item.rect)),
|
||||
radius: 5.0 * scale,
|
||||
color: crate::system_ui::theme::mix(
|
||||
background,
|
||||
crate::system_ui::theme::mix(
|
||||
crate::system_ui::theme::BAR,
|
||||
crate::system_ui::theme::TEXT,
|
||||
0.12,
|
||||
),
|
||||
close_hover,
|
||||
),
|
||||
});
|
||||
}
|
||||
list.push(DrawCommand::Text {
|
||||
origin: Point::new(
|
||||
(close_rect(item.rect).x
|
||||
+ (close_rect(item.rect).width
|
||||
- crate::text::measure("×", tab_style()).width)
|
||||
/ 2.0)
|
||||
* scale,
|
||||
((HEIGHT - tab_style().line_height) / 2.0) * scale,
|
||||
),
|
||||
value: "×".into(),
|
||||
style: tab_style().scaled(scale),
|
||||
color: crate::system_ui::theme::MUTED,
|
||||
});
|
||||
}
|
||||
{
|
||||
list.push(DrawCommand::PopClip);
|
||||
list.push(DrawCommand::PopClip);
|
||||
}
|
||||
}
|
||||
// Quiet boundaries only between two idle tabs; the active/hovered pill owns its edges.
|
||||
list.push(DrawCommand::PushClip(scaled(visual.viewport)));
|
||||
for pair in tabs.windows(2) {
|
||||
let left = visual.rect(pair[0].id, Rect::default());
|
||||
let hover = visual.hover(pair[0].id).max(visual.hover(pair[1].id));
|
||||
if !pair[0].active && !pair[1].active && hover < 1.0 {
|
||||
list.push(DrawCommand::FillRoundedRect {
|
||||
rect: scaled(Rect::new(
|
||||
left.x + left.width - 0.5,
|
||||
(HEIGHT - 12.0) / 2.0,
|
||||
1.0,
|
||||
12.0,
|
||||
)),
|
||||
radius: 0.5 * scale,
|
||||
color: crate::system_ui::theme::mix(
|
||||
crate::system_ui::theme::BAR,
|
||||
crate::system_ui::theme::TEXT,
|
||||
0.18 * (1.0 - hover),
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
list.push(DrawCommand::PopClip);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn insertion_gaps_share_visible_tab_geometry_and_exclude_close_drags() {
|
||||
let tabs: Vec<_> = (1..=4)
|
||||
.map(|id| Tab {
|
||||
id,
|
||||
title: id.to_string(),
|
||||
developer_tool: None,
|
||||
active: id == 1,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(
|
||||
insertion_index(
|
||||
None,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 1000.0,
|
||||
Point::new(LEADING + 10.0, 18.0)
|
||||
),
|
||||
Some(0)
|
||||
);
|
||||
assert_eq!(
|
||||
insertion_index(
|
||||
None,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 1000.0,
|
||||
Point::new(LEADING + 215.0, 18.0)
|
||||
),
|
||||
Some(1)
|
||||
);
|
||||
assert_eq!(
|
||||
insertion_index(
|
||||
None,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 1000.0,
|
||||
Point::new(LEADING + 990.0, 18.0)
|
||||
),
|
||||
Some(4)
|
||||
);
|
||||
assert!(
|
||||
drag_anchor(
|
||||
None,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 1000.0,
|
||||
1,
|
||||
Point::new(LEADING + 190.0, 18.0)
|
||||
)
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
drag_anchor(
|
||||
None,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 1000.0,
|
||||
1,
|
||||
Point::new(LEADING + 25.0, 18.0)
|
||||
)
|
||||
.is_some()
|
||||
);
|
||||
assert_eq!(
|
||||
insertion_index(
|
||||
None,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 1000.0,
|
||||
Point::new(LEADING + 20.0, HEIGHT)
|
||||
),
|
||||
None
|
||||
);
|
||||
assert!(
|
||||
drag_anchor(
|
||||
None,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 400.0,
|
||||
3,
|
||||
Point::new(LEADING + 390.0, 18.0)
|
||||
)
|
||||
.is_none()
|
||||
);
|
||||
assert_eq!(
|
||||
insertion_index(
|
||||
None,
|
||||
&[],
|
||||
LEADING + NEW_TAB_SPACE + 720.0,
|
||||
Point::new(LEADING + 20.0, 18.0)
|
||||
),
|
||||
Some(0)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflow_keeps_active_page_visible_without_navigation_buttons() {
|
||||
let tabs: Vec<_> = (1..=64)
|
||||
.map(|id| Tab {
|
||||
id,
|
||||
title: format!("Page {id}"),
|
||||
developer_tool: None,
|
||||
active: id == 32,
|
||||
})
|
||||
.collect();
|
||||
let visible = items(None, &tabs, LEADING + NEW_TAB_SPACE + 720.0);
|
||||
assert!(visible.iter().any(|item| item.tab.active));
|
||||
for item in &visible {
|
||||
let point = Point::new((item.rect.x + 12.0).max(LEADING + 3.0), 18.0);
|
||||
assert_eq!(
|
||||
hit(None, &tabs, LEADING + NEW_TAB_SPACE + 720.0, point),
|
||||
Some((item.tab.id, close_rect(item.rect).contains(point)))
|
||||
);
|
||||
}
|
||||
let tab = visible.iter().find(|item| item.tab.active).unwrap();
|
||||
assert_eq!(
|
||||
hit(
|
||||
None,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 720.0,
|
||||
Point::new(tab.rect.x + tab.rect.width - 15.0, 18.0)
|
||||
),
|
||||
Some((32, true))
|
||||
);
|
||||
assert_eq!(
|
||||
hit(
|
||||
None,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 720.0,
|
||||
Point::new(LEADING + 50.0, HEIGHT)
|
||||
),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drag_viewport_reaches_hidden_ends_without_changing_active_page() {
|
||||
let tabs: Vec<_> = (1..=64)
|
||||
.map(|id| Tab {
|
||||
id,
|
||||
title: id.to_string(),
|
||||
developer_tool: None,
|
||||
active: id == 32,
|
||||
})
|
||||
.collect();
|
||||
let width = LEADING + NEW_TAB_SPACE + 720.0;
|
||||
let mut start = None;
|
||||
while let Some(next) = scroll_start(&tabs, width, start, 1) {
|
||||
start = Some(next);
|
||||
}
|
||||
assert_eq!(visible_range(&tabs, width, start), 55..64);
|
||||
assert_eq!(
|
||||
edge_direction(&tabs, width, start, Point::new(LEADING + 710.0, 18.0)),
|
||||
0
|
||||
);
|
||||
assert_eq!(
|
||||
insertion_index(start, &tabs, width, Point::new(LEADING + 710.0, 18.0)),
|
||||
Some(64)
|
||||
);
|
||||
while let Some(next) = scroll_start(&tabs, width, start, -1) {
|
||||
start = Some(next);
|
||||
}
|
||||
assert_eq!(visible_range(&tabs, width, start), 0..9);
|
||||
assert_eq!(
|
||||
edge_direction(&tabs, width, start, Point::new(LEADING + 10.0, 18.0)),
|
||||
0
|
||||
);
|
||||
assert_eq!(
|
||||
insertion_index(start, &tabs, width, Point::new(LEADING + 10.0, 18.0)),
|
||||
Some(0)
|
||||
);
|
||||
assert_eq!(
|
||||
tabs.iter()
|
||||
.filter(|tab| tab.active)
|
||||
.map(|tab| tab.id)
|
||||
.collect::<Vec<_>>(),
|
||||
vec![32]
|
||||
);
|
||||
assert!(items(None, &tabs, width).iter().any(|item| item.tab.active));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scrolled_hit_and_gap_geometry_survive_resize_and_list_shrink() {
|
||||
let tabs: Vec<_> = (1..=16)
|
||||
.map(|id| Tab {
|
||||
id,
|
||||
title: id.to_string(),
|
||||
developer_tool: None,
|
||||
active: id == 1,
|
||||
})
|
||||
.collect();
|
||||
let start = Some(4.0 * MIN_WIDTH);
|
||||
assert_eq!(
|
||||
hit(
|
||||
start,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 720.0,
|
||||
Point::new(LEADING + 40.0, 18.0)
|
||||
),
|
||||
Some((5, false))
|
||||
);
|
||||
assert_eq!(
|
||||
insertion_index(
|
||||
start,
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 720.0,
|
||||
Point::new(LEADING + 40.0, 18.0)
|
||||
),
|
||||
Some(4)
|
||||
);
|
||||
assert_eq!(
|
||||
visible_range(&tabs[..3], LEADING + NEW_TAB_SPACE + 720.0, start),
|
||||
0..3
|
||||
);
|
||||
assert_eq!(
|
||||
visible_range(&tabs, LEADING + NEW_TAB_SPACE + 1920.0, start),
|
||||
0..16
|
||||
);
|
||||
assert_eq!(
|
||||
edge_direction(
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 720.0,
|
||||
start,
|
||||
Point::new(LEADING + 710.0, HEIGHT)
|
||||
),
|
||||
0
|
||||
);
|
||||
assert_eq!(
|
||||
edge_direction(
|
||||
&tabs,
|
||||
LEADING + NEW_TAB_SPACE + 720.0,
|
||||
start,
|
||||
Point::new(LEADING + 725.0, 18.0)
|
||||
),
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
//! Hover color transitions are independent of tab layout and selected state.
|
||||
use super::{Point, Tab, Visual};
|
||||
use std::{collections::BTreeMap, time::Instant};
|
||||
|
||||
#[derive(Default)]
|
||||
struct Fade {
|
||||
from: f32,
|
||||
target: f32,
|
||||
started: Option<Instant>,
|
||||
}
|
||||
|
||||
impl Fade {
|
||||
fn sample(&mut self, hovered: bool, now: Instant) -> (f32, bool) {
|
||||
let t = self.started.map_or(1.0, |start| {
|
||||
(now.duration_since(start).as_secs_f32() / 0.16).min(1.0)
|
||||
});
|
||||
let value = self.from + (self.target - self.from) * (1.0 - (1.0 - t).powi(3));
|
||||
let target = if hovered { 1.0 } else { 0.0 };
|
||||
if target != self.target {
|
||||
// Reversing mid-fade starts at the currently visible color.
|
||||
self.from = value;
|
||||
self.target = target;
|
||||
self.started = Some(now);
|
||||
(value, true)
|
||||
} else {
|
||||
(value, t < 1.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Hover {
|
||||
tabs: BTreeMap<u64, (Fade, Fade)>,
|
||||
plus: Fade,
|
||||
}
|
||||
|
||||
impl Hover {
|
||||
pub fn sample(
|
||||
&mut self,
|
||||
visual: &mut Visual,
|
||||
tabs: &[Tab],
|
||||
pointer: Option<Point>,
|
||||
now: Instant,
|
||||
) -> bool {
|
||||
let hit = pointer.and_then(|point| visual.hit(point));
|
||||
let (plus, mut animating) = self.plus.sample(
|
||||
pointer.is_some_and(|point| visual.plus.contains(point)),
|
||||
now,
|
||||
);
|
||||
visual.plus_hover = plus;
|
||||
self.tabs
|
||||
.retain(|id, _| tabs.iter().any(|tab| tab.id == *id));
|
||||
visual.hovers.clear();
|
||||
for tab in tabs {
|
||||
let (body, close) = self.tabs.entry(tab.id).or_default();
|
||||
let (body, body_animating) = body.sample(hit.is_some_and(|hit| hit.0 == tab.id), now);
|
||||
let (close, close_animating) = close.sample(hit == Some((tab.id, true)), now);
|
||||
visual.hovers.push((tab.id, body, close));
|
||||
animating |= body_animating || close_animating;
|
||||
}
|
||||
animating
|
||||
}
|
||||
}
|
||||
@@ -1,207 +0,0 @@
|
||||
//! Interpolated chrome geometry is shared by painting and pointer hit testing.
|
||||
use super::*;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Visual {
|
||||
pub rects: Vec<(u64, Rect)>,
|
||||
pub exiting: Vec<(Tab, Rect)>,
|
||||
pub viewport: Rect,
|
||||
pub plus: Rect,
|
||||
pub hovers: Vec<(u64, f32, f32)>,
|
||||
pub plus_hover: f32,
|
||||
}
|
||||
impl Visual {
|
||||
pub fn hover(&self, id: u64) -> f32 {
|
||||
self.hovers
|
||||
.iter()
|
||||
.find(|entry| entry.0 == id)
|
||||
.map_or(0.0, |entry| entry.1)
|
||||
}
|
||||
pub fn close_hover(&self, id: u64) -> f32 {
|
||||
self.hovers
|
||||
.iter()
|
||||
.find(|entry| entry.0 == id)
|
||||
.map_or(0.0, |entry| entry.2)
|
||||
}
|
||||
pub fn rect(&self, id: u64, fallback: Rect) -> Rect {
|
||||
self.rects
|
||||
.iter()
|
||||
.find(|(key, _)| *key == id)
|
||||
.map_or(fallback, |(_, rect)| *rect)
|
||||
}
|
||||
pub fn hit(&self, point: Point) -> Option<(u64, bool)> {
|
||||
if !self.viewport.contains(point) {
|
||||
return None;
|
||||
}
|
||||
self.rects
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|(_, rect)| pill_rect(*rect).contains(point))
|
||||
.map(|(id, rect)| (*id, close_rect(*rect).contains(point)))
|
||||
}
|
||||
}
|
||||
#[derive(Default)]
|
||||
pub struct Motion {
|
||||
hover: super::hover::Hover,
|
||||
target: Visual,
|
||||
from: Visual,
|
||||
current: Visual,
|
||||
published: Vec<Tab>,
|
||||
initialized: bool,
|
||||
direct_scroll: bool,
|
||||
started: Option<Instant>,
|
||||
pub offset: Option<f32>,
|
||||
pub next_frame: Option<Instant>,
|
||||
}
|
||||
fn blend(a: Rect, b: Rect, t: f32) -> Rect {
|
||||
Rect::new(
|
||||
a.x + (b.x - a.x) * t,
|
||||
a.y + (b.y - a.y) * t,
|
||||
a.width + (b.width - a.width) * t,
|
||||
a.height + (b.height - a.height) * t,
|
||||
)
|
||||
}
|
||||
impl Motion {
|
||||
pub fn sample_hover(&mut self, visual: &mut Visual, tabs: &[Tab], pointer: Option<Point>) {
|
||||
let now = Instant::now();
|
||||
if self.hover.sample(visual, tabs, pointer, now) {
|
||||
let next = now + Duration::from_millis(16);
|
||||
self.next_frame = Some(self.next_frame.map_or(next, |old| old.min(next)));
|
||||
}
|
||||
}
|
||||
pub fn scroll_to(&mut self, offset: f32) {
|
||||
self.offset = Some(offset);
|
||||
self.direct_scroll = true;
|
||||
}
|
||||
|
||||
fn advance(&mut self, now: Instant) {
|
||||
let t = self.started.map_or(1.0, |start| {
|
||||
(now.duration_since(start).as_secs_f32() / 0.22).min(1.0)
|
||||
});
|
||||
let eased = 1.0 - (1.0 - t).powi(3);
|
||||
self.current = Visual {
|
||||
rects: self
|
||||
.target
|
||||
.rects
|
||||
.iter()
|
||||
.map(|(id, target)| (*id, blend(self.from.rect(*id, *target), *target, eased)))
|
||||
.collect(),
|
||||
exiting: self
|
||||
.target
|
||||
.exiting
|
||||
.iter()
|
||||
.filter(|_| t < 1.0)
|
||||
.map(|(tab, target)| {
|
||||
let from = self
|
||||
.from
|
||||
.exiting
|
||||
.iter()
|
||||
.find(|(old, _)| old.id == tab.id)
|
||||
.map_or(*target, |(_, rect)| *rect);
|
||||
(tab.clone(), blend(from, *target, eased))
|
||||
})
|
||||
.collect(),
|
||||
viewport: blend(self.from.viewport, self.target.viewport, eased),
|
||||
plus: blend(self.from.plus, self.target.plus, eased),
|
||||
..Visual::default()
|
||||
};
|
||||
self.next_frame = (t < 1.0).then(|| now + Duration::from_millis(16));
|
||||
}
|
||||
|
||||
pub fn sample(&mut self, tabs: &[Tab], offset: Option<f32>, width: f32) -> Visual {
|
||||
let now = Instant::now();
|
||||
// Retarget from the geometry at this instant, even during rapid repeated clicks.
|
||||
self.advance(now);
|
||||
let mut offset = scroll_offset(tabs, width, offset.or(self.offset));
|
||||
let selection_changed = self
|
||||
.published
|
||||
.iter()
|
||||
.find(|tab| tab.active)
|
||||
.map(|tab| tab.id)
|
||||
!= tabs.iter().find(|tab| tab.active).map(|tab| tab.id);
|
||||
if selection_changed {
|
||||
let active = tabs.iter().position(|tab| tab.active).unwrap_or(0) as f32;
|
||||
let tab_width = tab_width(tabs, width);
|
||||
let left = active * tab_width;
|
||||
let right = left + tab_width;
|
||||
offset = offset.min(left).max(right - viewport(width).width).max(0.0);
|
||||
}
|
||||
self.offset = Some(offset);
|
||||
let mut target = Visual {
|
||||
rects: tab_rects(Some(offset), tabs, width),
|
||||
viewport: viewport(width),
|
||||
plus: plus_rect(Some(offset), tabs, width),
|
||||
..Visual::default()
|
||||
};
|
||||
if !self.initialized || self.direct_scroll {
|
||||
self.direct_scroll = false;
|
||||
self.started = None;
|
||||
self.next_frame = None;
|
||||
// Startup is already laid out; it is not an insertion of every restored tab.
|
||||
self.initialized = true;
|
||||
self.current = target.clone();
|
||||
self.from = target.clone();
|
||||
self.target = target;
|
||||
self.published = tabs.to_vec();
|
||||
return self.current.clone();
|
||||
}
|
||||
if self.target.rects != target.rects
|
||||
|| self.target.plus != target.plus
|
||||
|| self.target.viewport != target.viewport
|
||||
{
|
||||
self.from = self.current.clone();
|
||||
for (index, (id, rect)) in target.rects.iter().enumerate() {
|
||||
if !self.from.rects.iter().any(|(old, _)| old == id) {
|
||||
// Grow out of the previous trailing edge, not the final compressed x.
|
||||
let x = target.rects[..index]
|
||||
.iter()
|
||||
.rev()
|
||||
.find_map(|(previous, _)| {
|
||||
self.from
|
||||
.rects
|
||||
.iter()
|
||||
.find(|(old, _)| old == previous)
|
||||
.map(|(_, previous)| previous.x + previous.width)
|
||||
})
|
||||
.unwrap_or(rect.x);
|
||||
self.from
|
||||
.rects
|
||||
.push((*id, Rect::new(x, rect.y, 0.0, rect.height)));
|
||||
}
|
||||
}
|
||||
for tab in &self.published {
|
||||
if !tabs.iter().any(|live| live.id == tab.id) {
|
||||
self.from
|
||||
.exiting
|
||||
.push((tab.clone(), self.current.rect(tab.id, Rect::default())));
|
||||
}
|
||||
}
|
||||
// A closed tab shrinks with the gap; it never remains an interactive target.
|
||||
target.exiting = self
|
||||
.from
|
||||
.exiting
|
||||
.iter()
|
||||
.filter(|(tab, _)| !tabs.iter().any(|live| live.id == tab.id))
|
||||
.map(|(tab, rect)| {
|
||||
let next = self
|
||||
.published
|
||||
.iter()
|
||||
.position(|old| old.id == tab.id)
|
||||
.and_then(|index| {
|
||||
self.published[index + 1..]
|
||||
.iter()
|
||||
.find(|old| tabs.iter().any(|live| live.id == old.id))
|
||||
});
|
||||
let x = next.map_or(target.plus.x - 4.0, |next| target.rect(next.id, *rect).x);
|
||||
(tab.clone(), Rect::new(x, rect.y, 0.0, rect.height))
|
||||
})
|
||||
.collect();
|
||||
self.target = target;
|
||||
self.started = Some(now);
|
||||
}
|
||||
self.published = tabs.to_vec();
|
||||
self.advance(now);
|
||||
self.current.clone()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user