state.rs: Make get_preferred_height_and_arrangement easier to understand

Part-of: <https://gitlab.gnome.org/World/Phosh/squeekboard/-/merge_requests/677>
This commit is contained in:
MoonlightWave-12
2024-09-18 15:34:06 +02:00
committed by Marge Bot
parent 8f73e1300e
commit 9a261acdfd

View File

@ -358,52 +358,51 @@ Outcome:
output.get_pixel_size() output.get_pixel_size()
.map(|px_size| { .map(|px_size| {
// Assume isotropy. // Assume isotropy.
// Pixels/mm. // Pixel / Millimeter
let density = output.get_physical_size() let pixel_density = output.get_physical_size()
.and_then(|size| size.width) .and_then(|size| size.width)
.map(|width| Rational { .map(|width| Rational {
numerator: px_size.width as i32, numerator: px_size.width as i32, // Pixel
denominator: width.0 as u32, denominator: width.0 as u32, // Millimeter
}) })
// Whatever the Librem 5 has, // Default to the pixel-density of the Librem 5 (~281 DPI).
// as a good default.
.unwrap_or(Rational { .unwrap_or(Rational {
numerator: 720, numerator: 720, // Pixel
denominator: 65, denominator: 65, // Millimeter
}); });
// Based on what works on the L5. // Based on what works well on the Librem 5.
// Exceeding that probably wastes space. Reducing makes typing harder. // Exceeding that, probably wastes space. Reducing it, makes typing harder.
const IDEAL_TARGET_SIZE: Rational<Millimeter> = Rational { const IDEAL_BUTTON_SIZE: Rational<Millimeter> = Rational {
numerator: Millimeter(948), numerator: Millimeter(948), // 9.48 mm, actually.
denominator: 100, denominator: 100, // Increase precision to 0.01 mm.
}; };
// TODO: calculate based on selected layout // TODO: Calculate this, based on the selected layout.
const ROW_COUNT: u32 = 4; const ROW_COUNT: u32 = 4;
let ideal_height = IDEAL_TARGET_SIZE * ROW_COUNT as i32; let ideal_panel_height = IDEAL_BUTTON_SIZE * ROW_COUNT as i32;
let ideal_height_px = (ideal_height * density).ceil().0 as u32; let ideal_panel_height_px = (ideal_panel_height * pixel_density).ceil().0 as u32;
// Changes the point at which the layout-shape is changed to the wide shape. // Changes the point at which the layout-shape is changed to the wide shape.
// Slightly higher aspect-ratio (16:5.1) than the expected aspect-ratio of the wide shape (16:5). // Slightly higher aspect-ratio (16:5.1) than the expected aspect-ratio of the wide shape (16:5).
// 5.1/16 = 1/3.14 = 172/540 (rounded, height / width) // 5.1/16 = 1/3.14 = 172/540 (rounded, height / width)
// FIXME: This should be 172/540, but it is currently used as a workaround to improve shape-selection. // FIXME: This should be 172/540, but it is currently used as a workaround to improve shape-selection.
// For more information about that, read https://gitlab.gnome.org/World/Phosh/squeekboard/-/merge_requests/639 . // For more information about that, read https://gitlab.gnome.org/World/Phosh/squeekboard/-/merge_requests/639 .
let max_wide_height = Rational { let aspect_ratio_wide = Rational {
numerator: 188, numerator: 188,
denominator: 540, denominator: 540,
}; };
let ideal_panel_height = Rational { let ideal_aspect_ratio = Rational {
numerator: ideal_height_px as i32, numerator: ideal_panel_height_px as i32,
denominator: px_size.width, denominator: px_size.width,
}; };
// Reduce height to match what the layout can fill. // Reduce height, to match what the layout can fill.
// For this, we need to guess if normal or wide will be picked. // For this, we need to guess if normal or wide will be picked.
// This must match `eek_gtk_keyboard.c::get_type`. // This must match `eek_gtk_keyboard.c::get_type`.
// TODO: query layout database and choose one directly // TODO: query layout database and choose one directly
let (arrangement, height_as_widths) = { let (arrangement, layout_aspect_ratio) = {
if max_wide_height < ideal_panel_height {( if aspect_ratio_wide < ideal_aspect_ratio {(
ArrangementKind::Base, ArrangementKind::Base,
Rational { Rational {
numerator: 210, numerator: 210,
@ -411,20 +410,20 @@ Outcome:
}, },
)} else {( )} else {(
ArrangementKind::Wide, ArrangementKind::Wide,
max_wide_height, aspect_ratio_wide,
)} )}
}; };
// Set the height of the space available for Squeekboard // Set the height of the space available for Squeekboard
let height let panel_height
= cmp::min( = cmp::min(
ideal_height_px, ideal_panel_height_px,
(height_as_widths * px_size.width as i32).ceil() as u32, (layout_aspect_ratio * px_size.width as i32).ceil() as u32,
); );
( (
PixelSize { PixelSize {
scale_factor: output.scale as u32, scale_factor: output.scale as u32,
pixels: cmp::min(height, px_size.height / 2), pixels: cmp::min(panel_height, px_size.height / 2),
}, },
arrangement, arrangement,
) )
@ -949,8 +948,6 @@ pub mod test {
#[test] #[test]
fn size_galaxy_tab_a_8_0_horizontal() {scaling_test_wide(1024, 768, 163, 122, 1, 239)} fn size_galaxy_tab_a_8_0_horizontal() {scaling_test_wide(1024, 768, 163, 122, 1, 239)}
#[test] #[test]
fn size_galaxy_tab_s2_9_7() {scaling_test_wide(1536, 2048, 148, 197, 2, 394)} fn size_galaxy_tab_s2_9_7() {scaling_test_wide(1536, 2048, 148, 197, 2, 394)}
#[test] #[test]
@ -1082,3 +1079,4 @@ pub mod test {
#[test] #[test]
fn size_ultrawide_monitor_horizontal() {scaling_test_wide(5120, 1440, 1198, 337, 1, 163)} fn size_ultrawide_monitor_horizontal() {scaling_test_wide(5120, 1440, 1198, 337, 1, 163)}
} }