blockworx/script/
session.rs1use crate::gesture::Gesture;
13use blockworx_doc::document::{DocIndex, Document as DocDocument};
14
15use crate::gesture;
16use crate::path::BlockPath;
17use crate::schema::lower::{SourceIds, lower};
18use crate::schema::model as schema;
19use crate::theme::Theme;
20#[cfg(test)]
21use crate::tools::RenameTitle;
22use crate::tools::SelectTool;
23use crate::tools::tool::{Tool, ToolTrait};
24use blockworx_doc::repo::Repo;
25
26use super::driver::{SimDriver, SimTarget};
27use super::lowering::SimFrame;
28use super::step::CueScope;
29
30pub struct Session {
31 repo: Repo,
32 index: DocIndex,
33 path: BlockPath,
34 presentation: crate::presentation::Presentation,
35 gesture: Gesture,
36 tool: Tool,
37 driver: SimDriver,
38 ids: SourceIds,
41}
42
43impl Session {
44 pub fn new(initial_kdl: Option<&str>, name: &str) -> Self {
50 let lowered = match initial_kdl {
51 None => crate::schema::lower::Lowered::default(),
52 Some(src) => match schema::Document::parse_kdl(src, name) {
53 Ok(doc) => lower(&doc, name),
54 Err(e) => {
55 tracing::error!("session initial for {name} failed to parse: {e:?}");
56 crate::schema::lower::Lowered::default()
57 }
58 },
59 };
60 let repo = Repo::folding(&lowered.commits).unwrap_or_else(|e| {
61 tracing::error!("session initial for {name} will not seed a repo: {e}");
62 Repo::default()
63 });
64 let path = BlockPath::opening(repo.document());
65 Self {
66 repo,
67 index: DocIndex::default(),
68 path,
69 presentation: crate::presentation::Presentation::default(),
70 gesture: gesture::Gesture::idle(),
71 tool: Tool::Select(SelectTool),
72 driver: SimDriver::default(),
73 ids: lowered.ids,
74 }
75 }
76
77 pub fn idle_frame(&self) -> SimFrame {
78 self.driver.idle_frame()
79 }
80
81 pub fn step(&mut self, frame: SimFrame, theme: &Theme, egui_painter: &egui::Painter) {
82 let Self {
83 repo,
84 index,
85 path,
86 presentation,
87 gesture,
88 tool,
89 driver,
90 ..
91 } = self;
92 driver.apply(
93 frame,
94 SimTarget {
95 repo,
96 index,
97 path,
98 presentation,
99 gesture,
100 tool,
101 },
102 theme,
103 egui_painter,
104 );
105 }
106
107 pub fn cue_scope(&mut self) -> CueScope<'_> {
109 let doc: &DocDocument = self.repo.document();
110 CueScope::new(self.index.view(doc), &self.ids)
111 }
112
113 pub fn tool_name(&self) -> crate::tools::names::ToolName {
115 self.tool.name()
116 }
117
118 #[cfg(test)]
120 pub fn document(&self) -> &blockworx_doc::document::Document {
121 self.repo.document()
122 }
123
124 #[cfg(test)]
127 pub fn log(&self) -> &[blockworx_doc::commit::Commit] {
128 self.repo.log()
129 }
130
131 #[cfg(test)]
134 pub fn renaming(&self) -> bool {
135 matches!(self.tool, Tool::RenameTitle(RenameTitle::Renaming { .. }))
136 }
137}