8000 fix(reader): load a folded card's message only when it opens by gonzisdev · Pull Request #581 · hyprlab/hylki · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ui/message_view/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@ fn card_body(cards: &Cards<'_>, m: &Message) -> String {
zoom,
)
};
// A folded card hides its frame, but a hidden frame still loads and
// renders its whole document: a long conversation folded down to its
// newest card held every message in memory at once, and one of 83
// quoted replies took the app past 14 GB. Folded, the frame keeps its
// document aside and gets it when the card opens (`wake` in the
// wrapper script). The document is attribute-escaped, so the only
// `srcdoc="` in the markup is the frame's own.
if LIVE_FOLDED.with(|f| f.borrow().contains(&(m.account_id, m.id))) {
return body.replacen(" srcdoc=\"", " data-hylki-srcdoc=\"", 1);
}
body
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/message_view/folds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl MessageView {
for(var i=0;i<ms.length;i++){{var on=k.indexOf(ms[i].dataset.key)>=0;\
if(ms[i].classList.contains('hylki-folded')===on)continue;\
ms[i].classList.toggle('hylki-folded',on);\
if(!on){{var f=ms[i].querySelector('iframe.hylki-frame');if(f){{f._h=0;s(f);}}}}}}}})({})",
if(!on){{var f=ms[i].querySelector('iframe.hylki-frame');if(f){{wake(f);f._h=0;s(f);}}}}}}}})({})",
serde_json::to_string(&keys).unwrap_or_else(|_| "[]".into()),
);
self.run_js(&js);
Expand Down
4 changes: 3 additions & 1 deletion src/ui/message_view/size_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@ window.scrollTo(0,sy);\
setTimeout(function(){f.classList.remove('anim');s(f);\
if(on){var br=b.getBoundingClientRect();\
if(br.top>innerHeight*0.6)window.scrollBy({top:Math.min(br.top-innerHeight*0.3,to-from),behavior:'smooth'});}},280);});}catch(_){}}\
function wake(f){var d=f.getAttribute('data-hylki-srcdoc');if(d===null)return;\
f.removeAttribute('data-hylki-srcdoc');f._i=0;f._c=0;f._u=0;f.srcdoc=d;}\
function foldCard(sec){if(!sec)return;var on=!sec.classList.contains('hylki-folded');\
sec.classList.toggle('hylki-folded',on);\
try{window.webkit.messageHandlers.hylki.postMessage('fold:'+sec.dataset.key+':'+(on?1:0));}catch(_){}\
if(!on){var f=sec.querySelector('iframe.hylki-frame');if(f){f._h=0;s(f);setTimeout(function(){s(f);},80);}}}\
if(!on){var f=sec.querySelector('iframe.hylki-frame');if(f){wake(f);f._h=0;s(f);setTimeout(function(){s(f);},80);}}}\
function init(f){if(f._i){s(f);return;}f._i=1;quote(f);s(f);try{var d=f.contentDocument;if(d){if(window.ResizeObserver&&d.body){new ResizeObserver(function(){s(f);}).observe(d.body);}\
if(f.dataset.key&&!f._c){f._c=1;d.addEventListener('click',function(e){\
if(e.target&&e.target.closest&&e.target.closest('a'))return;pick(f.dataset.key,e);});}\
Expand Down
2 changes: 1 addition & 1 deletion src/ui/message_view/testdata/single.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/ui/message_view/testdata/single_carded_dark.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/ui/message_view/testdata/three_cards.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/ui/message_view/testdata/two_cards.html

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions src/ui/message_view/tests/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,52 @@ fn a_sender_cannot_put_markup_in_the_conversation_header() {
assert!(doc.contains("&lt;script&gt;x=1&lt;/script&gt;"), "{doc}");
assert!(doc.contains(" "{doc}");
}

/// A folded card hides its frame, and a hidden frame still loads its whole
/// document: a long conversation folded down to its newest card rendered
/// every message at once. The folded card's frame keeps its document aside
/// until the card opens; an open card's frame loads it as before.
#[test]
fn a_folded_card_does_not_load_its_message_until_opened() {
let mut first = msg_for_print();
first.body = "<p>opening</p>".into();
let mut second = msg_for_print();
second.id = 2;
second.body = "<p>reply</p>".into();
LIVE_FOLDED.with(|f| *f.borrow_mut() = [(1, 1)].into_iter().collect());

let doc = MessageView::conversation_document(
&[first, second],
&std::collections::HashMap::new(),
&Default::default(),
&Default::default(),
&[],
"#3584e4",
true,
false,
false,
false,
&crate::config::ReaderStyle::NONE,
&Default::default(),
false,
100,
);
LIVE_FOLDED.with(|f| f.borrow_mut().clear());

let frame = |key: &str| {
let at = doc
.find(&format!("<iframe class=\"hylki-frame\" data-key=\"{key}\""))
.expect("the card's frame");
doc[at..doc[at..].find("</iframe>").map(|e| at + e).unwrap()].to_string()
};
let folded = frame("1:1");
assert!(
folded.contains(" data-hylki-srcdoc=\"") && !folded.contains(" srcdoc=\""),
"a folded card keeps its document aside: {folded}"
);
let open = frame("1:2");
assert!(
open.contains(" srcdoc=\"") && !open.contains("data-hylki-srcdoc"),
"an open card loads its document: {open}"
);
}
0