Files
benilla-octo/crates/benilla-ui/src/script/layout_cache.rs
T
Sam SungandClaude Opus 5.5 fd386e75ae Comments in benilla-ui: one line of fact each, no history, no record numbers (#143)
Part of #41: the comments of `benilla-ui` (the Lua UI engine, its FrameXML loader and its widgets) are rewritten to the rule in `docs/METHOD.md`, which is what the code does and the reference fact behind it, in a line. 43,140 comment lines become 15,161.

- **The mechanical strip.** The citations that were only a private record number go first, and each edit is shown to be a pure removal.
- **Wrong facts corrected.** The rewrite corrects the comments that stated wrong facts. Among them:
  - `0x64a480` is the case-sensitive compare (a byte `repz cmpsb`), and `0x64a4c0` the case-insensitive one.
  - A frame's id sits at `+0xb0`, not `+0xb4`. `EnableKeyboard`/`IsKeyboardEnabled` are `0x776ec0`/`0x776f90`.
  - A region's unpinned edge has no fallback to its owner's rect: the owner supplies only the scale.
  - `$parent` expands to the nearest named ancestor, or "Top" when none is named (`0x76c5b0`), not simply to the parent.
  - A StatusBar fills from its internal normalised fraction, not from `GetValue()`.
  - The addresses of `StartDuel` and `StartDuelUnit` were swapped.
  - Many "untraced" notes now give the traced reference fact: the drag threshold, the per-grip sizing edges, the empty-`autoFocus` skip, and `SetTexCoordModifiesRect`'s default.
  - Stock FrameXML line citations are checked against the stock 1.12 files and corrected. `ref-` file names become the stock file names.
- **Deviation labels.** "Deviation:" now marks only a choice with a reason. A surface that exists only for the retired Era addon target says so plainly: it is not 1.12.
- **Verification.** Every file strips to identical code, and `cargo fmt --check` is clean. Nine second readers compared every file old against new, restored the facts the rewrite had dropped, and checked each new citation against the code, the stock files, vmangos and the reference.

Behaviour differences the rewrite surfaced are filed as their own issues (#131–#142), and more are being validated. The comments here state what the code does. String literals are untouched; they are #92.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 01:58:49 +07:00

172 lines
7.0 KiB
Rust

//! The layout cache: the reference writes a per-character `layout-cache.txt` at logout and seats
//! the user-placed frames from it at load (userPlaced is `frame+0xb4 & 0x1000`). This is the
//! engine half, a snapshot out, a restore in and a dirty bit; the app owns the file
//! (`benilla_app::ui_layout`).
//!
//! A saved frame carries its name, size and whole anchor list. An anchor's target is the screen
//! root (saved as no target, the `nil` `GetPoint` answers there) or a named frame; a frame anchored
//! to anything else is skipped whole, never half-restored.
use crate::layout::Anchor;
use crate::widget::FrameHandle;
use super::object::{point_from_str, point_name};
use super::{Model, SCREEN};
/// One anchor of a saved frame, in the spelling `GetPoint`/`SetPoint` use.
#[derive(Clone, Debug, PartialEq)]
pub struct LayoutPoint {
/// The point on the saved frame (`"BOTTOMLEFT"`, …).
pub point: String,
/// The target frame's name; `None` is the screen root, which `GetPoint` reports as `nil`.
pub relative_to: Option<String>,
/// The point on the target.
pub relative_point: String,
/// `CAnchor+0x4`, in the frame's own pre-scale units.
pub x: f32,
/// `CAnchor+0x8`.
pub y: f32,
}
/// One user-placed frame's persisted geometry.
#[derive(Clone, Debug, PartialEq)]
pub struct FrameLayout {
/// The frame's global name, which addresses the row across sessions.
pub name: String,
/// The frame's authored width (`LayoutInput::width`), which a resize drag writes.
pub width: f32,
/// The frame's authored height.
pub height: f32,
/// Every anchor the frame carries, in `GetPoint` order.
pub points: Vec<LayoutPoint>,
}
impl super::UiScript {
/// Snapshot, sorted by name so the file is stable, every frame the reference's writer
/// (`0x490e60`) writes: a non-empty name (`0x490e79`, `0x490e82`), `userPlaced` (`0x490e8e`,
/// `0x1000`) and `movable` or `resizable` (`0x490e97`, `0x100|0x200`), with every anchor
/// nameable. Every drag stamps `userPlaced` (`0x7652b0` at `0x7652e5`), so the last test is
/// what drops the row of a stock frame an addon once moved, once that addon is gone.
pub fn user_placed_layouts(&self) -> Vec<FrameLayout> {
let model = self.model_ref();
let mut out: Vec<FrameLayout> = model
.arena
.iter_frames()
.filter(|(_, f)| f.user_placed && (f.movable || f.resizable))
.filter_map(|(h, f)| {
let name = f.name.clone()?;
let input = model.layout_inputs.get(&h)?;
let points = input
.anchors
.iter()
.map(|a| saved_point(&model, a))
.collect::<Option<Vec<_>>>()?;
Some(FrameLayout {
name,
width: input.width,
height: input.height,
points,
})
})
.collect();
out.sort_by(|a, b| a.name.cmp(&b.name));
out
}
/// Seat saved geometry back onto the frames that exist, each row whole or not at all. The
/// apply is per arm (`0x4905e0`): the position needs `movable` (`0x490600`) and anchors (the
/// no-position bail, `0x490613`), the size needs `resizable` (`0x490689`) and a non-zero size
/// (`0x49069a`), and each arm that applies stamps `userPlaced` (`0x49067e`, `0x490706`), which
/// keeps `UIParent_ManageFramePositions` off the frame. Marks nothing dirty: the values came
/// from the file.
pub fn restore_user_placed_layouts(&mut self, layouts: impl IntoIterator<Item = FrameLayout>) {
let mut model = self.model_mut();
for l in layouts {
let Some(h) = model.arena.lookup(&l.name) else {
continue; // a window this build does not have
};
let mut anchors: Vec<Anchor> = Vec::with_capacity(l.points.len());
let mut usable = true;
for p in &l.points {
// `frame_id` mints the id lazily, so a target nothing has anchored to yet resolves.
let rel = match &p.relative_to {
None => Some(SCREEN),
Some(n) => model.arena.lookup(n).map(|t| model.frame_id(t)),
};
match (
point_from_str(&p.point),
rel,
point_from_str(&p.relative_point),
) {
(Some(point), Some(rel), Some(rp)) => {
anchors.push(Anchor::new(point, rel, rp, p.x, p.y));
}
_ => {
usable = false;
break;
}
}
}
if !usable {
continue;
}
// The two arm gates, read before the layout input is borrowed mutably.
let Some((movable, resizable)) = model.arena.frame(h).map(|f| (f.movable, f.resizable))
else {
continue;
};
let seat_position = movable && !anchors.is_empty();
let seat_size = resizable && (l.width != 0.0 || l.height != 0.0);
if !seat_position && !seat_size {
continue;
}
let Some(input) = model.layout_inputs.get_mut(&h) else {
continue;
};
let mut changed = false;
if seat_position {
changed |= input.anchors != anchors;
input.anchors = anchors;
}
if seat_size {
changed |= input.width.to_bits() != l.width.to_bits()
|| input.height.to_bits() != l.height.to_bits();
input.width = l.width;
input.height = l.height;
}
// Only an arm that applied stamps, so a row neither arm took falls out of the file.
if seat_position || seat_size {
if let Some(f) = model.arena.frame_mut(h) {
f.user_placed = true;
}
}
if changed {
// The whole graph, not the frame: a restored row may repoint an anchor.
model.touch_layout();
}
}
}
/// Whether a user-placed frame moved or resized since the last call: the host's cue to persist.
pub fn take_user_placed_change(&mut self) -> bool {
std::mem::take(&mut self.model_mut().user_placed_changed)
}
}
/// An anchor's saved form, or `None` when its target is an unnamed frame or a region.
fn saved_point(model: &Model, a: &Anchor) -> Option<LayoutPoint> {
let relative_to = if a.relative_to == SCREEN {
None
} else {
let h: FrameHandle = model.id_to_frame.get(&a.relative_to).copied()?;
Some(model.arena.frame(h)?.name.clone()?)
};
Some(LayoutPoint {
point: point_name(a.point).to_owned(),
relative_to,
relative_point: point_name(a.relative_point).to_owned(),
x: a.x_off,
y: a.y_off,
})
}