1616 lines
57 KiB
Rust
1616 lines
57 KiB
Rust
//! Native developer workspace. Coordinates and scroll offsets are logical pixels.
|
||
use crate::{
|
||
activity::{Activity, Exchange},
|
||
app::AppEvent,
|
||
display_list::{Color, DisplayList, DrawCommand},
|
||
document::{CodeNode, RenderDocument},
|
||
geometry::{Point, Rect},
|
||
sources::SourceWorkspace,
|
||
system_ui::{
|
||
Axis, Button, Cursor, Interaction, Role, Target, Ui, WidgetId,
|
||
theme::{BAR, BG, BORDER, ERROR, MUTED, OK, PALETTE, TEXT, Theme, readable_accent},
|
||
},
|
||
text::{self, TextStyle},
|
||
};
|
||
use std::{
|
||
io::{Read, Write},
|
||
net::TcpStream,
|
||
thread,
|
||
time::Duration,
|
||
};
|
||
use unicode_segmentation::UnicodeSegmentation;
|
||
|
||
const ROW: f32 = 19.0;
|
||
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||
pub enum Tab {
|
||
#[default]
|
||
Console,
|
||
Sources,
|
||
Network,
|
||
Settings,
|
||
}
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||
pub enum Region {
|
||
Transcript = 0,
|
||
Tree,
|
||
Source,
|
||
Requests,
|
||
Details,
|
||
}
|
||
#[derive(Clone, Copy, Debug, Default)]
|
||
struct Scroll {
|
||
x: f32,
|
||
y: f32,
|
||
}
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||
pub enum Focus {
|
||
Input,
|
||
Filter,
|
||
Custom,
|
||
}
|
||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||
pub enum Action {
|
||
Tab(Tab),
|
||
Close,
|
||
ClearConsole,
|
||
ClearNetwork,
|
||
Follow,
|
||
ResetSession,
|
||
Focus(Focus),
|
||
Snapshot,
|
||
Cloud,
|
||
CloudRefresh,
|
||
CloudOpen(String),
|
||
Node(String, usize),
|
||
Request(u64),
|
||
Theme(usize),
|
||
}
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||
pub enum Split {
|
||
Dock,
|
||
Tree,
|
||
Network,
|
||
}
|
||
#[derive(Clone, Copy, Debug)]
|
||
enum Drag {
|
||
Split(Split),
|
||
Scroll(Region, bool, f32),
|
||
}
|
||
|
||
#[derive(Debug)]
|
||
pub struct DeveloperToolsState {
|
||
pub open: bool,
|
||
pub interaction: Interaction,
|
||
pub repaint: bool,
|
||
pub proxy: Option<winit::event_loop::EventLoopProxy<AppEvent>>,
|
||
pub cloud_sources: SourceWorkspace,
|
||
pub tab: Tab,
|
||
pub selected_node_id: Option<String>,
|
||
pub focus: Option<Focus>,
|
||
pub input: String,
|
||
pub cursor: usize,
|
||
pub selection: bool,
|
||
pub preedit: String,
|
||
pub filter: String,
|
||
pub custom: String,
|
||
pub accent: Color,
|
||
pub input_error: String,
|
||
panel_width: f32,
|
||
tree_width: f32,
|
||
network_fraction: f32,
|
||
scroll: [Scroll; 5],
|
||
widths: [Option<(u64, f32)>; 5],
|
||
reveal_input: bool,
|
||
console_stamp: u64,
|
||
drag: Option<Drag>,
|
||
cloud: bool,
|
||
follow: bool,
|
||
selected_request: Option<u64>,
|
||
console_after: u64,
|
||
network_after: u64,
|
||
history: Vec<String>,
|
||
history_index: Option<usize>,
|
||
draft: String,
|
||
}
|
||
impl Default for DeveloperToolsState {
|
||
fn default() -> Self {
|
||
Self {
|
||
open: false,
|
||
interaction: Interaction::default(),
|
||
repaint: false,
|
||
proxy: None,
|
||
cloud_sources: SourceWorkspace::default(),
|
||
tab: Tab::Console,
|
||
selected_node_id: None,
|
||
focus: None,
|
||
input: String::new(),
|
||
cursor: 0,
|
||
selection: false,
|
||
preedit: String::new(),
|
||
filter: String::new(),
|
||
custom: "#1671D5".into(),
|
||
accent: PALETTE[0],
|
||
input_error: String::new(),
|
||
panel_width: 660.0,
|
||
tree_width: 190.0,
|
||
network_fraction: 0.42,
|
||
scroll: [Scroll::default(); 5],
|
||
widths: [None; 5],
|
||
reveal_input: true,
|
||
console_stamp: 0,
|
||
drag: None,
|
||
cloud: false,
|
||
follow: true,
|
||
selected_request: None,
|
||
console_after: 0,
|
||
network_after: 0,
|
||
history: Vec::new(),
|
||
history_index: None,
|
||
draft: String::new(),
|
||
}
|
||
}
|
||
}
|
||
#[derive(Clone, Debug, Default)]
|
||
pub struct DeveloperToolsLayout {
|
||
pub panel: Rect,
|
||
pub widgets: Vec<Target<Action>>,
|
||
splits: Vec<(Rect, Split)>,
|
||
regions: Vec<(Rect, Region, f32, f32)>,
|
||
thumbs: Vec<(Rect, Region, bool, f32)>,
|
||
input: Option<ConsoleInputLayout>,
|
||
}
|
||
#[derive(Clone, Copy, Debug)]
|
||
struct ConsoleInputLayout {
|
||
viewport: Rect,
|
||
origin: Point,
|
||
}
|
||
impl DeveloperToolsState {
|
||
pub fn toggle(&mut self, doc: &RenderDocument) {
|
||
self.open = !self.open;
|
||
self.interaction.blur();
|
||
self.focus = None;
|
||
self.drag = None;
|
||
self.document_updated(doc);
|
||
}
|
||
pub fn document_updated(&mut self, doc: &RenderDocument) {
|
||
if self
|
||
.selected_node_id
|
||
.as_deref()
|
||
.is_none_or(|id| doc.node(id).is_none())
|
||
{
|
||
self.selected_node_id = doc.root.as_ref().map(|n| n.id.clone());
|
||
}
|
||
}
|
||
pub fn cursor_kind(&self, layout: &DeveloperToolsLayout) -> winit::window::CursorIcon {
|
||
use winit::window::CursorIcon;
|
||
if !self.open {
|
||
return CursorIcon::Default;
|
||
}
|
||
match self.interaction.cursor(&layout.widgets) {
|
||
Cursor::Default => CursorIcon::Default,
|
||
Cursor::Pointer => CursorIcon::Pointer,
|
||
Cursor::Text => CursorIcon::Text,
|
||
Cursor::ResizeHorizontal => CursorIcon::ColResize,
|
||
Cursor::ResizeVertical => CursorIcon::RowResize,
|
||
Cursor::Grab => CursorIcon::Grab,
|
||
Cursor::Disabled => CursorIcon::NotAllowed,
|
||
}
|
||
}
|
||
pub fn pointer_left(&mut self) {
|
||
self.interaction.pointer_left();
|
||
}
|
||
pub fn blur(&mut self) {
|
||
self.interaction.blur();
|
||
self.drag = None;
|
||
self.focus = None;
|
||
self.preedit.clear();
|
||
}
|
||
pub fn press(&mut self, layout: &DeveloperToolsLayout, p: Point, activity: &Activity) {
|
||
if !self.open {
|
||
return;
|
||
}
|
||
self.preedit.clear();
|
||
self.interaction.pointer_moved(p, &layout.widgets);
|
||
let target = self.interaction.press(&layout.widgets);
|
||
self.focus = None;
|
||
if let Some(target) = target {
|
||
match target.role {
|
||
Role::Splitter(_) => {
|
||
if let Some((_, split)) = layout.splits.iter().find(|(r, _)| {
|
||
Rect::new(r.x - 3.0, r.y - 3.0, r.width + 6.0, r.height + 6.0).contains(p)
|
||
}) {
|
||
self.drag = Some(Drag::Split(*split));
|
||
}
|
||
}
|
||
Role::Scrollbar(_) => {
|
||
if let Some((_, region, h, ratio)) =
|
||
layout.thumbs.iter().find(|(r, _, _, _)| r.contains(p))
|
||
{
|
||
self.drag = Some(Drag::Scroll(*region, *h, *ratio));
|
||
}
|
||
}
|
||
Role::TextField => self.activate(target.action, layout, p, activity),
|
||
_ => {}
|
||
}
|
||
}
|
||
}
|
||
fn activate(
|
||
&mut self,
|
||
action: Option<Action>,
|
||
layout: &DeveloperToolsLayout,
|
||
p: Point,
|
||
activity: &Activity,
|
||
) {
|
||
self.focus = None;
|
||
match action {
|
||
Some(Action::Tab(t)) => {
|
||
self.tab = t;
|
||
}
|
||
Some(Action::Close) => {
|
||
self.open = false;
|
||
}
|
||
Some(Action::Focus(f)) => {
|
||
self.focus = Some(f);
|
||
self.selection = false;
|
||
if f == Focus::Input
|
||
&& let Some(input) = layout.input
|
||
{
|
||
let row = (((p.y - input.origin.y) / ROW).floor().max(0.0) as usize)
|
||
.min(self.input.split('\n').count() - 1);
|
||
let mut start = 0;
|
||
let line = self
|
||
.input
|
||
.split('\n')
|
||
.enumerate()
|
||
.find_map(|(i, line)| {
|
||
if i == row {
|
||
Some(line)
|
||
} else {
|
||
start += line.len() + 1;
|
||
None
|
||
}
|
||
})
|
||
.unwrap_or("");
|
||
let target = (p.x - input.origin.x).max(0.0);
|
||
let mut offset = line.len();
|
||
for (i, part) in line.grapheme_indices(true) {
|
||
let advance =
|
||
text::measure(&line[..i + part.len()], TextStyle::code()).width;
|
||
if advance >= target {
|
||
offset = i;
|
||
break;
|
||
}
|
||
}
|
||
self.cursor = (start + offset).min(self.input.len());
|
||
self.reveal_cursor();
|
||
}
|
||
}
|
||
Some(Action::ClearConsole) => {
|
||
self.console_after = activity.lock().unwrap().records.back().map_or(0, |r| r.id);
|
||
self.scroll[Region::Transcript as usize] = Scroll::default();
|
||
self.reveal_cursor();
|
||
}
|
||
Some(Action::ClearNetwork) => {
|
||
self.network_after = activity.lock().unwrap().records.back().map_or(0, |r| r.id);
|
||
self.selected_request = None;
|
||
}
|
||
Some(Action::Follow) => {
|
||
self.follow = !self.follow;
|
||
self.console_stamp = 0;
|
||
}
|
||
Some(Action::ResetSession) => self.submit_console(true),
|
||
Some(Action::Snapshot) => self.cloud = false,
|
||
Some(Action::Cloud) => self.cloud = true,
|
||
Some(Action::CloudRefresh) => self.cloud_sources.refresh(),
|
||
Some(Action::CloudOpen(id)) => {
|
||
self.cloud_sources.open(&id);
|
||
self.scroll[Region::Source as usize] = Scroll::default();
|
||
}
|
||
Some(Action::Node(id, line)) => {
|
||
self.selected_node_id = Some(id);
|
||
self.scroll[Region::Source as usize].x = 0.0;
|
||
self.scroll[Region::Source as usize].y = line.saturating_sub(1) as f32 * ROW;
|
||
}
|
||
Some(Action::Request(id)) => {
|
||
self.selected_request = Some(id);
|
||
if self.tab == Tab::Console {
|
||
self.tab = Tab::Network;
|
||
self.network_after = 0;
|
||
self.filter.clear();
|
||
}
|
||
self.scroll[Region::Details as usize] = Scroll::default();
|
||
}
|
||
Some(Action::Theme(i)) => {
|
||
self.accent = PALETTE[i];
|
||
self.custom = format!(
|
||
"#{:02X}{:02X}{:02X}",
|
||
self.accent.red, self.accent.green, self.accent.blue
|
||
);
|
||
}
|
||
None => {}
|
||
}
|
||
}
|
||
pub fn release(&mut self, layout: &DeveloperToolsLayout, p: Point, activity: &Activity) {
|
||
self.interaction.pointer_moved(p, &layout.widgets);
|
||
let action = self.interaction.release(&layout.widgets);
|
||
self.drag = None;
|
||
if action.is_some() {
|
||
self.activate(action, layout, p, activity);
|
||
}
|
||
}
|
||
pub fn keyboard_control(
|
||
&mut self,
|
||
event: &winit::event::KeyEvent,
|
||
shift: bool,
|
||
layout: &DeveloperToolsLayout,
|
||
activity: &Activity,
|
||
) -> bool {
|
||
use winit::{
|
||
event::ElementState,
|
||
keyboard::{Key, NamedKey},
|
||
};
|
||
if !self.open || !self.preedit.is_empty() {
|
||
return false;
|
||
}
|
||
if event.logical_key == Key::Named(NamedKey::Escape) && self.focus.is_none() {
|
||
if event.state == ElementState::Pressed {
|
||
self.interaction.blur();
|
||
}
|
||
return true;
|
||
}
|
||
if event.logical_key == Key::Named(NamedKey::Tab)
|
||
&& (self.focus != Some(Focus::Input) || shift)
|
||
{
|
||
if event.state == ElementState::Pressed && !event.repeat {
|
||
let target = self.interaction.focus_next(&layout.widgets, shift);
|
||
self.focus = target.and_then(|t| match t.action {
|
||
Some(Action::Focus(f)) => Some(f),
|
||
_ => None,
|
||
});
|
||
if self.focus == Some(Focus::Input) {
|
||
self.cursor = self.input.len();
|
||
self.reveal_cursor();
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
let control = self
|
||
.interaction
|
||
.focused(&layout.widgets)
|
||
.is_some_and(|t| t.role != Role::TextField);
|
||
if control
|
||
&& matches!(
|
||
event.logical_key,
|
||
Key::Named(NamedKey::Enter | NamedKey::Space)
|
||
)
|
||
{
|
||
if event.state == ElementState::Pressed {
|
||
if !event.repeat {
|
||
self.interaction.key_press(&layout.widgets);
|
||
}
|
||
} else {
|
||
let action = self.interaction.key_release(&layout.widgets);
|
||
self.activate(action, layout, Point::default(), activity);
|
||
}
|
||
return true;
|
||
}
|
||
false
|
||
}
|
||
pub fn move_pointer(
|
||
&mut self,
|
||
p: Point,
|
||
delta: Point,
|
||
layout: &DeveloperToolsLayout,
|
||
window_width: f32,
|
||
) {
|
||
self.interaction.pointer_moved(p, &layout.widgets);
|
||
match self.drag {
|
||
Some(Drag::Split(Split::Dock)) => {
|
||
self.panel_width =
|
||
(window_width - p.x).clamp(360.0, (window_width - 100.0).max(360.0))
|
||
}
|
||
Some(Drag::Split(Split::Tree)) => {
|
||
self.tree_width =
|
||
(p.x - layout.panel.x).clamp(120.0, (layout.panel.width - 180.0).max(120.0))
|
||
}
|
||
Some(Drag::Split(Split::Network)) => {
|
||
self.network_fraction =
|
||
((p.y - 92.0) / (layout.panel.height - 114.0).max(1.0)).clamp(0.2, 0.8)
|
||
}
|
||
Some(Drag::Scroll(r, h, ratio)) => {
|
||
let s = &mut self.scroll[r as usize];
|
||
if h {
|
||
s.x = (s.x + delta.x * ratio).max(0.0);
|
||
} else {
|
||
s.y = (s.y + delta.y * ratio).max(0.0);
|
||
}
|
||
if r == Region::Transcript {
|
||
self.follow = false;
|
||
}
|
||
}
|
||
None => {}
|
||
}
|
||
}
|
||
pub fn wheel(&mut self, layout: &DeveloperToolsLayout, p: Point, dx: f32, dy: f32) {
|
||
if let Some((_, r, _, _)) = layout
|
||
.regions
|
||
.iter()
|
||
.rev()
|
||
.find(|(area, _, _, _)| area.contains(p))
|
||
{
|
||
let s = &mut self.scroll[*r as usize];
|
||
s.x = (s.x - dx).max(0.0);
|
||
s.y = (s.y - dy).max(0.0);
|
||
if *r == Region::Transcript {
|
||
self.follow = false;
|
||
}
|
||
}
|
||
}
|
||
pub fn insert(&mut self, value: &str) {
|
||
match self.focus {
|
||
Some(Focus::Input) => {
|
||
if self.input.len() + value.len() > 1_048_576 {
|
||
self.input_error = "Input exceeds 1 MiB".into();
|
||
return;
|
||
}
|
||
if self.selection {
|
||
self.input.clear();
|
||
self.cursor = 0;
|
||
self.selection = false;
|
||
}
|
||
self.input.insert_str(self.cursor, value);
|
||
self.cursor += value.len();
|
||
self.history_index = None;
|
||
self.reveal_cursor();
|
||
}
|
||
Some(Focus::Filter) => {
|
||
if self.selection {
|
||
self.filter.clear();
|
||
self.selection = false;
|
||
}
|
||
self.filter
|
||
.extend(value.chars().filter(|c| !c.is_control()).take(200));
|
||
self.scroll[Region::Requests as usize].y = 0.0;
|
||
}
|
||
Some(Focus::Custom) => {
|
||
if self.selection {
|
||
self.custom.clear();
|
||
self.selection = false;
|
||
}
|
||
self.custom.extend(
|
||
value
|
||
.chars()
|
||
.filter(|c| c.is_ascii_hexdigit() || *c == '#')
|
||
.take(7),
|
||
);
|
||
}
|
||
None => {}
|
||
}
|
||
}
|
||
pub(crate) fn reveal_cursor(&mut self) {
|
||
self.reveal_input = true;
|
||
}
|
||
pub fn ime_position(&self, layout: &DeveloperToolsLayout) -> Point {
|
||
if let Some(input) = layout.input {
|
||
let before = &self.input[..self.cursor];
|
||
let row = before.matches('\n').count() as f32;
|
||
let col = before.rsplit('\n').next().unwrap_or("");
|
||
let area = input.viewport;
|
||
return Point::new(
|
||
(input.origin.x + text::measure(col, TextStyle::code()).width)
|
||
.clamp(area.x, area.x + area.width),
|
||
(input.origin.y + row * ROW).clamp(area.y, area.y + area.height),
|
||
);
|
||
}
|
||
Point::new(layout.panel.x + 20.0, 60.0)
|
||
}
|
||
pub fn selected_text(&self) -> String {
|
||
if !self.selection {
|
||
return String::new();
|
||
}
|
||
match self.focus {
|
||
Some(Focus::Input) => self.input.clone(),
|
||
Some(Focus::Filter) => self.filter.clone(),
|
||
Some(Focus::Custom) => self.custom.clone(),
|
||
None => String::new(),
|
||
}
|
||
}
|
||
pub fn key(&mut self, key: &winit::keyboard::NamedKey, shift: bool, command: bool) {
|
||
use winit::keyboard::NamedKey::*;
|
||
if *key == Escape {
|
||
self.focus = None;
|
||
self.preedit.clear();
|
||
self.interaction.blur();
|
||
return;
|
||
}
|
||
if self.focus == Some(Focus::Custom) && *key == Enter {
|
||
let hex = self.custom.trim_start_matches('#');
|
||
if hex.len() == 6
|
||
&& let Ok(v) = u32::from_str_radix(hex, 16)
|
||
{
|
||
self.accent = readable_accent(Color::rgb((v >> 16) as u8, (v >> 8) as u8, v as u8));
|
||
self.input_error.clear();
|
||
} else {
|
||
self.input_error = "Use a six-digit hex color, e.g. #1671D5".into();
|
||
}
|
||
return;
|
||
}
|
||
if self.focus != Some(Focus::Input) {
|
||
if *key == Backspace {
|
||
match self.focus {
|
||
Some(Focus::Filter) => {
|
||
if self.selection {
|
||
self.filter.clear();
|
||
} else {
|
||
self.filter.pop();
|
||
}
|
||
}
|
||
Some(Focus::Custom) => {
|
||
if self.selection {
|
||
self.custom.clear();
|
||
} else {
|
||
self.custom.pop();
|
||
}
|
||
}
|
||
_ => {}
|
||
}
|
||
self.selection = false;
|
||
}
|
||
return;
|
||
}
|
||
match key {
|
||
Enter if !shift => self.submit(),
|
||
Enter => self.insert("\n"),
|
||
Tab => self.insert(" "),
|
||
Space => self.insert(" "),
|
||
Backspace | Delete => {
|
||
if self.selection {
|
||
self.input.clear();
|
||
self.cursor = 0;
|
||
self.selection = false;
|
||
} else if *key == Backspace && self.cursor > 0 {
|
||
let n = self.input[..self.cursor]
|
||
.grapheme_indices(true)
|
||
.next_back()
|
||
.map_or(0, |(i, _)| i);
|
||
self.input.replace_range(n..self.cursor, "");
|
||
self.cursor = n;
|
||
} else if *key == Delete && self.cursor < self.input.len() {
|
||
let len = self.input[self.cursor..]
|
||
.graphemes(true)
|
||
.next()
|
||
.unwrap()
|
||
.len();
|
||
self.input.replace_range(self.cursor..self.cursor + len, "");
|
||
}
|
||
}
|
||
ArrowLeft => {
|
||
self.cursor = if command {
|
||
0
|
||
} else {
|
||
self.input[..self.cursor]
|
||
.grapheme_indices(true)
|
||
.next_back()
|
||
.map_or(0, |(i, _)| i)
|
||
};
|
||
self.selection = false;
|
||
}
|
||
ArrowRight => {
|
||
self.cursor = if command {
|
||
self.input.len()
|
||
} else {
|
||
self.cursor
|
||
+ self.input[self.cursor..]
|
||
.graphemes(true)
|
||
.next()
|
||
.map_or(0, str::len)
|
||
};
|
||
self.selection = false;
|
||
}
|
||
Home => {
|
||
self.cursor = if command {
|
||
0
|
||
} else {
|
||
self.input[..self.cursor].rfind('\n').map_or(0, |i| i + 1)
|
||
};
|
||
self.selection = false;
|
||
}
|
||
End => {
|
||
self.cursor = if command {
|
||
self.input.len()
|
||
} else {
|
||
self.input[self.cursor..]
|
||
.find('\n')
|
||
.map_or(self.input.len(), |i| self.cursor + i)
|
||
};
|
||
self.selection = false;
|
||
}
|
||
ArrowUp | ArrowDown if command || !self.input.contains('\n') => {
|
||
if self.history.is_empty() {
|
||
return;
|
||
}
|
||
if self.history_index.is_none() {
|
||
self.draft = self.input.clone();
|
||
}
|
||
let current = self.history_index.unwrap_or(self.history.len());
|
||
let index = if *key == ArrowUp {
|
||
current.saturating_sub(1)
|
||
} else {
|
||
(current + 1).min(self.history.len())
|
||
};
|
||
self.history_index = (index < self.history.len()).then_some(index);
|
||
self.input = self
|
||
.history_index
|
||
.map_or_else(|| self.draft.clone(), |i| self.history[i].clone());
|
||
self.cursor = self.input.len();
|
||
}
|
||
ArrowUp | ArrowDown => {
|
||
let start = self.input[..self.cursor].rfind('\n').map_or(0, |i| i + 1);
|
||
let col = self.input[start..self.cursor].graphemes(true).count();
|
||
let target = if *key == ArrowUp {
|
||
if start == 0 {
|
||
0
|
||
} else {
|
||
self.input[..start - 1].rfind('\n').map_or(0, |i| i + 1)
|
||
}
|
||
} else {
|
||
self.input[self.cursor..]
|
||
.find('\n')
|
||
.map_or(self.input.len(), |i| self.cursor + i + 1)
|
||
};
|
||
let end = self.input[target..]
|
||
.find('\n')
|
||
.map_or(self.input.len(), |i| target + i);
|
||
self.cursor = target
|
||
+ self.input[target..end]
|
||
.graphemes(true)
|
||
.take(col)
|
||
.map(str::len)
|
||
.sum::<usize>();
|
||
self.selection = false;
|
||
}
|
||
Escape => {
|
||
self.focus = None;
|
||
self.preedit.clear();
|
||
}
|
||
_ => {}
|
||
}
|
||
self.reveal_cursor();
|
||
}
|
||
fn submit(&mut self) {
|
||
self.submit_console(false);
|
||
}
|
||
fn submit_console(&mut self, reset: bool) {
|
||
if !reset && self.input.trim().is_empty() {
|
||
return;
|
||
}
|
||
let source = if reset {
|
||
String::new()
|
||
} else {
|
||
self.input.clone()
|
||
};
|
||
let proxy = self.proxy.clone();
|
||
let spawned=thread::Builder::new().name("tessera-console-client".into()).spawn(move||{
|
||
let send=||->std::io::Result<()> {
|
||
let mut stream=TcpStream::connect_timeout(&"127.0.0.1:4737".parse().unwrap(),Duration::from_secs(2))?;
|
||
stream.set_read_timeout(Some(Duration::from_secs(120)))?;stream.set_write_timeout(Some(Duration::from_secs(10)))?;
|
||
let method=if reset{"DELETE"}else{"POST"};
|
||
write!(stream,"{method} /v1/namespaces/app.main/repl HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\nContent-Type: text/plain; charset=utf-8\r\nX-Tessera-Origin: console\r\nContent-Length: {}\r\n\r\n{}",source.len(),source)?;
|
||
let mut response=String::new();stream.read_to_string(&mut response)?;Ok(())
|
||
};
|
||
if let Err(error)=send()&&let Some(proxy)=proxy{let _=proxy.send_event(AppEvent::ConsoleFailed{message:error.to_string(),source});}
|
||
});
|
||
match spawned {
|
||
Ok(_) => {
|
||
if !reset {
|
||
self.history.push(self.input.clone());
|
||
if self.history.len() > 80 {
|
||
self.history.remove(0);
|
||
}
|
||
self.input.clear();
|
||
self.cursor = 0;
|
||
self.history_index = None;
|
||
self.selection = false;
|
||
self.reveal_cursor();
|
||
}
|
||
self.follow = true;
|
||
self.input_error.clear();
|
||
}
|
||
Err(error) => self.input_error = error.to_string(),
|
||
}
|
||
}
|
||
}
|
||
|
||
struct Painter {
|
||
ui: Ui<Action>,
|
||
layout: DeveloperToolsLayout,
|
||
}
|
||
impl Painter {
|
||
fn fill(&mut self, r: Rect, c: Color) {
|
||
self.ui.fill(r, c);
|
||
}
|
||
fn label(&mut self, x: f32, y: f32, value: &str, c: Color, code: bool) {
|
||
self.ui.label(x, y, value, c, code);
|
||
}
|
||
fn button(&mut self, r: Rect, label: &str, action: Action, active: bool) {
|
||
let role = match action {
|
||
Action::Tab(_) => Role::Tab,
|
||
Action::Follow | Action::Snapshot | Action::Cloud => Role::Toggle,
|
||
_ => Role::Button,
|
||
};
|
||
self.ui
|
||
.button(r, action, Button::new(label).selected(active).role(role));
|
||
}
|
||
fn enabled_button(&mut self, r: Rect, label: &str, action: Action, enabled: bool) {
|
||
self.ui
|
||
.button(r, action, Button::new(label).enabled(enabled));
|
||
}
|
||
fn split(&mut self, r: Rect, kind: Split) {
|
||
// Paint and register splitters after all pane content, in one overlay pass.
|
||
self.layout.splits.push((r, kind));
|
||
}
|
||
fn lines(
|
||
&mut self,
|
||
area: Rect,
|
||
region: Region,
|
||
lines: &[Line],
|
||
state: &mut DeveloperToolsState,
|
||
follow: bool,
|
||
) {
|
||
let content_h = lines.len() as f32 * ROW + 12.0;
|
||
use std::hash::{Hash, Hasher};
|
||
let mut hash = std::collections::hash_map::DefaultHasher::new();
|
||
for line in lines {
|
||
line.value.hash(&mut hash);
|
||
}
|
||
let fingerprint = hash.finish();
|
||
let cache = &mut state.widths[region as usize];
|
||
let content_w = match *cache {
|
||
Some((previous, width)) if previous == fingerprint => width,
|
||
_ => {
|
||
let width = lines
|
||
.iter()
|
||
.map(|line| text::measure(&line.value, TextStyle::code()).width + 20.0)
|
||
.fold(0.0_f32, f32::max);
|
||
*cache = Some((fingerprint, width));
|
||
width
|
||
}
|
||
};
|
||
let s = &mut state.scroll[region as usize];
|
||
s.y = if follow {
|
||
(content_h - area.height + 12.0).max(0.0)
|
||
} else {
|
||
s.y.clamp(0.0, (content_h - area.height + 12.0).max(0.0))
|
||
};
|
||
s.x = s.x.clamp(0.0, (content_w - area.width + 12.0).max(0.0));
|
||
self.ui.list.push(DrawCommand::PushClip(Rect::new(
|
||
area.x,
|
||
area.y,
|
||
(area.width - 9.0).max(0.0),
|
||
(area.height - 9.0).max(0.0),
|
||
)));
|
||
let first = (s.y / ROW).floor() as usize;
|
||
let count = (area.height / ROW).ceil() as usize + 2;
|
||
for (i, line) in lines.iter().enumerate().skip(first).take(count) {
|
||
let y = area.y + 5.0 + i as f32 * ROW - s.y;
|
||
if let Some(divider) = line.divider {
|
||
self.fill(
|
||
Rect::new(area.x, y, area.width, 1.0),
|
||
match divider {
|
||
Divider::Accent => self.ui.theme.accent,
|
||
Divider::Neutral => BORDER,
|
||
},
|
||
);
|
||
}
|
||
let paint = Rect::new(area.x, y, area.width, ROW);
|
||
if let Some(action) = &line.action {
|
||
let top = y.max(area.y);
|
||
let bottom = (y + ROW).min(area.y + area.height - 9.0);
|
||
if bottom > top {
|
||
self.ui.row(
|
||
paint,
|
||
Rect::new(area.x, top, area.width - 9.0, bottom - top),
|
||
action.clone(),
|
||
line.selected,
|
||
);
|
||
}
|
||
} else if line.selected {
|
||
self.fill(paint, self.ui.theme.soft());
|
||
}
|
||
self.label(area.x + 8.0 - s.x, y, &line.value, line.color, true);
|
||
}
|
||
self.ui.list.push(DrawCommand::PopClip);
|
||
self.layout
|
||
.regions
|
||
.push((area, region, content_w, content_h));
|
||
self.scrollbar(area, region, false, s.y, content_h);
|
||
self.scrollbar(area, region, true, s.x, content_w);
|
||
}
|
||
fn scrollbar(&mut self, r: Rect, region: Region, h: bool, offset: f32, content: f32) {
|
||
let viewport = if h { r.width } else { r.height } - 10.0;
|
||
if content <= viewport || viewport <= 0.0 {
|
||
return;
|
||
}
|
||
let length = (viewport * viewport / content).max(24.0).min(viewport);
|
||
let travel = (viewport - length).max(1.0);
|
||
let maximum = (content - viewport).max(1.0);
|
||
let position = (offset / maximum).clamp(0.0, 1.0) * travel;
|
||
let track = if h {
|
||
Rect::new(r.x, r.y + r.height - 8.0, viewport, 8.0)
|
||
} else {
|
||
Rect::new(r.x + r.width - 8.0, r.y, 8.0, viewport)
|
||
};
|
||
self.fill(track, BAR);
|
||
let thumb = if h {
|
||
Rect::new(track.x + position, track.y + 1.0, length, 6.0)
|
||
} else {
|
||
Rect::new(track.x + 1.0, track.y + position, 6.0, length)
|
||
};
|
||
self.ui.scrollbar(
|
||
thumb,
|
||
WidgetId::new(("scroll", region, h)),
|
||
if h { Axis::Horizontal } else { Axis::Vertical },
|
||
);
|
||
self.layout
|
||
.thumbs
|
||
.push((thumb, region, h, maximum / travel));
|
||
}
|
||
}
|
||
#[derive(Clone)]
|
||
struct Line {
|
||
value: String,
|
||
color: Color,
|
||
divider: Option<Divider>,
|
||
selected: bool,
|
||
action: Option<Action>,
|
||
}
|
||
#[derive(Clone, Copy)]
|
||
enum Divider {
|
||
Accent,
|
||
Neutral,
|
||
}
|
||
impl Line {
|
||
fn new(value: impl Into<String>, color: Color) -> Self {
|
||
Self {
|
||
value: value.into(),
|
||
color,
|
||
divider: None,
|
||
selected: false,
|
||
action: None,
|
||
}
|
||
}
|
||
}
|
||
fn multiline(out: &mut Vec<Line>, value: &str, color: Color) {
|
||
out.extend(
|
||
value
|
||
.replace('\t', " ")
|
||
.split('\n')
|
||
.map(|s| Line::new(s, color)),
|
||
);
|
||
}
|
||
fn console_result(out: &mut Vec<Line>, body: &str, color: Color) {
|
||
if let Ok(value) = serde_json::from_str::<serde_json::Value>(body) {
|
||
if value["execution"] == "repl" {
|
||
out.push(Line::new(
|
||
if value["rendered"] == true {
|
||
format!(
|
||
"Rendered revision {} · session {}",
|
||
value["revision"], value["session"]
|
||
)
|
||
} else {
|
||
format!(
|
||
"Session {} · retained in memory; page unchanged",
|
||
value["session"]
|
||
)
|
||
},
|
||
MUTED,
|
||
));
|
||
}
|
||
if value["session_reset"] == true {
|
||
multiline(
|
||
out,
|
||
value["message"]
|
||
.as_str()
|
||
.or(value["error"].as_str())
|
||
.unwrap_or("Session reset"),
|
||
color,
|
||
);
|
||
return;
|
||
}
|
||
if let Some(result) = value["value"].as_str() {
|
||
multiline(out, result, color);
|
||
return;
|
||
}
|
||
if let Some(diagnostics) = value["diagnostics"].as_array()
|
||
&& !diagnostics.is_empty()
|
||
{
|
||
for d in diagnostics {
|
||
if let Some(line) = d["line"].as_u64() {
|
||
out.push(Line::new(
|
||
format!("Error · line {line}, column {}", d["column"]),
|
||
color,
|
||
));
|
||
}
|
||
multiline(
|
||
out,
|
||
d["message"].as_str().unwrap_or("Execution failed"),
|
||
color,
|
||
);
|
||
}
|
||
return;
|
||
}
|
||
if let Some(error) = value["error"].as_str() {
|
||
multiline(out, error, color);
|
||
return;
|
||
}
|
||
if let Some(revision) = value["revision"].as_u64() {
|
||
out.push(Line::new(format!("Committed revision {revision}"), color));
|
||
return;
|
||
}
|
||
}
|
||
multiline(out, body, color);
|
||
}
|
||
fn flatten<'a>(node: &'a CodeNode, depth: usize, out: &mut Vec<(usize, &'a CodeNode)>) {
|
||
out.push((depth, node));
|
||
for child in &node.children {
|
||
flatten(child, depth + 1, out);
|
||
}
|
||
}
|
||
|
||
pub fn build(
|
||
width: u32,
|
||
height: u32,
|
||
scale: f32,
|
||
doc: &RenderDocument,
|
||
state: &mut DeveloperToolsState,
|
||
activity: &Activity,
|
||
) -> (DisplayList, DeveloperToolsLayout) {
|
||
if !state.open {
|
||
return (Default::default(), Default::default());
|
||
}
|
||
let width = width as f32 / scale;
|
||
let height = height as f32 / scale;
|
||
let w = state
|
||
.panel_width
|
||
.clamp(360.0, (width - 100.0).max(360.0))
|
||
.min(width);
|
||
let x = width - w;
|
||
let panel = Rect::new(x, 0.0, w, height);
|
||
let mut p = Painter {
|
||
ui: Ui::new(Theme::new(state.accent), state.interaction.clone()),
|
||
layout: DeveloperToolsLayout {
|
||
panel,
|
||
..Default::default()
|
||
},
|
||
};
|
||
p.fill(panel, BG);
|
||
p.ui.list.push(DrawCommand::PushClip(panel));
|
||
p.fill(Rect::new(x, 0.0, w, 31.0), BAR);
|
||
p.split(Rect::new(x + 1.0, 0.0, 1.0, height), Split::Dock);
|
||
for (i, (label, tab)) in [
|
||
("Console", Tab::Console),
|
||
("Sources", Tab::Sources),
|
||
("Network", Tab::Network),
|
||
("Settings", Tab::Settings),
|
||
]
|
||
.iter()
|
||
.enumerate()
|
||
{
|
||
let r = Rect::new(x + 5.0 + i as f32 * 75.0, 0.0, 75.0, 30.0);
|
||
p.button(r, label, Action::Tab(*tab), state.tab == *tab);
|
||
}
|
||
p.button(
|
||
Rect::new(width - 32.0, 0.0, 31.0, 30.0),
|
||
"×",
|
||
Action::Close,
|
||
false,
|
||
);
|
||
let records = activity
|
||
.lock()
|
||
.unwrap()
|
||
.records
|
||
.iter()
|
||
.cloned()
|
||
.collect::<Vec<_>>();
|
||
let bottom = height - 22.0;
|
||
match state.tab {
|
||
Tab::Console => {
|
||
p.fill(Rect::new(x, 32.0, w, 30.0), BAR);
|
||
p.enabled_button(
|
||
Rect::new(x + 3.0, 32.0, 62.0, 29.0),
|
||
"Clear",
|
||
Action::ClearConsole,
|
||
records.iter().any(|r| {
|
||
r.id > state.console_after
|
||
&& matches!(r.method.as_str(), "PUT" | "POST" | "DELETE")
|
||
&& !r.path.ends_with("/events")
|
||
}),
|
||
);
|
||
p.button(
|
||
Rect::new(x + 68.0, 32.0, 106.0, 29.0),
|
||
if state.follow {
|
||
"Follow: on"
|
||
} else {
|
||
"Follow: off"
|
||
},
|
||
Action::Follow,
|
||
state.follow,
|
||
);
|
||
p.button(
|
||
Rect::new(x + 181.0, 32.0, 119.0, 29.0),
|
||
"Reset session",
|
||
Action::ResetSession,
|
||
false,
|
||
);
|
||
if w > 520.0 {
|
||
p.label(x + 309.0, 39.0, "app.main · live objects", MUTED, false);
|
||
}
|
||
let mut lines = Vec::new();
|
||
for r in records.iter().filter(|r| {
|
||
r.id > state.console_after
|
||
&& (r.method == "PUT" || r.method == "POST" || r.method == "DELETE")
|
||
&& !r.path.ends_with("/events")
|
||
}) {
|
||
let kind = if r.path.ends_with("/repl") {
|
||
if r.method == "DELETE" {
|
||
"RESET SESSION"
|
||
} else {
|
||
"REPL · live objects"
|
||
}
|
||
} else if r.path.ends_with("/evaluate") {
|
||
"ISOLATED API EVALUATION"
|
||
} else if r.method == "PUT" {
|
||
"FULL SOURCE · live program"
|
||
} else {
|
||
"CLEAR DOCUMENT"
|
||
};
|
||
let local =
|
||
r.origin == "Console" && r.path.ends_with("/repl") && r.method == "POST";
|
||
if !local {
|
||
let mut title = Line::new(
|
||
format!("#{} {} {} {}", r.id, r.timestamp, r.origin, kind),
|
||
MUTED,
|
||
);
|
||
title.divider = Some(Divider::Accent);
|
||
title.action = Some(Action::Request(r.id));
|
||
lines.push(title);
|
||
}
|
||
for (i, line) in r.request.split('\n').enumerate() {
|
||
lines.push(Line::new(
|
||
format!("{}{line}", if i == 0 { "> " } else { " " }),
|
||
TEXT,
|
||
));
|
||
}
|
||
let color = if r.status.is_some_and(|s| s >= 400) {
|
||
ERROR
|
||
} else {
|
||
OK
|
||
};
|
||
if let Some(status) = r.status {
|
||
if !local {
|
||
lines.push(Line::new(
|
||
format!("‹ {status} · {} ms", r.elapsed_ms),
|
||
color,
|
||
));
|
||
}
|
||
console_result(&mut lines, &r.response, color);
|
||
} else {
|
||
lines.push(Line::new(" Executing…", MUTED));
|
||
}
|
||
let mut separator = Line::new("", MUTED);
|
||
separator.divider = Some(Divider::Neutral);
|
||
lines.push(separator);
|
||
}
|
||
if lines.is_empty() {
|
||
multiline(
|
||
&mut lines,
|
||
"Endor · live session\nCreate objects here; call page.render(...) to display.\n",
|
||
MUTED,
|
||
);
|
||
}
|
||
use std::hash::{Hash, Hasher};
|
||
let mut hash = std::collections::hash_map::DefaultHasher::new();
|
||
for line in &lines {
|
||
line.value.hash(&mut hash);
|
||
}
|
||
let stamp = hash.finish();
|
||
let output_changed = stamp != state.console_stamp;
|
||
state.console_stamp = stamp;
|
||
let first_input = lines.len();
|
||
let area = Rect::new(x + 4.0, 63.0, w - 5.0, (bottom - 88.0).max(1.0));
|
||
p.ui.inline_input(
|
||
Rect::new(
|
||
area.x,
|
||
area.y,
|
||
(area.width - 9.0).max(0.0),
|
||
(area.height - 9.0).max(0.0),
|
||
),
|
||
Action::Focus(Focus::Input),
|
||
);
|
||
let mut input = state.input.clone();
|
||
if state.focus == Some(Focus::Input) {
|
||
input.insert_str(state.cursor, &state.preedit);
|
||
}
|
||
for (i, line) in input.split('\n').enumerate() {
|
||
let mut line =
|
||
Line::new(format!("{}{line}", if i == 0 { "> " } else { " " }), TEXT);
|
||
line.selected = state.selection && state.focus == Some(Focus::Input);
|
||
lines.push(line);
|
||
}
|
||
let prefix = text::measure("> ", TextStyle::code()).width;
|
||
let before = &state.input[..state.cursor];
|
||
let row = before.matches('\n').count() as f32;
|
||
let column = before.rsplit('\n').next().unwrap_or("");
|
||
let caret_x = prefix
|
||
+ text::measure(&format!("{column}{}", state.preedit), TextStyle::code()).width;
|
||
let caret_y = (first_input as f32 + row) * ROW;
|
||
let follow = state.follow && output_changed && !state.reveal_input;
|
||
if state.reveal_input {
|
||
let s = &mut state.scroll[Region::Transcript as usize];
|
||
s.y = s.y.clamp(
|
||
(caret_y + ROW + 16.0 - area.height).max(0.0).min(caret_y),
|
||
caret_y,
|
||
);
|
||
s.x = s.x.clamp((caret_x + 24.0 - area.width).max(0.0), caret_x);
|
||
state.reveal_input = false;
|
||
}
|
||
p.lines(area, Region::Transcript, &lines, state, follow);
|
||
let scroll = state.scroll[Region::Transcript as usize];
|
||
let origin = Point::new(
|
||
area.x + 8.0 + prefix - scroll.x,
|
||
area.y + 5.0 + first_input as f32 * ROW - scroll.y,
|
||
);
|
||
p.layout.input = Some(ConsoleInputLayout {
|
||
viewport: area,
|
||
origin,
|
||
});
|
||
if state.focus == Some(Focus::Input) {
|
||
p.ui.list.push(DrawCommand::PushClip(Rect::new(
|
||
area.x,
|
||
area.y,
|
||
(area.width - 9.0).max(0.0),
|
||
(area.height - 9.0).max(0.0),
|
||
)));
|
||
p.fill(
|
||
Rect::new(
|
||
area.x + 8.0 + caret_x - scroll.x,
|
||
origin.y + row * ROW + 1.0,
|
||
1.0,
|
||
ROW - 2.0,
|
||
),
|
||
TEXT,
|
||
);
|
||
p.ui.list.push(DrawCommand::PopClip);
|
||
}
|
||
p.label(
|
||
x + 10.0,
|
||
bottom - 23.0,
|
||
if state.input_error.is_empty() {
|
||
"Enter: run · Shift+Enter: newline · ⌘/Ctrl+↑↓: history"
|
||
} else {
|
||
&state.input_error
|
||
},
|
||
if state.input_error.is_empty() {
|
||
MUTED
|
||
} else {
|
||
ERROR
|
||
},
|
||
false,
|
||
);
|
||
}
|
||
Tab::Sources => {
|
||
p.fill(Rect::new(x, 32.0, w, 30.0), BAR);
|
||
p.button(
|
||
Rect::new(x + 3.0, 32.0, 139.0, 29.0),
|
||
"Current snapshot",
|
||
Action::Snapshot,
|
||
!state.cloud,
|
||
);
|
||
p.button(
|
||
Rect::new(x + 145.0, 32.0, 107.0, 29.0),
|
||
"Cloud files",
|
||
Action::Cloud,
|
||
state.cloud,
|
||
);
|
||
if state.cloud {
|
||
p.button(
|
||
Rect::new(width - 92.0, 32.0, 88.0, 29.0),
|
||
"Refresh",
|
||
Action::CloudRefresh,
|
||
false,
|
||
);
|
||
let tree = state.tree_width.clamp(120.0, (w - 180.0).max(120.0));
|
||
let code_x = x + tree + 2.0;
|
||
p.label(x + 10.0, 69.0, "CLOUD FILES", MUTED, false);
|
||
let file_lines = state
|
||
.cloud_sources
|
||
.files
|
||
.iter()
|
||
.map(|file| {
|
||
let mut l = Line::new(&file.path, TEXT);
|
||
l.action = Some(Action::CloudOpen(file.id.clone()));
|
||
l
|
||
})
|
||
.collect::<Vec<_>>();
|
||
p.lines(
|
||
Rect::new(x + 1.0, 92.0, tree - 1.0, bottom - 92.0),
|
||
Region::Tree,
|
||
&file_lines,
|
||
state,
|
||
false,
|
||
);
|
||
p.split(Rect::new(x + tree, 63.0, 1.0, bottom - 63.0), Split::Tree);
|
||
let mut source_lines = Vec::new();
|
||
if let Some(source) = &state.cloud_sources.opened {
|
||
p.label(code_x + 10.0, 69.0, &source.path, MUTED, false);
|
||
for (i, line) in source.code.split('\n').enumerate() {
|
||
source_lines.push(Line::new(format!("{:>5} {}", i + 1, line), TEXT));
|
||
}
|
||
} else {
|
||
multiline(
|
||
&mut source_lines,
|
||
"No cloud file open\n\nNo provider connected.\nCloud files will open here as full source.\nCurrent snapshot contains your last successful script.",
|
||
MUTED,
|
||
);
|
||
}
|
||
if !state.cloud_sources.error.is_empty() {
|
||
source_lines.push(Line::new(&state.cloud_sources.error, ERROR));
|
||
}
|
||
p.lines(
|
||
Rect::new(code_x, 92.0, width - code_x, bottom - 92.0),
|
||
Region::Source,
|
||
&source_lines,
|
||
state,
|
||
false,
|
||
);
|
||
} else {
|
||
let tree = state.tree_width.clamp(120.0, (w - 180.0).max(120.0));
|
||
let code_x = x + tree + 2.0;
|
||
p.label(x + 10.0, 69.0, "ASTER AST", MUTED, false);
|
||
p.label(
|
||
code_x + 10.0,
|
||
69.0,
|
||
&format!(
|
||
"{} · {} lines · read only",
|
||
if doc
|
||
.root
|
||
.as_ref()
|
||
.is_some_and(|root| root.kind == "repl-session")
|
||
{
|
||
"Console transcript"
|
||
} else {
|
||
"app.main.endor"
|
||
},
|
||
doc.source.split('\n').count()
|
||
),
|
||
MUTED,
|
||
false,
|
||
);
|
||
let mut tree_lines = Vec::new();
|
||
if let Some(root) = &doc.root {
|
||
let mut flat = Vec::new();
|
||
flatten(root, 0, &mut flat);
|
||
for (depth, n) in flat {
|
||
let mut l =
|
||
Line::new(format!("{}{} {}", " ".repeat(depth), n.kind, n.name), TEXT);
|
||
l.selected = state.selected_node_id.as_deref() == Some(&n.id);
|
||
l.action = Some(Action::Node(n.id.clone(), n.start_line));
|
||
tree_lines.push(l);
|
||
}
|
||
} else {
|
||
tree_lines.push(Line::new("No submitted source", MUTED));
|
||
}
|
||
p.lines(
|
||
Rect::new(x + 1.0, 92.0, tree - 1.0, bottom - 92.0),
|
||
Region::Tree,
|
||
&tree_lines,
|
||
state,
|
||
false,
|
||
);
|
||
p.split(Rect::new(x + tree, 63.0, 1.0, bottom - 63.0), Split::Tree);
|
||
let selected = state
|
||
.selected_node_id
|
||
.as_deref()
|
||
.and_then(|id| doc.node(id));
|
||
let code_lines = doc
|
||
.source
|
||
.replace("\r\n", "\n")
|
||
.split('\n')
|
||
.enumerate()
|
||
.map(|(i, s)| {
|
||
let mut l =
|
||
Line::new(format!("{:>5} {}", i + 1, s.replace('\t', " ")), TEXT);
|
||
l.selected =
|
||
selected.is_some_and(|n| i + 1 >= n.start_line && i < n.end_line);
|
||
l
|
||
})
|
||
.collect::<Vec<_>>();
|
||
p.lines(
|
||
Rect::new(code_x, 92.0, width - code_x, bottom - 92.0),
|
||
Region::Source,
|
||
&code_lines,
|
||
state,
|
||
false,
|
||
);
|
||
}
|
||
}
|
||
Tab::Network => {
|
||
p.fill(Rect::new(x, 32.0, w, 30.0), BAR);
|
||
p.enabled_button(
|
||
Rect::new(x + 3.0, 32.0, 62.0, 29.0),
|
||
"Clear",
|
||
Action::ClearNetwork,
|
||
records.iter().any(|r| r.id > state.network_after),
|
||
);
|
||
let filter_area = Rect::new(x + 72.0, 35.0, (w - 85.0).max(100.0), 24.0);
|
||
p.ui.text_field(
|
||
filter_area,
|
||
Action::Focus(Focus::Filter),
|
||
state.focus == Some(Focus::Filter),
|
||
);
|
||
p.label(
|
||
filter_area.x + 7.0,
|
||
filter_area.y + 3.0,
|
||
&format!(
|
||
"Filter: {}{}",
|
||
state.filter,
|
||
if state.focus == Some(Focus::Filter) {
|
||
"│"
|
||
} else {
|
||
""
|
||
}
|
||
),
|
||
MUTED,
|
||
false,
|
||
);
|
||
|
||
p.label(
|
||
x + 10.0,
|
||
69.0,
|
||
"METHOD STATUS TIME PATH",
|
||
MUTED,
|
||
true,
|
||
);
|
||
let divider = 92.0 + (bottom - 92.0) * state.network_fraction;
|
||
let filter = state.filter.to_lowercase();
|
||
let visible = records
|
||
.iter()
|
||
.filter(|r| r.id > state.network_after)
|
||
.filter(|r| {
|
||
format!(
|
||
"{} {} {}",
|
||
r.method,
|
||
r.path,
|
||
r.status.map_or("pending".into(), |s| s.to_string())
|
||
)
|
||
.to_lowercase()
|
||
.contains(&filter)
|
||
})
|
||
.collect::<Vec<_>>();
|
||
let mut lines = Vec::new();
|
||
for r in &visible {
|
||
let mut l = Line::new(
|
||
format!(
|
||
"{:<7} {:<6} {:>5} ms {}",
|
||
r.method,
|
||
r.status.map_or("…".into(), |s| s.to_string()),
|
||
r.elapsed_ms,
|
||
r.path
|
||
),
|
||
if r.status.is_some_and(|s| s >= 400) {
|
||
ERROR
|
||
} else {
|
||
TEXT
|
||
},
|
||
);
|
||
l.selected = state.selected_request == Some(r.id);
|
||
l.action = Some(Action::Request(r.id));
|
||
lines.push(l);
|
||
}
|
||
if lines.is_empty() {
|
||
lines.push(Line::new(
|
||
"No matching requests. Listening to Tessera's local API.",
|
||
MUTED,
|
||
));
|
||
}
|
||
p.lines(
|
||
Rect::new(x + 1.0, 92.0, w - 2.0, divider - 92.0),
|
||
Region::Requests,
|
||
&lines,
|
||
state,
|
||
false,
|
||
);
|
||
p.split(Rect::new(x + 1.0, divider, w - 2.0, 1.0), Split::Network);
|
||
let selected = records
|
||
.iter()
|
||
.find(|r| Some(r.id) == state.selected_request);
|
||
let mut details = Vec::new();
|
||
if let Some(r) = selected {
|
||
request_details(&mut details, r);
|
||
} else {
|
||
multiline(
|
||
&mut details,
|
||
"Select a request to inspect headers and bodies.\n\nCapture scope: 127.0.0.1:4737 in this Tessera process.\n80 most recent requests; body previews up to 32 KiB.\nOnly Content-Type and Content-Length request headers are recorded.\nExternal browser / OS traffic is not captured.",
|
||
MUTED,
|
||
);
|
||
}
|
||
p.lines(
|
||
Rect::new(x + 1.0, divider + 5.0, w - 2.0, bottom - divider - 5.0),
|
||
Region::Details,
|
||
&details,
|
||
state,
|
||
false,
|
||
);
|
||
}
|
||
Tab::Settings => {
|
||
p.label(x + 24.0, 62.0, "Appearance", TEXT, false);
|
||
p.label(
|
||
x + 24.0,
|
||
90.0,
|
||
"Accent color · tabs, focus and execution separators",
|
||
MUTED,
|
||
false,
|
||
);
|
||
for (i, color) in PALETTE.iter().enumerate() {
|
||
let r = Rect::new(x + 24.0 + i as f32 * 44.0, 122.0, 32.0, 32.0);
|
||
p.ui.swatch(r, Action::Theme(i), *color, *color == state.accent);
|
||
}
|
||
p.label(
|
||
x + 24.0,
|
||
176.0,
|
||
"Custom hex · Enter to apply (bright colors adjusted)",
|
||
MUTED,
|
||
false,
|
||
);
|
||
let custom = Rect::new(x + 24.0, 204.0, 200.0, 32.0);
|
||
p.ui.text_field(
|
||
custom,
|
||
Action::Focus(Focus::Custom),
|
||
state.focus == Some(Focus::Custom),
|
||
);
|
||
p.label(
|
||
custom.x + 10.0,
|
||
custom.y + 6.0,
|
||
&format!(
|
||
"{}{}",
|
||
state.custom,
|
||
if state.focus == Some(Focus::Custom) {
|
||
"│"
|
||
} else {
|
||
""
|
||
}
|
||
),
|
||
TEXT,
|
||
true,
|
||
);
|
||
|
||
if !state.input_error.is_empty() {
|
||
p.label(x + 24.0, 245.0, &state.input_error, ERROR, false);
|
||
}
|
||
p.fill(Rect::new(x + 24.0, 285.0, w - 48.0, 1.0), BORDER);
|
||
p.label(x + 24.0, 305.0, "Theme preview", TEXT, false);
|
||
p.fill(Rect::new(x + 24.0, 340.0, w - 48.0, 2.0), state.accent);
|
||
p.label(x + 32.0, 353.0, "› \"Hello\"", TEXT, true);
|
||
p.label(x + 32.0, 380.0, "‹ \"Hello\"", OK, true);
|
||
p.label(
|
||
x + 24.0,
|
||
430.0,
|
||
"Settings last for this window session. Source is not persisted.",
|
||
MUTED,
|
||
false,
|
||
);
|
||
}
|
||
}
|
||
p.fill(Rect::new(x + 1.0, bottom, w - 1.0, 22.0), BAR);
|
||
p.label(
|
||
x + 9.0,
|
||
bottom + 3.0,
|
||
&text::fit(
|
||
&format!(
|
||
"● 127.0.0.1:4737 · {} captured · F12 / ⌘⌥I",
|
||
records
|
||
.iter()
|
||
.filter(|r| r.id > state.network_after)
|
||
.count()
|
||
),
|
||
w - 20.0,
|
||
TextStyle::code(),
|
||
),
|
||
MUTED,
|
||
false,
|
||
);
|
||
// Splitters must be above toolbars, pane backgrounds and status bars for their
|
||
// whole length. Register them last so their hit priority matches their paint order.
|
||
for (rect, kind) in p.layout.splits.clone().into_iter().rev() {
|
||
p.ui.splitter(
|
||
rect,
|
||
WidgetId::new(("split", kind)),
|
||
match kind {
|
||
Split::Dock | Split::Tree => Axis::Horizontal,
|
||
Split::Network => Axis::Vertical,
|
||
},
|
||
);
|
||
}
|
||
p.ui.list.push(DrawCommand::PopClip);
|
||
let mut scaled = DisplayList::default();
|
||
for cmd in p.ui.list.commands() {
|
||
let rect =
|
||
|r: &Rect| Rect::new(r.x * scale, r.y * scale, r.width * scale, r.height * scale);
|
||
scaled.push(match cmd {
|
||
DrawCommand::FillRoundedRect {
|
||
rect: r,
|
||
radius,
|
||
color,
|
||
} => DrawCommand::FillRoundedRect {
|
||
rect: rect(r),
|
||
radius: radius * scale,
|
||
color: *color,
|
||
},
|
||
DrawCommand::Text {
|
||
origin,
|
||
value,
|
||
style,
|
||
color,
|
||
} => DrawCommand::Text {
|
||
origin: Point::new(origin.x * scale, origin.y * scale),
|
||
value: value.clone(),
|
||
style: style.scaled(scale),
|
||
color: *color,
|
||
},
|
||
DrawCommand::PushClip(r) => DrawCommand::PushClip(rect(r)),
|
||
DrawCommand::StrokeRoundedRect {
|
||
rect: r,
|
||
radius,
|
||
width,
|
||
color,
|
||
} => DrawCommand::StrokeRoundedRect {
|
||
rect: rect(r),
|
||
radius: radius * scale,
|
||
width: width * scale,
|
||
color: *color,
|
||
},
|
||
other => other.clone(),
|
||
});
|
||
}
|
||
p.layout.widgets = p.ui.targets;
|
||
state.repaint = state.interaction.reconcile(&p.layout.widgets);
|
||
(scaled, p.layout)
|
||
}
|
||
fn request_details(out: &mut Vec<Line>, r: &Exchange) {
|
||
multiline(
|
||
out,
|
||
&format!(
|
||
"#{} {} {}\n{} · {} · {} ms\nStatus: {}",
|
||
r.id,
|
||
r.method,
|
||
r.path,
|
||
r.timestamp,
|
||
r.origin,
|
||
r.elapsed_ms,
|
||
r.status.map_or("pending".into(), |s| s.to_string())
|
||
),
|
||
TEXT,
|
||
);
|
||
for (title, body) in [
|
||
("REQUEST HEADERS", &r.request_headers),
|
||
("REQUEST BODY", &r.request),
|
||
("RESPONSE HEADERS", &r.response_headers),
|
||
("RESPONSE BODY", &r.response),
|
||
] {
|
||
out.push(Line::new("", MUTED));
|
||
let mut l = Line::new(title, MUTED);
|
||
l.divider = Some(Divider::Accent);
|
||
out.push(l);
|
||
multiline(
|
||
out,
|
||
if body.is_empty() { "(empty)" } else { body },
|
||
if title == "RESPONSE BODY" && r.status.is_some_and(|s| s >= 400) {
|
||
ERROR
|
||
} else {
|
||
TEXT
|
||
},
|
||
);
|
||
}
|
||
}
|