waterui 0.3.0

A modern UI framework for Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// WuiTextField.swift
// Text field component - merged UIKit and AppKit implementation

import CWaterUI

#if canImport(UIKit)
  import UIKit
#elseif canImport(AppKit)
  import AppKit
#endif

#if canImport(UIKit)
  extension CWaterUI.WuiKeyboardType {
    var uiKeyboardType: UIKeyboardType {
      switch self {
      case WuiKeyboardType_Text: return .default
      case WuiKeyboardType_Email: return .emailAddress
      case WuiKeyboardType_URL: return .URL
      case WuiKeyboardType_Number: return .numberPad
      case WuiKeyboardType_PhoneNumber: return .phonePad
      default: fatalError("Unsupported WaterUI keyboard type: \(rawValue)")
      }
    }
  }
#endif

@MainActor
final class WuiTextField: PlatformView, WuiComponent {
  static var rawId: CWaterUI.WuiTypeId { waterui_text_field_id() }

  private(set) var stretchAxis: WuiStretchAxis

  #if canImport(UIKit)
    private let textView = UITextView()
    private let placeholderLabel = UILabel()
    private lazy var focusTarget = WuiUIKitFocusTarget(control: textView)
  #elseif canImport(AppKit)
    private let textField = NSTextField()
    private lazy var focusTarget = WuiAppKitTextFieldFocusTarget(control: textField)
  #endif

  private var bindingWatcher: WatcherGuard?
  private var promptWatcher: WatcherGuard?
  private var isSyncingFromBinding = false
  private var bindingRenderer: WuiStyledStrRenderer?
  private var promptRenderer: WuiStyledStrRenderer?
  private var borderColorObservation: WuiComputedObservation<WuiResolvedColor>?
  private var fillColorObservation: WuiComputedObservation<WuiResolvedColor>?
  private var promptAlignmentObservation: WuiComputedObservation<WuiHorizontalAlignment>?
  private var bodyFontObservation: WuiComputedObservation<WuiResolvedFontValue>?
  private var accessibility: WuiControlAccessibility?

  private let labelView: WuiAnyView
  private let binding: WuiBinding<WuiStyledStr>
  private let prompt: WuiComputed<WuiStyledStr>
  private var selectionMenuTree: WuiMenuTree?
  #if canImport(UIKit)
    private let keyboard: CWaterUI.WuiKeyboardType
  #endif
  /// Maximum number of lines the field accepts: `1` is single-line, a larger
  /// value caps a multi-line field, `0` means no limit.
  private let lineLimit: Int
  private var isSingleLine: Bool { lineLimit == 1 }
  private let env: WuiEnvironment

  private var labelTopConstraint: NSLayoutConstraint?
  private var labelLeadingConstraint: NSLayoutConstraint?
  private var inputTopToLabelConstraint: NSLayoutConstraint?
  private var inputTopToSelfConstraint: NSLayoutConstraint?

  private let verticalSpacing: CGFloat = 4.0

  convenience init(anyview: OpaquePointer, env: WuiEnvironment) {
    let stretchAxis = WuiStretchAxis(waterui_view_stretch_axis(anyview))
    let ffiTextField: CWaterUI.WuiTextField = waterui_force_as_text_field(anyview)
    let labelView = WuiAnyView(anyview: ffiTextField.label.view, env: env)
    let binding = WuiBinding<WuiStyledStr>(ffiTextField.value)
    let prompt = WuiComputed<WuiStyledStr>(ffiTextField.prompt.content)
    let promptAlignment = WuiComputed<WuiHorizontalAlignment>(
      ffiTextField.prompt.paragraph_alignment
    )
    let selectionMenu = ffiTextField.selection_menu
    #if canImport(UIKit)
      self.init(
        stretchAxis: stretchAxis,
        label: labelView,
        binding: binding,
        prompt: prompt,
        promptAlignment: promptAlignment,
        semanticLabel: ffiTextField.label,
        selectionMenu: selectionMenu,
        keyboard: ffiTextField.keyboard,
        lineLimit: Int(ffiTextField.line_limit),
        env: env
      )
    #elseif canImport(AppKit)
      self.init(
        stretchAxis: stretchAxis,
        label: labelView,
        binding: binding,
        prompt: prompt,
        promptAlignment: promptAlignment,
        semanticLabel: ffiTextField.label,
        selectionMenu: selectionMenu,
        lineLimit: Int(ffiTextField.line_limit),
        env: env
      )
    #endif
  }

  #if canImport(UIKit)
    init(
      stretchAxis: WuiStretchAxis,
      label: WuiAnyView,
      binding: WuiBinding<WuiStyledStr>,
      prompt: WuiComputed<WuiStyledStr>,
      promptAlignment: WuiComputed<WuiHorizontalAlignment>,
      semanticLabel: CWaterUI.WuiLabel,
      selectionMenu: OpaquePointer?,
      keyboard: CWaterUI.WuiKeyboardType,
      lineLimit: Int,
      env: WuiEnvironment
    ) {
      self.stretchAxis = stretchAxis
      self.labelView = label
      self.binding = binding
      self.prompt = prompt
      self.keyboard = keyboard
      self.lineLimit = lineLimit
      self.env = env
      super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
      configureSubviews()
      configureTextInput()
      startPromptWatcher()
      applyPrompt(prompt.value)
      installPromptAlignment(promptAlignment)
      startBindingWatcher()
      applyBindingValue(binding.value)
      accessibility = WuiControlAccessibility(
        consuming: semanticLabel,
        target: textView,
        visualLabel: label
      )
      installSelectionMenu(selectionMenu)
    }
  #elseif canImport(AppKit)
    init(
      stretchAxis: WuiStretchAxis,
      label: WuiAnyView,
      binding: WuiBinding<WuiStyledStr>,
      prompt: WuiComputed<WuiStyledStr>,
      promptAlignment: WuiComputed<WuiHorizontalAlignment>,
      semanticLabel: CWaterUI.WuiLabel,
      selectionMenu: OpaquePointer?,
      lineLimit: Int,
      env: WuiEnvironment
    ) {
      self.stretchAxis = stretchAxis
      self.labelView = label
      self.binding = binding
      self.prompt = prompt
      self.lineLimit = lineLimit
      self.env = env
      super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
      configureSubviews()
      configureTextInput()
      startPromptWatcher()
      applyPrompt(prompt.value)
      installPromptAlignment(promptAlignment)
      startBindingWatcher()
      applyBindingValue(binding.value)
      accessibility = WuiControlAccessibility(
        consuming: semanticLabel,
        target: textField,
        visualLabel: label
      )
      installSelectionMenu(selectionMenu)
    }
  #endif

  @available(*, unavailable)
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  func sizeThatFits(_ proposal: WuiProposalSize) -> CGSize {
    let labelSize = labelView.sizeThatFits(WuiProposalSize())

    #if canImport(UIKit)
      let minTextWidth: CGFloat = 100.0
      let proposedWidth = proposal.width.map(CGFloat.init) ?? minTextWidth
      let targetWidth = max(minTextWidth, max(labelSize.width, proposedWidth))
      let textHeight = max(
        36.0,
        textView.sizeThatFits(CGSize(width: targetWidth, height: .greatestFiniteMagnitude)).height)
    #elseif canImport(AppKit)
      let textHeight = textField.intrinsicContentSize.height
    #endif

    let hasLabel = labelSize.height > 0
    let spacing = hasLabel ? verticalSpacing : 0
    let intrinsicHeight = labelSize.height + spacing + textHeight

    let minWidth = max(labelSize.width, 100.0)
    let width = proposal.width.map { max(CGFloat($0), minWidth) } ?? minWidth
    let height = proposal.height.map { CGFloat($0) } ?? intrinsicHeight

    return CGSize(width: width, height: max(height, intrinsicHeight))
  }

  #if canImport(AppKit)
    nonisolated override var isFlipped: Bool { true }
  #endif

  private func configureSubviews() {
    labelView.translatesAutoresizingMaskIntoConstraints = false
    addSubview(labelView)

    #if canImport(UIKit)
      textView.translatesAutoresizingMaskIntoConstraints = false
      addSubview(textView)
      inputTopToSelfConstraint = textView.topAnchor.constraint(equalTo: topAnchor)
      inputTopToLabelConstraint = textView.topAnchor.constraint(
        equalTo: labelView.bottomAnchor, constant: verticalSpacing)
    #elseif canImport(AppKit)
      textField.translatesAutoresizingMaskIntoConstraints = false
      addSubview(textField)
      inputTopToSelfConstraint = textField.topAnchor.constraint(equalTo: topAnchor)
      inputTopToLabelConstraint = textField.topAnchor.constraint(
        equalTo: labelView.bottomAnchor, constant: verticalSpacing)
    #endif

    labelTopConstraint = labelView.topAnchor.constraint(equalTo: topAnchor)
    labelLeadingConstraint = labelView.leadingAnchor.constraint(equalTo: leadingAnchor)

    #if canImport(UIKit)
      NSLayoutConstraint.activate(
        [
          labelTopConstraint,
          labelLeadingConstraint,
          textView.leadingAnchor.constraint(equalTo: leadingAnchor),
          textView.trailingAnchor.constraint(equalTo: trailingAnchor),
        ].compactMap { $0 })
    #elseif canImport(AppKit)
      NSLayoutConstraint.activate(
        [
          labelTopConstraint,
          labelLeadingConstraint,
          textField.leadingAnchor.constraint(equalTo: leadingAnchor),
          textField.trailingAnchor.constraint(equalTo: trailingAnchor),
        ].compactMap { $0 })
    #endif

    updateLabelLayout()
  }

  private func updateLabelLayout() {
    let labelSize = labelView.sizeThatFits(WuiProposalSize())
    let hasLabel = labelSize.height > 0

    labelView.isHidden = !hasLabel
    inputTopToLabelConstraint?.isActive = hasLabel
    inputTopToSelfConstraint?.isActive = !hasLabel
  }

  private func configureTextInput() {
    #if canImport(UIKit)
      textView.keyboardType = keyboard.uiKeyboardType
      textView.delegate = self
      textView.installWuiFocusTarget(focusTarget)
      textView.isScrollEnabled = false
      // `lineLimit == 0` means no limit, which is also UITextView's encoding for
      // `maximumNumberOfLines`, so it maps straight through.
      textView.textContainer.maximumNumberOfLines = lineLimit
      textView.textContainer.lineBreakMode = isSingleLine ? .byTruncatingTail : .byWordWrapping
      textView.textContainerInset = UIEdgeInsets(top: 8, left: 12, bottom: 8, right: 12)
      textView.textContainer.lineFragmentPadding = 0
      textView.layer.cornerRadius = 10
      textView.layer.borderWidth = 1
      borderColorObservation = WuiComputedObservation(
        themeColor: WuiColorSlot_Border,
        env: env
      ) { [weak self] color, _ in
        self?.textView.layer.borderColor = color.toUIColor().cgColor
      }
      textView.layer.borderColor = borderColorObservation?.value.toUIColor().cgColor
      fillColorObservation = WuiComputedObservation(
        themeColor: WuiColorSlot_SurfaceVariant,
        env: env
      ) { [weak self] color, _ in
        self?.textView.backgroundColor = color.toUIColor()
      }
      textView.backgroundColor = fillColorObservation?.value.toUIColor()
      installBodyFont()

      placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
      placeholderLabel.numberOfLines = 1
      placeholderLabel.backgroundColor = .clear
      textView.addSubview(placeholderLabel)

      NSLayoutConstraint.activate([
        placeholderLabel.leadingAnchor.constraint(
          equalTo: textView.leadingAnchor, constant: textView.textContainerInset.left),
        placeholderLabel.trailingAnchor.constraint(
          lessThanOrEqualTo: textView.trailingAnchor, constant: -textView.textContainerInset.right),
        placeholderLabel.topAnchor.constraint(
          equalTo: textView.topAnchor, constant: textView.textContainerInset.top),
      ])
    #elseif canImport(AppKit)
      // The native rounded bezel is what SwiftUI's default TextField renders
      // on macOS: platform fill, hairline border, focus ring, and content
      // padding all come from AppKit and adapt to the effective appearance.
      textField.isBezeled = true
      textField.bezelStyle = .roundedBezel
      textField.isEditable = true
      textField.isSelectable = true
      textField.usesSingleLineMode = isSingleLine
      textField.maximumNumberOfLines = lineLimit
      textField.cell?.wraps = !isSingleLine
      textField.cell?.isScrollable = isSingleLine
      textField.delegate = self
      textField.installWuiFocusTarget(focusTarget)
      installBodyFont()
    #endif
  }

  private func installBodyFont() {
    bodyFontObservation = .bodyFont(env: env) { [weak self] font in
      guard let self else { return }
      #if canImport(UIKit)
        textView.font = font.toPlatformFont()
      #elseif canImport(AppKit)
        textField.font = font.toPlatformFont()
      #endif
      // Setting the control font rewrites attribute runs on existing content
      // (UIKit) — restore the styled value on top of it.
      applyResolvedBindingValue()
      invalidateLayoutHierarchy()
    }
  }

  private func applyBindingValue(_ styled: WuiStyledStr) {
    bindingRenderer = WuiStyledStrRenderer(styled: styled, env: env) { [weak self] in
      self?.applyResolvedBindingValue()
    }
    applyResolvedBindingValue()
  }

  private func applyResolvedBindingValue() {
    guard let bindingRenderer else { return }
    let attributed = bindingRenderer.attributedString()
    #if canImport(UIKit)
      if textView.attributedText.isEqual(to: attributed) {
        updatePlaceholderVisibility()
        return
      }
      let selected = textView.selectedRange
      textView.attributedText = attributed
      let length = textView.attributedText.length
      textView.selectedRange = NSRange(location: min(selected.location, length), length: 0)
      updatePlaceholderVisibility()
    #elseif canImport(AppKit)
      textField.attributedStringValue = attributed
    #endif
    invalidateLayoutHierarchy()
  }

  private func applyPrompt(_ styled: WuiStyledStr) {
    promptRenderer = WuiStyledStrRenderer(
      styled: styled,
      env: env,
      defaultForegroundSlot: WuiColorSlot_MutedForeground
    ) { [weak self] in
      self?.applyResolvedPrompt()
    }
    applyResolvedPrompt()
  }

  private func applyResolvedPrompt() {
    guard let promptRenderer else { return }
    let attributed = promptRenderer.attributedString()
    #if canImport(UIKit)
      placeholderLabel.attributedText = attributed
      updatePlaceholderVisibility()
    #elseif canImport(AppKit)
      textField.placeholderAttributedString = attributed
    #endif
    invalidateCapturedRendering()
  }

  private func startBindingWatcher() {
    bindingWatcher = binding.watch { [weak self] newValue, _ in
      guard let self else { return }
      guard !isSyncingFromBinding else { return }
      isSyncingFromBinding = true
      applyBindingValue(newValue)
      isSyncingFromBinding = false
    }
  }

  private func startPromptWatcher() {
    promptWatcher = prompt.watch { [weak self] newValue, _ in
      self?.applyPrompt(newValue)
    }
  }

  private func installPromptAlignment(_ alignment: WuiComputed<WuiHorizontalAlignment>) {
    let observation = WuiComputedObservation(alignment) { [weak self] alignment, _ in
      self?.applyPromptAlignment(alignment)
    }
    promptAlignmentObservation = observation
    applyPromptAlignment(observation.value)
  }

  private func applyPromptAlignment(_ alignment: WuiHorizontalAlignment) {
    #if canImport(UIKit)
      let direction = UIView.userInterfaceLayoutDirection(for: semanticContentAttribute)
      switch alignment {
      case WuiHorizontalAlignment_Leading:
        textView.textAlignment = .natural
        placeholderLabel.textAlignment = .natural
      case WuiHorizontalAlignment_Trailing:
        let nativeAlignment: NSTextAlignment = direction == .rightToLeft ? .left : .right
        textView.textAlignment = nativeAlignment
        placeholderLabel.textAlignment = nativeAlignment
      case WuiHorizontalAlignment_Center:
        textView.textAlignment = .center
        placeholderLabel.textAlignment = .center
      default:
        fatalError("Unsupported WaterUI prompt alignment: \(alignment.rawValue)")
      }
    #elseif canImport(AppKit)
      let direction = userInterfaceLayoutDirection
      switch alignment {
      case WuiHorizontalAlignment_Leading:
        textField.alignment = .natural
      case WuiHorizontalAlignment_Trailing:
        textField.alignment = direction == .rightToLeft ? .left : .right
      case WuiHorizontalAlignment_Center:
        textField.alignment = .center
      default:
        fatalError("Unsupported WaterUI prompt alignment: \(alignment.rawValue)")
      }
    #endif
    invalidateCapturedRendering()
  }

  private func installSelectionMenu(_ source: OpaquePointer?) {
    selectionMenuTree = source.map { source in
      WuiMenuTree(consuming: source) { [weak self] _ in
        self?.invalidateCapturedRendering()
      }
    }
  }

  #if canImport(UIKit)
    private func updatePlaceholderVisibility() {
      placeholderLabel.isHidden = !textView.text.isEmpty
    }
  #endif
}

#if canImport(UIKit)
  extension WuiTextField: UITextViewDelegate {
    func textViewDidBeginEditing(_ textView: UITextView) {
      focusTarget.emitPlatformFocusChange(true)
    }

    func textViewDidEndEditing(_ textView: UITextView) {
      focusTarget.emitPlatformFocusChange(false)
    }

    func textViewDidChange(_ textView: UITextView) {
      guard !isSyncingFromBinding else { return }
      if textView.markedTextRange != nil {
        updatePlaceholderVisibility()
        return
      }
      binding.setPlain(textView.text)
      updatePlaceholderVisibility()
    }

    func textView(
      _ textView: UITextView,
      shouldChangeTextIn range: NSRange,
      replacementText text: String
    ) -> Bool {
      guard text.contains("\n") else { return true }
      if isSingleLine {
        // Keep single-line contract, but don't block IME composition updates.
        return textView.markedTextRange != nil
      }
      guard lineLimit > 0 else { return true }
      // A capped multi-line field rejects an edit that would add lines beyond
      // the limit rather than truncating what the user already typed.
      let current = textView.text ?? ""
      guard let editRange = Range(range, in: current) else { return true }
      let candidate = current.replacingCharacters(in: editRange, with: text)
      return candidate.components(separatedBy: "\n").count <= lineLimit
    }

    func textView(
      _ textView: UITextView,
      editMenuForTextIn range: NSRange,
      suggestedActions: [UIMenuElement]
    ) -> UIMenu? {
      guard let selectionMenuTree, !selectionMenuTree.nodes.isEmpty else { return nil }
      let custom = buildUIKitMenuElements(from: selectionMenuTree.nodes) { [weak self] command in
        guard let self else { return }
        waterui_call_shared_action(command.action, self.env.inner)
      }

      return UIMenu(title: "", children: suggestedActions + custom)
    }
  }
#endif

#if canImport(AppKit)
  extension WuiTextField: NSTextFieldDelegate {
    private func editingString() -> String {
      guard let editor = textField.currentEditor() else {
        fatalError("WaterUI AppKit text field changed without an active field editor")
      }
      return editor.string
    }

    func controlTextDidBeginEditing(_ obj: Notification) {
      focusTarget.emitPlatformFocusChange(true)
    }

    func controlTextDidChange(_ obj: Notification) {
      guard !isSyncingFromBinding else { return }
      binding.setPlain(editingString())
    }

    func controlTextDidEndEditing(_ obj: Notification) {
      focusTarget.emitPlatformFocusChange(false)
    }

    func textView(
      _ view: NSTextView,
      menu: NSMenu,
      for event: NSEvent,
      at charIndex: Int
    ) -> NSMenu? {
      guard let selectionMenuTree, !selectionMenuTree.nodes.isEmpty else { return menu }
      if !menu.items.isEmpty {
        menu.addItem(.separator())
      }
      appendAppKitMenuItems(
        selectionMenuTree.nodes,
        to: menu,
        target: self,
        action: #selector(performSelectionMenuAction(_:))
      )
      return menu
    }

    @objc private func performSelectionMenuAction(_ sender: NSMenuItem) {
      guard let action = sender.representedObject as? MenuActionRef else {
        fatalError("WaterUI selection menu item has no semantic action")
      }
      waterui_call_shared_action(action.command.action, env.inner)
    }
  }
#endif