Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/native/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ to the right from anywhere on the page (as in Slack) does that under the finger,
finishes at the finger's speed, the runtime drawing where it goes while the page slides off. A
press on the tab bar never waits on a picture: the bar and the page stay live while the tab draws.

**Haptics** (`web-foldkit/src/platform/feedback.ts`, `host/src/feedback.rs`). Components own
them, never features: a Switch, Checkbox, Radio, ChoiceBox or tab marks its pressed element
`data-native-feedback="selection"`, and the host plays it as the finger lifts, before the press
reaches the runtime; a success, warning or error toast plays its outcome through the bridge
(`{ feedback }`). `NATIVE_DEBUG=1` logs each one (`[host] haptic Selection`), since the
simulator plays none.

**Live data** (`HAZEL_DATA=live bun run ios`, with `VITE_APP_URL`/`VITE_BACKEND_URL` for a dev
stack on this Mac, and `HAZEL_SESSION_TOKEN` from `dev:sign-in` for a PR preview, which has no
OAuth) signs in as the Mac app does: the sign-in link opens in the system's sign-in
Expand Down
9 changes: 6 additions & 3 deletions apps/native/host/ios/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn environment(name: &str) -> Option<String> {
}

/// Whether a debug setting is on; read once.
fn environment_flag(name: &'static str) -> bool {
pub fn environment_flag(name: &'static str) -> bool {
thread_local! {
static FLAGS: RefCell<HashMap<&'static str, bool>> = RefCell::new(HashMap::new());
}
Expand Down Expand Up @@ -368,8 +368,11 @@ fn request(mtm: MainThreadMarker, body: &NSDictionary<NSString, AnyObject>) {
STORAGE.set(Some(entries));
} else if let Some(text) = field("copy") {
unsafe { UIPasteboard::generalPasteboard().setString(Some(&NSString::from_str(&text))) };
} else if body.objectForKey(ns_string!("haptic")).is_some() {
crate::system::haptic(mtm);
} else if let Some(kind) = field("feedback") {
match hazel_native::feedback::Feedback::parse(&kind) {
Some(kind) => hazel_native::feedback::play(kind),
None => log(&format!("[host] no haptic is called {kind}")),
}
} else if let Some(path) = field("swipeBack") {
crate::system::set_swipe_back(mtm, &path);
} else if let Some(navigation) = field("navigate") {
Expand Down
2 changes: 2 additions & 0 deletions apps/native/host/ios/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ fn main() {
// The UIKit views the app's `data-native` elements get, and HTML renders' web views, go
// where their boxes were drawn, after every frame.
islands::install();
// Presses marked `data-native-feedback`, and outcomes the runtime asks for, play UIKit haptics.
hazel_native::feedback::set_player(system::feedback);
// Before the app runs, as Apple asks: a tap on a notification that launches it is heard.
if let Some(mtm) = objc2::MainThreadMarker::new() {
push::install(mtm);
Expand Down
46 changes: 35 additions & 11 deletions apps/native/host/ios/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! The phone's own controls the app asks for (`web-foldkit/src/platform/system-ui.ts`): a tap of
//! the finger, and navigation that moves as a navigation stack's does.
//! The phone's own controls the app asks for (`web-foldkit/src/platform/system-ui.ts`): haptics,
//! and navigation that moves as a navigation stack's does.
//!
//! Rune draws the whole page in one view, so the page left behind is gone once the next is
//! drawn. The runtime says before every in-app navigation where it goes (`navigate`), and the
Expand All @@ -17,29 +17,53 @@ use std::time::{Duration, Instant};

use block2::RcBlock;
use dispatch2::{DispatchQueue, DispatchTime};
use hazel_native::feedback::Feedback;
use objc2::rc::Retained;
use objc2::runtime::{Bool, ProtocolObject};
use objc2::{MainThreadMarker, MainThreadOnly, define_class, msg_send, sel};
use objc2_core_foundation::{CGAffineTransform, CGFloat, CGSize};
use objc2_foundation::{NSObject, NSObjectProtocol};
use objc2_ui_kit::{
UIBezierPath, UIColor, UIGestureRecognizer, UIGestureRecognizerDelegate,
UIImpactFeedbackGenerator, UIImpactFeedbackStyle, UIPanGestureRecognizer, UIView,
UIImpactFeedbackGenerator, UIImpactFeedbackStyle, UINotificationFeedbackGenerator,
UINotificationFeedbackType, UIPanGestureRecognizer, UISelectionFeedbackGenerator, UIView,
UIViewAnimationOptions,
};
use serde_json::Value;

use crate::bridge::{log, reply, window};

/// A tap of the finger, as a long-press's menu gives.
pub fn haptic(mtm: MainThreadMarker) {
// `feedbackGeneratorWithStyle:forView:` replaces it from iOS 17.5; the app runs on 17.0.
/// Plays a haptic (`hazel_native::feedback`): a press's, or an outcome the runtime asks for.
/// `NATIVE_DEBUG=1` logs each, since the simulator plays none.
pub fn feedback(kind: Feedback) {
let Some(mtm) = MainThreadMarker::new() else {
return;
};
if crate::bridge::environment_flag("NATIVE_DEBUG") {
log(&format!("[host] haptic {kind:?}"));
}
// The generators' `…forView:` constructors replace these from iOS 17.5; the app runs on 17.0.
#[allow(deprecated)]
let generator = UIImpactFeedbackGenerator::initWithStyle(
UIImpactFeedbackGenerator::alloc(mtm),
UIImpactFeedbackStyle::Medium,
);
generator.impactOccurred();
match kind {
Feedback::Selection => {
UISelectionFeedbackGenerator::init(UISelectionFeedbackGenerator::alloc(mtm))
.selectionChanged()
}
Feedback::Impact => UIImpactFeedbackGenerator::initWithStyle(
UIImpactFeedbackGenerator::alloc(mtm),
UIImpactFeedbackStyle::Medium,
)
.impactOccurred(),
Feedback::Success | Feedback::Warning | Feedback::Error => {
let generator =
UINotificationFeedbackGenerator::init(UINotificationFeedbackGenerator::alloc(mtm));
generator.notificationOccurred(match kind {
Feedback::Success => UINotificationFeedbackType::Success,
Feedback::Warning => UINotificationFeedbackType::Warning,
_ => UINotificationFeedbackType::Error,
});
}
}
}

// PICTURES OF PAGES
Expand Down
52 changes: 52 additions & 0 deletions apps/native/host/src/feedback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! Haptic feedback (`web-foldkit/src/platform/feedback.ts`). A component marks the element whose
//! press means something (`data-native-feedback`); the press plays it here, as the finger lifts,
//! before the event reaches the runtime. Outcomes (a toast's kind) come from the runtime instead.
//! A shell with haptics installs the player (`set_player`); without one nothing plays.
use std::collections::HashMap;
use std::sync::OnceLock;

/// The attribute a component puts on the pressed element.
pub const ATTR: &str = "data-native-feedback";

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Feedback {
/// A discrete value changed.
Selection,
/// A deliberate commit with weight.
Impact,
Success,
Warning,
Error,
}

impl Feedback {
pub fn parse(name: &str) -> Option<Self> {
match name {
"selection" => Some(Self::Selection),
"impact" => Some(Self::Impact),
"success" => Some(Self::Success),
"warning" => Some(Self::Warning),
"error" => Some(Self::Error),
_ => None,
}
}
}

static PLAYER: OnceLock<fn(Feedback)> = OnceLock::new();

/// Installs the shell's haptics; called once, on the main thread's side of the shell.
pub fn set_player(play: fn(Feedback)) {
let _ = PLAYER.set(play);
}

/// Plays `kind`, where the shell has haptics.
pub fn play(kind: Feedback) {
if let Some(play) = PLAYER.get() {
play(kind);
}
}

/// What a press on the element plays: its `data-native-feedback`, if it names a kind.
pub fn of(attrs: &HashMap<String, String>) -> Option<Feedback> {
attrs.get(ATTR).and_then(|name| Feedback::parse(name))
}
1 change: 1 addition & 0 deletions apps/native/host/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Hazel's Foldkit app drawn with Rune: the runtime's wire tree as Rune elements, and the events,
//! layout and edits sent back to it. A shell (the macOS app in `main.rs`) runs the runtime and
//! installs where the messages go (`outbox::set_writer`).
pub mod feedback;
pub mod fetch;
pub mod frames;
pub mod fx;
Expand Down
20 changes: 17 additions & 3 deletions apps/native/host/src/render/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::collections::HashMap;
use rune::{Cursor, El};
use serde_json::json;

use crate::feedback::{self, Feedback};
use crate::html::{focusable, is_checkable, script_focusable};
use crate::layout::focus_styles;
use crate::outbox::{write_line, write_text};
Expand All @@ -17,8 +18,14 @@ use super::state::State;

/// A press as the browser reports it, for the handlers the element has. Rune reports a press
/// when it is released, so all of them go then (a menu opens on release, not on press). A link's
/// click carries its `href`, which the runtime follows unless the handler prevents it.
pub(super) fn press_handlers(e: El, on: &HashMap<String, u32>, href: Option<&String>) -> El {
/// click carries its `href`, which the runtime follows unless the handler prevents it. `feedback`
/// plays first, so the finger feels it before the runtime has seen the press.
pub(super) fn press_handlers(
e: El,
on: &HashMap<String, u32>,
href: Option<&String>,
feedback: Option<Feedback>,
) -> El {
let events: Vec<(&'static str, u32)> = PRESS_EVENTS
.iter()
.filter_map(|kind| on.get(*kind).map(|&id| (*kind, id)))
Expand All @@ -28,6 +35,9 @@ pub(super) fn press_handlers(e: El, on: &HashMap<String, u32>, href: Option<&Str
}
let href = href.cloned();
e.cursor(Cursor::Pointer).on_press(move |_: &mut State| {
if let Some(kind) = feedback {
feedback::play(kind);
}
for (kind, id) in &events {
match (&href, *kind) {
(Some(href), "click") => send_click(*id, href),
Expand All @@ -50,6 +60,7 @@ pub(super) fn presses(
if ctx.no_pointer {
return e;
}
let feedback = feedback::of(attrs);
// A press or Space on a checkbox toggles it, and on an unchecked radio checks it: `change`.
if is_checkable(tag, attrs)
&& let Some(&change) = on.get("change")
Expand All @@ -58,6 +69,9 @@ pub(super) fn presses(
attrs.get("type").is_some_and(|t| t == "checkbox") || !attrs.contains_key("checked");
let click = on.get("click").copied();
return e.cursor(Cursor::Pointer).on_press(move |_: &mut State| {
if let Some(kind) = feedback {
feedback::play(kind);
}
if let Some(id) = click {
send(id, "click", None);
}
Expand All @@ -67,7 +81,7 @@ pub(super) fn presses(
});
}
if PRESS_EVENTS.iter().any(|k| on.contains_key(*k)) {
return press_handlers(e, on, link_href(tag, attrs));
return press_handlers(e, on, link_href(tag, attrs), feedback);
}
if let Some(href) = link_href(tag, attrs) {
let href = href.clone();
Expand Down
2 changes: 1 addition & 1 deletion apps/native/host/src/render/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub(super) fn build_img(
_ => e.pixels(image.pixels()),
};
}
press_handlers(apply_box(e, st), on, None)
press_handlers(apply_box(e, st), on, None, None)
}

/// The box of an `<img>` that sets neither its width nor its height (`w-auto h-auto`, a chat
Expand Down
36 changes: 36 additions & 0 deletions apps/native/host/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3651,3 +3651,39 @@ fn pre_code_with_token_spans_breaks_at_its_newlines() {
"boot starts its own line: {first:?} {boot:?}"
);
}

#[test]
fn a_press_plays_its_elements_haptic_before_the_runtime_hears_it() {
use crate::feedback::{self, Feedback};
static PLAYED: std::sync::Mutex<Vec<Feedback>> = std::sync::Mutex::new(Vec::new());
feedback::set_player(|kind| PLAYED.lock().unwrap().push(kind));
sent_events(0);
let mut marked = with_attrs(
e(
"label",
json!({ "display": "block", "h": 20 }),
vec![t("Mute")],
),
json!({ "data-native-feedback": "selection" }),
);
marked["on"] = json!({ "click": 981 });
let mut plain = e("button", json!({ "display": "block", "h": 20 }), vec![]);
plain["on"] = json!({ "click": 982 });
let mut rt = runtime(
e("div", json!({ "flex": "col" }), vec![marked, plain]),
(200.0, 100.0),
);
for y in [10.0, 30.0] {
rt.pointer_move(5.0, y);
rt.pointer_down(5.0, y);
rt.pointer_up(5.0, y);
rt.frame(0.1);
}
assert_eq!(
*PLAYED.lock().unwrap(),
[Feedback::Selection],
"only the marked press plays"
);
assert_eq!(sent_events(981).len(), 1, "the marked press still clicks");
assert_eq!(sent_events(982).len(), 1);
}
7 changes: 4 additions & 3 deletions apps/native/src/ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { nativeClient, nativeHeaders } from "@hazel/auth/contract"
import type { PrecompiledStyles } from "@hazel/foldkit-rune"
import { parseHostLine } from "@hazel/foldkit-rune"
import type { Feedback } from "../../web-foldkit/src/platform/feedback"
import { PUSH_DEVICE_GLOBAL, type PushDevice } from "../../web-foldkit/src/platform/push-device"
import { SYSTEM_UI_GLOBAL, type SystemUi } from "../../web-foldkit/src/platform/system-ui"
import { readConfig } from "./config"
Expand Down Expand Up @@ -33,8 +34,8 @@ export type IosRequest =
* the host takes a picture of the page it leaves. `back`: to where the back chevron leads.
*/
| { readonly navigate: string }
/** A tap of the finger (`host/ios/src/system.rs`). */
| { readonly haptic: true }
/** A haptic of this kind (`host/ios/src/system.rs`, `web-foldkit/src/platform/feedback.ts`). */
| { readonly feedback: Feedback }
/** Where the edge swipe goes back to; empty turns the swipe off. */
| { readonly swipeBack: string }
/** The runtime followed a notification's path (`openPath`): the host stops holding it. */
Expand Down Expand Up @@ -228,7 +229,7 @@ const platform: NativePlatform = {
}

const systemUi: SystemUi = {
haptic: () => request({ haptic: true }),
feedback: (kind) => request({ feedback: kind }),
setSwipeBack: (href) => {
const target = { href }
backTargets.push(target)
Expand Down
4 changes: 3 additions & 1 deletion apps/web-foldkit/src/app/modal-host.scene.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as CreateSection from "../overlay/modal/create-section"
import { orgSwitcherEntries, userMenuEntries } from "../shell/menus"
import * as Shell from "../shell/model"
import { ada, after, hazelOrg, rootScene, signedIn } from "../test/root-fixtures"
import { drainToast } from "../test/toast-fixtures"
import { drainToast, playToastFeedback } from "../test/toast-fixtures"
import * as Menu from "../ui/menu"
import { FocusTriggerOnPress } from "../ui/menu-view"
import * as Modal from "../ui/modal"
Expand Down Expand Up @@ -75,6 +75,7 @@ describe("modal host", () => {
sceneExpect(dialog).toBeAbsent(),
Mount.expectEnded(Modal.AcquireResources),
sceneExpect(text("Section created successfully")).toExist(),
playToastFeedback,
...drainToast(),
)
})
Expand All @@ -94,6 +95,7 @@ describe("modal host", () => {
sceneExpect(dialog).toExist(),
sceneExpect(role("button", { name: "Create section" })).toBeEnabled(),
sceneExpect(text("Could not create the section")).toExist(),
playToastFeedback,
...drainToast(),
)
})
Expand Down
2 changes: 2 additions & 0 deletions apps/web-foldkit/src/app/out-messages.story.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Command, given, message, model, story } from "foldkit/story"
import { describe, expect, test } from "vitest"
import { update } from "../main"
import * as CommandPalette from "../overlay/command-palette"
import * as Toaster from "../overlay/toaster"
import { FocusInput, SetPresenceStatus } from "../overlay/command-palette/commands"
import { LayoutHotkeyActionId } from "../overlay/hotkeys"
import * as ChannelsSidebar from "../shell/channels-sidebar"
Expand Down Expand Up @@ -157,6 +158,7 @@ describe("RequestedSignOut", () => {
model((m) =>
expect(m.toasts.entries.map((entry) => entry.payload.title)).toEqual(["Failed to sign out"]),
),
Command.resolve(Toaster.PlayToastFeedback, Toaster.Message.CompletedToastFeedback()),
UiToast.test.drainEntry({ entryId: "toaster-entry-0" }),
model((m) => expect(m.toasts.entries).toEqual([])),
)
Expand Down
3 changes: 2 additions & 1 deletion apps/web-foldkit/src/app/shell.scene.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { CreateChannel, FocusInput } from "../overlay/command-palette/commands"
import * as ChannelsSidebar from "../shell/channels-sidebar"
import * as Shell from "../shell/model"
import { after, ada, hazelOrg, rootScene, signedIn } from "../test/root-fixtures"
import { drainToast } from "../test/toast-fixtures"
import { drainToast, playToastFeedback } from "../test/toast-fixtures"
import * as CommandMenu from "../ui/command-menu"
import { PortalCommandMenu } from "../ui/command-menu-view"
import * as Menu from "../ui/menu"
Expand Down Expand Up @@ -185,6 +185,7 @@ describe("command palette", () => {
Mount.expectEnded(PortalCommandMenu),
sceneExpect(text("Channel created successfully")).toExist(),
// The toast enters, its 4s lifetime runs out, then it is removed after its exit animation.
playToastFeedback,
...drainToast(),
sceneExpect(text("Channel created successfully")).toBeAbsent(),
)
Expand Down
Loading
Loading