use objc2::rc::Retained;
use objc2::runtime::{NSObject, NSObjectProtocol, ProtocolObject};
use objc2::{DefinedClass, MainThreadMarker, MainThreadOnly, define_class, msg_send, sel};
use objc2_app_kit::{
NSButton, NSComboBoxDelegate, NSControlStateValueOff, NSControlStateValueOn, NSControlTextEditingDelegate, NSSearchFieldDelegate, NSSecureTextField, NSSwitch,
NSTextField, NSTextFieldDelegate, NSView,
};
use objc2_foundation::{NSNotification, NSString};
use crate::signal::{Effect, Signal, batch, untrack};
use ferrokit_core::text::Color;
use crate::appkit_views::{ActionTarget, text};
use crate::view::{Cx, Node, Size, View, ViewExt, hstack};
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "FerrokitFieldDelegate"]
#[ivars = Signal<String>]
pub(crate) struct FieldDelegate;
unsafe impl NSObjectProtocol for FieldDelegate {}
unsafe impl NSControlTextEditingDelegate for FieldDelegate {
#[unsafe(method(controlTextDidChange:))]
fn did_change(&self, notification: &NSNotification) {
let Some(field) = notification.object().and_then(|o| o.downcast::<NSTextField>().ok()) else { return };
let value = field.stringValue().to_string();
let signal = *self.ivars();
batch(|| signal.set(value));
}
}
unsafe impl NSTextFieldDelegate for FieldDelegate {}
unsafe impl NSSearchFieldDelegate for FieldDelegate {}
);
impl FieldDelegate {
pub(crate) fn new(mtm: MainThreadMarker, value: Signal<String>) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(value);
unsafe { msg_send![super(this), init] }
}
}
pub struct TextField {
value: Signal<String>,
placeholder: Option<String>,
on_submit: Option<Box<dyn Fn()>>,
secure: bool,
on_end_editing: bool,
}
impl TextField {
pub fn placeholder(mut self, text: &str) -> Self {
self.placeholder = Some(text.into());
self
}
pub fn on_submit(mut self, f: impl Fn() + 'static) -> Self {
self.on_submit = Some(Box::new(f));
self
}
pub fn on_commit(mut self, f: impl Fn() + 'static) -> Self {
self.on_submit = Some(Box::new(f));
self.on_end_editing = true;
self
}
}
impl View for TextField {
fn build(self, cx: &mut Cx) -> Node {
let mtm = cx.mtm;
let field: Retained<NSTextField> = if self.secure {
Retained::into_super(NSSecureTextField::new(mtm))
} else {
NSTextField::new(mtm)
};
if let Some(p) = &self.placeholder {
field.setPlaceholderString(Some(&NSString::from_str(p)));
}
let delegate = FieldDelegate::new(mtm, self.value);
unsafe { field.setDelegate(Some(ProtocolObject::from_ref(&*delegate))) };
let (mut node, _) = Node::leaf(
cx,
Retained::into_super(Retained::into_super(field.clone())),
(true, false),
Size { w: 120.0, h: 0.0 },
);
if let Some(f) = self.on_submit {
let target = ActionTarget::new(mtm, f);
unsafe {
field.setTarget(Some(&target));
field.setAction(Some(sel!(fire:)));
}
node.keep(Retained::into_super(target));
if self.on_end_editing
&& let Some(cell) = field.cell()
{
cell.setSendsActionOnEndEditing(true);
}
}
let value = self.value;
node.own(Effect::new(move || {
let v = value.get();
if field.stringValue().to_string() != v {
field.setStringValue(&NSString::from_str(&v));
}
}));
node.keep(Retained::into_super(delegate));
node
}
}
pub fn text_field(value: &Signal<String>) -> TextField {
TextField { value: *value, placeholder: None, on_submit: None, on_end_editing: false, secure: false }
}
pub fn secure_field(value: &Signal<String>) -> TextField {
TextField { value: *value, placeholder: None, on_submit: None, on_end_editing: false, secure: true }
}
fn state_of(on: bool) -> isize {
if on { NSControlStateValueOn } else { NSControlStateValueOff }
}
fn bound_switch(cx: &mut Cx, control: Retained<NSView>, set_state: impl Fn(isize) + 'static, get_state: impl Fn() -> isize + 'static, value: Signal<bool>) -> (Node, Retained<NSObject>) {
let v = value;
let get = std::rc::Rc::new(get_state);
let g = get.clone();
let target = ActionTarget::new(cx.mtm, move || v.set(g() == NSControlStateValueOn));
let (mut node, _) = Node::leaf(cx, control, (false, false), Size::default());
node.own(Effect::new(move || {
let want = state_of(value.get());
if get() != want {
set_state(want);
}
}));
(node, Retained::into_super(target))
}
pub struct Checkbox {
title: String,
value: Signal<bool>,
}
impl View for Checkbox {
fn build(self, cx: &mut Cx) -> Node {
let mtm = cx.mtm;
let b = unsafe { NSButton::checkboxWithTitle_target_action(&NSString::from_str(&self.title), None, None, mtm) };
let (b1, b2) = (b.clone(), b.clone());
let (mut node, target) = bound_switch(
cx,
Retained::into_super(Retained::into_super(b.clone())),
move |s| b1.setState(s),
move || b2.state(),
self.value,
);
unsafe {
b.setTarget(Some(&target));
b.setAction(Some(sel!(fire:)));
}
node.keep(target);
node
}
}
pub(crate) fn bound_toggle(cx: &mut Cx, view: Retained<objc2_app_kit::NSView>, b: Retained<NSButton>, value: Signal<bool>) -> Node {
let (b1, b2) = (b.clone(), b.clone());
let (mut node, target) = bound_switch(cx, view, move |s| b1.setState(s), move || b2.state(), value);
unsafe {
b.setTarget(Some(&target));
b.setAction(Some(sel!(fire:)));
}
node.keep(target);
node
}
pub fn checkbox(title: &str, value: &Signal<bool>) -> Checkbox {
Checkbox { title: title.into(), value: *value }
}
struct SwitchView(Signal<bool>);
impl View for SwitchView {
fn build(self, cx: &mut Cx) -> Node {
let s = NSSwitch::new(cx.mtm);
let (s1, s2) = (s.clone(), s.clone());
let (mut node, target) = bound_switch(
cx,
Retained::into_super(Retained::into_super(s.clone())),
move |v| s1.setState(v),
move || s2.state(),
self.0,
);
unsafe {
s.setTarget(Some(&target));
s.setAction(Some(sel!(fire:)));
}
node.keep(target);
node
}
}
pub fn toggle(title: &str, value: &Signal<bool>) -> impl View {
if title.is_empty() {
return crate::view::AnyView::new(SwitchView(*value));
}
crate::view::AnyView::new(hstack((text(title), SwitchView(*value).accessibility_label(title))))
}
fn on_action(cx: &Cx, control: &objc2_app_kit::NSControl, f: impl Fn() + 'static) -> Retained<NSObject> {
let target = ActionTarget::new(cx.mtm, f);
unsafe {
control.setTarget(Some(&target));
control.setAction(Some(sel!(fire:)));
}
Retained::into_super(target)
}
pub struct Picker<T> {
value: Signal<T>,
options: Vec<(String, T)>,
}
impl<T: PartialEq + Clone + 'static> View for Picker<T> {
fn build(self, cx: &mut Cx) -> Node {
let popup = objc2_app_kit::NSPopUpButton::new(cx.mtm);
for (title, _) in &self.options {
popup.addItemWithTitle(&NSString::from_str(title));
}
let options = std::rc::Rc::new(self.options);
let (value, opts, p) = (self.value, options.clone(), popup.clone());
let target = on_action(cx, &popup, move || {
if let Some((_, v)) = usize::try_from(p.indexOfSelectedItem()).ok().and_then(|i| opts.get(i)) {
value.set(v.clone());
}
});
let (mut node, _) =
Node::leaf(cx, Retained::into_super(Retained::into_super(Retained::into_super(popup.clone()))), (false, false), Size::default());
let value = self.value;
node.own(Effect::new(move || {
let current = value.get();
if let Some(i) = options.iter().position(|(_, v)| *v == current)
&& popup.indexOfSelectedItem() != i as isize
{
popup.selectItemAtIndex(i as isize);
}
}));
node.keep(target);
node
}
}
pub fn picker<T: PartialEq + Clone + 'static>(value: &Signal<T>, options: Vec<(&str, T)>) -> Picker<T> {
Picker { value: *value, options: options.into_iter().map(|(t, v)| (t.to_string(), v)).collect() }
}
pub struct Slider {
value: Signal<f64>,
range: (f64, f64),
ticks: usize,
snap: bool,
shape: SliderShape,
size: crate::ControlSize,
}
#[derive(Clone, Copy, PartialEq)]
enum SliderShape {
Horizontal,
Vertical,
Circular,
}
impl Slider {
pub fn ticks(mut self, n: usize) -> Self {
self.ticks = n;
self
}
pub fn snap_to_ticks(mut self) -> Self {
self.snap = true;
self
}
pub fn vertical(mut self) -> Self {
self.shape = SliderShape::Vertical;
self
}
pub fn circular(mut self) -> Self {
self.shape = SliderShape::Circular;
self
}
pub fn control_size(mut self, size: crate::ControlSize) -> Self {
self.size = size;
self
}
}
impl View for Slider {
fn build(self, cx: &mut Cx) -> Node {
let slider = objc2_app_kit::NSSlider::new(cx.mtm);
slider.setMinValue(self.range.0);
slider.setMaxValue(self.range.1);
slider.setContinuous(true);
slider.setControlSize(self.size.ns());
if self.ticks > 0 {
slider.setNumberOfTickMarks(self.ticks as isize);
slider.setAllowsTickMarkValuesOnly(self.snap);
}
let (grow, base) = match self.shape {
SliderShape::Horizontal => ((true, false), Size { w: 100.0, h: 0.0 }),
SliderShape::Vertical => {
slider.setVertical(true);
((false, true), Size { w: 0.0, h: 100.0 })
}
SliderShape::Circular => {
slider.setSliderType(objc2_app_kit::NSSliderType::Circular);
((false, false), Size::default())
}
};
let (value, s) = (self.value, slider.clone());
let target = on_action(cx, &slider, move || value.set(s.doubleValue()));
let (mut node, _) = Node::leaf(cx, Retained::into_super(Retained::into_super(slider.clone())), grow, base);
let value = self.value;
node.own(Effect::new(move || {
let v = value.get();
if slider.doubleValue() != v {
slider.setDoubleValue(v);
}
}));
node.keep(target);
node
}
}
pub fn slider(value: &Signal<f64>, min: f64, max: f64) -> Slider {
Slider { value: *value, range: (min, max), ticks: 0, snap: false, shape: SliderShape::Horizontal, size: crate::ControlSize::Regular }
}
pub struct Stepper {
value: Signal<f64>,
range: (f64, f64),
step: f64,
}
impl View for Stepper {
fn build(self, cx: &mut Cx) -> Node {
let stepper = objc2_app_kit::NSStepper::new(cx.mtm);
stepper.setMinValue(self.range.0);
stepper.setMaxValue(self.range.1);
stepper.setIncrement(self.step);
stepper.setValueWraps(false);
let (value, s) = (self.value, stepper.clone());
let target = on_action(cx, &stepper, move || value.set(s.doubleValue()));
let (mut node, _) =
Node::leaf(cx, Retained::into_super(Retained::into_super(stepper.clone())), (false, false), Size::default());
let value = self.value;
node.own(Effect::new(move || {
let v = value.get();
if stepper.doubleValue() != v {
stepper.setDoubleValue(v);
}
}));
node.keep(target);
node
}
}
pub fn stepper(value: &Signal<f64>, min: f64, max: f64, step: f64) -> Stepper {
Stepper { value: *value, range: (min, max), step }
}
pub struct Progress {
f: Box<dyn Fn() -> Option<f64>>,
spinner: bool,
size: crate::ControlSize,
}
impl Progress {
pub fn spinner(mut self) -> Self {
self.spinner = true;
self
}
pub fn control_size(mut self, size: crate::ControlSize) -> Self {
self.size = size;
self
}
}
impl View for Progress {
fn build(self, cx: &mut Cx) -> Node {
use objc2_app_kit::NSProgressIndicatorStyle;
let bar = objc2_app_kit::NSProgressIndicator::new(cx.mtm);
bar.setStyle(if self.spinner { NSProgressIndicatorStyle::Spinning } else { NSProgressIndicatorStyle::Bar });
bar.setControlSize(self.size.ns());
bar.setMinValue(0.0);
bar.setMaxValue(1.0);
let (grow, base) = if self.spinner {
bar.sizeToFit();
let s = bar.frame().size;
((false, false), Size { w: s.width, h: s.height })
} else {
((true, false), Size { w: 100.0, h: 0.0 })
};
let (mut node, _) = Node::leaf(cx, Retained::into_super(bar.clone()), grow, base);
if self.spinner
&& let crate::view::Kind::Leaf(l) = &node.kind
{
l.content.set(Some(base));
}
let f = self.f;
node.own(Effect::new(move || match f() {
Some(v) => {
unsafe { bar.stopAnimation(None) };
bar.setIndeterminate(false);
bar.setDoubleValue(v.clamp(0.0, 1.0));
}
None => {
bar.setIndeterminate(true);
unsafe { bar.startAnimation(None) };
}
}));
node
}
}
pub fn progress(f: impl Fn() -> Option<f64> + 'static) -> Progress {
Progress { f: Box::new(f), spinner: false, size: crate::ControlSize::Regular }
}
pub struct Image {
source: ImageSource,
description: Option<String>,
symbol: SymbolLook,
fill: bool,
radius: f64,
effects: Vec<(SymbolEffect, EffectWhen)>,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SymbolEffect {
Bounce,
Pulse,
VariableColor,
Scale,
Wiggle,
Rotate,
Breathe,
}
impl SymbolEffect {
fn class_name(self) -> &'static std::ffi::CStr {
match self {
SymbolEffect::Bounce => c"NSSymbolBounceEffect",
SymbolEffect::Pulse => c"NSSymbolPulseEffect",
SymbolEffect::VariableColor => c"NSSymbolVariableColorEffect",
SymbolEffect::Scale => c"NSSymbolScaleEffect",
SymbolEffect::Wiggle => c"NSSymbolWiggleEffect",
SymbolEffect::Rotate => c"NSSymbolRotateEffect",
SymbolEffect::Breathe => c"NSSymbolBreatheEffect",
}
}
fn object(self) -> Option<Retained<objc2::runtime::AnyObject>> {
let class = objc2::runtime::AnyClass::get(self.class_name())?;
unsafe { msg_send![class, effect] }
}
}
enum EffectWhen {
While(crate::Value<bool>),
On(Box<dyn Fn() -> u64>),
}
thread_local! {
pub(crate) static SYMBOL_EFFECTS: std::cell::Cell<(isize, usize)> = const { std::cell::Cell::new((0, 0)) };
}
enum ImageSource {
Symbol(String),
File(std::path::PathBuf),
Data(Vec<u8>),
}
#[derive(Default)]
struct SymbolLook {
size: Option<f64>,
weight: Option<crate::Weight>,
color: Option<Color>,
rendering: Option<Rendering>,
}
enum Rendering {
Hierarchical(Color),
Palette(Vec<Color>),
Multicolor,
}
impl Image {
pub fn description(mut self, text: &str) -> Self {
self.description = Some(text.into());
self
}
pub fn symbol_size(mut self, points: f64) -> Self {
self.symbol.size = Some(points);
self
}
pub fn symbol_weight(mut self, weight: crate::Weight) -> Self {
self.symbol.weight = Some(weight);
self
}
pub fn color(mut self, color: Color) -> Self {
self.symbol.color = Some(color);
self
}
pub fn hierarchical(mut self, color: Color) -> Self {
self.symbol.rendering = Some(Rendering::Hierarchical(color));
self
}
pub fn palette(mut self, colors: &[Color]) -> Self {
self.symbol.rendering = Some(Rendering::Palette(colors.to_vec()));
self
}
pub fn multicolor(mut self) -> Self {
self.symbol.rendering = Some(Rendering::Multicolor);
self
}
pub fn fill(mut self) -> Self {
self.fill = true;
self
}
pub fn effect(mut self, effect: SymbolEffect, active: impl crate::IntoValue<bool>) -> Self {
self.effects.push((effect, EffectWhen::While(active.into_value())));
self
}
pub fn effect_on<T: std::hash::Hash>(mut self, effect: SymbolEffect, trigger: impl Fn() -> T + 'static) -> Self {
self.effects.push((
effect,
EffectWhen::On(Box::new(move || {
use std::hash::{BuildHasher, BuildHasherDefault, DefaultHasher};
BuildHasherDefault::<DefaultHasher>::default().hash_one(trigger())
})),
));
self
}
pub fn corner_radius(mut self, points: f64) -> Self {
self.radius = points;
self
}
}
impl View for Image {
fn build(mut self, cx: &mut Cx) -> Node {
use objc2_app_kit::{NSImage, NSImageSymbolConfiguration, NSImageView};
let mut effects: Vec<Effect> = Vec::new();
let description = self.description.as_deref().map(NSString::from_str);
let (view, size): (Retained<NSView>, Option<Size>) = match &self.source {
ImageSource::Symbol(name) => {
let image = NSImage::imageWithSystemSymbolName_accessibilityDescription(&NSString::from_str(name), description.as_deref())
.unwrap_or_else(|| panic!("no SF Symbol named {name:?}"));
let view = NSImageView::imageViewWithImage(&image, cx.mtm);
let look = &self.symbol;
let mut config: Option<Retained<NSImageSymbolConfiguration>> = None;
let mut add = |c: Retained<NSImageSymbolConfiguration>| {
config = Some(match config.take() {
Some(prev) => prev.configurationByApplyingConfiguration(&c),
None => c,
});
};
if look.size.is_some() || look.weight.is_some() {
let size = look.size.unwrap_or_else(objc2_app_kit::NSFont::systemFontSize);
add(NSImageSymbolConfiguration::configurationWithPointSize_weight(size, look.weight.unwrap_or(crate::Weight::Regular).ns()));
}
match &look.rendering {
Some(Rendering::Hierarchical(c)) => add(NSImageSymbolConfiguration::configurationWithHierarchicalColor(&c.ns())),
Some(Rendering::Palette(cs)) => {
let colors: Vec<_> = cs.iter().map(|c| c.ns()).collect();
add(NSImageSymbolConfiguration::configurationWithPaletteColors(&objc2_foundation::NSArray::from_retained_slice(&colors)));
}
Some(Rendering::Multicolor) => add(NSImageSymbolConfiguration::configurationPreferringMulticolor()),
None => {}
}
if let Some(config) = &config {
view.setSymbolConfiguration(Some(config));
}
if let Some(c) = look.color {
view.setContentTintColor(Some(&c.ns()));
}
for (effect, when) in std::mem::take(&mut self.effects) {
let Some(object) = effect.object() else { continue };
let v = view.clone();
let options: Option<Retained<objc2::runtime::AnyObject>> = objc2::runtime::AnyClass::get(c"NSSymbolEffectOptions").and_then(|c| unsafe { msg_send![c, options] });
let Some(options) = options else { continue };
match when {
EffectWhen::While(active) => {
let on = std::cell::Cell::new(false);
effects.push(Effect::new(move || {
let want = active.get();
if want == on.replace(want) {
return;
}
unsafe {
if want {
let _: () = msg_send![&*v, addSymbolEffect: &*object, options: &*options, animated: true];
} else {
let _: () = msg_send![&*v, removeSymbolEffectOfType: &*object, options: &*options, animated: true];
}
}
SYMBOL_EFFECTS.with(|c| {
let (running, played) = c.get();
c.set((running + if want { 1 } else { -1 }, played));
});
}));
}
EffectWhen::On(trigger) => {
let last = std::cell::Cell::new(None);
effects.push(Effect::new(move || {
let now = trigger();
if last.replace(Some(now)).is_some_and(|before| before != now) {
unsafe {
let _: () = msg_send![&*v, addSymbolEffect: &*object, options: &*options, animated: true];
}
SYMBOL_EFFECTS.with(|c| {
let (running, played) = c.get();
c.set((running, played + 1));
});
}
}));
}
}
}
(Retained::into_super(Retained::into_super(view)), None)
}
ImageSource::File(_) | ImageSource::Data(_) => {
let image = match &self.source {
ImageSource::File(path) => NSImage::initWithContentsOfFile(objc2::AnyThread::alloc(), &NSString::from_str(&path.to_string_lossy())),
ImageSource::Data(bytes) => NSImage::initWithData(objc2::AnyThread::alloc(), &objc2_foundation::NSData::with_bytes(bytes)),
ImageSource::Symbol(_) => unreachable!(),
};
let size = image.as_ref().map(|i| i.size()).map(|s| Size { w: s.width, h: s.height });
let view = crate::surface::picture(cx.mtm, image, self.fill, self.radius);
if let Some(d) = &description {
use objc2_app_kit::NSAccessibility;
view.setAccessibilityElement(true);
view.setAccessibilityRole(Some(unsafe { objc2_app_kit::NSAccessibilityImageRole }));
view.setAccessibilityLabel(Some(d));
}
(view, size)
}
};
if self.radius > 0.0 && matches!(self.source, ImageSource::Symbol(_)) {
view.setWantsLayer(true);
if let Some(layer) = view.layer() {
layer.setCornerRadius(self.radius);
layer.setMasksToBounds(true);
}
}
if self.description.is_none() {
<crate::platform::Native as crate::platform::Platform>::set_accessibility_element(&view, false);
}
let (mut node, _) = Node::leaf(cx, view, (false, false), Size::default());
if let (Some(size), crate::view::Kind::Leaf(l)) = (size, &node.kind) {
l.content.set(Some(size));
}
for e in effects {
node.own(e);
}
node
}
}
fn new_image(source: ImageSource) -> Image {
Image { source, description: None, symbol: SymbolLook::default(), fill: false, radius: 0.0, effects: Vec::new() }
}
pub fn image(symbol: &str) -> Image {
new_image(ImageSource::Symbol(symbol.into()))
}
pub fn image_file(path: impl AsRef<std::path::Path>) -> Image {
new_image(ImageSource::File(path.as_ref().to_path_buf()))
}
pub fn image_data(bytes: &[u8]) -> Image {
new_image(ImageSource::Data(bytes.to_vec()))
}
pub struct DatePicker {
value: Signal<std::time::SystemTime>,
time: bool,
calendar: bool,
range: Option<(std::time::SystemTime, std::time::SystemTime)>,
}
impl DatePicker {
pub fn with_time(mut self) -> Self {
self.time = true;
self
}
pub fn calendar(mut self) -> Self {
self.calendar = true;
self
}
pub fn range(mut self, min: std::time::SystemTime, max: std::time::SystemTime) -> Self {
self.range = Some((min, max));
self
}
}
fn ns_date(t: std::time::SystemTime) -> Retained<objc2_foundation::NSDate> {
let secs = match t.duration_since(std::time::UNIX_EPOCH) {
Ok(d) => d.as_secs_f64(),
Err(e) => -e.duration().as_secs_f64(),
};
objc2_foundation::NSDate::dateWithTimeIntervalSince1970(secs)
}
fn system_time(d: &objc2_foundation::NSDate) -> std::time::SystemTime {
let secs = d.timeIntervalSince1970();
let delta = std::time::Duration::from_secs_f64(secs.abs());
if secs >= 0.0 { std::time::UNIX_EPOCH + delta } else { std::time::UNIX_EPOCH - delta }
}
impl View for DatePicker {
fn build(self, cx: &mut Cx) -> Node {
use objc2_app_kit::{NSDatePickerElementFlags, NSDatePickerStyle};
let picker = objc2_app_kit::NSDatePicker::new(cx.mtm);
let mut elements = NSDatePickerElementFlags::YearMonthDay;
if self.time {
elements |= NSDatePickerElementFlags::HourMinute;
}
picker.setDatePickerElements(elements);
picker.setDatePickerStyle(if self.calendar { NSDatePickerStyle::ClockAndCalendar } else { NSDatePickerStyle::TextFieldAndStepper });
if let Some((min, max)) = self.range {
picker.setMinDate(Some(&ns_date(min)));
picker.setMaxDate(Some(&ns_date(max)));
}
let (value, p) = (self.value, picker.clone());
let target = on_action(cx, &picker, move || value.set(system_time(&p.dateValue())));
let (mut node, _) = Node::leaf(cx, Retained::into_super(Retained::into_super(picker.clone())), (false, false), Size::default());
let value = self.value;
node.own(Effect::new(move || {
let want = ns_date(value.get());
if (picker.dateValue().timeIntervalSince1970() - want.timeIntervalSince1970()).abs() >= 1.0 {
picker.setDateValue(&want);
}
}));
node.keep(target);
node
}
}
pub fn date_picker(value: &Signal<std::time::SystemTime>) -> DatePicker {
DatePicker { value: *value, time: false, calendar: false, range: None }
}
pub struct TimePicker(Signal<(u32, u32)>);
impl View for TimePicker {
fn build(self, cx: &mut Cx) -> Node {
use objc2_app_kit::{NSDatePickerElementFlags, NSDatePickerStyle};
use objc2_foundation::{NSCalendar, NSCalendarOptions, NSCalendarUnit};
let picker = objc2_app_kit::NSDatePicker::new(cx.mtm);
picker.setDatePickerElements(NSDatePickerElementFlags::HourMinute);
picker.setDatePickerStyle(NSDatePickerStyle::TextFieldAndStepper);
let date_at = |(hour, minute): (u32, u32)| {
NSCalendar::currentCalendar().dateBySettingHour_minute_second_ofDate_options(hour as isize, minute as isize, 0, &objc2_foundation::NSDate::now(), NSCalendarOptions::empty())
};
let time_of = |d: &objc2_foundation::NSDate| {
let c = NSCalendar::currentCalendar();
(c.component_fromDate(NSCalendarUnit::Hour, d) as u32, c.component_fromDate(NSCalendarUnit::Minute, d) as u32)
};
let (value, p) = (self.0, picker.clone());
let target = on_action(cx, &picker, move || value.set(time_of(&p.dateValue())));
let (mut node, _) = Node::leaf(cx, Retained::into_super(Retained::into_super(picker.clone())), (false, false), Size::default());
let value = self.0;
node.own(Effect::new(move || {
let want = value.get();
if time_of(&picker.dateValue()) != want
&& let Some(d) = date_at(want)
{
picker.setDateValue(&d);
}
}));
node.keep(target);
node
}
}
pub fn time_picker(value: &Signal<(u32, u32)>) -> TimePicker {
TimePicker(*value)
}
fn close_to(a: Color, b: Color) -> bool {
match (a.components(), b.components()) {
(Some(a), Some(b)) => [(a.0, b.0), (a.1, b.1), (a.2, b.2), (a.3, b.3)].iter().all(|(x, y)| (x - y).abs() < 1.0 / 512.0),
_ => a == b,
}
}
pub struct ColorWell(Signal<Color>);
impl View for ColorWell {
fn build(self, cx: &mut Cx) -> Node {
let well = objc2_app_kit::NSColorWell::new(cx.mtm);
let (value, w) = (self.0, well.clone());
let target = on_action(cx, &well, move || {
if let Some(c) = Color::from_ns(&w.color()) {
value.set(c);
}
});
let (mut node, _) = Node::leaf(cx, Retained::into_super(Retained::into_super(well.clone())), (false, false), Size { w: 44.0, h: 24.0 });
if let crate::view::Kind::Leaf(l) = &node.kind {
l.content.set(Some(Size { w: 44.0, h: 24.0 }));
}
let value = self.0;
node.own(Effect::new(move || {
let want = value.get();
if !Color::from_ns(&well.color()).is_some_and(|c| close_to(c, want)) {
well.setColor(&want.ns());
}
}));
node.keep(target);
node
}
}
pub fn color_well(value: &Signal<Color>) -> ColorWell {
ColorWell(*value)
}
pub struct Segmented<T> {
value: Signal<T>,
options: SegmentOptions<T>,
}
type LiveOptions<T> = Box<dyn Fn() -> Vec<(String, T)>>;
enum SegmentOptions<T> {
Fixed(Vec<(String, T)>),
Live(LiveOptions<T>),
}
impl<T: PartialEq + Clone + 'static> View for Segmented<T> {
fn build(self, cx: &mut Cx) -> Node {
use objc2_app_kit::NSSegmentSwitchTracking;
let control = objc2_app_kit::NSSegmentedControl::new(cx.mtm);
control.setTrackingMode(NSSegmentSwitchTracking::SelectOne);
let shown: std::rc::Rc<std::cell::RefCell<Vec<(String, T)>>> = Default::default();
let set = {
let (control, shown) = (control.clone(), shown.clone());
move |options: Vec<(String, T)>| {
let changed = {
let old = shown.borrow();
old.len() != options.len() || old.iter().zip(&options).any(|(a, b)| a.0 != b.0)
};
if changed {
control.setSegmentCount(options.len() as isize);
for (i, (title, _)) in options.iter().enumerate() {
control.setLabel_forSegment(&NSString::from_str(title), i as isize);
control.setWidth_forSegment(0.0, i as isize);
}
}
*shown.borrow_mut() = options;
changed
}
};
let live = match self.options {
SegmentOptions::Fixed(options) => {
set(options);
None
}
SegmentOptions::Live(f) => {
set(untrack(&f));
Some(f)
}
};
let (value, opts, c) = (self.value, shown.clone(), control.clone());
let target = on_action(cx, &control, move || {
let chosen = usize::try_from(c.selectedSegment()).ok().and_then(|i| opts.borrow().get(i).map(|(_, v)| v.clone()));
if let Some(v) = chosen {
value.set(v);
}
});
let (mut node, handle) = Node::leaf(cx, Retained::into_super(Retained::into_super(control.clone())), (false, false), Size::default());
let value = self.value;
let select = {
let (control, shown) = (control.clone(), shown.clone());
move |current: &T| {
let i = shown.borrow().iter().position(|(_, v)| v == current).map_or(-1, |i| i as isize);
if control.selectedSegment() != i {
control.setSelectedSegment(i);
}
}
};
if let Some(f) = live {
let (control, select) = (control.clone(), select.clone());
node.own(Effect::new(move || {
let options = f();
if set(options) {
crate::appkit_views::remeasure(&control, &handle);
}
select(&untrack(|| value.get()));
}));
}
node.own(Effect::new(move || select(&value.get())));
node.keep(target);
node
}
}
pub fn segmented<T: PartialEq + Clone + 'static>(value: &Signal<T>, options: Vec<(&str, T)>) -> Segmented<T> {
Segmented { value: *value, options: SegmentOptions::Fixed(options.into_iter().map(|(t, v)| (t.to_string(), v)).collect()) }
}
pub fn segmented_with<T: PartialEq + Clone + 'static>(value: &Signal<T>, options: impl Fn() -> Vec<(String, T)> + 'static) -> Segmented<T> {
Segmented { value: *value, options: SegmentOptions::Live(Box::new(options)) }
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "FerrokitComboDelegate"]
#[ivars = Signal<String>]
struct ComboDelegate;
unsafe impl NSObjectProtocol for ComboDelegate {}
unsafe impl NSControlTextEditingDelegate for ComboDelegate {
#[unsafe(method(controlTextDidChange:))]
fn did_change(&self, notification: &NSNotification) {
let Some(field) = notification.object().and_then(|o| o.downcast::<NSTextField>().ok()) else { return };
let value = field.stringValue().to_string();
let signal = *self.ivars();
batch(|| signal.set_if_changed(value));
}
}
unsafe impl NSTextFieldDelegate for ComboDelegate {}
unsafe impl NSComboBoxDelegate for ComboDelegate {
#[unsafe(method(comboBoxSelectionDidChange:))]
fn selection_did_change(&self, notification: &NSNotification) {
let Some(combo) = notification.object().and_then(|o| o.downcast::<objc2_app_kit::NSComboBox>().ok()) else { return };
let chosen = combo.objectValueOfSelectedItem().and_then(|o| o.downcast::<NSString>().ok()).map(|s| s.to_string());
if let Some(value) = chosen {
let signal = *self.ivars();
batch(|| signal.set_if_changed(value));
}
}
}
);
pub struct ComboBox {
value: Signal<String>,
suggestions: Vec<String>,
}
impl View for ComboBox {
fn build(self, cx: &mut Cx) -> Node {
let combo = objc2_app_kit::NSComboBox::new(cx.mtm);
for s in &self.suggestions {
unsafe { combo.addItemWithObjectValue(&NSString::from_str(s)) };
}
combo.setCompletes(true);
let delegate = ComboDelegate::alloc(cx.mtm).set_ivars(self.value);
let delegate: Retained<ComboDelegate> = unsafe { msg_send![super(delegate), init] };
unsafe { combo.setDelegate(Some(ProtocolObject::from_ref(&*delegate))) };
let (mut node, _) = Node::leaf(cx, Retained::into_super(Retained::into_super(Retained::into_super(combo.clone()))), (true, false), Size { w: 120.0, h: 0.0 });
let value = self.value;
node.own(Effect::new(move || {
let v = value.get();
if combo.stringValue().to_string() != v {
combo.setStringValue(&NSString::from_str(&v));
}
}));
node.keep(Retained::into_super(delegate));
node
}
}
pub fn combo_box(value: &Signal<String>, suggestions: Vec<&str>) -> ComboBox {
ComboBox { value: *value, suggestions: suggestions.into_iter().map(String::from).collect() }
}
pub struct SearchField {
value: Signal<String>,
placeholder: Option<String>,
on_submit: Option<Box<dyn Fn()>>,
}
impl SearchField {
pub fn placeholder(mut self, text: &str) -> Self {
self.placeholder = Some(text.into());
self
}
pub fn on_submit(mut self, f: impl Fn() + 'static) -> Self {
self.on_submit = Some(Box::new(f));
self
}
}
impl View for SearchField {
fn build(self, cx: &mut Cx) -> Node {
let field = objc2_app_kit::NSSearchField::new(cx.mtm);
if let Some(p) = &self.placeholder {
field.setPlaceholderString(Some(&NSString::from_str(p)));
}
field.setSendsSearchStringImmediately(true);
let delegate = FieldDelegate::new(cx.mtm, self.value);
unsafe { field.setDelegate(Some(ProtocolObject::from_ref(&*delegate))) };
let (value, f, submit) = (self.value, field.clone(), self.on_submit);
let target = on_action(cx, &field, move || {
let text = f.stringValue().to_string();
if text.is_empty() || value.get_untracked() != text {
value.set_if_changed(text);
} else if let Some(s) = &submit {
s();
}
});
let (mut node, _) = Node::leaf(cx, Retained::into_super(Retained::into_super(Retained::into_super(field.clone()))), (true, false), Size { w: 160.0, h: 0.0 });
let value = self.value;
node.own(Effect::new(move || {
let v = value.get();
if field.stringValue().to_string() != v {
field.setStringValue(&NSString::from_str(&v));
}
}));
node.keep(target);
node.keep(Retained::into_super(delegate));
node
}
}
pub fn search_field(value: &Signal<String>) -> SearchField {
SearchField { value: *value, placeholder: None, on_submit: None }
}