Add WithRemSize element (#11928)

This PR adds a new `WithRemSize` element to the `ui` crate.

This element can be used to create an element tree that has a different
rem size than the base window.

`WithRemSize` can be nested, allowing for subtrees that have a different
rem size than their parent and their children.

<img width="912" alt="Screenshot 2024-05-16 at 2 25 28 PM"
src="https://github.com/zed-industries/zed/assets/1486634/f599cd9f-c101-496b-93e8-06e570fbf74f">

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-05-16 14:37:55 -04:00 committed by GitHub
parent 13bbaf1e18
commit fdadbc7174
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 150 additions and 10 deletions

View file

@ -491,14 +491,11 @@ pub struct Window {
sprite_atlas: Arc<dyn PlatformAtlas>,
text_system: Arc<WindowTextSystem>,
rem_size: Pixels,
/// An override value for the window's rem size.
/// The stack of override values for the window's rem size.
///
/// This is used by `with_rem_size` to allow rendering an element tree with
/// a given rem size.
///
/// Note: Right now we only allow for a single override value at a time, but
/// this could likely be changed to be a stack of rem sizes.
rem_size_override: Option<Pixels>,
rem_size_override_stack: SmallVec<[Pixels; 8]>,
pub(crate) viewport_size: Size<Pixels>,
layout_engine: Option<TaffyLayoutEngine>,
pub(crate) root_view: Option<AnyView>,
@ -771,7 +768,7 @@ impl Window {
sprite_atlas,
text_system,
rem_size: px(16.),
rem_size_override: None,
rem_size_override_stack: SmallVec::new(),
viewport_size: content_size,
layout_engine: Some(TaffyLayoutEngine::new()),
root_view: None,
@ -1212,7 +1209,9 @@ impl<'a> WindowContext<'a> {
/// UI to scale, just like zooming a web page.
pub fn rem_size(&self) -> Pixels {
self.window
.rem_size_override
.rem_size_override_stack
.last()
.copied()
.unwrap_or(self.window.rem_size)
}
@ -1238,9 +1237,9 @@ impl<'a> WindowContext<'a> {
);
if let Some(rem_size) = rem_size {
self.window.rem_size_override = Some(rem_size.into());
self.window.rem_size_override_stack.push(rem_size.into());
let result = f(self);
self.window.rem_size_override.take();
self.window.rem_size_override_stack.pop();
result
} else {
f(self)