mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-01 06:53:19 -10:00
raea's window: the page is painted at the display's scale, so a Retina window is sharp
v0.1.1 laid the window's page out at scale 1 and stretched its image over the window: on a Retina display every glyph and hairline was drawn at half the pixels (a bug report of 11-12: "UI renders at 1x on Retina", the blur and pixelation people noticed). page.rs size() now gives the page the window's scale factor along with its size in points, read each frame so a window moved to another display follows; headless, the image target's scale stands. RAEA_WINDOW_SCALE=2 opens the window at that scale on any display, to check it on a 1x Mac. tests/retina.rs covers the scaled window and the headless case.
This commit is contained in:
1 parent
be220a6064
commit
a78fb95e75
3 files changed
+62
-4
No files matched your search
@@ -78,17 +78,28 @@ pub fn mount(world: &mut World, m: Mount) -> Result<Entity, String> {
|
||||
Ok(page)
|
||||
}
|
||||
|
||||
/// The page at the window's size.
|
||||
pub fn size(win: Query<&Window, With<PrimaryWindow>>, page: Res<HostPage>, mut pages: Query<&mut raea_sys_page::Page>) {
|
||||
/// The page at the window's size in points, painted at the window's scale on screen: on a Retina display the page's
|
||||
/// image is twice the points each way and the document lays out with hidpi_scale 2, so text and hairlines are drawn
|
||||
/// at the display's pixels (v0.1.1 painted every window at 1x and stretched it: "UI renders at 1x on Retina"). The
|
||||
/// scale is read every frame, so a window dragged to another display follows it. Headless, the image target's scale.
|
||||
pub fn size(win: Query<&Window, With<PrimaryWindow>>, page: Res<HostPage>, target: Option<Res<crate::inject::Target>>, mut pages: Query<&mut raea_sys_page::Page>) {
|
||||
let Ok(w) = win.single() else { return };
|
||||
let want = UVec2::new(w.width().round() as u32, w.height().round() as u32);
|
||||
let scale = page_scale(target.as_deref().is_none_or(|t| t.windowed), w, target.as_deref().map_or(1.0, |t| t.scale));
|
||||
if let Ok(mut p) = pages.get_mut(page.0)
|
||||
&& p.size != want
|
||||
&& (p.size != want || p.scale != scale)
|
||||
{
|
||||
p.size = want;
|
||||
p.scale = scale;
|
||||
}
|
||||
}
|
||||
|
||||
/// The scale a page is painted at: the window's on screen (a Retina display's 2, a window's override), else the
|
||||
/// headless image's.
|
||||
pub fn page_scale(windowed: bool, win: &Window, headless: f32) -> f32 {
|
||||
if windowed { (win.scale_factor() as f32).max(0.5) } else { headless }
|
||||
}
|
||||
|
||||
/// The page's image on the node that shows it, once raea-page made it (and again when a resize remade it).
|
||||
pub fn show(page: Res<HostPage>, images: Query<&raea_sys_page::PageImage>, mut shows: Query<(Entity, Option<&mut ImageNode>), With<Shows>>, mut commands: Commands) {
|
||||
let Ok(img) = images.get(page.0) else { return };
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
//! The page the host shows is painted at the window's scale (page.rs size): on a Retina display its document lays out
|
||||
//! at hidpi_scale 2 and its image is twice the points each way, so text and hairlines land on the display's pixels.
|
||||
//! v0.1.1 left the page at scale 1 and stretched it over the window ("UI renders at 1x on Retina", a bug report of
|
||||
//! 2026-11-12). Headless, the image target's scale stands.
|
||||
use bevy::camera::{NormalizedRenderTarget, RenderTarget};
|
||||
use bevy::ecs::system::RunSystemOnce;
|
||||
use bevy::prelude::*;
|
||||
use bevy::window::{PrimaryWindow, WindowRef, WindowResolution};
|
||||
use raea_page_host::inject::Target;
|
||||
use raea_page_host::page::{self, HostPage};
|
||||
use raea_sys_page::Page;
|
||||
|
||||
fn app(scale: f32, windowed: bool) -> (App, Entity) {
|
||||
let mut app = App::new();
|
||||
app.add_plugins(MinimalPlugins);
|
||||
let window = app.world_mut().spawn((Window { resolution: WindowResolution::new((1440.0 * scale) as u32, (900.0 * scale) as u32).with_scale_factor_override(scale), ..default() }, PrimaryWindow)).id();
|
||||
let norm: NormalizedRenderTarget = RenderTarget::Window(WindowRef::Primary).normalize(Some(window)).unwrap();
|
||||
app.world_mut().insert_resource(Target { image: Handle::default(), norm, scale: 1.0, windowed });
|
||||
let page = app.world_mut().spawn(Page { size: UVec2::new(400, 300), scale: 1.0 }).id();
|
||||
app.world_mut().insert_resource(HostPage(page));
|
||||
(app, page)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn on_screen_the_page_takes_the_windows_scale_and_its_size_in_points() {
|
||||
let (mut app, page) = app(2.0, true);
|
||||
app.world_mut().run_system_once(page::size).unwrap();
|
||||
let p = *app.world().get::<Page>(page).unwrap();
|
||||
assert_eq!((p.size, p.scale), (UVec2::new(1440, 900), 2.0));
|
||||
// the window moved to a 1x display: the page follows
|
||||
app.world_mut().query_filtered::<&mut Window, With<PrimaryWindow>>().single_mut(app.world_mut()).unwrap().resolution.set_scale_factor_override(Some(1.0));
|
||||
app.world_mut().run_system_once(page::size).unwrap();
|
||||
assert_eq!(app.world().get::<Page>(page).unwrap().scale, 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn headless_the_page_keeps_the_images_scale() {
|
||||
let (mut app, page) = app(2.0, false);
|
||||
app.world_mut().run_system_once(page::size).unwrap();
|
||||
let p = *app.world().get::<Page>(page).unwrap();
|
||||
assert_eq!((p.size, p.scale), (UVec2::new(1440, 900), 1.0));
|
||||
}
|
||||
@@ -31,7 +31,12 @@ impl Chrome {
|
||||
/// The primary window, titled and sized, with this chrome's title bar.
|
||||
pub fn window(self, title: &str, size: UVec2) -> Window {
|
||||
let inset = self == Self::Inset;
|
||||
let resolution = bevy::window::WindowResolution::new(size.x, size.y);
|
||||
let mut resolution = bevy::window::WindowResolution::new(size.x, size.y);
|
||||
// RAEA_WINDOW_SCALE=2: the window at that scale whatever the display's, so a Retina window can be checked on a
|
||||
// 1x display (the page paints at the window's scale: raea-page-host page.rs size)
|
||||
if let Some(s) = std::env::var("RAEA_WINDOW_SCALE").ok().and_then(|s| s.parse::<f32>().ok()).filter(|s| *s > 0.0) {
|
||||
resolution = resolution.with_scale_factor_override(s);
|
||||
}
|
||||
Window { title: title.into(), resolution, titlebar_transparent: inset, titlebar_show_title: !inset, fullsize_content_view: inset, ..default() }
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user