use std::cell::RefCell;
use ferrokit::*;
thread_local! {
static EDITORS: RefCell<Vec<TextModel>> = const { RefCell::new(Vec::new()) };
}
fn journal() -> DocumentType<Vec<u8>> {
DocumentType::new(&["rtf"], Vec::new, |doc: &Document<Vec<u8>>| {
let model = TextModel::new("");
if std::env::var("CHECK").is_ok() {
EDITORS.with_borrow_mut(|e| e.push(model.clone()));
}
let (m1, m2) = (model.clone(), model.clone());
let rtf = *doc.content();
vstack((
text_editor(&model).format_bar().bind_rtf(doc.content()).accessibility_label("Entry"),
hstack((
text(move || rtf.with(|_| format!("{} words", m1.text().split_whitespace().count()))).color(Color::Secondary),
spacer(),
button("Export as HTML…", move || export(&m2)),
))
.padding_xy(10.0, 6.0),
))
.spacing(0.0)
})
.read(|path| std::fs::read(path))
.write_returning(|rtf, path| {
std::fs::write(path, rtf)?;
std::fs::read(path)
})
.size(640.0, 460.0)
}
fn export(model: &TextModel) {
let Some(w) = focused_window() else { return };
let html = model.html();
save_panel(&w, "Entry.html", None, move |path| {
if let Some(path) = path
&& let Err(e) = std::fs::write(&path, html)
{
eprintln!("couldn't export: {e}");
}
});
}
fn main() {
let checks = std::env::var("CHECK").is_ok_and(|v| v == "all");
App::new("Journal")
.documents(journal())
.menu("Format", format_menu())
.on_launch(move || {
if checks {
spawn(|| std::thread::sleep(std::time::Duration::from_millis(400)), |()| self_checks::run());
}
})
.run();
}
mod self_checks {
use std::cell::Cell;
use std::path::{Path, PathBuf};
use ferrokit::testing::ax_audit;
use ferrokit::*;
use objc2::rc::Retained;
use objc2_app_kit::{NSApplication, NSMenu, NSWindow};
use objc2_foundation::NSString;
use super::EDITORS;
thread_local! {
static FAILED: Cell<u32> = const { Cell::new(0) };
}
fn check(name: &str, pass: bool, note: String) {
eprintln!("[check] {name} {} {note}", if pass { "PASS" } else { "FAIL" });
if !pass {
FAILED.with(|f| f.set(f.get() + 1));
}
}
fn then(f: impl FnOnce() + 'static) {
spawn(|| std::thread::sleep(std::time::Duration::from_millis(150)), move |()| f());
}
fn editor(w: &NSWindow) -> TextModel {
EDITORS.with_borrow(|e| e.iter().find(|m| m.text_view().window().is_some_and(|x| std::ptr::eq(&*x, w))).cloned()).unwrap()
}
fn format(title: &str) -> bool {
let app = NSApplication::sharedApplication(objc2::MainThreadMarker::new().unwrap());
let Some(format) = app.mainMenu().and_then(|m| m.itemWithTitle(&NSString::from_str("Format"))).and_then(|i| i.submenu()) else { return false };
fn find(menu: &NSMenu, title: &str) -> Option<(Retained<NSMenu>, isize)> {
for i in 0..menu.numberOfItems() {
let item = menu.itemAtIndex(i)?;
if item.title().to_string() == title {
return Some((menu.retain(), i));
}
if let Some(sub) = item.submenu()
&& let Some(found) = find(&sub, title)
{
return Some(found);
}
}
None
}
use objc2::Message;
let Some((menu, i)) = find(&format, title) else { return false };
menu.update();
let enabled = menu.itemAtIndex(i).is_some_and(|m| m.isEnabled());
menu.performActionForItemAtIndex(i);
enabled
}
pub fn run() {
let w = windows()[0].clone();
ferrokit::testing::input::ready(&w);
then(move || {
let active = ferrokit::testing::input::ready(&w);
checks(w, active);
});
}
fn checks(w: Retained<NSWindow>, active: bool) {
let dir = std::env::temp_dir().join(format!("ferrokit-journal-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let m = editor(&w);
let tv = m.text_view();
w.makeFirstResponder(Some(&tv));
check(
"rich-editor",
m.is_rich() && m.has_format_bar() && !m.is_ruler_visible() && ax_audit(&w).is_empty(),
format!("rich {}; format bar {}; ruler hidden {}; audit {:?}", m.is_rich(), m.has_format_bar(), !m.is_ruler_visible(), ax_audit(&w)),
);
unsafe { objc2::msg_send![&*tv, insertText: &*NSString::from_str("Ferry to Reine at dawn"), replacementRange: objc2_foundation::NSRange::new(0, 0)] }
m.set_selection(9..14);
let (bold, center, ruler) = if active {
(format("Bold"), format("Center"), format("Show Ruler"))
} else {
if let Some(storage) = unsafe { tv.textStorage() } {
unsafe { objc2::msg_send![&*storage, applyFontTraits: objc2_app_kit::NSFontTraitMask::BoldFontMask, range: objc2_foundation::NSRange::new(9, 5)] }
}
(false, false, false)
};
then(move || {
let (word, plain) = (m.style_at(10), m.style_at(2));
let rtf = String::from_utf8_lossy(&m.rtf()).to_string();
let edited = document_edited(&w);
if !active {
eprintln!("[check] format-menu SKIP the app couldn't come to the front (another app's window is in front)");
return save_checks(w, m, dir);
}
check(
"format-menu",
bold && center && ruler && word.bold && !plain.bold && rtf.contains("\\qc") && m.is_ruler_visible() && edited,
format!("Bold, Center, Show Ruler enabled and run {bold}/{center}/{ruler}: \"Reine\" bold {}, \"Fe\" plain {}; centred in the RTF {}; ruler shown {}; edited {edited}", word.bold, !plain.bold, rtf.contains("\\qc"), m.is_ruler_visible()),
);
save_checks(w, m, dir);
});
}
fn save_checks(w: Retained<NSWindow>, m: TextModel, dir: PathBuf) {
let path = dir.join("Lofoten.rtf");
let saved = save_document_as(&w, &path).is_ok();
let file = std::fs::read(&path).unwrap_or_default();
let head = String::from_utf8_lossy(&file[..file.len().min(6)]).to_string();
let html = m.html();
check(
"save-rtf",
saved && head == "{\\rtf1" && !document_edited(&w) && html.contains("Reine") && (html.contains("font-weight: bold") || html.contains("<b>")),
format!("saved {saved}: the file starts {head:?}; HTML export has the bold word {}", html.contains("font-weight: bold") || html.contains("<b>")),
);
ferrokit::close_window(&w);
then(move || {
let opened = open_document(&path);
then(move || {
let w2 = opened.ok();
let back = w2.as_ref().map(|w| editor(w));
let ok = back.as_ref().is_some_and(|m| m.text() == "Ferry to Reine at dawn" && m.style_at(10).bold && !m.style_at(2).bold);
check("open-rtf", ok, format!("reopened: text {:?}, bold kept {}", back.as_ref().map(|m| m.text()), back.as_ref().is_some_and(|m| m.style_at(10).bold)));
html_checks(back, &path);
});
});
}
fn html_checks(m: Option<TextModel>, path: &Path) {
let _ = path;
let Some(m) = m else { return finish() };
let loaded = m.set_html("<h1>Day two</h1><p>Kayaks in <i>Reine</i>, then <b>Å</b>.</p>");
let text = m.text();
let at = |s: &str| text.find(s).unwrap_or(0);
let ok = loaded && text.contains("Day two") && m.style_at(at("Reine")).italic && m.style_at(at("Å")).bold && !m.style_at(at("Kayaks")).bold;
check("load-html", ok, format!("loaded {loaded}: {text:?}; italic Reine {}, bold Å {}", m.style_at(at("Reine")).italic, m.style_at(at("Å")).bold));
let not_rtf = !m.set_rtf(b"not rtf") && m.text() == text;
check("bad-rtf", not_rtf, "text that isn't RTF is refused, the entry kept".into());
finish();
}
fn finish() {
EDITORS.with_borrow_mut(Vec::clear);
let failed = FAILED.with(Cell::get);
eprintln!("[check] done, {failed} failed");
std::process::exit(if failed == 0 { 0 } else { 1 });
}
}