use std::rc::Rc;
use crate::state::PackedState;
#[derive(Clone, Copy, PartialEq)]
pub struct Keybind {
pub key: &'static str,
pub alt: bool,
pub ctrl: bool,
}
impl Keybind {
pub(crate) fn matches(&self, key: &str, alt: bool, ctrl: bool) -> bool {
self.key == key && self.alt == alt && self.ctrl == ctrl
}
}
#[derive(Clone, Copy, Default, PartialEq)]
pub struct Keybinds {
pub undo: Keybind = Keybind { key: "u", alt: false, ctrl: false },
pub redo: Keybind = Keybind { key: "U", alt: false, ctrl: false },
pub close: Keybind = Keybind { key: "Backspace", alt: false, ctrl: false },
pub maximize: Keybind = Keybind { key: "f", alt: false, ctrl: false },
pub help: Keybind = Keybind { key: "?", alt: false, ctrl: false },
pub save: Keybind = Keybind { key: "s", alt: true, ctrl: false },
pub publish: Keybind = Keybind { key: "S", alt: true, ctrl: false },
}
pub type Action = Rc<dyn Fn(&mut PackedState)>;
pub enum Saved {
Cached { band: Band },
Published { band: Band, json: String },
}
#[derive(Clone, Default)]
pub struct Config {
pub keybinds: Keybinds,
pub actions: Vec<(Keybind, Action)>,
pub steps: u32 = 64,
pub rows: u32 = 36,
pub storage_key: Option<String> = None,
pub on_save: Option<Rc<dyn Fn(Saved)>> = None,
pub title_h_rem: f64 = 2.0,
}
impl PartialEq for Config {
fn eq(&self, other: &Self) -> bool {
self.keybinds == other.keybinds
&& self.steps == other.steps
&& self.rows == other.rows
&& self.storage_key == other.storage_key
&& self.on_save.is_some() == other.on_save.is_some()
&& self.title_h_rem == other.title_h_rem
&& self.actions.len() == other.actions.len()
&& self.actions.iter().zip(&other.actions).all(|(a, b)| a.0 == b.0)
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum Breakpoint {
Xs,
Sm,
Md,
Lg,
#[default]
Xl,
}
impl Breakpoint {
pub(crate) fn of(width: f64) -> Self {
match width {
w if w < 576.0 => Self::Xs,
w if w < 768.0 => Self::Sm,
w if w < 992.0 => Self::Md,
w if w < 1200.0 => Self::Lg,
_ => Self::Xl,
}
}
const fn design(self) -> f64 {
match self {
Self::Xs => 576.0,
Self::Sm => 768.0,
Self::Md => 992.0,
Self::Lg => 1200.0,
Self::Xl => 1600.0,
}
}
pub(crate) fn scale_cols(self, base: u32) -> u32 {
((base as f64 * self.design() / Self::Xl.design()).round() as u32).max(1)
}
pub(crate) const fn band(self) -> Band {
match self {
Self::Xs | Self::Sm => Band::Sm,
Self::Md => Band::Md,
Self::Lg | Self::Xl => Band::Xl,
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum Band {
Sm,
Md,
#[default]
Xl,
}
impl std::fmt::Display for Band {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Sm => "sm",
Self::Md => "md",
Self::Xl => "xl",
})
}
}
impl std::fmt::Display for Breakpoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Xs => "xs",
Self::Sm => "sm",
Self::Md => "md",
Self::Lg => "lg",
Self::Xl => "xl",
})
}
}