Skip to content

Instantly share code, notes, and snippets.

@2tvenom
Created October 28, 2024 20:10
Show Gist options
  • Save 2tvenom/2328c99a4ea099ee72883275a3fc3434 to your computer and use it in GitHub Desktop.
Save 2tvenom/2328c99a4ea099ee72883275a3fc3434 to your computer and use it in GitHub Desktop.
use crate::outline::{data_source::OutlineViewDataSource, delegate::OutlineViewDelegate};
use crate::AppDelegate;
use objc2::__framework_prelude::ProtocolObject;
use objc2::rc::{Retained, Weak};
use objc2::{declare_class, msg_send_id, sel, ClassType, DeclaredClass, MainThreadOnly};
use objc2_app_kit::{
NSButton, NSColor, NSOutlineView, NSScrollView, NSSplitViewController, NSSplitViewItem,
NSViewController,
};
use objc2_foundation::{ns_string, MainThreadMarker, NSObjectProtocol};
use std::cell::OnceCell;
use std::ops::Deref;
#[derive(Debug)]
pub struct SplitViewIvars {
app_delegate: OnceCell<Retained<AppDelegate>>,
outline_view_data_source: OnceCell<Retained<OutlineViewDataSource>>,
outline_view_delegate: OnceCell<Retained<OutlineViewDelegate>>,
split_view: OnceCell<Retained<NSSplitViewController>>,
scroll_view: OnceCell<Retained<NSScrollView>>,
vc1: OnceCell<Retained<NSViewController>>,
si: OnceCell<Retained<NSSplitViewItem>>,
}
declare_class!(
#[derive(Debug)]
pub struct SplitViewController;
unsafe impl ClassType for SplitViewController {
type Super = NSViewController;
const NAME: &'static str = "SecondViewController";
}
impl DeclaredClass for SplitViewController {
type Ivars = SplitViewIvars;
}
unsafe impl NSObjectProtocol for SplitViewController {}
unsafe impl SplitViewController {
#[method(loadView)]
fn load_view(&self) {
let mtm = MainThreadMarker::from(self);
self._load_view(mtm)
}
}
);
impl SplitViewController {
pub fn new(mtm: MainThreadMarker, app: Retained<AppDelegate>) -> Retained<Self> {
let this = mtm.alloc();
let app_once: OnceCell<Retained<AppDelegate>> = OnceCell::new();
app_once.set(app).expect("app already initialized");
let this = this.set_ivars(SplitViewIvars {
app_delegate: app_once,
outline_view_data_source: OnceCell::default(),
outline_view_delegate: OnceCell::default(),
split_view: OnceCell::default(),
scroll_view: OnceCell::default(),
vc1: OnceCell::default(),
si: OnceCell::default(),
});
unsafe { msg_send_id![super(this), init] }
}
fn _load_view(&self, mtm: MainThreadMarker) {
let color = unsafe {
NSColor::colorWithDisplayP3Red_green_blue_alpha(31. / 255., 33. / 255., 35. / 255., 1.)
};
let data_source = OutlineViewDataSource::new(mtm);
self.ivars()
.outline_view_data_source
.set(data_source.clone())
.expect("error set outline datasource");
let data_source_obj = ProtocolObject::from_ref(data_source.deref());
let delegate = OutlineViewDelegate::new(mtm);
self.ivars()
.outline_view_delegate
.set(delegate.clone())
.expect("error set outline delegate");
let delegate_obj = ProtocolObject::from_ref(delegate.deref());
let ov = unsafe {
let ov = NSOutlineView::init(mtm.alloc());
ov.setBackgroundColor(&color);
ov.setHeaderView(None);
ov.setDataSource(Some(data_source_obj));
ov.setDelegate(Some(delegate_obj));
ov
};
let sc = unsafe {
let scroll = NSScrollView::init(mtm.alloc());
scroll.setDocumentView(Some(&ov));
scroll.setBackgroundColor(&color);
scroll
};
let vc = unsafe {
let v = NSViewController::init(mtm.alloc());
v.setView(&sc);
v
};
let sidebar = unsafe {
let s = NSSplitViewItem::init(mtm.alloc());
s.setAllowsFullHeightLayout(true);
s.setMinimumThickness(100.);
s.setMaximumThickness(200.);
s.setCanCollapse(true);
s.setViewController(&vc.clone());
s
};
self.ivars().vc1.set(vc).expect("aaaaa");
let detail = unsafe {
let v = NSScrollView::new(mtm);
v.setBackgroundColor(&color);
let b1 = NSButton::buttonWithTitle_target_action(
ns_string!("second view button 1"),
Some(self.ivars().app_delegate.get().unwrap()),
Some(sel!(action1:)),
mtm,
);
v.addSubview(&b1);
let vc = NSViewController::init(mtm.alloc());
vc.setView(&v);
let detail = NSSplitViewItem::init(mtm.alloc());
detail.setViewController(&vc);
detail.setAllowsFullHeightLayout(true);
detail
};
let split = unsafe {
let s = NSSplitViewController::init(mtm.alloc());
// s.view().setWantsLayer(true);
s.addSplitViewItem(&sidebar.clone());
s.addSplitViewItem(&detail);
s
};
unsafe { self.setView(&split.clone().view()) }
self.ivars().si.set(sidebar).expect("aaaa");
self.ivars().split_view.set(split).expect("split view already initialized");
self.ivars().scroll_view.set(sc).expect("split view already initialized");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment