Skip to content

Commit 6f34369

Browse files
committed
The address bar is reconciled, not pushed — so the sidebar moves it too
Walking the sidebar left the URL behind, and the reason is the architecture rather than a missing line. What is shown can change four ways — a click on the sidebar, `j` in it, the row menu's "Go to", and the back button — and the last commit pushed the route from `Message::Select`, which covered three. The one it missed is how this window is actually driven: in the sidebar the cursor *is* the selection, so every `j` changes what is shown without going anywhere near `Select`. A rule that each call site has to remember is a rule that is already broken. So `update` is a wrapper now — it runs `step`, then `sync_route` — and `sync_route` is the only thing in this program that writes the address bar. Every path reconciles, including the several that `return` early out of `step` and the ones that recurse back into `update`. Nothing has to remember anything, which is the only version of this that stays true. **Walking the sidebar is one navigation, not forty.** Since the cursor is the selection, holding `j` through forty albums changes what is shown forty times, and forty history entries is a back button that needs forty presses to undo one scroll. `Nav::Replace` rewrites rather than adds, `show_under_cursor` is the only thing that sets it, and `sync_route` resets it every time so only the step that meant it gets it. The URL is right at every one of the forty — which is what "one to one" has to mean. Clean on all three: the desktop, the demo, and wasm32. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XcBd33WHuwVJf6eCw7CpeG
1 parent 2674f14 commit 6f34369

3 files changed

Lines changed: 104 additions & 27 deletions

File tree

‎CLAUDE.md‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,12 +1026,34 @@ every row is one line. Four things about it are load-bearing:
10261026
subscribe to. The tick already runs twenty times a second for the
10271027
transport, and reading `location.hash` on it is a string compare that
10281028
gives the same answer a frame later.
1029-
- **`pushState`, not `location.hash = …`.** Both change the URL; the second
1030-
also fires a `hashchange` the poll would read back as somebody pressing
1031-
the back button. And the push is skipped when the address bar already says
1032-
it, which is exactly the case where the route *came* from the back
1033-
button — pushing there would make one place two history entries and the
1034-
back button need pressing twice.
1029+
- **The history API, not `location.hash = …`.** Both change the URL; the
1030+
second also fires a `hashchange` the poll would read back as somebody
1031+
pressing the back button. And the write is skipped when the address bar
1032+
already says it, which is exactly the case where the route *came* from the
1033+
back button — writing there would make one place two history entries and
1034+
the back button need pressing twice.
1035+
- **The bar is reconciled, not pushed.** This is the part the first version
1036+
got wrong, and it is worth the paragraph. There are four ways what is
1037+
shown can change — a click on the sidebar, `j` in it, the row menu's
1038+
"Go to", and the back button — and pushing the route from `Message::Select`
1039+
covered three of them. The one it missed was the sidebar's own cursor,
1040+
which is how this window is actually driven: the cursor *is* the selection
1041+
there, so every `j` changes what is shown without going through `Select`
1042+
at all, and the URL silently stopped matching the screen.
1043+
1044+
So `update` is a wrapper now: it runs `step`, then `sync_route`, which is
1045+
the only thing in this program that writes the address bar. Every path
1046+
reconciles, including the several that `return` early out of `step` and
1047+
the ones that recurse back into `update`. A rule each call site has to
1048+
remember is a rule that is already broken; this one cannot be missed
1049+
because nothing has to remember it.
1050+
- **Walking the sidebar is one navigation, not forty.** Since the cursor is
1051+
the selection, holding `j` through forty albums changes what is shown
1052+
forty times — and forty history entries is a back button that needs forty
1053+
presses to undo one scroll. `Nav::Replace` says rewrite rather than add,
1054+
it is set by `show_under_cursor` alone, and `sync_route` resets it every
1055+
time so only the step that meant it gets it. The URL is still right at
1056+
every one of the forty.
10351057

10361058
`App::routed` is what stops the tick re-resolving the same fragment twenty
10371059
times a second, and a route is not consumed until the sidebar has something

‎iced/src/main.rs‎

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,17 @@ enum Message {
380380
Tick,
381381
}
382382

383+
/// How the next change to what is shown should reach the history.
384+
///
385+
/// Everything is a place you went, except walking the sidebar's cursor: that
386+
/// is one navigation however many rows it passes through, and recording each
387+
/// would leave a back button that needs forty presses to undo one scroll.
388+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
389+
enum Nav {
390+
Push,
391+
Replace,
392+
}
393+
383394
/// A row's menu: what the three dots open, and what a right click opens.
384395
///
385396
/// Held by id rather than by index, because the list under it can move while
@@ -710,6 +721,10 @@ struct App {
710721
/// other way. Seeded with what `main` asks for and kept in step by
711722
/// `window::resize_events`.
712723
window: iced::Size,
724+
/// Whether the next reconciliation adds to the history or rewrites it.
725+
/// Reset to [`Nav::Push`] every time, so only the step that meant
726+
/// otherwise is the one that gets it.
727+
nav: Nav,
713728
/// The last fragment this window acted on.
714729
///
715730
/// Without it the tick would re-resolve the same route twenty times a
@@ -1015,6 +1030,7 @@ impl App {
10151030
help: false,
10161031
picker: None,
10171032
menu: None,
1033+
nav: Nav::Push,
10181034
routed: None,
10191035
cursor: iced::Point::ORIGIN,
10201036
window: iced::Size::new(860.0, 600.0),
@@ -1041,6 +1057,7 @@ impl App {
10411057
help: false,
10421058
picker: None,
10431059
menu: None,
1060+
nav: Nav::Push,
10441061
routed: None,
10451062
cursor: iced::Point::ORIGIN,
10461063
window: iced::Size::new(860.0, 600.0),
@@ -1339,6 +1356,10 @@ impl App {
13391356
return;
13401357
}
13411358
peer.source = source;
1359+
// Moving onto a row *is* showing it here — there is no `<Enter>`
1360+
// in between — so the cursor walking down the sidebar changes what
1361+
// is shown on every step. One navigation, not one per row.
1362+
self.nav = Nav::Replace;
13421363
peer.reload_shown();
13431364
// A different list is a different row one, so the cursor goes back
13441365
// to the top rather than to wherever it happened to be.
@@ -1559,7 +1580,45 @@ impl App {
15591580
.unwrap_or_default()
15601581
}
15611582

1583+
/// Do the thing, then make the address bar agree with what is on screen.
1584+
///
1585+
/// The agreeing is *here* and not in the places that change what is shown,
1586+
/// which is the whole architecture: there are four of them — a click on
1587+
/// the sidebar, `j` in it, the row menu's "Go to", and the back button
1588+
/// itself — and the first version pushed the route from one of them. The
1589+
/// one it missed was the sidebar's own cursor, which is the way this
1590+
/// window is actually driven. A rule that every call site has to remember
1591+
/// is a rule that is already broken; this one cannot be missed, because
1592+
/// nothing has to remember it.
15621593
fn update(&mut self, message: Message) -> Task<Message> {
1594+
let task = self.step(message);
1595+
self.sync_route();
1596+
task
1597+
}
1598+
1599+
/// Where the address bar is made to agree. Nothing else writes it.
1600+
fn sync_route(&mut self) {
1601+
let Some(peer) = &self.peer else {
1602+
return;
1603+
};
1604+
let want = peer.source.route();
1605+
if self.routed.as_ref() == Some(&want) {
1606+
return;
1607+
}
1608+
// Skipped when the bar already says it, which is exactly the case
1609+
// where the route *came* from the back button: writing there would
1610+
// make one place two history entries.
1611+
if route::read().as_ref() != Some(&want) {
1612+
// The sidebar's cursor is a scrub, not forty places visited.
1613+
// Holding `j` through forty albums should leave the back button
1614+
// where it was, and leave the URL right the whole way down.
1615+
route::write(&want, self.nav == Nav::Push);
1616+
}
1617+
self.routed = Some(want);
1618+
self.nav = Nav::Push;
1619+
}
1620+
1621+
fn step(&mut self, message: Message) -> Task<Message> {
15631622
// An entry was picked: the menu has said everything it had to say.
15641623
// `OpenPicker` is not here because it reads the menu on its way out.
15651624
if matches!(message, Message::PlayItem(_) | Message::Select(_)) {
@@ -1790,16 +1849,6 @@ impl App {
17901849
// would carry on from wherever it was and the highlight
17911850
// would be somewhere the table is not.
17921851
Message::Select(source) => {
1793-
// Record it, unless the address bar is already saying
1794-
// it — which is the case when this *came* from the
1795-
// address bar, and pushing then would mean two history
1796-
// entries for one place and a back button that needs
1797-
// pressing twice.
1798-
let route = source.route();
1799-
if route::read().as_ref() != Some(&route) {
1800-
route::push(&route);
1801-
}
1802-
self.routed = Some(route);
18031852
let at = peer.choices.iter().position(|c| c.source == source);
18041853
peer.source = source;
18051854
peer.reload_shown();

‎iced/src/route.rs‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn decode(s: &str) -> String {
9898
}
9999

100100
#[cfg(target_arch = "wasm32")]
101-
pub use browser::{push, read};
101+
pub use browser::{read, write};
102102

103103
#[cfg(target_arch = "wasm32")]
104104
mod browser {
@@ -110,22 +110,28 @@ mod browser {
110110
(!hash.is_empty()).then(|| Route::parse(&hash))
111111
}
112112

113-
/// Put `route` in the address bar, as a new entry in the history.
113+
/// Put `route` in the address bar, as a new entry in the history or in
114+
/// place of the current one.
114115
///
115-
/// `pushState` rather than setting `location.hash`: the two do the same
116-
/// thing to the URL, and the second also fires a `hashchange` that the
117-
/// poll would read back as somebody pressing the back button. Writing
118-
/// through the history API leaves the page's own read of the hash to be
119-
/// the one it just wrote.
120-
pub fn push(route: &Route) {
116+
/// The history API rather than setting `location.hash`: the two do the
117+
/// same thing to the URL, and the second also fires a `hashchange` that
118+
/// the poll would read back as somebody pressing the back button. Writing
119+
/// through `history` leaves the page's own read of the hash to be the one
120+
/// it just wrote.
121+
pub fn write(route: &Route, keep: bool) {
121122
let Some(window) = web_sys::window() else {
122123
return;
123124
};
124125
let Ok(history) = window.history() else {
125126
return;
126127
};
127-
let _ =
128-
history.push_state_with_url(&wasm_bindgen::JsValue::NULL, "", Some(&route.fragment()));
128+
let url = Some(route.fragment());
129+
let null = wasm_bindgen::JsValue::NULL;
130+
let _ = if keep {
131+
history.push_state_with_url(&null, "", url.as_deref())
132+
} else {
133+
history.replace_state_with_url(&null, "", url.as_deref())
134+
};
129135
}
130136
}
131137

@@ -138,7 +144,7 @@ pub fn read() -> Option<Route> {
138144
}
139145

140146
#[cfg(not(target_arch = "wasm32"))]
141-
pub fn push(_route: &Route) {}
147+
pub fn write(_route: &Route, _keep: bool) {}
142148

143149
#[cfg(test)]
144150
mod tests {

0 commit comments

Comments
 (0)