use std::rc::Rc;
use mitsuami_core::reactive::{IntoValue, Value};
use mitsuami_core::{List, Opaque, Table, Tweak};
use mitsuami_widgets::{
Button, Checkbox, ColorWell, FileIcon, Group, Icon, Image, MenuButton, NumberInput, PasswordInput, Progress,
RadioGroup, ScrollView, SearchInput, Select, Separator, Slider, Spinner, Split, SplitButton, Switch, Text,
TextArea, TextInput, ToggleButton,
};
use mitsuami_widgets::{DocumentTabs, Sidebar};
use objc2::DowncastTarget;
use objc2_app_kit::{
NSBox, NSButton, NSImageView, NSPopUpButton, NSProgressIndicator, NSScrollView, NSSearchField, NSSecureTextField,
NSSlider, NSSplitView, NSStackView, NSSwitch, NSTableView, NSTextField, NSTextView, NSView,
};
pub trait Tweakable {
type Native: DowncastTarget;
}
impl Tweakable for Button {
type Native = NSButton;
}
impl Tweakable for ToggleButton {
type Native = NSButton;
}
impl Tweakable for Checkbox {
type Native = NSButton;
}
impl Tweakable for Switch {
type Native = NSSwitch;
}
impl Tweakable for Select {
type Native = NSPopUpButton;
}
impl Tweakable for RadioGroup {
type Native = NSStackView;
}
impl Tweakable for Slider {
type Native = NSSlider;
}
impl Tweakable for ColorWell {
type Native = objc2_app_kit::NSColorWell;
}
impl Tweakable for NumberInput {
type Native = crate::NumberField;
}
impl Tweakable for Image {
type Native = NSImageView;
}
impl Tweakable for Group {
type Native = NSBox;
}
impl Tweakable for Split {
type Native = NSSplitView;
}
impl Tweakable for MenuButton {
type Native = NSPopUpButton;
}
impl Tweakable for SplitButton {
type Native = objc2_app_kit::NSControl;
}
impl Tweakable for Icon {
type Native = NSImageView;
}
impl Tweakable for FileIcon {
type Native = NSImageView;
}
impl Tweakable for Progress {
type Native = NSProgressIndicator;
}
impl Tweakable for Spinner {
type Native = NSProgressIndicator;
}
impl Tweakable for Separator {
type Native = NSBox;
}
impl Tweakable for TextInput {
type Native = NSTextField;
}
impl Tweakable for PasswordInput {
type Native = NSSecureTextField;
}
impl Tweakable for SearchInput {
type Native = NSSearchField;
}
impl Tweakable for TextArea {
type Native = NSTextView;
}
impl Tweakable for ScrollView {
type Native = NSScrollView;
}
impl Tweakable for List {
type Native = NSTableView;
}
impl Tweakable for Table {
type Native = NSTableView;
}
impl<T> Tweakable for Sidebar<T> {
type Native = NSTableView;
}
impl<T> Tweakable for DocumentTabs<T> {
type Native = NSView;
}
impl Tweakable for Text {
type Native = NSTextField;
}
pub(crate) type TweakFn = Rc<dyn Fn(&NSView)>;
pub fn tweak<W: Tweakable>(apply: impl Fn(&W::Native) + 'static) -> Tweak<W> {
tweak_with(Value::Static(()), move |native, ()| apply(native))
}
pub fn tweak_with<W: Tweakable, T: Clone + 'static>(
value: impl IntoValue<T>,
apply: impl Fn(&W::Native, &T) + 'static,
) -> Tweak<W> {
let apply = Rc::new(apply);
Tweak::new(value.into_value(), move |value| {
let apply = apply.clone();
let run: TweakFn = Rc::new(move |view: &NSView| {
if let Some(native) = view.downcast_ref::<W::Native>() {
apply(native, &value);
}
});
Opaque::new("appkit tweak", run)
})
}