yazi_core/manager/
manager.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
use ratatui::layout::Rect;
use yazi_adapter::Dimension;
use yazi_config::popup::{Origin, Position};
use yazi_fs::File;
use yazi_shared::{Id, url::Url};

use super::{Mimetype, Tabs, Watcher, Yanked};
use crate::tab::{Folder, Tab};

pub struct Manager {
	pub tabs:   Tabs,
	pub yanked: Yanked,

	pub(super) watcher: Watcher,
	pub mimetype:       Mimetype,
}

impl Manager {
	pub fn make() -> Self {
		Self {
			tabs:   Tabs::make(),
			yanked: Default::default(),

			watcher:  Watcher::serve(),
			mimetype: Default::default(),
		}
	}

	pub fn area(&self, pos: Position) -> Rect {
		if pos.origin == Origin::Hovered {
			self.active().hovered_rect_based(pos)
		} else {
			pos.rect(Dimension::available())
		}
	}

	pub fn shutdown(&mut self) { self.tabs.iter_mut().for_each(|t| t.shutdown()); }
}

impl Manager {
	#[inline]
	pub fn cwd(&self) -> &Url { self.active().cwd() }

	#[inline]
	pub fn active(&self) -> &Tab { self.tabs.active() }

	#[inline]
	pub fn active_mut(&mut self) -> &mut Tab { self.tabs.active_mut() }

	#[inline]
	pub fn active_or(&self, id: Option<Id>) -> &Tab { self.tabs.active_or(id) }

	#[inline]
	pub fn active_or_mut(&mut self, id: Option<Id>) -> &mut Tab { self.tabs.active_or_mut(id) }

	#[inline]
	pub fn current(&self) -> &Folder { &self.active().current }

	#[inline]
	pub fn current_mut(&mut self) -> &mut Folder { &mut self.active_mut().current }

	#[inline]
	pub fn current_or(&self, id: Option<Id>) -> &Folder { &self.active_or(id).current }

	#[inline]
	pub fn current_or_mut(&mut self, id: Option<Id>) -> &mut Folder {
		&mut self.active_or_mut(id).current
	}

	#[inline]
	pub fn parent(&self) -> Option<&Folder> { self.active().parent.as_ref() }

	#[inline]
	pub fn parent_mut(&mut self) -> Option<&mut Folder> { self.active_mut().parent.as_mut() }

	#[inline]
	pub fn hovered(&self) -> Option<&File> { self.active().hovered() }

	#[inline]
	pub fn hovered_folder(&self) -> Option<&Folder> { self.active().hovered_folder() }

	#[inline]
	pub fn selected_or_hovered(&self) -> Box<dyn Iterator<Item = &Url> + '_> {
		self.tabs.active().selected_or_hovered()
	}

	#[inline]
	pub fn hovered_and_selected(&self) -> Box<dyn Iterator<Item = &Url> + '_> {
		self.tabs.active().hovered_and_selected()
	}
}