Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions native/gpui/src/art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,16 @@ impl Cats {
pub const CAT_STEP: std::time::Duration = std::time::Duration::from_millis(33);
/// Ask the view a moving cat is drawn in to draw again in CAT_STEP; one wait per view, however many cats it has.
fn cat_tick(window: &mut Window, cx: &mut App) {
thread_local! { static WAITING: std::cell::RefCell<std::collections::HashSet<EntityId>> = Default::default(); }
let view = window.current_view();
thread_local! { static WAITING: std::cell::RefCell<std::collections::HashSet<Option<EntityId>>> = Default::default(); }
let view = window.try_current_view();
if !WAITING.with(|w| w.borrow_mut().insert(view)) { return }
window.spawn(cx, async move |cx| {
cx.background_executor().timer(CAT_STEP).await;
WAITING.with(|w| w.borrow_mut().remove(&view));
let _ = cx.update(|_, cx| cx.notify(view));
let _ = cx.update(|window, cx| match view {
Some(v) => cx.notify(v),
None => window.refresh(),
});
}).detach();
}
/// A cat, `size` px square. Only a moving mood asks for another frame (at CAT_STEP), so an idle window never repaints.
Expand Down
116 changes: 115 additions & 1 deletion native/gpui/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl Backplane {
window.on_next_frame(move |_, cx| {
let _ = app_this.update(cx, |v, cx| v.app_opened(cx));
});
window.request_animation_frame();
window.refresh();
});
match window.gpu_specs() {
Some(g) => metric(started, "gpu", json!({"device": g.device_name, "driver": g.driver_name, "driver_info": g.driver_info, "software": g.is_software_emulated})),
Expand Down Expand Up @@ -1505,4 +1505,118 @@ mod tests {
assert_eq!(rail_row_state(&v, "gerbers"), Some("hidden"));
assert_eq!(rail_row_state(&json!({}), "board"), None);
}
fn strip_rust_comments(src: &str, keep_strings: bool) -> String {
let mut out = String::with_capacity(src.len());
let bytes = src.as_bytes();
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'/' && bytes.get(i + 1) == Some(&b'/') {
while i < bytes.len() && bytes[i] != b'\n' { i += 1; }
continue;
}
if bytes[i] == b'/' && bytes.get(i + 1) == Some(&b'*') {
let mut depth = 1usize;
i += 2;
while i < bytes.len() && depth > 0 {
if bytes[i] == b'/' && bytes.get(i + 1) == Some(&b'*') {
depth += 1; i += 2;
} else if bytes[i] == b'*' && bytes.get(i + 1) == Some(&b'/') {
depth -= 1; i += 2;
} else {
if bytes[i] == b'\n' { out.push('\n'); }
i += 1;
}
}
out.push(' ');
continue;
}
if bytes[i] == b'\'' {
if bytes.get(i + 1) == Some(&b'\\') && bytes.get(i + 3) == Some(&b'\'') {
if keep_strings { out.push_str(&src[i..i + 4]); }
i += 4;
continue;
}
if let Some(ch) = src[i + 1..].chars().next()
&& ch != '\\' && ch != '\n' && ch != '\r'
&& bytes.get(i + 1 + ch.len_utf8()) == Some(&b'\'')
{
let end = i + 1 + ch.len_utf8() + 1;
if keep_strings { out.push_str(&src[i..end]); }
i = end;
continue;
}
}
if bytes[i] == b'r' {
let raw_prefix = i == 0
|| !(bytes[i - 1].is_ascii_alphanumeric() || bytes[i - 1] == b'_')
|| ((bytes[i - 1] == b'b' || bytes[i - 1] == b'c')
&& (i == 1 || !(bytes[i - 2].is_ascii_alphanumeric() || bytes[i - 2] == b'_')));
if raw_prefix {
let mut hashes = 0usize;
while bytes.get(i + 1 + hashes) == Some(&b'#') { hashes += 1; }
if bytes.get(i + 1 + hashes) == Some(&b'"') {
let start = i;
i += 1 + hashes + 1;
while i < bytes.len() {
if bytes[i] == b'"' && (0..hashes).all(|k| bytes.get(i + 1 + k) == Some(&b'#')) {
i += 1 + hashes;
break;
}
i += 1;
}
if keep_strings { out.push_str(&src[start..i]); } else { out.push_str("\"\""); }
continue;
}
}
}
if bytes[i] == b'"' {
let start = i;
i += 1;
let mut escape = false;
while i < bytes.len() {
if escape { escape = false; i += 1; }
else if bytes[i] == b'\\' { escape = true; i += 1; }
else if bytes[i] == b'"' { i += 1; break; }
else { i += 1; }
}
if keep_strings { out.push_str(&src[start..i]); } else { out.push_str("\"\""); }
continue;
}
let ch = src[i..].chars().next().unwrap();
out.push(ch);
i += ch.len_utf8();
}
out
}
#[test] fn no_unwrapped_current_view_calls_in_client_src() {
assert!(!strip_rust_comments(r##"let a = '"'; let b = '\"'; let c = r#"// ok"#; // x.current_view()"##, false).contains("current_view"));
assert!(strip_rust_comments("let a = '\"'; x.current_view();", false).contains("current_view"));
let needle = concat!(".current_", "view(");
let mut todo = vec![std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src")];
let mut hits = Vec::new();
while let Some(d) = todo.pop() {
for e in std::fs::read_dir(&d).unwrap().flatten() {
let p = e.path();
if p.is_dir() { todo.push(p); continue; }
if p.extension().is_none_or(|x| x != "rs") { continue; }
let text = std::fs::read_to_string(&p).unwrap();
let code = text.split("\n#[cfg(test)]\nmod ").next().unwrap_or(&text);
let stripped = strip_rust_comments(code, false);
if stripped.contains(needle) {
hits.push(p.display().to_string());
}
}
}
assert!(hits.is_empty(), "client code must use try_current_view() instead of panicking current_view(): {hits:?}");
}
#[test] fn first_frame_callback_does_not_call_request_animation_frame() {
let text = include_str!("script.rs");
let code = text.split("\n#[cfg(test)]\nmod ").next().unwrap();
let stripped = strip_rust_comments(code, true);
let start = stripped.find("metric(started, \"first_frame\"").expect("first_frame metric");
let end = stripped[start..].find("match window.gpu_specs()").expect("gpu_specs after first_frame") + start;
let block = &stripped[start..end];
assert!(!block.contains("request_animation_frame"), "first_frame on_next_frame callback must not call request_animation_frame():\n{block}");
assert!(block.contains("window.refresh()"), "first_frame on_next_frame callback should call window.refresh():\n{block}");
}
}
1 change: 1 addition & 0 deletions native/gpui/vendor/gpui/PATCH.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ gpui 0.2.2 (Apache-2.0) vendored without `examples/` (and its `[[example]]` targ
- `src/window.rs`: `DispatchEventResult` is `pub` (was `pub(crate)`), so the client's test-mode `pointer` script step can call the public
`Window::dispatch_event` with a mouse move, down and up at a control's rect (scripted e2e only; no behaviour change).
- `src/audit.rs`, hooks in window.rs, elements/text.rs, elements/div.rs: the layout audit (records painted text, named divs, opaque quads; off unless enabled), for the size-matrix e2e.
- `src/window.rs` & `src/view.rs`: added `Window::try_current_view()` and updated `Window::request_animation_frame()` and `Window::use_asset()` to fall back to `window.refresh()` when called outside a rendered view (e.g., from an `on_next_frame` callback where `rendered_entity_stack` is empty), matching `request_animation_frame`'s doc contract instead of panicking in `current_view()`. Wrapped `Entity<V>::request_layout` in `window.with_rendered_view` so `view.render` sees its view ID like `AnyView::request_layout`.
11 changes: 6 additions & 5 deletions native/gpui/vendor/gpui/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ impl<V: Render> Element for Entity<V> {
window: &mut Window,
cx: &mut App,
) -> (LayoutId, Self::RequestLayoutState) {
let mut element = self.update(cx, |view, cx| view.render(window, cx).into_any_element());
let layout_id = window.with_rendered_view(self.entity_id(), |window| {
element.request_layout(window, cx)
});
(layout_id, element)
window.with_rendered_view(self.entity_id(), |window| {
let mut element =
self.update(cx, |view, cx| view.render(window, cx).into_any_element());
let layout_id = element.request_layout(window, cx);
(layout_id, element)
})
}

fn prepaint(
Expand Down
22 changes: 17 additions & 5 deletions native/gpui/vendor/gpui/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1679,8 +1679,11 @@ impl Window {
///
/// If called from within a view, it will notify that view on the next frame. Otherwise, it will refresh the entire window.
pub fn request_animation_frame(&self) {
let entity = self.current_view();
self.on_next_frame(move |_, cx| cx.notify(entity));
if let Some(entity) = self.try_current_view() {
self.on_next_frame(move |_, cx| cx.notify(entity));
} else {
self.on_next_frame(|window, _| window.refresh());
}
}

/// Spawn the future returned by the given closure on the application thread pool.
Expand Down Expand Up @@ -2546,14 +2549,18 @@ impl Window {
let (task, is_first) = cx.fetch_asset::<A>(source);
task.clone().now_or_never().or_else(|| {
if is_first {
let entity_id = self.current_view();
let entity_id = self.try_current_view();
self.spawn(cx, {
let task = task.clone();
async move |cx| {
task.await;

cx.on_next_frame(move |_, cx| {
cx.notify(entity_id);
cx.on_next_frame(move |window, cx| {
if let Some(entity_id) = entity_id {
cx.notify(entity_id);
} else {
window.refresh();
}
});
}
})
Expand Down Expand Up @@ -3423,6 +3430,11 @@ impl Window {
self.next_frame.dispatch_tree.set_view_id(view_id);
}

/// Get the entity ID for the currently rendering view, if inside a view's draw pass.
pub fn try_current_view(&self) -> Option<EntityId> {
self.rendered_entity_stack.last().copied()
}

/// Get the entity ID for the currently rendering view
pub fn current_view(&self) -> EntityId {
self.invalidator.debug_assert_paint_or_prepaint();
Expand Down
Loading