use std::rc::Rc;
use crate::draw::{Rect, Rgb};
use crate::host::WindowId;
use crate::player::Status;
use crate::text::TextData;
pub type ObjId = usize;
pub type ContainerId = usize;
pub fn to_bool(v: &str) -> bool {
v == "1" || v.eq_ignore_ascii_case("true")
}
pub fn to_int_lenient(v: &str) -> Option<i32> {
let t = v.trim();
t.parse::<i32>()
.ok()
.or_else(|| crate::maki::value::parse_double(t).map(crate::maki::value::double_to_int))
}
pub fn parse_rgb(v: &str) -> Option<Rgb> {
let name = v.trim().to_lowercase();
let parts: Vec<Option<i32>> = v.split(',').map(|p| p.trim().parse::<i32>().ok()).collect();
if name == "white" {
return Some(Rgb(0xffffff));
}
if name == "black" {
return Some(Rgb(0));
}
if parts.len() >= 3 && parts.iter().all(|p| p.is_some()) {
let r = parts[0].unwrap_or(0);
let g = parts[1].unwrap_or(0);
let b = parts[2].unwrap_or(0);
return Some(Rgb(((r << 16) | (g << 8) | b) as u32));
}
let hex = v.trim().strip_prefix('#').unwrap_or(v.trim());
i64::from_str_radix(hex, 16)
.ok()
.filter(|_| !hex.is_empty())
.map(|n| Rgb((n & 0xffffff) as u32))
}
#[derive(Debug, Clone)]
pub struct GuiBase {
pub tag: String,
pub id: String,
pub parent: Option<ObjId>,
pub x: Option<i32>,
pub y: Option<i32>,
pub w: Option<i32>,
pub h: Option<i32>,
pub default_w: Option<i32>,
pub default_h: Option<i32>,
pub relat_x: i32,
pub relat_y: i32,
pub relat_w: i32,
pub relat_h: i32,
pub visible: bool,
pub active_alpha: i32,
pub inactive_alpha: i32,
pub ghost: bool,
pub enabled: bool,
pub sysregion: i32,
pub sysregion_set: bool,
pub tooltip: Option<String>,
pub dbl_click_action: Option<String>,
pub move_window: Option<bool>,
pub pad_left: i32,
pub pad_right: i32,
pub attrs: Vec<(String, String)>,
pub declared_resize: Option<String>,
pub rect: Rect,
pub effective_alpha: i32,
pub rect_rgn: bool,
pub target: [Option<i32>; 5],
pub target_seconds: f64,
pub move_from: Option<[i32; 5]>,
pub move_to: Option<[Option<i32>; 5]>,
pub move_start: Option<i64>,
pub move_millis: i64,
}
impl GuiBase {
pub fn new(tag: &str) -> Self {
GuiBase {
tag: tag.to_string(),
id: String::new(),
parent: None,
x: None,
y: None,
w: None,
h: None,
default_w: None,
default_h: None,
relat_x: 0,
relat_y: 0,
relat_w: 0,
relat_h: 0,
visible: true,
active_alpha: 255,
inactive_alpha: 255,
ghost: false,
enabled: true,
sysregion: 0,
sysregion_set: false,
tooltip: None,
dbl_click_action: None,
move_window: None,
pad_left: 0,
pad_right: 0,
attrs: Vec::new(),
declared_resize: None,
rect: Rect::EMPTY,
effective_alpha: 255,
rect_rgn: false,
target: [None; 5],
target_seconds: 0.0,
move_from: None,
move_to: None,
move_start: None,
move_millis: 0,
}
}
pub fn alpha(&self) -> i32 {
self.active_alpha
}
pub fn set_alpha(&mut self, v: i32) {
self.active_alpha = v;
self.inactive_alpha = v;
}
pub fn attr(&self, key: &str) -> Option<&str> {
self.attrs
.iter()
.find(|(k, _)| k == key)
.map(|(_, v)| v.as_str())
}
pub fn set_attr(&mut self, key: &str, value: &str) {
match self.attrs.iter_mut().find(|(k, _)| k == key) {
Some(entry) => entry.1 = value.to_string(),
None => self.attrs.push((key.to_string(), value.to_string())),
}
}
pub fn paint_key(&self) -> i32 {
(self.active_alpha << 9) | (self.inactive_alpha << 1) | (self.enabled as i32)
}
pub fn placement(&self) -> [Option<i32>; 12] {
[
self.x,
self.y,
self.w,
self.h,
self.default_w,
self.default_h,
Some(self.relat_x),
Some(self.relat_y),
Some(self.relat_w),
Some(self.relat_h),
Some(self.pad_left),
Some(self.pad_right),
]
}
}
#[derive(Debug, Clone, Default)]
pub struct GroupData {
pub children: Vec<ObjId>,
pub background: Option<String>,
pub draw_background: bool,
pub instance_id: Option<String>,
pub auto_width_source: Option<String>,
pub auto_height_source: Option<String>,
pub system_objects: Vec<usize>,
pub behavior: Option<Behavior>,
pub xui_params: Vec<(String, String)>,
pub embed_xui: Option<String>,
}
#[derive(Debug, Clone)]
pub enum Behavior {
StandardFrame {
area: [i32; 4],
},
TitleBar {
pad_left: i32,
pad_right: i32,
},
}
#[derive(Debug, Clone)]
pub struct LayoutData {
pub window_id: WindowId,
pub container: Option<ContainerId>,
pub min_w: i32,
pub min_h: i32,
pub max_w: i32,
pub max_h: i32,
pub user_size: Option<(i32, i32)>,
pub desktop_alpha: bool,
pub snap_left: i32,
pub snap_top: i32,
pub snap_right: i32,
pub snap_bottom: i32,
pub view_x: i32,
pub free_placed: bool,
pub view_y: i32,
pub edge_x: Option<crate::host::Edge>,
pub edge_y: Option<crate::host::Edge>,
pub link_width: Option<String>,
pub link_height: Option<String>,
pub composed: Option<crate::draw::Region>,
pub cuts: Vec<crate::draw::Region>,
pub composed_at: i64,
pub shape: Option<crate::draw::Region>,
pub shape_at: i64,
pub right_now: i32,
pub right_at: i64,
pub bottom_now: i32,
pub bottom_at: i64,
}
impl LayoutData {
pub fn new(window_id: WindowId) -> Self {
LayoutData {
window_id,
container: None,
min_w: 0,
min_h: 0,
max_w: 0,
max_h: 0,
user_size: None,
desktop_alpha: false,
snap_left: 0,
snap_top: 0,
snap_right: 0,
snap_bottom: 0,
view_x: 0,
free_placed: false,
view_y: 0,
edge_x: None,
edge_y: None,
link_width: None,
link_height: None,
composed: None,
cuts: Vec::new(),
composed_at: -1,
shape: None,
shape_at: -1,
right_now: 0,
right_at: -1,
bottom_now: 0,
bottom_at: -1,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct LayerData {
pub image: Option<String>,
pub tile: bool,
pub inactive_image: Option<String>,
}
#[derive(Debug, Clone)]
pub struct AnimData {
pub frame_w: Option<i32>,
pub frame_h: Option<i32>,
pub frame: i32,
pub vertical_set: Option<bool>,
pub start_frame: i32,
pub end_frame: Option<i32>,
pub speed: i32,
pub auto_replay: bool,
pub auto_play: bool,
pub playing: bool,
pub paused: bool,
pub step: i32,
pub next_at: i64,
}
impl Default for AnimData {
fn default() -> Self {
AnimData {
frame_w: None,
frame_h: None,
frame: 0,
vertical_set: None,
start_frame: 0,
end_frame: None,
speed: 200,
auto_replay: true,
auto_play: false,
playing: false,
paused: false,
step: 1,
next_at: 0,
}
}
}
#[derive(Debug, Clone)]
pub enum ButtonKind {
Plain,
Toggle {
cfg_attrib: Option<String>,
cfg_val: i32,
auto_toggle: bool,
},
NStates {
states: i32,
values: Option<Vec<i32>>,
cfg_attrib: Option<String>,
local_state: i32,
auto_elements: bool,
},
}
#[derive(Debug, Clone)]
pub struct ButtonData {
pub kind: ButtonKind,
pub image: Option<String>,
pub down_image: Option<String>,
pub hover_image: Option<String>,
pub active_image: Option<String>,
pub action: Option<String>,
pub param: Option<String>,
pub action_target: Option<String>,
pub pressed: bool,
pub activated: bool,
pub borders: bool,
pub center_image: bool,
pub label: Option<String>,
pub drawn_state: Option<i32>,
}
impl ButtonData {
pub fn new(kind: ButtonKind) -> Self {
ButtonData {
kind,
image: None,
down_image: None,
hover_image: None,
active_image: None,
action: None,
param: None,
action_target: None,
pressed: false,
activated: false,
borders: false,
center_image: false,
label: None,
drawn_state: None,
}
}
}
#[derive(Debug, Clone)]
pub struct SliderData {
pub action: Option<String>,
pub param: Option<String>,
pub thumb: Option<String>,
pub down_thumb: Option<String>,
pub bar_left: Option<String>,
pub bar_middle: Option<String>,
pub bar_right: Option<String>,
pub vertical: bool,
pub low: i32,
pub high: i32,
pub low_set: bool,
pub high_set: bool,
pub position: i32,
pub dragging: bool,
pub locked: bool,
pub grab: i32,
pub synced: bool,
}
impl Default for SliderData {
fn default() -> Self {
SliderData {
action: None,
param: None,
thumb: None,
down_thumb: None,
bar_left: None,
bar_middle: None,
bar_right: None,
vertical: false,
low: 0,
high: 255,
low_set: false,
high_set: false,
position: 0,
dragging: false,
locked: false,
grab: 0,
synced: false,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct StatusData {
pub stop: Option<String>,
pub play: Option<String>,
pub pause: Option<String>,
}
impl StatusData {
pub fn image(&self, status: Status) -> Option<&str> {
match status {
Status::Stopped => self.stop.as_deref(),
Status::Playing => self.play.as_deref(),
Status::Paused => self.pause.as_deref(),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct GridData {
pub parts: Vec<(String, String)>,
}
#[derive(Debug, Clone)]
pub struct ProgressData {
pub orientation: String,
pub drawn_filled: i32,
pub asked_position: i32,
pub asked_filled: i32,
pub asked_due: i64,
}
impl Default for ProgressData {
fn default() -> Self {
ProgressData {
orientation: "right".into(),
drawn_filled: -1,
asked_position: -1,
asked_filled: -1,
asked_due: 0,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct RectData {
pub color: Rgb,
pub color_id: Option<String>,
pub filled: bool,
}
#[derive(Debug, Clone, Default)]
pub struct ImagesData {
pub image: Option<String>,
pub spacing: i32,
pub source: String,
}
#[derive(Debug, Clone, Default)]
pub struct MenuData {
pub menu: Option<String>,
pub normal: Option<String>,
pub down: Option<String>,
}
#[derive(Debug, Clone)]
pub struct FrameData {
pub vertical: bool,
pub from_far: bool,
pub pos: i32,
pub min_pos: i32,
pub max_pos: Option<i32>,
pub first: Option<ObjId>,
pub second: Option<ObjId>,
pub divider: Option<String>,
pub grabber: Option<String>,
pub resizable: bool,
pub sizer_rect: Rect,
}
impl Default for FrameData {
fn default() -> Self {
FrameData {
vertical: true,
from_far: false,
pos: 128,
min_pos: 0,
max_pos: None,
first: None,
second: None,
divider: None,
grabber: None,
resizable: true,
sizer_rect: Rect::EMPTY,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct BucketData {
pub window_type: Option<String>,
pub vertical: bool,
pub spacing: i32,
pub left_margin: i32,
pub right_margin: i32,
pub scroll: i32,
}
#[derive(Debug, Clone)]
pub struct XFadeData {
pub speed: f64,
pub pages: Vec<(String, ObjId)>,
pub current: Option<ObjId>,
}
impl Default for XFadeData {
fn default() -> Self {
XFadeData {
speed: 0.25,
pages: Vec::new(),
current: None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScopeStyle {
Dots,
Line,
Filled,
}
#[derive(Debug, Clone)]
pub struct VisData {
pub rows: [Rgb; 16],
pub peak_color: Rgb,
pub scope_colors: [Rgb; 5],
pub gamma_group: Option<String>,
pub mode: i32,
pub scope_style: ScopeStyle,
pub peaks: bool,
pub thin: bool,
pub flip_v: bool,
pub flip_h: bool,
pub falloff: i32,
pub peak_falloff: i32,
pub bars: [f32; 75],
pub peak: [f32; 75],
pub speed: [f32; 75],
pub last_frame_at: i64,
pub scope_at: i64,
pub scope_flat: bool,
}
pub const CLASSIC_BANDS: [u32; 16] = [
0xEF3110, 0xCE2910, 0xD65A00, 0xD66600, 0xD67300, 0xC67B08, 0xDEA518, 0xD6B521, 0xBDDE29,
0x94DE21, 0x29CE10, 0x32BE10, 0x39B510, 0x319C08, 0x299400, 0x188408,
];
pub const CLASSIC_SCOPE: [u32; 5] = [0xFFFFFF, 0xD6D6DE, 0xB5BDBD, 0xA0AAAF, 0x949CA5];
impl Default for VisData {
fn default() -> Self {
VisData {
rows: CLASSIC_BANDS.map(Rgb),
peak_color: Rgb(0x959596),
scope_colors: CLASSIC_SCOPE.map(Rgb),
gamma_group: None,
mode: 1,
scope_style: ScopeStyle::Line,
peaks: true,
thin: false,
flip_v: false,
flip_h: false,
falloff: 2,
peak_falloff: 1,
bars: [0.0; 75],
peak: [0.0; 75],
speed: [3.0; 75],
last_frame_at: -1,
scope_at: -1,
scope_flat: true,
}
}
}
#[derive(Debug, Clone)]
pub struct EqVisData {
pub top: Rgb,
pub middle: Rgb,
pub bottom: Rgb,
pub preamp: Rgb,
pub gamma_group: Option<String>,
}
impl Default for EqVisData {
fn default() -> Self {
EqVisData {
top: Rgb(0xffffff),
middle: Rgb(0xffffff),
bottom: Rgb(0xffffff),
preamp: Rgb(0x888888),
gamma_group: None,
}
}
}
#[derive(Debug, Clone)]
pub struct ThemeListData {
pub selected: Option<String>,
pub header: bool,
pub hscroll: bool,
pub scroll_px: i32,
pub shown_for: Option<String>,
pub down_y: i32,
pub down_scroll: i32,
pub dragged: bool,
}
impl Default for ThemeListData {
fn default() -> Self {
ThemeListData {
selected: None,
header: true,
hscroll: true,
scroll_px: 0,
shown_for: None,
down_y: 0,
down_scroll: 0,
dragged: false,
}
}
}
#[derive(Debug, Clone)]
pub struct GradientData {
pub x1: f32,
pub y1: f32,
pub x2: f32,
pub y2: f32,
pub stops: Vec<(f32, u32)>,
pub gamma_group: Option<String>,
pub made: Option<Rc<[u32]>>,
pub made_for: Option<(i32, i32, Option<crate::draw::Gamma>)>,
}
impl Default for GradientData {
fn default() -> Self {
GradientData {
x1: 0.0,
y1: 0.0,
x2: 1.0,
y2: 0.0,
stops: Vec::new(),
gamma_group: None,
made: None,
made_for: None,
}
}
}
#[derive(Debug, Clone)]
pub struct PlaylistData {
pub selected: i32,
pub scroll_px: i32,
pub down_y: i32,
pub down_scroll: i32,
pub dragged: bool,
}
impl Default for PlaylistData {
fn default() -> Self {
PlaylistData {
selected: -1,
scroll_px: 0,
down_y: 0,
down_scroll: 0,
dragged: false,
}
}
}
#[derive(Debug, Clone)]
pub struct LayoutStatusData {
pub include_only: Option<Vec<String>>,
pub exclude: Vec<String>,
pub background: bool,
pub pressed: Option<usize>,
}
impl Default for LayoutStatusData {
fn default() -> Self {
LayoutStatusData {
include_only: None,
exclude: Vec::new(),
background: true,
pressed: None,
}
}
}
#[derive(Debug, Clone)]
pub enum Kind {
Unknown {
class_name: String,
},
Group,
Layout(Box<LayoutData>),
Layer(LayerData),
AnimatedLayer(LayerData, AnimData),
Button(ButtonData),
Text(Box<TextData>),
Slider(SliderData),
Status(StatusData),
ClassicWindow(crate::classic::ClassicWindow),
Grid(GridData),
ProgressGrid(GridData, ProgressData),
AlbumArt {
not_found: Option<String>,
},
RectObj(RectData),
Images(ImagesData),
Menu(MenuData),
Frame(FrameData),
Bucket(BucketData),
XFade(XFadeData),
Vis(Box<VisData>),
EqVis(EqVisData),
ColorThemeList(ThemeListData),
Gradient(GradientData),
PlaylistView(PlaylistData),
LayoutStatus(LayoutStatusData),
}
#[derive(Debug, Clone)]
pub struct Obj {
pub base: GuiBase,
pub group: Option<GroupData>,
pub kind: Kind,
}
impl Obj {
pub fn new(tag: &str, kind: Kind, grouped: bool) -> Self {
Obj {
base: GuiBase::new(tag),
group: if grouped {
Some(GroupData::default())
} else {
None
},
kind,
}
}
pub fn class_name(&self) -> &str {
match &self.kind {
Kind::Unknown { class_name } => class_name,
Kind::Group => "Group",
Kind::Layout(_) => "Layout",
Kind::Layer(_) => "Layer",
Kind::AnimatedLayer(..) => "AnimatedLayer",
Kind::Button(b) => match b.kind {
ButtonKind::Toggle { .. } => "ToggleButton",
_ => "Button",
},
Kind::Text(_) => "Text",
Kind::Slider(_) => "Slider",
Kind::Status(_) => "Status",
Kind::Grid(_)
| Kind::ProgressGrid(..)
| Kind::AlbumArt { .. }
| Kind::RectObj(_)
| Kind::Images(_)
| Kind::ColorThemeList(_)
| Kind::Gradient(_)
| Kind::PlaylistView(_) => "GuiObject",
Kind::Menu(_) => "Menu",
Kind::Frame(_) | Kind::XFade(_) => "Group",
Kind::Bucket(_) => "ComponentBucket",
Kind::Vis(_) => "Vis",
Kind::ClassicWindow(_) => "ClassicWindow",
Kind::EqVis(_) => "EqVis",
Kind::LayoutStatus(_) => "LayoutStatus",
}
}
pub fn is_layout(&self) -> bool {
matches!(self.kind, Kind::Layout(_))
}
pub fn children(&self) -> &[ObjId] {
self.group
.as_ref()
.map(|g| g.children.as_slice())
.unwrap_or(&[])
}
pub fn layout(&self) -> Option<&LayoutData> {
match &self.kind {
Kind::Layout(l) => Some(l),
_ => None,
}
}
pub fn layout_mut(&mut self) -> Option<&mut LayoutData> {
match &mut self.kind {
Kind::Layout(l) => Some(l),
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct Container {
pub id: String,
pub attrs: Vec<(String, String)>,
pub layouts: Vec<ObjId>,
pub current: Option<ObjId>,
pub visible: bool,
pub name: String,
pub default_x: i32,
pub default_y: i32,
pub has_default_spot: bool,
pub scale: f32,
}
impl Container {
pub fn new(id: &str, attrs: Vec<(String, String)>) -> Self {
let attr = |k: &str| {
attrs
.iter()
.find(|(key, _)| key == k)
.map(|(_, v)| v.clone())
};
Container {
id: id.to_string(),
visible: attr("default_visible").map(|v| to_bool(&v)).unwrap_or(true),
name: attr("name").unwrap_or_else(|| id.to_string()),
default_x: attr("default_x")
.and_then(|v| to_int_lenient(&v))
.unwrap_or(0),
default_y: attr("default_y")
.and_then(|v| to_int_lenient(&v))
.unwrap_or(0),
has_default_spot: attr("default_x").is_some() || attr("default_y").is_some(),
scale: 1.0,
attrs,
layouts: Vec::new(),
current: None,
}
}
pub fn attr(&self, key: &str) -> Option<&str> {
self.attrs
.iter()
.find(|(k, _)| k == key)
.map(|(_, v)| v.as_str())
}
pub fn can_close(&self) -> bool {
self.attr("canclose").map(to_bool).unwrap_or(true)
}
}
pub type Shared = Rc<str>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn colors_parse_as_winamp_writes_them() {
assert_eq!(parse_rgb("255,0,128"), Some(Rgb(0xff0080)));
assert_eq!(parse_rgb("#00ff00"), Some(Rgb(0x00ff00)));
assert_eq!(parse_rgb("White"), Some(Rgb(0xffffff)));
assert_eq!(parse_rgb("wasabi.text"), None);
}
#[test]
fn lenient_ints() {
assert_eq!(to_int_lenient(" 12 "), Some(12));
assert_eq!(to_int_lenient("12.7"), Some(12));
assert_eq!(to_int_lenient("x"), None);
}
}