use unicode_width::UnicodeWidthStr;
use super::Tab;
impl Tab {
pub fn new(id: impl Into<String>, label: impl Into<String>) -> Self {
Self {
id: id.into(),
label: label.into(),
closable: false,
modified: false,
icon: None,
}
}
pub fn with_closable(mut self, closable: bool) -> Self {
self.closable = closable;
self
}
pub fn with_modified(mut self, modified: bool) -> Self {
self.modified = modified;
self
}
pub fn with_icon(mut self, icon: impl Into<String>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn id(&self) -> &str {
&self.id
}
pub fn label(&self) -> &str {
&self.label
}
pub fn closable(&self) -> bool {
self.closable
}
pub fn modified(&self) -> bool {
self.modified
}
pub fn icon(&self) -> Option<&str> {
self.icon.as_deref()
}
pub fn set_label(&mut self, label: impl Into<String>) {
self.label = label.into();
}
pub fn set_closable(&mut self, closable: bool) {
self.closable = closable;
}
pub fn set_modified(&mut self, modified: bool) {
self.modified = modified;
}
pub fn set_icon(&mut self, icon: Option<String>) {
self.icon = icon;
}
pub(super) fn rendered_width(&self, max_tab_width: Option<usize>) -> usize {
let mut w: usize = 2; if let Some(icon) = &self.icon {
w += icon.width() + 1; }
w += self.label.width();
if self.modified {
w += 1; }
if self.closable {
w += 2; }
if let Some(max) = max_tab_width {
w.min(max)
} else {
w
}
}
}