use super::*;
use std::ffi::CString;
use std::os::raw::{c_char, c_void};
use day_core::{BuildCx, Flex, Layout, LayoutOps, RNode, with_tree};
use day_spec::{Event, NodeId, Proposal, Rect, Size};
use day_toolkit_winui::{WinHandle, Xaml};
const KIND: &str = "day-piece-settings.row";
const PAD: f64 = 16.0;
const GAP: f64 = 16.0;
const MIN_HEIGHT: f64 = 68.0;
const DEFAULT_WIDTH: f64 = 480.0;
const GLYPH_CHEVRON: u32 = 0xE974;
const GLYPH_OPEN: u32 = 0xE8A7;
pub(super) fn sidebar_symbols(nav: AnyPiece, groups: &[&Group]) -> AnyPiece {
use day_toolkit_winui::xaml_raw;
let paths = groups
.iter()
.map(|g| match &g.icon {
Some(GroupIcon::Symbol(symbol)) => symbol.outline_path().unwrap_or_default(),
_ => "",
})
.collect::<Vec<_>>()
.join("\u{1f}");
nav.tweak(xaml_raw(move |abi, _| unsafe {
day_settings_xaml_sidebar_symbols(abi, cstr(&paths).as_ptr());
}))
.any()
}
unsafe extern "C" {
fn day_settings_xaml_sidebar_symbols(abi: *mut c_void, paths: *const c_char);
fn day_settings_xaml_card_new(
header: *const c_char,
description: *const c_char,
glyph: u32,
clickable: i32,
destructive: i32,
id: u64,
cb: extern "C" fn(u64),
) -> *mut c_void;
fn day_settings_xaml_card_set_text(
w: *mut c_void,
header: *const c_char,
description: *const c_char,
);
fn day_settings_xaml_card_measure(
w: *mut c_void,
text_w: f64,
out_w: *mut f64,
out_h: *mut f64,
);
fn day_settings_xaml_card_place_text(w: *mut c_void, x: f64, y: f64, width: f64, height: f64);
}
#[derive(Clone, Debug, PartialEq)]
struct CardProps {
header: String,
description: String,
glyph: u32,
clickable: bool,
destructive: bool,
}
#[derive(Clone, Debug, PartialEq)]
enum CardPatch {
Text { header: String, description: String },
}
extern "C" fn on_card_tap(id: u64) {
day_toolkit_winui::emit(NodeId(id), Event::Tap(day_spec::Point::ZERO));
}
fn cstr(s: &str) -> CString {
CString::new(s).unwrap_or_default()
}
fn make(_backend: &mut Xaml, p: &CardProps, id: NodeId) -> WinHandle {
WinHandle(unsafe {
day_settings_xaml_card_new(
cstr(&p.header).as_ptr(),
cstr(&p.description).as_ptr(),
p.glyph,
i32::from(p.clickable),
i32::from(p.destructive),
id.0,
on_card_tap,
)
})
}
fn update(_backend: &mut Xaml, h: &WinHandle, patch: &CardPatch) {
let CardPatch::Text {
header,
description,
} = patch;
unsafe {
day_settings_xaml_card_set_text(h.0, cstr(header).as_ptr(), cstr(description).as_ptr());
}
}
fn measure(_backend: &mut Xaml, h: &WinHandle, p: Proposal) -> Size {
let mut w = 0.0;
let mut hh = 0.0;
unsafe {
day_settings_xaml_card_measure(h.0, p.width.unwrap_or(-1.0), &raw mut w, &raw mut hh);
};
Size::new(p.width.unwrap_or(w), hh)
}
day_spec::renderer!(day_toolkit_winui::RENDERERS, Xaml,
kind: KIND, props: CardProps, patch: CardPatch,
make: make, update: update, measure: measure);
#[derive(Default)]
struct CardLayout {
handle: std::cell::Cell<*mut c_void>,
}
impl CardLayout {
fn control_size(cx: &mut dyn LayoutOps, c: RNode, row_w: f64) -> Size {
let content_w = (row_w - 2.0 * PAD).max(0.0);
let nat = cx.measure_child(c, Proposal::new(None, None));
let w = if cx.flex_of(c).grow_w {
let share = (content_w * 0.45).min((content_w - 140.0).max(0.0));
nat.width.max(share).min(content_w)
} else {
nat.width.min(content_w)
};
let s = cx.measure_child(c, Proposal::new(Some(w), None));
Size::new(w, s.height)
}
fn text_width(row_w: f64, control: Option<Size>) -> f64 {
let content_w = (row_w - 2.0 * PAD).max(0.0);
match control {
Some(s) if s.width > 0.0 => (content_w - s.width - GAP).max(0.0),
_ => content_w,
}
}
fn content(
cx: &mut dyn LayoutOps,
children: &[RNode],
row_w: f64,
) -> (Option<Size>, Size, bool) {
let control = children.first().map(|&c| Self::control_size(cx, c, row_w));
let natural_text = cx.measure_leaf(Proposal::new(None, None));
let stacked =
control.is_some() && Self::text_width(row_w, control) < natural_text.width.min(140.0);
let width = Self::text_width(row_w, if stacked { None } else { control });
let text = cx.measure_leaf(Proposal::new(Some(width), None));
(control, Size::new(width, text.height), stacked)
}
}
impl Layout for CardLayout {
fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
let row_w = p.width.unwrap_or(DEFAULT_WIDTH);
let (control, text, stacked) = Self::content(cx, children, row_w);
let inner = if stacked {
text.height + GAP + control.map_or(0.0, |s| s.height)
} else {
text.height.max(control.map_or(0.0, |s| s.height))
};
Size::new(row_w, (inner + 2.0 * PAD).max(MIN_HEIGHT).ceil())
}
fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
let row_w = bounds.size.width;
let (control, text, stacked) = Self::content(cx, children, row_w);
unsafe {
day_settings_xaml_card_place_text(
self.handle.get(),
PAD,
if stacked { PAD } else { 0.0 },
text.width,
if stacked {
text.height
} else {
bounds.size.height
},
);
}
if let (Some(&c), Some(s)) = (children.first(), control) {
let x = (row_w - PAD - s.width).max(PAD);
let y = if stacked {
PAD + text.height + GAP
} else {
((bounds.size.height - s.height) / 2.0).max(0.0)
};
cx.place_child(c, Rect::new(x, y, s.width, s.height));
}
}
}
struct Card {
props: CardProps,
title: TextSource,
caption: Option<TextSource>,
control: Option<AnyPiece>,
on_tap: Option<Rc<dyn Fn()>>,
}
impl Piece for Card {
fn build(self, cx: &mut BuildCx) -> RNode {
let Card {
props,
title,
caption,
control,
on_tap,
} = self;
let layout = Rc::new(CardLayout::default());
let node = cx.native(
KIND,
&props,
layout.clone(),
Flex {
grow_w: true,
..Default::default()
},
day_core::Boundary::No,
);
with_tree(|t| {
if let Some(handle) = t
.node_handle_any(node)
.and_then(|h| h.downcast::<WinHandle>().ok())
{
layout.handle.set(handle.0);
}
});
if let Some(control) = control {
cx.under(node, |cx| {
let _ = control.build(cx);
});
}
if let Some(on_tap) = on_tap {
cx.on(node, move |ev| {
if let Event::Tap(_) = ev {
on_tap();
}
});
}
let dynamic =
matches!(title, TextSource::Dyn(_)) || matches!(caption, Some(TextSource::Dyn(_)));
if dynamic {
watch(
move || (resolve_text(&title), caption.as_ref().map(resolve_text)),
move |(header, description), _| {
let patch = CardPatch::Text {
header: header.clone(),
description: description.clone().unwrap_or_default(),
};
with_tree(|t| t.patch(node, Box::new(patch), true));
},
);
}
node
}
}
pub(super) fn row_piece(inst: Rc<Installed>, r: &Row, _desktop: bool) -> Option<AnyPiece> {
let id = r.id.clone();
let enabled = r.enabled.clone();
let with_id = |p: AnyPiece| match &id {
Some(id) => p.id(id.clone()).any(),
None => p,
};
let enabled_fn = move || enabled.as_ref().is_none_or(day_pieces::Reactive::get);
let named = |p: AnyPiece| {
let title = text_source_fn(&r.title);
p.a11y(move |a| a.label(title)).any()
};
let mut props = CardProps {
header: r.title.initial(),
description: r.caption.as_ref().map_or_default(TextSource::initial),
glyph: 0,
clickable: false,
destructive: false,
};
let mut on_tap: Option<Rc<dyn Fn()>> = None;
let mut caption_only = false;
let mut show_caption_only = |props: &mut CardProps, c: &TextSource| {
props.header = String::new();
props.description = c.initial();
caption_only = true;
};
let control: Option<AnyPiece> = match &r.kind {
RowKind::Toggle(sig) => Some(with_id(named(toggle(*sig).enabled(enabled_fn).any()))),
RowKind::Choice {
labels,
index,
style,
} => {
let p = choice_picker(labels, *index, *style == ChoiceStyle::Segmented, enabled_fn);
Some(with_id(named(p.any())))
}
RowKind::Slider {
value,
range,
step,
value_text,
} => {
let mut s = slider(*value).range(range.clone()).enabled(enabled_fn);
if let Some(st) = step {
s = s.step(*st);
}
let ctl = match &id {
Some(i) => named(
s.id(i.clone())
.grow(Dimension::Width)
.min_width(140.0)
.any(),
),
None => named(s.grow(Dimension::Width).min_width(140.0).any()),
};
Some(match value_text {
Some(f) => {
let f = f.clone();
let v = *value;
row((ctl, label(move || f(v.get())).tabular().secondary()))
.spacing(8.0)
.align(VAlign::Center)
.grow(Dimension::Width)
.any()
}
None => ctl,
})
}
RowKind::Stepper { value, range, step } => {
let v = *value;
let (dec, inc) = stepper_buttons(v, range, *step, enabled_fn);
Some(stepper_control(v, dec, inc, id.as_ref()))
}
RowKind::Text {
value,
placeholder,
secure,
} => {
let mut f = if *secure {
secure_field(*value)
} else {
text_field(*value)
};
if let Some(ph) = placeholder {
f = f.placeholder(text_source_fn(ph));
}
Some(named(match &id {
Some(i) => f
.enabled(enabled_fn)
.id(i.clone())
.grow(Dimension::Width)
.min_width(160.0)
.any(),
None => f
.enabled(enabled_fn)
.grow(Dimension::Width)
.min_width(160.0)
.any(),
}))
}
RowKind::Action {
on,
destructive,
confirm,
confirm_label,
busy,
} => {
props.destructive = *destructive;
let on = on.clone();
let title = text_source_fn(&r.title);
let confirm = confirm.clone();
let confirm_label = confirm_label.clone();
let busy = busy.clone();
let busy_gate = busy.clone();
let run = move || {
let on = on.clone();
match &confirm {
Some(message) => {
let message = message.initial();
let title = title();
let label = confirm_label.as_ref().map(TextSource::initial);
day_core::task(async move {
if crate::confirm_dialog(&title, &message, label).await {
on();
}
});
}
None => on(),
}
};
let b = button(text_source_fn(&r.title))
.action(run)
.enabled(move || {
enabled_fn() && !busy_gate.as_ref().is_some_and(day_pieces::Reactive::get)
});
let ctl = with_id(b.any());
if let Some(c) = &r.caption {
show_caption_only(&mut props, c);
}
Some(match busy {
Some(b) => row((when(move || b.get(), spinner), ctl))
.spacing(8.0)
.align(VAlign::Center)
.any(),
None => ctl,
})
}
RowKind::Link(url) => {
let url = url.clone();
if let Some(c) = &r.caption {
show_caption_only(&mut props, c);
Some(with_id(link(text_source_fn(&r.title), url).any()))
} else {
props.glyph = GLYPH_OPEN;
props.clickable = true;
on_tap = Some(Rc::new(move || day_core::open_url(&url)));
None
}
}
RowKind::Navigate(build) => {
props.glyph = GLYPH_CHEVRON;
props.clickable = true;
let build = build.clone();
let title = text_source_fn(&r.title);
on_tap = Some(Rc::new(move || {
push_subpage(inst.clone(), title(), build.clone());
}));
None
}
RowKind::Info { value, copyable } => {
let v = text_label(value).secondary();
let v = if *copyable {
let text = value.clone();
v.on_tap(move || {
day_part_clipboard::set_text(&text.initial());
})
.any()
} else {
v.any()
};
Some(with_id(v))
}
RowKind::Note | RowKind::Custom(_) => return None,
};
let (title, caption) = if caption_only {
(TextSource::Static(String::new()), r.caption.clone())
} else {
(r.title.clone(), r.caption.clone())
};
let clickable = on_tap.is_some();
let card = Card {
props,
title,
caption,
control,
on_tap,
};
let piece = AnyPiece::new(card);
Some(match &id {
Some(id) if clickable => piece.id(id.clone()).any(),
_ => piece,
})
}