yazi_core/manager/
tabs.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
use std::ops::{Deref, DerefMut};

use yazi_boot::BOOT;
use yazi_dds::Pubsub;
use yazi_proxy::ManagerProxy;
use yazi_shared::{Id, url::Url};

use crate::tab::Tab;

pub struct Tabs {
	pub cursor:       usize,
	pub(super) items: Vec<Tab>,
}

impl Tabs {
	pub fn make() -> Self {
		let mut tabs =
			Self { cursor: 0, items: (0..BOOT.cwds.len()).map(|_| Tab::default()).collect() };

		for (i, tab) in tabs.iter_mut().enumerate() {
			let file = &BOOT.files[i];
			if file.is_empty() {
				tab.cd(Url::from(&BOOT.cwds[i]));
			} else {
				tab.reveal(Url::from(BOOT.cwds[i].join(file)));
			}
		}
		tabs
	}

	pub(super) fn absolute(&self, rel: isize) -> usize {
		if rel > 0 {
			(self.cursor + rel as usize).min(self.items.len() - 1)
		} else {
			self.cursor.saturating_sub(rel.unsigned_abs())
		}
	}

	pub(super) fn set_idx(&mut self, idx: usize) {
		// Reset the preview of the last active tab
		if let Some(active) = self.items.get_mut(self.cursor) {
			active.preview.reset_image();
		}

		self.cursor = idx;
		ManagerProxy::refresh();
		ManagerProxy::peek(true);
		Pubsub::pub_from_tab(self.active().id);
	}
}

impl Tabs {
	#[inline]
	pub fn active(&self) -> &Tab { &self.items[self.cursor] }

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

	#[inline]
	pub fn active_or(&self, id: Option<Id>) -> &Tab {
		id.and_then(|id| self.iter().find(|&t| t.id == id)).unwrap_or(self.active())
	}

	#[inline]
	pub(super) fn active_or_mut(&mut self, id: Option<Id>) -> &mut Tab {
		if let Some(i) = id.and_then(|id| self.iter().position(|t| t.id == id)) {
			&mut self.items[i]
		} else {
			self.active_mut()
		}
	}

	#[inline]
	pub fn find_mut(&mut self, id: Id) -> Option<&mut Tab> { self.iter_mut().find(|t| t.id == id) }
}

impl Deref for Tabs {
	type Target = Vec<Tab>;

	fn deref(&self) -> &Self::Target { &self.items }
}

impl DerefMut for Tabs {
	fn deref_mut(&mut self) -> &mut Self::Target { &mut self.items }
}