-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathmod.rs
920 lines (816 loc) · 32.7 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
// Modules
pub mod export;
pub mod import;
pub mod rendering;
pub mod visual_debug;
// Re-exports
pub use self::export::ExportPrefs;
pub use self::import::ImportPrefs;
// Imports
use self::import::XoppImportPrefs;
use crate::document::{background, Layout};
use crate::pens::PenStyle;
use crate::pens::{penbehaviour::PenProgress, PenMode, PensConfig};
use crate::store::render_comp::{self, RenderCompState};
use crate::store::{ChronoComponent, StrokeKey};
use crate::strokes::strokebehaviour::GeneratedStrokeImages;
use crate::strokes::Stroke;
use crate::{render, AudioPlayer, WidgetFlags};
use crate::{Camera, Document, PenHolder, StrokeStore};
use anyhow::Context;
use futures::channel::{mpsc, oneshot};
use gtk4::gsk;
use p2d::bounding_volume::{Aabb, BoundingVolume};
use rnote_compose::helpers::AabbHelpers;
use rnote_compose::penevents::{PenEvent, ShortcutKey};
use rnote_compose::shapes::ShapeBehaviour;
use rnote_fileformats::{rnoteformat, xoppformat, FileFormatLoader};
use serde::{Deserialize, Serialize};
use slotmap::{HopSlotMap, SecondaryMap};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;
/// An immutable view into the engine, excluding the penholder.
#[derive(Debug)]
pub struct EngineView<'a> {
pub tasks_tx: EngineTaskSender,
pub pens_config: &'a PensConfig,
pub doc: &'a Document,
pub store: &'a StrokeStore,
pub camera: &'a Camera,
pub audioplayer: &'a Option<AudioPlayer>,
}
/// A mutable view into the engine, excluding the penholder.
#[derive(Debug)]
pub struct EngineViewMut<'a> {
pub tasks_tx: EngineTaskSender,
pub pens_config: &'a mut PensConfig,
pub doc: &'a mut Document,
pub store: &'a mut StrokeStore,
pub camera: &'a mut Camera,
pub audioplayer: &'a mut Option<AudioPlayer>,
}
impl<'a> EngineViewMut<'a> {
// Converts itself to the immutable view.
pub fn as_im<'m>(&'m self) -> EngineView<'m> {
EngineView::<'m> {
tasks_tx: self.tasks_tx.clone(),
pens_config: self.pens_config,
doc: self.doc,
store: self.store,
camera: self.camera,
audioplayer: self.audioplayer,
}
}
}
#[derive(Debug, Clone)]
/// An engine task, usually coming from a spawned thread and to be processed with [RnoteEngine::handle_engine_task].
pub enum EngineTask {
/// Replace the images for rendering of the given stroke.
///
/// The state of the render component should be set **before** spawning a thread, generating images and sending this task,
/// to avoid spawning large amounts of already outdated rendering tasks when checking the render component's state on resize/zooming, etc. .
UpdateStrokeWithImages {
/// The stroke key.
key: StrokeKey,
/// The generated images.
images: GeneratedStrokeImages,
/// The image scale-factor the render task was using while generating the images.
image_scale: f64,
/// The stroke bounds at the time when the render task has launched.
stroke_bounds: Aabb,
},
/// Appends the images to the rendering of the given stroke.
///
/// The state of the render component should be set **before** spawning a thread, generating images and sending this task,
/// to avoid spawning large amounts of already outdated rendering tasks when checking the render component's state on resize/zooming, etc. .
AppendImagesToStroke {
/// The stroke key
key: StrokeKey,
/// The generated images
images: GeneratedStrokeImages,
},
/// Indicates that the application is quitting. Sent to quit the handler which receives the tasks.
Quit,
}
/// The engine configuration. Used when loading/saving the current configuration from/into persistent application settings.
#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(default, rename = "engine_config")]
pub struct EngineConfig {
#[serde(rename = "document")]
document: Document,
#[serde(rename = "pens_config")]
pens_config: PensConfig,
#[serde(rename = "penholder")]
penholder: PenHolder,
#[serde(rename = "import_prefs")]
import_prefs: ImportPrefs,
#[serde(rename = "export_prefs")]
export_prefs: ExportPrefs,
#[serde(rename = "pen_sounds")]
pen_sounds: bool,
}
// An engine snapshot, used when loading/saving the current document from/into a file.
#[derive(Debug, Serialize, Deserialize)]
#[serde(default, rename = "engine_snapshot")]
pub struct EngineSnapshot {
#[serde(rename = "document")]
pub document: Document,
#[serde(rename = "stroke_components")]
pub stroke_components: Arc<HopSlotMap<StrokeKey, Arc<Stroke>>>,
#[serde(rename = "chrono_components")]
pub chrono_components: Arc<SecondaryMap<StrokeKey, Arc<ChronoComponent>>>,
#[serde(rename = "chrono_counter")]
pub chrono_counter: u32,
}
impl Default for EngineSnapshot {
fn default() -> Self {
Self {
document: Document::default(),
stroke_components: Arc::new(HopSlotMap::with_key()),
chrono_components: Arc::new(SecondaryMap::new()),
chrono_counter: 0,
}
}
}
impl EngineSnapshot {
/// Loads a snapshot from the bytes of a .rnote file.
///
/// To import this snapshot into the current engine, use `import_snapshot()`.
pub async fn load_from_rnote_bytes(bytes: Vec<u8>) -> anyhow::Result<Self> {
let (snapshot_sender, snapshot_receiver) = oneshot::channel::<anyhow::Result<Self>>();
rayon::spawn(move || {
let result = || -> anyhow::Result<Self> {
let rnote_file = rnoteformat::RnoteFile::load_from_bytes(&bytes)
.context("RnoteFile load_from_bytes() failed")?;
serde_json::from_value(rnote_file.engine_snapshot)
.context("serde_json::from_value() for rnote_file.engine_snapshot failed")
};
if let Err(_data) = snapshot_sender.send(result()) {
log::error!("sending result to receiver in open_from_rnote_bytes() failed. Receiver already dropped");
}
});
snapshot_receiver.await?
}
/// Loads from the bytes of a Xournal++ .xopp file.
///
/// To import this snapshot into the current engine, use `import_snapshot()`.
pub async fn load_from_xopp_bytes(
bytes: Vec<u8>,
xopp_import_prefs: XoppImportPrefs,
) -> anyhow::Result<Self> {
let (snapshot_sender, snapshot_receiver) = oneshot::channel::<anyhow::Result<Self>>();
rayon::spawn(move || {
let result = || -> anyhow::Result<Self> {
let xopp_file = xoppformat::XoppFile::load_from_bytes(&bytes)?;
// Extract the largest width of all pages, add together all heights
let (doc_width, doc_height) = xopp_file
.xopp_root
.pages
.iter()
.map(|page| (page.width, page.height))
.fold((0_f64, 0_f64), |prev, next| {
// Max of width, sum heights
(prev.0.max(next.0), prev.1 + next.1)
});
let no_pages = xopp_file.xopp_root.pages.len() as u32;
let mut engine = RnoteEngine::default();
// We convert all values from the hardcoded 72 DPI of Xopp files to the preferred dpi
engine.document.format.dpi = xopp_import_prefs.dpi;
engine.document.x = 0.0;
engine.document.y = 0.0;
engine.document.width = crate::utils::convert_value_dpi(
doc_width,
xoppformat::XoppFile::DPI,
xopp_import_prefs.dpi,
);
engine.document.height = crate::utils::convert_value_dpi(
doc_height,
xoppformat::XoppFile::DPI,
xopp_import_prefs.dpi,
);
engine.document.format.width = crate::utils::convert_value_dpi(
doc_width,
xoppformat::XoppFile::DPI,
xopp_import_prefs.dpi,
);
engine.document.format.height = crate::utils::convert_value_dpi(
doc_height / (no_pages as f64),
xoppformat::XoppFile::DPI,
xopp_import_prefs.dpi,
);
if let Some(first_page) = xopp_file.xopp_root.pages.get(0) {
if let xoppformat::XoppBackgroundType::Solid {
color: _color,
style: _style,
} = &first_page.background.bg_type
{
// Xopp background styles are not compatible with Rnotes, so everything is plain for now
engine.document.background.pattern = background::PatternStyle::None;
}
}
// Offsetting as rnote has one global coordinate space
let mut offset = na::Vector2::<f64>::zeros();
for (_page_i, page) in xopp_file.xopp_root.pages.into_iter().enumerate() {
for layers in page.layers.into_iter() {
// import strokes
for new_xoppstroke in layers.strokes.into_iter() {
match Stroke::from_xoppstroke(
new_xoppstroke,
offset,
xopp_import_prefs.dpi,
) {
Ok((new_stroke, layer)) => {
engine.store.insert_stroke(new_stroke, Some(layer));
}
Err(e) => {
log::error!(
"from_xoppstroke() failed in open_from_xopp_bytes() with Err {:?}",
e
);
}
}
}
// import images
for new_xoppimage in layers.images.into_iter() {
match Stroke::from_xoppimage(
new_xoppimage,
offset,
xopp_import_prefs.dpi,
) {
Ok(new_image) => {
engine.store.insert_stroke(new_image, None);
}
Err(e) => {
log::error!(
"from_xoppimage() failed in open_from_xopp_bytes() with Err {:?}",
e
);
}
}
}
}
// Only add to y offset, results in vertical pages
offset[1] += crate::utils::convert_value_dpi(
page.height,
xoppformat::XoppFile::DPI,
xopp_import_prefs.dpi,
);
}
Ok(engine.take_snapshot())
};
if let Err(_data) = snapshot_sender.send(result()) {
log::error!("sending result to receiver in open_from_xopp_bytes() failed. Receiver already dropped");
}
});
snapshot_receiver.await?
}
}
pub const RNOTE_STROKE_CONTENT_MIME_TYPE: &str = "application/rnote-stroke-content";
/// Stroke content. Used when copying/cutting/pasting a selection into/from the clipboard
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename = "stroke_content")]
pub struct StrokeContent {
#[serde(rename = "strokes")]
pub strokes: Vec<Arc<Stroke>>,
}
pub type EngineTaskSender = mpsc::UnboundedSender<EngineTask>;
pub type EngineTaskReceiver = mpsc::UnboundedReceiver<EngineTask>;
/// The engine.
#[derive(Debug, Serialize, Deserialize)]
#[serde(default, rename = "engine")]
pub struct RnoteEngine {
#[serde(rename = "document")]
pub document: Document,
#[serde(rename = "store")]
pub store: StrokeStore,
#[serde(rename = "pens_config")]
pub pens_config: PensConfig,
#[serde(rename = "camera")]
pub camera: Camera,
#[serde(rename = "penholder")]
pub penholder: PenHolder,
#[serde(rename = "import_prefs")]
pub import_prefs: ImportPrefs,
#[serde(rename = "export_prefs")]
pub export_prefs: ExportPrefs,
#[serde(rename = "pen_sounds")]
pen_sounds: bool,
#[serde(skip)]
pub audioplayer: Option<AudioPlayer>,
#[serde(skip)]
pub visual_debug: bool,
// the task sender. Must not be modified, only cloned.
// To install a new engine task handler, regenerate the channel through `regenerate_channel()`
#[serde(skip)]
pub tasks_tx: EngineTaskSender,
// Background rendering
#[serde(skip)]
pub background_tile_image: Option<render::Image>,
#[serde(skip)]
background_rendernodes: Vec<gsk::RenderNode>,
}
impl Default for RnoteEngine {
fn default() -> Self {
let (tasks_tx, _tasks_rx) = futures::channel::mpsc::unbounded::<EngineTask>();
Self {
document: Document::default(),
store: StrokeStore::default(),
pens_config: PensConfig::default(),
camera: Camera::default(),
penholder: PenHolder::default(),
import_prefs: ImportPrefs::default(),
export_prefs: ExportPrefs::default(),
pen_sounds: false,
audioplayer: None,
visual_debug: false,
tasks_tx,
background_tile_image: None,
background_rendernodes: Vec::default(),
}
}
}
impl RnoteEngine {
pub fn tasks_tx(&self) -> EngineTaskSender {
self.tasks_tx.clone()
}
/// Regenerates the tasks channel, saves the sender in the struct and returns the receiver
/// which can be awaited in a engine tasks handler through `handle_engine_tasks()`
pub fn regenerate_channel(&mut self) -> EngineTaskReceiver {
let (tasks_tx, tasks_rx) = futures::channel::mpsc::unbounded::<EngineTask>();
self.tasks_tx = tasks_tx;
tasks_rx
}
/// Gets the EngineView
pub fn view(&self) -> EngineView {
EngineView {
tasks_tx: self.tasks_tx.clone(),
pens_config: &self.pens_config,
doc: &self.document,
store: &self.store,
camera: &self.camera,
audioplayer: &self.audioplayer,
}
}
/// Gets the EngineViewMut
pub fn view_mut(&mut self) -> EngineViewMut {
EngineViewMut {
tasks_tx: self.tasks_tx.clone(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
}
}
/// whether pen sounds are enabled
pub fn pen_sounds(&self) -> bool {
self.pen_sounds
}
/// whether pen is idle
pub fn pen_idle(&self) -> bool {
self.penholder.current_pen_progress() == PenProgress::Idle
}
/// Enables/disables the pen sounds.
/// If pen sound should be enabled, the pkg data dir must be provided.
pub fn set_pen_sounds(&mut self, pen_sounds: bool, pkg_data_dir: Option<PathBuf>) {
self.pen_sounds = pen_sounds;
if pen_sounds {
if let Some(pkg_data_dir) = pkg_data_dir {
// Only create and init a new audioplayer if it does not already exists
if self.audioplayer.is_none() {
self.audioplayer = match AudioPlayer::new_init(pkg_data_dir) {
Ok(audioplayer) => Some(audioplayer),
Err(e) => {
log::error!("creating a new audioplayer failed, Err: {e:?}");
None
}
}
}
}
} else {
self.audioplayer.take();
}
}
/// Takes a snapshot of the current state.
pub fn take_snapshot(&self) -> EngineSnapshot {
let mut store_history_entry = self.store.history_entry_from_current_state();
// Remove all trashed strokes
let trashed_keys = store_history_entry
.trash_components
.iter()
.filter_map(|(key, trash_comp)| if trash_comp.trashed { Some(key) } else { None })
.collect::<Vec<StrokeKey>>();
for key in trashed_keys {
Arc::make_mut(&mut Arc::make_mut(&mut store_history_entry).stroke_components)
.remove(key);
}
EngineSnapshot {
document: self.document,
stroke_components: Arc::clone(&store_history_entry.stroke_components),
chrono_components: Arc::clone(&store_history_entry.chrono_components),
chrono_counter: store_history_entry.chrono_counter,
}
}
/// Imports an engine snapshot. A save file should always be loaded with this method.
///
/// The store then needs to update its rendering.
pub fn load_snapshot(&mut self, snapshot: EngineSnapshot) -> WidgetFlags {
self.document = snapshot.document;
self.store.import_from_snapshot(&snapshot);
self.update_state_current_pen()
}
/// Records the current store state and saves it as a history entry.
pub fn record(&mut self, now: Instant) -> WidgetFlags {
self.store.record(now)
}
/// Undo the latest changes.
pub fn undo(&mut self, now: Instant) -> WidgetFlags {
let mut widget_flags = WidgetFlags::default();
widget_flags.merge(
self.penholder
.reinstall_pen_current_style(&mut EngineViewMut {
tasks_tx: self.tasks_tx(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
}),
);
widget_flags.merge(self.store.undo(now));
widget_flags.merge(self.update_state_current_pen());
self.resize_autoexpand();
if let Err(e) = self.update_rendering_current_viewport() {
log::error!("failed to update rendering for current viewport while undo, Err: {e:?}");
}
widget_flags.redraw = true;
widget_flags
}
/// Redo the latest changes.
pub fn redo(&mut self, now: Instant) -> WidgetFlags {
let mut widget_flags = WidgetFlags::default();
widget_flags.merge(
self.penholder
.reinstall_pen_current_style(&mut EngineViewMut {
tasks_tx: self.tasks_tx(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
}),
);
widget_flags.merge(self.store.redo(now));
widget_flags.merge(self.update_state_current_pen());
self.resize_autoexpand();
if let Err(e) = self.update_rendering_current_viewport() {
log::error!("failed to update rendering for current viewport while redo, Err: {e:?}");
}
widget_flags.redraw = true;
widget_flags
}
pub fn can_undo(&self) -> bool {
self.store.can_undo()
}
pub fn can_redo(&self) -> bool {
self.store.can_redo()
}
// Clears the entire store.
pub fn clear(&mut self) -> WidgetFlags {
self.store.clear();
self.update_state_current_pen()
}
/// Handle a received task from tasks_rx.
/// Returns [WidgetFlags] to indicate what needs to be updated in the UI.
///
/// An example how to use it:
/// ```rust, ignore
///
/// glib::MainContext::default().spawn_local(clone!(@weak canvas, @weak appwindow => async move {
/// let mut task_rx = canvas.engine().borrow_mut().regenerate_channel();
///
/// loop {
/// if let Some(task) = task_rx.next().await {
/// let (widget_flags, quit) = canvas.engine().borrow_mut().handle_engine_task(task);
/// canvas.emit_handle_widget_flags(widget_flags);
/// if quit {
/// break;
/// }
/// }
/// }
/// }));
/// ```
pub fn handle_engine_task(&mut self, task: EngineTask) -> (WidgetFlags, bool) {
let mut widget_flags = WidgetFlags::default();
let mut quit = false;
match task {
EngineTask::UpdateStrokeWithImages {
key,
images,
image_scale,
stroke_bounds,
} => {
if let Some(state) = self.store.render_comp_state(key) {
match state {
RenderCompState::Complete | RenderCompState::ForViewport(_) => {
// The rendering was already regenerated in the meantime,
// so we just discard the the render task result
}
RenderCompState::BusyRenderingInTask => {
if (self.camera.image_scale()
- render_comp::RENDER_IMAGE_SCALE_EQUALITY_TOLERANCE
..self.camera.image_scale()
+ render_comp::RENDER_IMAGE_SCALE_EQUALITY_TOLERANCE)
.contains(&image_scale)
&& self
.store
.get_stroke_ref(key)
.map(|s| s.bounds() == stroke_bounds)
.unwrap_or(true)
{
// Only when the image scale and stroke bounds are the same
// to when the render task was started,
// the new images are considered valid and can replace the old.
self.store.replace_rendering_with_images(key, images);
}
widget_flags.redraw = true;
}
RenderCompState::Dirty => {
// If the state was flagged dirty in the meantime,
// it is expected that retriggering rendering will be handled elsewhere
}
}
}
}
EngineTask::AppendImagesToStroke { key, images } => {
self.store.append_rendering_images(key, images);
widget_flags.redraw = true;
}
EngineTask::Quit => {
quit = true;
}
}
(widget_flags, quit)
}
/// Handle a pen event.
pub fn handle_pen_event(
&mut self,
event: PenEvent,
pen_mode: Option<PenMode>,
now: Instant,
) -> WidgetFlags {
self.penholder.handle_pen_event(
event,
pen_mode,
now,
&mut EngineViewMut {
tasks_tx: self.tasks_tx(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
},
)
}
/// Handle a pressed shortcut key.
pub fn handle_pressed_shortcut_key(
&mut self,
shortcut_key: ShortcutKey,
now: Instant,
) -> WidgetFlags {
self.penholder.handle_pressed_shortcut_key(
shortcut_key,
now,
&mut EngineViewMut {
tasks_tx: self.tasks_tx(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
},
)
}
/// Change the pen style.
pub fn change_pen_style(&mut self, new_style: PenStyle) -> WidgetFlags {
self.penholder.change_style(
new_style,
&mut EngineViewMut {
tasks_tx: self.tasks_tx(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
},
)
}
/// Change the pen style (temporary) override.
pub fn change_pen_style_override(
&mut self,
new_style_override: Option<PenStyle>,
) -> WidgetFlags {
self.penholder.change_style_override(
new_style_override,
&mut EngineViewMut {
tasks_tx: self.tasks_tx(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
},
)
}
/// Change the pen mode. Relevant for stylus input.
pub fn change_pen_mode(&mut self, pen_mode: PenMode) -> WidgetFlags {
self.penholder.change_pen_mode(
pen_mode,
&mut EngineViewMut {
tasks_tx: self.tasks_tx(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
},
)
}
/// Reinstall the pen in the current style.
pub fn reinstall_pen_current_style(&mut self) -> WidgetFlags {
self.penholder
.reinstall_pen_current_style(&mut EngineViewMut {
tasks_tx: self.tasks_tx(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
})
}
/// Generates bounds for each page on the document which contains content.
pub fn pages_bounds_w_content(&self) -> Vec<Aabb> {
let doc_bounds = self.document.bounds();
let keys = self.store.stroke_keys_as_rendered();
let strokes_bounds = self.store.strokes_bounds(&keys);
let pages_bounds = doc_bounds
.split_extended_origin_aligned(na::vector![
self.document.format.width,
self.document.format.height
])
.into_iter()
.filter(|page_bounds| {
// Filter the pages out that don't intersect with any stroke
strokes_bounds
.iter()
.any(|stroke_bounds| stroke_bounds.intersects(page_bounds))
})
.collect::<Vec<Aabb>>();
if pages_bounds.is_empty() {
// If no page has content, return the origin page
vec![Aabb::new(
na::point![0.0, 0.0],
na::point![self.document.format.width, self.document.format.height],
)]
} else {
pages_bounds
}
}
/// Generates bounds which contain all pages on the doc with content, extended to fit the current format.
pub fn bounds_w_content_extended(&self) -> Option<Aabb> {
let pages_bounds = self.pages_bounds_w_content();
if pages_bounds.is_empty() {
return None;
}
Some(
pages_bounds
.into_iter()
.fold(Aabb::new_invalid(), |prev, next| prev.merged(&next)),
)
}
/// Resizes the doc to the format and to fit all strokes.
///
/// Background rendering then needs to be updated.
pub fn resize_to_fit_strokes(&mut self) {
self.document
.resize_to_fit_strokes(&self.store, &self.camera);
}
/// Resize the doc when in autoexpanding layouts. called e.g. when finishing a new stroke.
///
/// Background rendering then needs to be updated.
pub fn resize_autoexpand(&mut self) {
self.document.resize_autoexpand(&self.store, &self.camera);
}
/// Expand the doc when in autoexpanding layouts. e.g. when dragging with touch.
pub fn expand_doc_autoexpand(&mut self) {
match self.document.layout {
Layout::FixedSize | Layout::ContinuousVertical => {
// not resizing in these modes, the size is not dependent on the camera
}
Layout::SemiInfinite => {
// only expand, don't resize to fit strokes
self.document
.expand_doc_semi_infinite_layout(self.camera.viewport());
}
Layout::Infinite => {
// only expand, don't resize to fit strokes
self.document
.expand_doc_infinite_layout(self.camera.viewport());
}
}
}
/// Add a page to the document when in fixed size layout.
///
/// Returns true when document is in fixed size layout and a pages was added,
/// else false.
///
/// Background and strokes rendering then need to be updated.
pub fn add_page_doc_fixed_size(&mut self) -> bool {
if self.document.layout != Layout::FixedSize {
return false;
}
let format_height = self.document.format.height;
let new_doc_height = self.document.height + format_height;
self.document.height = new_doc_height;
true
}
/// Remove a page from the document when in fixed size layout.
///
/// Returns true when document is in fixed size layout and a pages was removed,
/// else false.
///
/// Background and strokes rendering then need to be updated.
pub fn remove_page_doc_fixed_size(&mut self) -> bool {
if self.document.layout != Layout::FixedSize {
return false;
}
let format_height = self.document.format.height;
let doc_y = self.document.y;
let doc_height = self.document.height;
let new_doc_height = doc_height - format_height;
if doc_height > format_height {
let remove_area_keys = self.store.keys_below_y(doc_y + new_doc_height);
self.store.set_trashed_keys(&remove_area_keys, true);
self.document.height = new_doc_height;
}
true
}
/// Update the camera and updates doc dimensions with the new offset and size.
///
/// Background and strokes rendering then need to be updated.
pub fn update_camera_offset_size(
&mut self,
new_offset: na::Vector2<f64>,
new_size: na::Vector2<f64>,
) {
self.camera.offset = new_offset;
self.camera.size = new_size;
}
/// Update the current pen with the current engine state.
///
/// Needs to be called when the engine state was changed outside of pen events. ( e.g. trash all strokes, set strokes selected, etc. )
pub fn update_state_current_pen(&mut self) -> WidgetFlags {
self.penholder.update_state_current_pen(&mut EngineViewMut {
tasks_tx: self.tasks_tx.clone(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
})
}
/// Fetch clipboard content from the current pen.
///
/// Returns (the clipboard content, MIME-type).
#[allow(clippy::type_complexity)]
pub fn fetch_clipboard_content(
&self,
) -> anyhow::Result<(Option<(Vec<u8>, String)>, WidgetFlags)> {
self.penholder.fetch_clipboard_content(&EngineView {
tasks_tx: self.tasks_tx(),
pens_config: &self.pens_config,
doc: &self.document,
store: &self.store,
camera: &self.camera,
audioplayer: &self.audioplayer,
})
}
/// Cut clipboard content from the current pen.
///
/// Returns (the clipboard content, MIME-type).
#[allow(clippy::type_complexity)]
pub fn cut_clipboard_content(
&mut self,
) -> anyhow::Result<(Option<(Vec<u8>, String)>, WidgetFlags)> {
self.penholder.cut_clipboard_content(&mut EngineViewMut {
tasks_tx: self.tasks_tx(),
pens_config: &mut self.pens_config,
doc: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
})
}
}