From 59af2bcbe17936901ddc7660dd0e120caa65241d Mon Sep 17 00:00:00 2001 From: DarkSky Date: Sun, 29 Nov 2026 05:02:16 +0900 Subject: [PATCH] fix(hosts,ci): wire edge-triggered show_soft_keyboard on iOS/Android and enable shared CI Cargo cache --- .github/workflows/np-mobile.yml | 49 ++++++++------- .../app/affine/pro/host/MainActivity.kt | 4 +- .../hosts/android/src/editor_platform.rs | 6 ++ .../hosts/android/src/editor_runtime/mod.rs | 14 ++++- .../frontend/hosts/android/src/jni_editor.rs | 9 +++ .../ios/Sources/MetalHostViewController.swift | 17 ++--- .../hosts/ios/include/affine_mobile_host.h | 2 + .../frontend/hosts/ios/src/editor_platform.rs | 6 ++ .../frontend/hosts/ios/src/editor_runtime.rs | 62 ++++++++++++++++++- packages/frontend/hosts/ios/src/ffi_editor.rs | 19 ++++++ 10 files changed, 150 insertions(+), 38 deletions(-) diff --git a/.github/workflows/np-mobile.yml b/.github/workflows/np-mobile.yml index 9eac80c208..556e269068 100644 --- a/.github/workflows/np-mobile.yml +++ b/.github/workflows/np-mobile.yml @@ -59,6 +59,7 @@ jobs: with: workspaces: packages/frontend -> target key: np-mobile-gates + shared-key: np-mobile-gates - name: Format, clippy, invariants, and host/preview/mobile tests working-directory: packages/frontend @@ -149,6 +150,7 @@ jobs: with: workspaces: packages/frontend -> target key: np-mobile-preview-wasm + shared-key: np-mobile-preview-wasm - name: Install wasm-bindgen CLI and Binaryen wasm-opt run: | @@ -226,6 +228,7 @@ jobs: with: workspaces: packages/frontend -> target key: np-mobile-android-host + shared-key: np-mobile-android-host - name: Install Linux fontconfig and freetype headers for host tests run: | @@ -294,36 +297,38 @@ jobs: with: workspaces: packages/frontend -> target key: np-mobile-ios-host + shared-key: np-mobile-ios-host add-rust-environment-hash-key: false cache-on-failure: true - name: Build affine-ios-host staticlib and check device targets working-directory: packages/frontend + env: + CARGO_PROFILE_DEV_PACKAGE_MERMAN_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_MERMAN_ANALYSIS_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_MERMAN_ASCII_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_MERMAN_CORE_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_MERMAN_EDITOR_CORE_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_MERMAN_ELK_LAYERED_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_MERMAN_LAYOUT_ELK_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_MERMAN_MARKUP_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_MERMAN_MODEL_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_MERMAN_RENDER_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_MERMAN_THEME_CONTRACT_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_TYPST_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_TYPST_EVAL_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_TYPST_HTML_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_TYPST_LAYOUT_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_TYPST_LIBRARY_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_TYPST_REALIZE_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_TYPST_SVG_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_TYPST_SYNTAX_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_TYPST_UTILS_OPT_LEVEL: '0' + CARGO_PROFILE_DEV_PACKAGE_COMEMO_OPT_LEVEL: '0' run: | cargo test --locked -p affine-ios-host --lib cargo check --locked -p ui -p affine-ios-host --target aarch64-apple-ios - CARGO_PROFILE_DEV_PACKAGE_MERMAN_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_MERMAN_ANALYSIS_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_MERMAN_ASCII_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_MERMAN_CORE_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_MERMAN_EDITOR_CORE_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_MERMAN_ELK_LAYERED_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_MERMAN_LAYOUT_ELK_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_MERMAN_MARKUP_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_MERMAN_MODEL_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_MERMAN_RENDER_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_MERMAN_THEME_CONTRACT_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_TYPST_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_TYPST_EVAL_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_TYPST_HTML_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_TYPST_LAYOUT_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_TYPST_LIBRARY_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_TYPST_REALIZE_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_TYPST_SVG_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_TYPST_SYNTAX_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_TYPST_UTILS_OPT_LEVEL=0 \ - CARGO_PROFILE_DEV_PACKAGE_COMEMO_OPT_LEVEL=0 \ - cargo build --locked -p affine-ios-host --target aarch64-apple-ios-sim + cargo build --locked -p affine-ios-host --target aarch64-apple-ios-sim test -s target/aarch64-apple-ios-sim/debug/libaffine_ios_host.a IOS_LIB_BYTES=$(stat -f '%z' target/aarch64-apple-ios-sim/debug/libaffine_ios_host.a) echo "iOS staticlib libaffine_ios_host.a bytes: ${IOS_LIB_BYTES}" diff --git a/packages/frontend/hosts/android/kotlin/app/affine/pro/host/MainActivity.kt b/packages/frontend/hosts/android/kotlin/app/affine/pro/host/MainActivity.kt index 719e2966b8..2d77d22ebd 100644 --- a/packages/frontend/hosts/android/kotlin/app/affine/pro/host/MainActivity.kt +++ b/packages/frontend/hosts/android/kotlin/app/affine/pro/host/MainActivity.kt @@ -343,6 +343,7 @@ object NativeBridge { external fun nativeGetTextInputChangeSeq(handle: Long): Long external fun nativeGetFocusedBlockHash(handle: Long): Long external fun nativeWantsSoftwareKeyboard(handle: Long): Boolean + external fun nativeTakeSoftwareKeyboardRequest(handle: Long): Boolean external fun nativeWantsCodeInput(handle: Long): Boolean external fun nativeGetCaretRectDpX(handle: Long): Float external fun nativeGetCaretRectDpY(handle: Long): Float @@ -2587,8 +2588,9 @@ internal class AffineSurfaceView( if (event.actionMasked == MotionEvent.ACTION_UP) { drainPendingOpenUrl(context, handle) val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager + val requestedKb = NativeBridge.nativeTakeSoftwareKeyboardRequest(handle) if (NativeBridge.nativeWantsSoftwareKeyboard(handle)) { - if (!touchMovedBeyondSlop) { + if (requestedKb && !touchMovedBeyondSlop) { requestFocus() imm?.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) } diff --git a/packages/frontend/hosts/android/src/editor_platform.rs b/packages/frontend/hosts/android/src/editor_platform.rs index 574d263e95..8deac9e2d3 100644 --- a/packages/frontend/hosts/android/src/editor_platform.rs +++ b/packages/frontend/hosts/android/src/editor_platform.rs @@ -93,6 +93,7 @@ pub(crate) struct AndroidWindowState { pub(crate) text_input_change_seq: u64, pub(crate) last_text_input_change: Option, pub(crate) last_scene_quads: usize, + pub(crate) soft_keyboard_requested: bool, } #[derive(Clone)] @@ -147,6 +148,7 @@ impl AndroidEditorWindow { text_input_change_seq: 0, last_text_input_change: None, last_scene_quads: 0, + soft_keyboard_requested: false, })), } } @@ -476,6 +478,10 @@ impl PlatformWindow for AndroidEditorWindow { self.state.borrow_mut().ime_position = Some(bounds); } + fn show_soft_keyboard(&self) { + self.state.borrow_mut().soft_keyboard_requested = true; + } + fn text_input_state_changed(&self, change: TextInputStateChange) { let mut st = self.state.borrow_mut(); st.text_input_change_seq = st.text_input_change_seq.saturating_add(1); diff --git a/packages/frontend/hosts/android/src/editor_runtime/mod.rs b/packages/frontend/hosts/android/src/editor_runtime/mod.rs index aa03252aba..9d2ea60aa3 100644 --- a/packages/frontend/hosts/android/src/editor_runtime/mod.rs +++ b/packages/frontend/hosts/android/src/editor_runtime/mod.rs @@ -113,10 +113,10 @@ impl AndroidEditorRuntime { window.set_touch_gestures_enabled(true); let entity = cx.new(|cx| PlaygroundView::with_options(options, cx)); entity.update(cx, |view, cx| { - if load_showcase { - view.focus_first_text_block(window, cx); - } else { + if !load_showcase { view.load_empty_page_document(window, cx); + } else if !form_factor.is_touch_form_factor() { + view.focus_first_text_block(window, cx); } }); entity @@ -140,6 +140,7 @@ impl AndroidEditorRuntime { }; let _ = rt.dispatcher.drain_ready(); rt.window.request_frame_now(); + rt.window.state.borrow_mut().soft_keyboard_requested = false; Some(rt) } @@ -263,6 +264,10 @@ impl AndroidEditorRuntime { self.read_bool(PlaygroundView::wants_software_keyboard, false) } + pub(crate) fn take_software_keyboard_request(&self) -> bool { + std::mem::take(&mut self.window.state.borrow_mut().soft_keyboard_requested) + } + pub(crate) fn wants_code_input(&self) -> bool { self.read_bool(PlaygroundView::wants_code_input_mode, false) } @@ -304,6 +309,9 @@ impl AndroidEditorRuntime { pub(crate) fn dispatch_touch(&mut self, pointer_id: u64, phase: u8, x_dp: f32, y_dp: f32) { self.preserved_selection_during_composing_region = None; + if phase == 0 { + self.window.state.borrow_mut().soft_keyboard_requested = false; + } let touch_phase = match phase { 0 => TouchPhase::Started, 1 => TouchPhase::Moved, diff --git a/packages/frontend/hosts/android/src/jni_editor.rs b/packages/frontend/hosts/android/src/jni_editor.rs index 10b8703f6a..898712aa4f 100644 --- a/packages/frontend/hosts/android/src/jni_editor.rs +++ b/packages/frontend/hosts/android/src/jni_editor.rs @@ -198,6 +198,15 @@ pub extern "C" fn Java_app_affine_pro_host_NativeBridge_nativeWantsSoftwareKeybo u8::from(with_editor_mut(handle, |ed| ed.wants_software_keyboard()).unwrap_or(false)) } +#[unsafe(no_mangle)] +pub extern "C" fn Java_app_affine_pro_host_NativeBridge_nativeTakeSoftwareKeyboardRequest( + _env: JniEnvPtr, + _class: JClass, + handle: JLong, +) -> JBoolean { + u8::from(with_editor_mut(handle, |ed| ed.take_software_keyboard_request()).unwrap_or(false)) +} + #[unsafe(no_mangle)] pub extern "C" fn Java_app_affine_pro_host_NativeBridge_nativeGetCaretRectDpX( _env: JniEnvPtr, diff --git a/packages/frontend/hosts/ios/Sources/MetalHostViewController.swift b/packages/frontend/hosts/ios/Sources/MetalHostViewController.swift index 3221039f3c..99779c1bd7 100644 --- a/packages/frontend/hosts/ios/Sources/MetalHostViewController.swift +++ b/packages/frontend/hosts/ios/Sources/MetalHostViewController.swift @@ -1617,16 +1617,16 @@ final class MetalHostView: UIView, UITextInput { } private static let panSlopPt: CGFloat = 10.0 - private static let topBarHeightPt: CGFloat = 53.0 - func handleForwardedTouchCompletion(endedEditableTap: Bool) { + func handleForwardedTouchCompletion(endedEditableTap: Bool = true) { guard let handle = hostHandle else { return } syncCodeInputTraitsIfNeeded() syncKeyboardInputViewIfNeeded() + let requestedKb = affine_ios_host_take_software_keyboard_request(handle) if affine_ios_host_wants_software_keyboard(handle), !affine_ios_host_is_surface_readonly(handle) { - if endedEditableTap && !isFirstResponder && window != nil { + if requestedKb && endedEditableTap && !isFirstResponder && window != nil { _ = becomeFirstResponder() } } else if !affine_ios_host_document_selection_snapshot(handle).has_selection, @@ -1690,22 +1690,17 @@ final class MetalHostView: UIView, UITextInput { override func touchesEnded(_ touches: Set, with event: UIEvent?) { guard let handle = hostHandle else { return } lastTouchUpTimestamp = CACurrentMediaTime() - var endedEditableTap = false for touch in touches { let loc = touch.location(in: self) let id = touchId(for: touch, remove: true) - let start = touchStartPoints.removeValue(forKey: id) ?? loc + touchStartPoints.removeValue(forKey: id) touchStartTimes.removeValue(forKey: id) - let wasPan = touchExceededPanThreshold.remove(id) != nil - || hypot(loc.x - start.x, loc.y - start.y) > Self.panSlopPt + touchExceededPanThreshold.remove(id) if touchForwardedToRust.remove(id) != nil { affine_ios_host_touch_event(handle, id, 2, Float(loc.x), Float(loc.y)) - if !wasPan && start.y >= Self.topBarHeightPt && loc.y >= Self.topBarHeightPt { - endedEditableTap = true - } } } - handleForwardedTouchCompletion(endedEditableTap: endedEditableTap) + handleForwardedTouchCompletion() onInteractionWake?() } diff --git a/packages/frontend/hosts/ios/include/affine_mobile_host.h b/packages/frontend/hosts/ios/include/affine_mobile_host.h index 4ae517a530..5611b38e29 100644 --- a/packages/frontend/hosts/ios/include/affine_mobile_host.h +++ b/packages/frontend/hosts/ios/include/affine_mobile_host.h @@ -319,6 +319,8 @@ bool affine_ios_host_database_readonly(const AffineIosHost *host); bool affine_ios_host_wants_software_keyboard(const AffineIosHost *host); +bool affine_ios_host_take_software_keyboard_request(AffineIosHost *host); + void affine_ios_host_set_mode( AffineIosHost *host, uint8_t mode); diff --git a/packages/frontend/hosts/ios/src/editor_platform.rs b/packages/frontend/hosts/ios/src/editor_platform.rs index 1d91f2ec67..c31c8dab08 100644 --- a/packages/frontend/hosts/ios/src/editor_platform.rs +++ b/packages/frontend/hosts/ios/src/editor_platform.rs @@ -70,6 +70,7 @@ pub(crate) struct IosEditorWindowState { pub(crate) pending_text_input_changes: Vec, pub(crate) text_input_observer: Option, pub(crate) frame_wake_observer: Option>, + pub(crate) soft_keyboard_requested: bool, } /// Shared handle to the live GPUI `PlatformWindow` owned by `affine-ios-host`. @@ -133,6 +134,7 @@ impl IosEditorWindow { pending_text_input_changes: Vec::new(), text_input_observer: None, frame_wake_observer: None, + soft_keyboard_requested: false, })), }) } @@ -488,6 +490,10 @@ impl PlatformWindow for IosEditorWindow { self.state.borrow_mut().ime_position = Some(bounds); } + fn show_soft_keyboard(&self) { + self.state.borrow_mut().soft_keyboard_requested = true; + } + fn text_input_state_changed(&self, change: TextInputStateChange) { let mut st = self.state.borrow_mut(); st.text_input_change_seq = st.text_input_change_seq.saturating_add(1); diff --git a/packages/frontend/hosts/ios/src/editor_runtime.rs b/packages/frontend/hosts/ios/src/editor_runtime.rs index 7666ad1c9d..c7e9709e9d 100644 --- a/packages/frontend/hosts/ios/src/editor_runtime.rs +++ b/packages/frontend/hosts/ios/src/editor_runtime.rs @@ -120,7 +120,7 @@ impl IosEditorRuntime { entity.update(cx, |view, cx| { if !load_showcase { view.load_empty_page_document(window, cx); - } else { + } else if !form_factor.is_touch_form_factor() { view.focus_first_text_block(window, cx); } }); @@ -150,6 +150,7 @@ impl IosEditorRuntime { }; // Render initial frame so layout and `PlatformInputHandler` are live immediately. runtime.window.request_frame_now(); + runtime.window.state.borrow_mut().soft_keyboard_requested = false; Some(runtime) } @@ -276,6 +277,10 @@ impl IosEditorRuntime { .unwrap_or(false) } + pub(crate) fn take_software_keyboard_request(&self) -> bool { + std::mem::take(&mut self.window.state.borrow_mut().soft_keyboard_requested) + } + pub(crate) fn wants_code_input(&self) -> bool { self.app .update(|cx| { @@ -319,6 +324,9 @@ impl IosEditorRuntime { } pub(crate) fn dispatch_touch(&mut self, touch_id: u64, phase: u8, x: f32, y: f32) { + if phase == 0 { + self.window.state.borrow_mut().soft_keyboard_requested = false; + } let touch_phase = match phase { 0 => TouchPhase::Started, 1 => TouchPhase::Moved, @@ -790,4 +798,56 @@ mod tests { .unwrap_or(true); assert!(!has_toast_after, "toast must auto-dismiss after 2500ms"); } + + #[test] + fn touch_cold_start_showcase_does_not_request_keyboard_on_control_tap_and_supports_tail_undo() { + let mut rt = + IosEditorRuntime::new(390.0, 844.0, 3.0, FormFactor::Phone, true).expect("runtime"); + assert!( + !rt.wants_software_keyboard() && !rt.take_software_keyboard_request(), + "cold-start showcase on Phone must not pre-arm software keyboard" + ); + + // Scroll down and tap empty non-text gutter / control area: keyboard stays unrequested + // and scroll position does not jump back to 0. + rt.dispatch_scroll_wheel(195.0, 400.0, 0.0, -320.0); + let scroll_before = rt + .app + .update(|cx| rt.view.read_with(cx, |v, _| v.scroll_y())) + .unwrap_or(0.0); + assert!(scroll_before > 100.0); + rt.dispatch_touch(1, 0, 8.0, 220.0); + rt.dispatch_touch(1, 2, 8.0, 220.0); + assert!( + !rt.take_software_keyboard_request() && !rt.wants_software_keyboard(), + "non-editing tap after cold start must not request software keyboard" + ); + let scroll_after = rt + .app + .update(|cx| rt.view.read_with(cx, |v, _| v.scroll_y())) + .unwrap_or(0.0); + assert!((scroll_after - scroll_before).abs() < 1.0); + + // Scroll to bottom, tap empty canvas space below last block -> requests keyboard; + // Undo + typing writes into a visible editable block (never Surface EdgelessText). + rt.dispatch_scroll_wheel(195.0, 400.0, 0.0, -5000.0); + rt.dispatch_touch(2, 0, 195.0, 800.0); + rt.dispatch_touch(2, 2, 195.0, 800.0); + assert!( + rt.take_software_keyboard_request() && rt.wants_software_keyboard(), + "tapping empty tail below page must edge-request software keyboard" + ); + rt.dispatch_key_down("z", Some("z"), false, true, false, false); + assert!( + rt.wants_software_keyboard(), + "undoing empty-tail appended paragraph must restore editable caret and keep keyboard" + ); + let zy: Vec = "ZY".encode_utf16().collect(); + rt.insert_text(&zy); + let focused_text = String::from_utf16_lossy(&rt.utf16_text()); + assert!( + focused_text.contains("ZY") && !focused_text.contains("Rotated"), + "typing after empty-tail undo must land in visible Page block, got {focused_text:?}" + ); + } } diff --git a/packages/frontend/hosts/ios/src/ffi_editor.rs b/packages/frontend/hosts/ios/src/ffi_editor.rs index 375f1605b8..af22cd2f69 100644 --- a/packages/frontend/hosts/ios/src/ffi_editor.rs +++ b/packages/frontend/hosts/ios/src/ffi_editor.rs @@ -306,6 +306,25 @@ pub unsafe extern "C" fn affine_ios_host_wants_software_keyboard( .unwrap_or(false) } +/// Atomically consumes any edge-triggered software-keyboard request emitted during the +/// latest touch/gesture (`Window::request_virtual_keyboard`). +/// +/// # Safety +/// `host` must be null or a live pointer returned by `affine_ios_host_create`. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn affine_ios_host_take_software_keyboard_request( + host: *mut AffineIosHost, +) -> bool { + let Some(host) = (unsafe { host.as_mut() }) else { + return false; + }; + host.editor + .borrow() + .as_ref() + .map(IosEditorRuntime::take_software_keyboard_request) + .unwrap_or(false) +} + /// # Safety /// `host` must be null or a live pointer returned by `affine_ios_host_create`. #[unsafe(no_mangle)]