Files
hapi/ios/Hapi/Features/Links/LinkOpenHandling.swift
T
weishu 6066f20c20 feat(ios): zh-CN localization + catalog dedupe (A-M5)
Part 1 — Simplified Chinese localization of the iOS app layer:
- ios/Hapi/Resources/Localizable.xcstrings: hand-authored String Catalog
  (395 keys, zh-Hans; en implicit as source). Terminology mined from
  web/src/lib/locales/zh-CN.ts (会话/新建会话/权限模式/允许/拒绝/工作树/
  机器/语音输入/用量/草稿夹 …).
- Mechanical edits only: merged multi-part string concatenations into
  single LocalizedStringKey literals, converted ternary/plain-String user
  copy to String(localized:), switched helper params (detailRow,
  DashboardCard, optionPicker, notice) to LocalizedStringKey.
- LocalizedNoticeMapper (app layer): display-point translation of the 27
  known HapiKit-emitted strings (ChatInteractor notices, dictation errors,
  window-sync warnings, files fallbacks, worktree-name validation) with
  verbatim passthrough for server-originated text; package stays
  language-free.
- Deliberately untranslated (web parity / non-copy): event rows
  (EventPresentation — web renders presentation.ts verbatim), catalog
  option labels (Default/Auto/Sonnet/permission modes — web shows them
  verbatim), tool names, code-like titles (grep(pattern:), MCP:, Skill,
  Task), unit suffixes (B/KB/MB, m/h/d/w), decorative separators.
- Language row wired: AppLanguage gains .system (follow system, new
  default); explicit picks write the AppleLanguages override, Follow
  system removes it; footer notes a relaunch applies it (no supported
  in-place SwiftUI locale swap).
- project.pbxproj: zh-Hans added to knownRegions.

Part 2 — #39 catalog dedupe (HapiProtocol):
- NewSessionCatalogs.claudeModels/claudeEfforts now derive from
  ClaudeModels/ClaudeEfforts (single source shared with ModelCatalog);
  codexReasoningEfforts stays own data (web CODEX_REASONING_EFFORT_OPTIONS
  minus max); effortLabel delegates to ModelCatalog.capitalizedFirst.
- New CatalogTests lock test for the derived option lists.

Gate: bash ios/scripts/linux-test.sh — 459 tests green.
2026-08-18 16:04:03 +08:00

146 lines
4.9 KiB
Swift

import HapiUI
import SafariServices
import SwiftUI
/// App-level handler for everything HapiUI markdown emits through
/// `\.hapiOpenURL` (the renderer never opens anything itself and has already
/// dropped `HrefPolicy.blocked` links):
///
/// - `hapi-file://?path=&line=` workspace citations → `openWorkspaceFile`
/// when installed (the chat routes them into the A-M4a session file
/// viewer, full mode, cited line as a hint), else an explanatory alert;
/// - `https` (and, after confirmation, `http`) → in-app
/// `SFSafariViewController`;
/// - other `HrefPolicy.allowed` schemes (`mailto`, …) → the system open;
/// - `HrefPolicy.confirmFirst` schemes → alert first, then open.
///
/// Installed once at the root (see `RootView`) so links behave identically
/// wherever markdown renders; `ChatView` re-installs it over its subtree
/// with the real file opener.
struct LinkOpenHandling: ViewModifier {
/// Routes workspace-file citations to a real destination; nil shows the
/// informational alert (the root fallback outside any session).
var openWorkspaceFile: ((FilePathLink) -> Void)?
@State private var safariURL: IdentifiedURL?
@State private var confirmURL: IdentifiedURL?
@State private var fileNotice: FilePathLink?
@Environment(\.openURL) private var systemOpenURL
func body(content: Content) -> some View {
content
.environment(\.hapiOpenURL, HapiOpenURLAction { url in
// Hop to the main actor: link taps arrive from view code,
// but the action type is a plain @Sendable closure.
Task { @MainActor in
handle(url)
}
})
.sheet(item: $safariURL) { item in
SafariView(url: item.url)
.ignoresSafeArea()
}
.alert(
"Open link?",
isPresented: Binding(
get: { confirmURL != nil },
set: { presented in
if !presented {
confirmURL = nil
}
}
),
presenting: confirmURL
) { item in
Button("Open") {
confirmURL = nil
open(item.url)
}
Button("Cancel", role: .cancel) {
confirmURL = nil
}
} message: { item in
Text(item.url.absoluteString)
}
.alert(
"Workspace file",
isPresented: Binding(
get: { fileNotice != nil },
set: { presented in
if !presented {
fileNotice = nil
}
}
),
presenting: fileNotice
) { _ in
Button("OK") {
fileNotice = nil
}
} message: { link in
Text(String(
format: String(localized: "%@\n\nOpen a session chat to browse its files."),
link.path + (link.line.map { ":\($0)" } ?? "")
))
}
}
@MainActor
private func handle(_ url: URL) {
if let fileLink = FilePathLink(url: url) {
if let openWorkspaceFile {
openWorkspaceFile(fileLink)
} else {
fileNotice = fileLink
}
return
}
switch HrefPolicy.classify(url) {
case .allowed:
open(url)
case .confirmFirst:
confirmURL = IdentifiedURL(url: url)
case .blocked:
break // defense in depth; the renderer never links these
}
}
@MainActor
private func open(_ url: URL) {
let scheme = url.scheme?.lowercased()
if scheme == "https" || scheme == "http" {
safariURL = IdentifiedURL(url: url)
} else {
systemOpenURL(url)
}
}
}
extension View {
/// Installs the app's `\.hapiOpenURL` handler (plus its presentation
/// surfaces) for this subtree. Pass `openWorkspaceFile` to route
/// `hapi-file://` citations into a real viewer instead of the fallback
/// alert.
func handlesHapiLinks(openWorkspaceFile: ((FilePathLink) -> Void)? = nil) -> some View {
modifier(LinkOpenHandling(openWorkspaceFile: openWorkspaceFile))
}
}
private struct IdentifiedURL: Identifiable {
let id = UUID()
let url: URL
}
/// Thin `SFSafariViewController` wrapper — reader-friendly in-app browsing
/// without leaving the session.
private struct SafariView: UIViewControllerRepresentable {
let url: URL
func makeUIViewController(context: Context) -> SFSafariViewController {
SFSafariViewController(url: url)
}
func updateUIViewController(_ controller: SFSafariViewController, context: Context) {}
}