Skip to content

Commit 5b9abd0

Browse files
authored
Merge pull request #47 from ranfdev/unresponsive_ui
Fix UI unresponsiveness while loading big pages
2 parents bedc5a6 + f4bc592 commit 5b9abd0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/widgets/tab.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use crate::common::glibctx;
2626
use crate::lossy_text_read::*;
2727
use hypertext::HypertextEvent;
2828

29+
const BYTES_BEFORE_YIELD: usize = 1024 * 10;
30+
2931
#[derive(Debug, Clone, PartialEq)]
3032
pub struct HistoryItem {
3133
pub url: url::Url,
@@ -502,16 +504,25 @@ impl Tab {
502504
)
503505
.unwrap();
504506
let mut line = String::with_capacity(1024);
507+
let mut total = 0;
508+
let mut last_yield_at_bytes = 0;
505509

506510
loop {
507511
let n = stream.read_line_lossy(&mut line).await?;
508512
if n == 0 {
509513
break;
510514
}
515+
total += n;
511516

512517
if let Err(err) = page.render([gemini::Event::Text(&line)].into_iter(), &mut pe) {
513518
anyhow::bail!("Error while parsing the page: {}", err);
514519
}
520+
521+
// Yield control to main thread after every 10KB, to not block the UI
522+
if total - last_yield_at_bytes >= BYTES_BEFORE_YIELD {
523+
glib::timeout_future(std::time::Duration::from_millis(1)).await;
524+
last_yield_at_bytes = total;
525+
}
515526
line.clear();
516527
}
517528
page.render([gemini::Event::End].into_iter(), &mut pe)
@@ -601,6 +612,7 @@ impl Tab {
601612
let mut data = String::with_capacity(1024);
602613
let mut total = 0;
603614
let mut n;
615+
let mut last_yield_at_bytes = 0;
604616

605617
let page = self.new_hypertext_page();
606618
let mut page_events = vec![];
@@ -630,7 +642,14 @@ impl Tab {
630642
}
631643
}
632644
}
645+
646+
// Yield control to main thread after every 10KB, to not block the UI
647+
if total - last_yield_at_bytes >= BYTES_BEFORE_YIELD {
648+
glib::timeout_future(std::time::Duration::from_millis(1)).await;
649+
last_yield_at_bytes = total;
650+
}
633651
}
652+
634653
Ok(data.into_bytes())
635654
}
636655
pub fn display_error(&self, error: anyhow::Error) {

0 commit comments

Comments
 (0)