1use crossterm::cursor::SetCursorStyle;
2use ratatui::layout::{Position, Rect};
3use yazi_shared::Layer;
4
5use crate::{cmp::Cmp, confirm::Confirm, help::Help, input::Input, mgr::Mgr, notify::Notify, pick::Pick, tab::{Folder, Tab}, tasks::Tasks, which::Which};
6
7pub struct Core {
8 pub mgr: Mgr,
9 pub tasks: Tasks,
10 pub pick: Pick,
11 pub input: Input,
12 pub confirm: Confirm,
13 pub help: Help,
14 pub cmp: Cmp,
15 pub which: Which,
16 pub notify: Notify,
17}
18
19impl Core {
20 pub fn make() -> Self {
21 Self {
22 mgr: Mgr::make(),
23 tasks: Tasks::serve(),
24 pick: Default::default(),
25 input: Default::default(),
26 confirm: Default::default(),
27 help: Default::default(),
28 cmp: Default::default(),
29 which: Default::default(),
30 notify: Default::default(),
31 }
32 }
33
34 pub fn cursor(&self) -> Option<(Position, SetCursorStyle)> {
35 if self.input.visible {
36 let Rect { x, y, .. } = self.mgr.area(self.input.position);
37 return Some((
38 Position { x: x + 1 + self.input.cursor(), y: y + 1 },
39 self.input.cursor_shape(),
40 ));
41 }
42 if let Some((x, y)) = self.help.cursor() {
43 return Some((Position { x, y }, self.help.cursor_shape()));
44 }
45 None
46 }
47
48 pub fn layer(&self) -> Layer {
49 if self.which.visible {
50 Layer::Which
51 } else if self.cmp.visible {
52 Layer::Cmp
53 } else if self.help.visible {
54 Layer::Help
55 } else if self.confirm.visible {
56 Layer::Confirm
57 } else if self.input.visible {
58 Layer::Input
59 } else if self.pick.visible {
60 Layer::Pick
61 } else if self.active().spot.visible() {
62 Layer::Spot
63 } else if self.tasks.visible {
64 Layer::Tasks
65 } else {
66 Layer::Mgr
67 }
68 }
69}
70
71impl Core {
72 #[inline]
73 pub fn active(&self) -> &Tab { self.mgr.active() }
74
75 #[inline]
76 pub fn active_mut(&mut self) -> &mut Tab { self.mgr.active_mut() }
77
78 #[inline]
79 pub fn current_mut(&mut self) -> &mut Folder { self.mgr.current_mut() }
80
81 #[inline]
82 pub fn parent_mut(&mut self) -> Option<&mut Folder> { self.mgr.parent_mut() }
83}