refactor(engine): audit and rationalize platform cfg gates (closes #49)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
funman300
2026-05-27 18:00:57 -07:00
parent 561395fca6
commit ce536b0176
13 changed files with 302 additions and 243 deletions
+66 -88
View File
@@ -41,6 +41,7 @@ use crate::game_plugin::GameMutation;
#[cfg(target_os = "android")]
use crate::input_plugin::TouchDragSet;
use crate::layout::LayoutSystem;
use crate::platform::{SHOW_KEYBOARD_ACCELERATORS, USE_TOUCH_UI_LAYOUT};
#[cfg(target_os = "android")]
use crate::pause_plugin::PausedResource;
use crate::resources::GameStateResource;
@@ -140,9 +141,8 @@ pub struct HudColumn;
#[derive(Component, Debug)]
pub struct HudActionBar;
/// Marker on the text node inside each action-bar button (Android only).
/// Marker on the text node inside each touch-layout action-bar button.
/// Used by `resize_action_bar_labels` to update font size on window resize.
#[cfg(target_os = "android")]
#[derive(Component, Debug)]
struct ActionButtonLabel;
@@ -309,6 +309,23 @@ pub struct HintButton;
#[cfg(target_os = "android")]
pub(crate) const ANDROID_HINT_LABEL: &str = "!";
#[cfg(target_os = "android")]
const ACTION_BAR_LABELS: [&str; 7] = ["\u{2261}", "\u{2190}", "||", "?", ANDROID_HINT_LABEL, "M", "+"];
#[cfg(not(target_os = "android"))]
const ACTION_BAR_LABELS: [&str; 7] = ["Menu \u{2193}", "Undo", "Pause", "Help", "Hint", "Modes \u{2193}", "New Game"];
#[cfg(target_os = "android")]
const ACTION_BAR_COLUMN_GAP: Val = Val::Px(4.0);
#[cfg(not(target_os = "android"))]
const ACTION_BAR_COLUMN_GAP: Val = VAL_SPACE_2;
#[cfg(target_os = "android")]
const ACTION_POPOVER_BOTTOM_PX: f32 = 200.0;
#[cfg(not(target_os = "android"))]
const ACTION_POPOVER_BOTTOM_PX: f32 = 80.0;
#[cfg(target_os = "android")]
const HINT_WON_MSG: &str = "Game won! Tap New Game to play again";
#[cfg(not(target_os = "android"))]
const HINT_WON_MSG: &str = "Game won! Press N for a new game";
/// Marker on the "Modes" action button. Click toggles the [`ModesPopover`]
/// (a small dropdown panel) below the action bar. Each popover row starts
/// the corresponding game mode.
@@ -857,53 +874,13 @@ fn spawn_action_buttons(
windows: Query<&Window>,
mut commands: Commands,
) {
// On Android the glyph labels must scale with the viewport so they remain
// legible on any screen density. Use the window width at startup; the
// resize_action_bar_labels system keeps this current on window changes.
#[cfg(target_os = "android")]
let action_font_size = {
let w = windows.iter().next().map_or(900.0, |win| win.width());
action_bar_font_size(w)
};
#[cfg(not(target_os = "android"))]
let action_font_size = TYPE_BODY;
#[cfg(not(target_os = "android"))]
let _windows = windows;
let action_font_size = action_bar_font_size(windows.iter().next().map_or(900.0, |win| win.width()));
let font = TextFont {
font: font_res.as_ref().map(|f| f.0.clone()).unwrap_or_default(),
font_size: action_font_size,
..default()
};
// On Android, compact Unicode symbols fit all 7 buttons in one row.
// On desktop, keep the descriptive text labels.
#[cfg(target_os = "android")]
let col_gap = Val::Px(4.0);
#[cfg(not(target_os = "android"))]
let col_gap = VAL_SPACE_2;
#[cfg(target_os = "android")]
let labels = (
/* menu */ "\u{2261}", // ≡ identical-to (Arrows/Math-Op, confirmed FiraMono)
/* undo */ "\u{2190}", // ← leftwards arrow (Arrows block, confirmed FiraMono)
/* pause */ "||", // || ASCII double-pipe — ‖ (U+2016) absent from FiraMono
/* help */ "?",
/* hint */ ANDROID_HINT_LABEL,
/* modes */ "M", // plain ASCII — U+21BB and U+21C4 both render as tofu on FiraMono
/* new */ "+",
);
#[cfg(not(target_os = "android"))]
let labels = (
"Menu \u{2193}",
"Undo",
"Pause",
"Help",
"Hint",
"Modes \u{2193}",
"New Game",
);
// Bottom bar: full-width, centered, sits above the gesture-navigation zone.
// `SafeAreaAnchoredBottom` applies the correct logical-pixel inset once
// Android reports it (frames 1-3); initial value is 0.0.
@@ -917,7 +894,7 @@ fn spawn_action_buttons(
flex_direction: FlexDirection::Row,
flex_wrap: FlexWrap::Wrap,
justify_content: JustifyContent::Center,
column_gap: col_gap,
column_gap: ACTION_BAR_COLUMN_GAP,
row_gap: VAL_SPACE_2,
align_items: AlignItems::Center,
padding: UiRect {
@@ -938,13 +915,13 @@ fn spawn_action_buttons(
// so Tab cycles the action bar in visual reading order.
// Undo and Pause are the primary gameplay actions — full brightness.
// Menu, Help, Hint, Modes, New are navigation/utility — dimmed.
spawn_action_button(row, MenuButton, labels.0, None, "Open Stats, Achievements, Profile, Settings, or Leaderboard.", &font, 0, TEXT_SECONDARY);
spawn_action_button(row, UndoButton, labels.1, Some("U"), "Take back your last move. Costs points and blocks No Undo.", &font, 1, TEXT_PRIMARY);
spawn_action_button(row, PauseButton, labels.2, Some("Esc"), "Pause the game and freeze the timer.", &font, 2, TEXT_PRIMARY);
spawn_action_button(row, HelpButton, labels.3, Some("F1"), "Show controls, rules, and keyboard shortcuts.", &font, 3, TEXT_SECONDARY);
spawn_action_button(row, HintButton, labels.4, Some("H"), "Highlight a suggested move. Cycles through alternatives on repeat taps.", &font, 4, TEXT_SECONDARY);
spawn_action_button(row, ModesButton, labels.5, None, "Switch modes: Classic, Daily, Zen, Challenge, Time Attack.", &font, 5, TEXT_SECONDARY);
spawn_action_button(row, NewGameButton,labels.6, Some("N"), "Start a fresh deal. Confirms first if a game is in progress.", &font, 6, TEXT_SECONDARY);
spawn_action_button(row, MenuButton, ACTION_BAR_LABELS[0], None, "Open Stats, Achievements, Profile, Settings, or Leaderboard.", &font, 0, TEXT_SECONDARY);
spawn_action_button(row, UndoButton, ACTION_BAR_LABELS[1], Some("U"), "Take back your last move. Costs points and blocks No Undo.", &font, 1, TEXT_PRIMARY);
spawn_action_button(row, PauseButton, ACTION_BAR_LABELS[2], Some("Esc"), "Pause the game and freeze the timer.", &font, 2, TEXT_PRIMARY);
spawn_action_button(row, HelpButton, ACTION_BAR_LABELS[3], Some("F1"), "Show controls, rules, and keyboard shortcuts.", &font, 3, TEXT_SECONDARY);
spawn_action_button(row, HintButton, ACTION_BAR_LABELS[4], Some("H"), "Highlight a suggested move. Cycles through alternatives on repeat taps.", &font, 4, TEXT_SECONDARY);
spawn_action_button(row, ModesButton, ACTION_BAR_LABELS[5], None, "Switch modes: Classic, Daily, Zen, Challenge, Time Attack.", &font, 5, TEXT_SECONDARY);
spawn_action_button(row, NewGameButton, ACTION_BAR_LABELS[6], Some("N"), "Start a fresh deal. Confirms first if a game is in progress.", &font, 6, TEXT_SECONDARY);
});
}
@@ -973,25 +950,16 @@ fn spawn_action_button<M: Component>(
) {
// Hotkey hint chips ("U", "Esc", "F1", "N") are meaningless on a
// touch device — the button itself is the affordance — and they
// visibly clutter the narrow-viewport action row. Force the hint
// off on Android; the chevrons on Menu/Modes remain because they
// indicate dropdown behaviour and still apply on touch.
let hotkey = if cfg!(target_os = "android") { None } else { hotkey };
// visibly clutter the narrow-viewport action row. The chevrons on
// Menu/Modes remain because they indicate dropdown behaviour.
let hotkey = if SHOW_KEYBOARD_ACCELERATORS { hotkey } else { None };
let hotkey_font = TextFont {
font: font.font.clone(),
font_size: TYPE_CAPTION,
..default()
};
// On Android, use tighter padding and a slightly smaller min-size so all
// 7 icon-label buttons fit in one row on a ~411 dp phone. 44 dp ≥
// Apple's minimum touch target; padding of 4 dp each side keeps the icon
// centred with room to breathe. On desktop, keep the comfortable 48 dp
// floor and 8 dp side padding.
#[cfg(target_os = "android")]
let (pad, min_w, min_h) = (UiRect::axes(Val::Px(4.0), Val::Px(4.0)), Val::Px(52.0), Val::Px(44.0));
#[cfg(not(target_os = "android"))]
let (pad, min_w, min_h) = (UiRect::axes(VAL_SPACE_2, VAL_SPACE_2), Val::Px(48.0), Val::Px(48.0));
let (pad, min_w, min_h) = action_button_metrics();
row.spawn((
marker,
@@ -1017,10 +985,7 @@ fn spawn_action_button<M: Component>(
HighContrastBorder::with_default(BORDER_SUBTLE),
))
.with_children(|b| {
#[cfg(target_os = "android")]
b.spawn((ActionButtonLabel, Text::new(label), font.clone(), TextColor(text_color)));
#[cfg(not(target_os = "android"))]
b.spawn((Text::new(label), font.clone(), TextColor(text_color)));
spawn_action_button_label(b, label, font, text_color);
if let Some(key) = hotkey {
// Hotkey hint rendered as a dim caption next to the label —
// keeps the keyboard accelerator discoverable without
@@ -1096,11 +1061,7 @@ fn handle_hint_button(
}
let Some(ref g) = game else { return };
if g.0.is_won {
#[cfg(target_os = "android")]
let won_msg = "Game won! Tap New Game to play again";
#[cfg(not(target_os = "android"))]
let won_msg = "Game won! Press N for a new game";
info_toast.write(InfoToastEvent(won_msg.to_string()));
info_toast.write(InfoToastEvent(HINT_WON_MSG.to_string()));
return;
}
if let (Some(cfg), Some(hint)) = (solver_config.as_ref(), pending_hint.as_mut()) {
@@ -1195,10 +1156,7 @@ fn spawn_modes_popover(
// Popover opens upward from just above the bottom action bar.
// Use a platform-aware offset that clears the bar height + safe-area
// gesture zone on Android, and the flat bar height on desktop.
#[cfg(target_os = "android")]
let popover_bottom = Val::Px(200.0);
#[cfg(not(target_os = "android"))]
let popover_bottom = Val::Px(80.0);
let popover_bottom = Val::Px(ACTION_POPOVER_BOTTOM_PX);
commands
.spawn((
@@ -1393,10 +1351,7 @@ fn spawn_menu_popover(commands: &mut Commands, font_res: Option<&FontResource>)
];
// Same upward-opening placement as ModesPopover.
#[cfg(target_os = "android")]
let popover_bottom = Val::Px(200.0);
#[cfg(not(target_os = "android"))]
let popover_bottom = Val::Px(80.0);
let popover_bottom = Val::Px(ACTION_POPOVER_BOTTOM_PX);
commands
.spawn((
@@ -2511,14 +2466,37 @@ fn restore_hud_on_modal(
}
}
/// Returns the action-bar glyph font size for a given logical window width.
/// Scales linearly so glyphs remain legible at any phone density.
#[cfg(target_os = "android")]
/// Returns the action-bar label font size for a given logical window width.
fn action_bar_font_size(window_width: f32) -> f32 {
// ~1/40 of the window width gives ~22 px on a 900 logical-px phone.
// Clamped so it never goes too tiny on narrow viewports or too large
// on landscape tablets.
(window_width / 40.0).clamp(16.0, 30.0)
if USE_TOUCH_UI_LAYOUT {
// ~1/40 of the window width gives ~22 px on a 900 logical-px phone.
// Clamped so it never goes too tiny on narrow viewports or too large
// on landscape tablets.
(window_width / 40.0).clamp(16.0, 30.0)
} else {
TYPE_BODY
}
}
fn action_button_metrics() -> (UiRect, Val, Val) {
if USE_TOUCH_UI_LAYOUT {
(UiRect::axes(Val::Px(4.0), Val::Px(4.0)), Val::Px(52.0), Val::Px(44.0))
} else {
(UiRect::axes(VAL_SPACE_2, VAL_SPACE_2), Val::Px(48.0), Val::Px(48.0))
}
}
fn spawn_action_button_label(
parent: &mut ChildSpawnerCommands,
label: &str,
font: &TextFont,
text_color: Color,
) {
if USE_TOUCH_UI_LAYOUT {
parent.spawn((ActionButtonLabel, Text::new(label), font.clone(), TextColor(text_color)));
} else {
parent.spawn((Text::new(label), font.clone(), TextColor(text_color)));
}
}
/// Resizes the glyph text inside every [`ActionButtonLabel`] to match the