Reviews: T16a–e done. After the cap: a lagging synced chart leaves the shared window alone, and a price step keeps its places by magnitude
This commit is contained in:
1 parent
1fe5673de1
commit
a894e5f973
4 files changed
+96
-41
No files matched your search
+20
-20
@@ -40,7 +40,7 @@ pub enum ChartKind {
|
||||
pub struct ChartSync {
|
||||
pub(crate) visible: Option<Visible>,
|
||||
pub(crate) hover: Option<i64>,
|
||||
/// How many candles the shared window last moved for, so one append moves it once.
|
||||
/// The most candles the shared window has moved for: an append moves it once, a lagging chart never.
|
||||
pub(crate) seen: usize,
|
||||
}
|
||||
|
||||
@@ -230,37 +230,37 @@ impl RenderOnce for CandlestickChart {
|
||||
..frame
|
||||
};
|
||||
let total = self.candles.len();
|
||||
let held = match &self.sync {
|
||||
Some(sync) => (sync.read(cx).visible, sync.read(cx).hover),
|
||||
None => (stage.read(cx).visible, stage.read(cx).hover),
|
||||
};
|
||||
let seen = match &self.sync {
|
||||
Some(sync) => sync.read(cx).seen,
|
||||
None => stage.read(cx).seen,
|
||||
let own = stage.read(cx).seen;
|
||||
let (held, shared) = match &self.sync {
|
||||
Some(sync) => {
|
||||
let sync = sync.read(cx);
|
||||
((sync.visible, sync.hover), sync.seen)
|
||||
}
|
||||
None => ((stage.read(cx).visible, stage.read(cx).hover), own),
|
||||
};
|
||||
let fresh = total.saturating_sub(own).min(total.saturating_sub(shared));
|
||||
let visible = match held.0 {
|
||||
Some(visible)
|
||||
if total > seen
|
||||
&& seen > 0
|
||||
&& visible.start + visible.count >= seen as f64 - 0.5 =>
|
||||
if fresh > 0
|
||||
&& own > 0
|
||||
&& visible.start + visible.count >= (total - fresh) as f64 - 0.5 =>
|
||||
{
|
||||
visible.pan((total - seen) as f64, total)
|
||||
visible.pan(fresh as f64, total)
|
||||
}
|
||||
Some(visible) => visible.fitted(total),
|
||||
None => Visible::latest(total, f64::from(main.w / pixels(sizes.candle)).max(1.0)),
|
||||
};
|
||||
if seen != total {
|
||||
log::debug!("market chart: {seen} candles to {total}");
|
||||
let kept = held.0.map(|_| visible);
|
||||
if own != total {
|
||||
log::debug!("market chart: {own} candles to {total}");
|
||||
let shrunk = total < own;
|
||||
let kept = held.0.filter(|_| fresh > 0 || shrunk).map(|_| visible);
|
||||
stage.update(cx, |stage, _| stage.seen = total);
|
||||
match &self.sync {
|
||||
Some(sync) => sync.update(cx, |sync, _| {
|
||||
sync.seen = total;
|
||||
sync.seen = if shrunk { total } else { sync.seen.max(total) };
|
||||
sync.visible = kept.or(sync.visible);
|
||||
}),
|
||||
None => stage.update(cx, |stage, _| {
|
||||
stage.seen = total;
|
||||
stage.visible = kept.or(stage.visible);
|
||||
}),
|
||||
None => stage.update(cx, |stage, _| stage.visible = kept.or(stage.visible)),
|
||||
}
|
||||
}
|
||||
let range = visible.range(total);
|
||||
|
||||
@@ -104,13 +104,17 @@ pub(crate) fn fit(low: f64, high: f64) -> ((f64, f64), Vec<f64>) {
|
||||
((low, high), inside)
|
||||
}
|
||||
|
||||
/// Digits kept past a step's first; float noise sits near the sixteenth.
|
||||
const DIGITS: usize = 6;
|
||||
|
||||
/// Places after the point that show every multiple of `step` exactly: none for whole steps, four for 0.0025.
|
||||
pub(crate) fn decimals(step: f64) -> usize {
|
||||
assert!(
|
||||
step.is_finite() && step > 0.0,
|
||||
"a step is positive, not {step}"
|
||||
);
|
||||
let text = format!("{step:.10}");
|
||||
let leading = (-step.log10().floor()).max(0.0) as usize;
|
||||
let text = format!("{step:.*}", leading + DIGITS);
|
||||
let fraction = text.split_once('.').map_or("", |(_, fraction)| fraction);
|
||||
fraction.trim_end_matches('0').len()
|
||||
}
|
||||
@@ -207,5 +211,6 @@ mod tests {
|
||||
1,
|
||||
"float noise from a difference drops away"
|
||||
);
|
||||
assert_eq!(decimals(1e-11), 11, "a tiny step keeps its places");
|
||||
}
|
||||
}
|
||||
+52
-10
@@ -2,7 +2,7 @@ use std::rc::Rc;
|
||||
|
||||
use gpui::{
|
||||
AppContext as _, Context, Entity, IntoElement, ParentElement, Render, Styled, TestAppContext,
|
||||
Window, div, px,
|
||||
VisualTestContext, Window, div, px,
|
||||
};
|
||||
use jiff::Timestamp;
|
||||
|
||||
@@ -21,7 +21,8 @@ fn candles(count: usize) -> Rc<Vec<Candle>> {
|
||||
}
|
||||
|
||||
struct Pair {
|
||||
candles: Rc<Vec<Candle>>,
|
||||
first: Rc<Vec<Candle>>,
|
||||
second: Rc<Vec<Candle>>,
|
||||
sync: Entity<ChartSync>,
|
||||
}
|
||||
|
||||
@@ -29,18 +30,19 @@ impl Render for Pair {
|
||||
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.w(px(800.))
|
||||
.child(CandlestickChart::new("first", self.candles.clone()).sync(&self.sync))
|
||||
.child(CandlestickChart::new("second", self.candles.clone()).sync(&self.sync))
|
||||
.child(CandlestickChart::new("first", self.first.clone()).sync(&self.sync))
|
||||
.child(CandlestickChart::new("second", self.second.clone()).sync(&self.sync))
|
||||
}
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn synced_charts_move_their_shared_window_once_per_append(cx: &mut TestAppContext) {
|
||||
/// Two synced charts of 40 candles, their shared window held on the newest 20.
|
||||
fn held(cx: &mut TestAppContext) -> (Entity<Pair>, Entity<ChartSync>, &mut VisualTestContext) {
|
||||
cx.update(Theme::init);
|
||||
let sync = cx.new(|_| ChartSync::default());
|
||||
let shared = sync.clone();
|
||||
let (view, cx) = cx.add_window_view(|_, _| Pair {
|
||||
candles: candles(40),
|
||||
first: candles(40),
|
||||
second: candles(40),
|
||||
sync: shared,
|
||||
});
|
||||
cx.run_until_parked();
|
||||
@@ -53,14 +55,54 @@ fn synced_charts_move_their_shared_window_once_per_append(cx: &mut TestAppContex
|
||||
});
|
||||
view.update(cx, |_, cx| cx.notify());
|
||||
cx.run_until_parked();
|
||||
(view, sync, cx)
|
||||
}
|
||||
|
||||
fn start(sync: &Entity<ChartSync>, cx: &mut VisualTestContext) -> f64 {
|
||||
sync.read_with(cx, |sync, _| sync.visible.expect("held").start)
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn synced_charts_move_their_shared_window_once_per_append(cx: &mut TestAppContext) {
|
||||
let (view, sync, cx) = held(cx);
|
||||
view.update(cx, |pair, cx| {
|
||||
pair.candles = candles(45);
|
||||
pair.first = candles(45);
|
||||
pair.second = candles(45);
|
||||
cx.notify();
|
||||
});
|
||||
cx.run_until_parked();
|
||||
let start = sync.read_with(cx, |sync, _| sync.visible.expect("held").start);
|
||||
assert_eq!(
|
||||
start, 25.0,
|
||||
start(&sync, cx),
|
||||
25.0,
|
||||
"five new candles move the shared window five, not ten"
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_lagging_chart_leaves_the_shared_window_alone(cx: &mut TestAppContext) {
|
||||
let (view, sync, cx) = held(cx);
|
||||
view.update(cx, |pair, cx| {
|
||||
pair.second = candles(45);
|
||||
cx.notify();
|
||||
});
|
||||
cx.run_until_parked();
|
||||
for _ in 0..2 {
|
||||
view.update(cx, |_, cx| cx.notify());
|
||||
cx.run_until_parked();
|
||||
}
|
||||
assert_eq!(
|
||||
(start(&sync, cx), sync.read_with(cx, |sync, _| sync.seen)),
|
||||
(25.0, 45),
|
||||
"redraws while the first chart lags move nothing"
|
||||
);
|
||||
view.update(cx, |pair, cx| {
|
||||
pair.first = candles(45);
|
||||
cx.notify();
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert_eq!(
|
||||
start(&sync, cx),
|
||||
25.0,
|
||||
"the lagging chart's append is already shown"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user