@@ -493,10 +493,14 @@ fn ChartPane() -> Element {
493493 }
494494 Msg :: Keep => {
495495 if give_back_place ( key) . await
496- && let Some ( kept) = place. borrow ( ) . clone ( )
497496 && let Some ( now) = pane_place ( key)
498497 {
499- kept. set ( now. to_string ( ) ) ;
498+ if let Some ( kept) = place. borrow ( ) . clone ( ) {
499+ kept. set ( now. to_string ( ) ) ;
500+ }
501+ // And the chart's own, for whichever pane opens
502+ // it next (this one closed, the app restarted).
503+ keep_document_place ( key, & now) . await ;
500504 }
501505 }
502506 }
@@ -620,6 +624,7 @@ fn ChartPane() -> Element {
620624 // input's cursor drawn on it, which a restore before the path
621625 // came could not.
622626 give_back_place ( key) . await ;
627+ give_back_document_place ( key, & path) . await ;
623628 error. set ( message) ;
624629 }
625630 } ) ;
@@ -778,6 +783,111 @@ fn pane_place(key: u64) -> Option<Json> {
778783 Some ( place)
779784}
780785
786+ /// The data key a chart's view is kept under in this extension's data: its
787+ /// path's, so a pane that opens the chart again finds it.
788+ fn document_place_key ( path : & str ) -> String {
789+ format ! ( "view:{path}" )
790+ }
791+
792+ /// Each chart's view as last written to this extension's data (by path),
793+ /// and the panes that were given theirs back (by mount key and path): a
794+ /// view is written only once its chart's kept one has been applied, or a
795+ /// fresh pane would write its defaults over it.
796+ static DOCUMENT_PLACES : Mutex < BTreeMap < String , String > > = Mutex :: new ( BTreeMap :: new ( ) ) ;
797+ static DOCUMENT_GIVEN : Mutex < std:: collections:: BTreeSet < ( u64 , String ) > > =
798+ Mutex :: new ( std:: collections:: BTreeSet :: new ( ) ) ;
799+
800+ /// Pane `key`'s chart's view (`place`'s zoom, scroll and selection) kept
801+ /// with the chart, when it changed.
802+ async fn keep_document_place ( key : u64 , place : & Json ) {
803+ let ( Some ( path) , Some ( chart) ) = ( place. get ( "path" ) . and_then ( Json :: as_str) , place. get ( "chart" ) )
804+ else {
805+ return ;
806+ } ;
807+ let given = DOCUMENT_GIVEN
808+ . lock ( )
809+ . unwrap_or_else ( PoisonError :: into_inner)
810+ . contains ( & ( key, path. to_owned ( ) ) ) ;
811+ if !given {
812+ return ;
813+ }
814+ let text = chart. to_string ( ) ;
815+ {
816+ let mut places = DOCUMENT_PLACES
817+ . lock ( )
818+ . unwrap_or_else ( PoisonError :: into_inner) ;
819+ if places. get ( path) == Some ( & text) {
820+ return ;
821+ }
822+ places. insert ( path. to_owned ( ) , text) ;
823+ }
824+ if let Ok ( handle) = handle ( ) {
825+ let _ = handle
826+ . host ( HostRequest :: SetData {
827+ key : document_place_key ( path) ,
828+ value : Some ( chart. clone ( ) ) ,
829+ } )
830+ . await ;
831+ }
832+ }
833+
834+ /// Pane `key`, showing `path`, given the chart's kept view once, unless its
835+ /// own kept place (its pane's) was for this chart and has been given back.
836+ async fn give_back_document_place ( key : u64 , path : & str ) {
837+ if path. is_empty ( ) {
838+ return ;
839+ }
840+ let first = DOCUMENT_GIVEN
841+ . lock ( )
842+ . unwrap_or_else ( PoisonError :: into_inner)
843+ . insert ( ( key, path. to_owned ( ) ) ) ;
844+ if !first {
845+ return ;
846+ }
847+ let pane_had_it = PLACES
848+ . lock ( )
849+ . unwrap_or_else ( PoisonError :: into_inner)
850+ . get ( & key)
851+ . is_some_and ( |( text, _) | {
852+ serde_json:: from_str :: < Json > ( text)
853+ . ok ( )
854+ . and_then ( |p| p. get ( "path" ) . and_then ( Json :: as_str) . map ( |p| p == path) )
855+ . unwrap_or ( false )
856+ } ) ;
857+ if pane_had_it {
858+ return ;
859+ }
860+ let Ok ( handle) = handle ( ) else { return } ;
861+ let Ok ( kept) = handle
862+ . host ( HostRequest :: Data {
863+ key : document_place_key ( path) ,
864+ } )
865+ . await
866+ else {
867+ return ;
868+ } ;
869+ if kept. is_null ( ) {
870+ return ;
871+ }
872+ let Some ( o) = open_charts ( ) . get ( & key) . cloned ( ) else {
873+ return ;
874+ } ;
875+ if o. path != path {
876+ return ;
877+ }
878+ let ( zoom, scroll, selection) = keep:: chart_from ( & kept) ;
879+ o. data . update ( |s| s. view = ChartView { zoom, scroll } ) ;
880+ o. selection . set ( ChartSelection {
881+ keys : selection,
882+ hover : None ,
883+ } ) ;
884+ o. wake . wake ( ) ;
885+ DOCUMENT_PLACES
886+ . lock ( )
887+ . unwrap_or_else ( PoisonError :: into_inner)
888+ . insert ( path. to_owned ( ) , kept. to_string ( ) ) ;
889+ }
890+
781891/// What each pane's view was given back (its kept place, as text), and
782892/// whether it has been applied: a pane is given its place once, when its
783893/// chart's path is known and is the chart it was kept for.
0 commit comments