@@ -480,11 +480,11 @@ def refresh_text_display(self) -> None:
480480 if tn :
481481 self .scroll_to_turn (tn )
482482
483- def refresh_ui (self ) -> None :
483+ def refresh_ui (self , scroll_to_last : bool = True ) -> None :
484484 self .render_story ()
485485 # There is nothing to read until the opening turn has been written
486486 self .read_action .setEnabled (self .state is not None and bool (self .state .turns ))
487- if self .state is not None and self .state .turns :
487+ if scroll_to_last and self .state is not None and self .state .turns :
488488 self .scroll_to_turn (len (self .state .turns ))
489489 self .update_window_title ()
490490 self .update_status ()
@@ -1040,10 +1040,31 @@ def on_retry() -> None:
10401040 self .session_cost += res .cost
10411041 self .streamed_narrative = ''
10421042 self .narrative_render_timer .stop ()
1043+ # Before render_story() clears the document, decide whether to scroll
1044+ # to the start of the new turn. Skip the scroll if the user has already
1045+ # reached or passed the turn start (so the completion doesn't yank them
1046+ # back), and save the scroll fraction to restore afterwards.
1047+ sv = self .story_view
1048+ scroll_to_last_turn = True
1049+ saved_scroll_fraction = 1.0
1050+ if self .streaming_block_start >= 0 :
1051+ vsb = sv .verticalScrollBar ()
1052+ vp = sv .viewport ()
1053+ if vsb is not None and vp is not None and vp .height () > 0 :
1054+ c = sv .textCursor ()
1055+ c .setPosition (self .streaming_block_start )
1056+ if sv .cursorRect (c ).top () < vp .height ():
1057+ scroll_to_last_turn = False
1058+ if vsb .maximum () > 0 :
1059+ saved_scroll_fraction = vsb .value () / vsb .maximum ()
10431060 self .prompt_edit .clear ()
10441061 self .prompt_edit .setFocus (Qt .FocusReason .OtherFocusReason )
10451062 self .autosave ()
1046- self .refresh_ui ()
1063+ self .refresh_ui (scroll_to_last_turn )
1064+ if not scroll_to_last_turn :
1065+ vsb = sv .verticalScrollBar ()
1066+ if vsb is not None and vsb .maximum () > 0 :
1067+ vsb .setValue (round (saved_scroll_fraction * vsb .maximum ()))
10471068 if self .images_enabled :
10481069 self .request_image (len (snapshot .turns ))
10491070 self ._notify_turn_ready ()
0 commit comments