#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default)]
pub struct Rect {
pub x: i32,
pub y: i32,
pub w: i32,
pub h: i32,
}
impl Rect {
pub fn contains(&self, px: i32, py: i32) -> bool {
px >= self.x && px < self.x + self.w && py >= self.y && py < self.y + self.h
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Viewport {
pub width: i32,
pub height: i32,
pub cell_w: i32,
pub cell_h: i32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum PanelStyle {
Title,
Label,
Hint,
Error,
Row {
selected: bool,
},
Button {
primary: bool,
},
Field {
focused: bool,
valid: bool,
},
Swatch(u32),
Tile {
selected: bool,
},
Popup,
Dim(u8),
FloatLabel,
FolderIcon,
Badge(u8),
Progress {
done: u16,
hover: u16,
},
Scrollbar {
start: u16,
len: u16,
},
Option {
selected: bool,
highlight: u64,
},
FontOption {
selected: bool,
highlight: u64,
},
ThemePreview {
fg: u32,
bg: u32,
cursor: u32,
accent: u32,
},
Md {
flags: u8,
heading: u8,
},
MdCode {
top: bool,
bottom: bool,
},
MdToken(u8),
MdQuote,
MdRule,
Frame {
focused: bool,
},
Bubble {
mine: bool,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum Action {
#[default]
None,
Mode { action: String, arg: i32 },
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct PanelItem<A = Action> {
pub rect: Rect,
pub text: String,
pub style: PanelStyle,
pub action: A,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Panel<A = Action> {
pub rect: Rect,
pub items: Vec<PanelItem<A>>,
}
impl<A> Panel<A> {
pub fn map<B>(self, mut f: impl FnMut(A) -> B) -> Panel<B> {
Panel {
rect: self.rect,
items: self.items.into_iter().map(|i| PanelItem { rect: i.rect, text: i.text, style: i.style, action: f(i.action) }).collect(),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DockSize {
#[serde(default)]
pub permille: Option<u16>,
#[serde(default)]
pub full: bool,
}
pub fn dock_chrome_height(cell_h: i32) -> i32 {
cell_h + 10
}
impl DockSize {
pub fn height(&self, area: Rect, cell_h: i32, default_h: i32) -> i32 {
let min = (cell_h * 4).min(area.h.max(0));
if self.full {
return area.h.max(min);
}
let max = (area.h - cell_h * 2).max(min);
match self.permille {
Some(p) => ((area.h as i64 * p as i64 / 1000) as i32).clamp(min, max),
None => default_h,
}
}
pub fn from_payload(v: &serde_json::Value) -> Self {
serde_json::from_value(v["dock"].clone()).unwrap_or_default()
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct MenuEntry {
pub id: String,
pub arg: i32,
pub label: String,
pub key: String,
pub enabled: bool,
}
#[cfg(test)]
mod dock_tests {
use super::*;
#[test]
fn dock_size_heights() {
let a = Rect { x: 0, y: 0, w: 800, h: 600 };
assert_eq!(DockSize::default().height(a, 20, 300), 300);
assert_eq!(DockSize { permille: Some(750), full: false }.height(a, 20, 300), 450);
assert_eq!(DockSize { permille: Some(1000), full: false }.height(a, 20, 300), 560, "two rows stay the shell's");
assert_eq!(DockSize { permille: Some(10), full: false }.height(a, 20, 300), 80, "at least four rows");
assert_eq!(DockSize { permille: None, full: true }.height(a, 20, 300), 600);
let p = serde_json::json!({"dock": {"permille": 500, "full": false}});
assert_eq!(DockSize::from_payload(&p).permille, Some(500));
assert_eq!(DockSize::from_payload(&serde_json::json!({})), DockSize::default(), "an older host");
assert_eq!(dock_chrome_height(18), 28);
}
}