// The bar above the keyboard and the formatting panel that takes the // keyboard's place. Which keys there are, their order, words and symbols are // the core's (`WritingWords`); the bar's own five are found by action: // `undo`, `redo`, `tag`, `hide`, `format`. #if canImport(UIKit) import UIKit /// What a key wears: its drawn mark when the icon set has one, else its /// glyph ("H1", "B") in the look's face and the glyph's style, else the /// phone's own symbol the key's icon names, else its label. @MainActor func wear(_ key: WritingKey, on button: UIButton, look: WritingLook, size: CGFloat = 17) { var config = button.configuration ?? .plain() if let icon = key.icon, let image = WritingMarks.image(icon, size: size + 5) { // Notes' own drawing of the key (`WritingMarks`), before any glyph or system symbol. config.image = image config.attributedTitle = nil } else if let glyph = key.glyph, !glyph.isEmpty { var face: WritingFace = [] switch key.style { case "bold": face = .bold case "italic": face = .italic case "mono": face = .mono default: face = glyph.count <= 2 ? .bold : [] } var title = AttributeContainer() title.font = look.font(face).withSize(size) if key.style == "underline" { title.underlineStyle = .single } if key.style == "strike" { title.strikethroughStyle = .single } config.attributedTitle = AttributedString(glyph, attributes: title) config.image = nil } else if let icon = key.icon, let image = UIImage(systemName: WritingIcons.symbol(icon), withConfiguration: UIImage.SymbolConfiguration(pointSize: size, weight: .regular)) { config.image = image config.attributedTitle = nil } else { var title = AttributeContainer() title.font = look.font([]).withSize(size * 0.8) config.attributedTitle = AttributedString(key.label, attributes: title) } config.contentInsets = NSDirectionalEdgeInsets(top: 4, leading: 4, bottom: 4, trailing: 4) button.configuration = config button.accessibilityLabel = key.label button.largeContentTitle = key.label button.showsLargeContentViewer = true } /// The phone's own symbol for each icon the core names. enum WritingIcons { static func symbol(_ icon: String) -> String { switch icon { case "undo": "arrow.uturn.backward" case "redo": "arrow.uturn.forward" case "tag": "number" case "import", "picture": "photo" case "hide", "hide_keyboard": "keyboard.chevron.compact.down" case "format": "textformat" case "divider": "minus" case "highlight": "highlighter" case "bullets": "list.bullet" case "numbers": "list.number" case "check": "checklist" case "quote": "text.quote" case "code": "chevron.left.forwardslash.chevron.right" case "link": "link" case "indent": "increase.indent" case "outdent": "decrease.indent" case "up": "arrow.up" case "down": "arrow.down" case "delete": "delete.left" case "space": "space" case "return": "return" case "keyboard": "keyboard" case "archive": "archivebox" case "bin": "trash" case "restore": "arrow.uturn.backward" case "share": "square.and.arrow.up" case "export": "doc.text" case "settings": "textformat.size" case "search": "magnifyingglass" case "info": "info" case "more": "ellipsis" case "back": "chevron.backward" case "compose": "square.and.pencil" default: icon } } } @MainActor final class WritingBar: UIInputView { private let words: WritingWords private var look: WritingLook private let stack = UIStackView() private let offers = UIStackView() private let offersScroll = UIScrollView() private let openRow = UIButton(configuration: .plain()) private var openHeight: NSLayoutConstraint? private var height: NSLayoutConstraint? private var formatButton: UIButton? private var buttons: [UIButton] = [] private var opening: String? var onKey: ((WritingKey) -> Void)? var onOffer: ((WritingTagOffer) -> Void)? var onLinkOffer: ((WritingTagOffer) -> Void)? var onOpen: ((String) -> Void)? static let height: CGFloat = 48 static let openRowHeight: CGFloat = 38 init(words: WritingWords, look: WritingLook) { self.words = words self.look = look super.init(frame: CGRect(x: 0, y: 0, width: 320, height: Self.height), inputViewStyle: .default) allowsSelfSizing = true translatesAutoresizingMaskIntoConstraints = false stack.axis = .horizontal stack.alignment = .center stack.distribution = .equalSpacing stack.translatesAutoresizingMaskIntoConstraints = false addSubview(stack) offers.axis = .horizontal offers.spacing = 8 offers.alignment = .center offers.translatesAutoresizingMaskIntoConstraints = false offersScroll.showsHorizontalScrollIndicator = false offersScroll.translatesAutoresizingMaskIntoConstraints = false offersScroll.isHidden = true offersScroll.addSubview(offers) addSubview(offersScroll) // The cursor in a link: Open it (an address in the phone's browser, a [[note]] in Notes), a row above the keys. openRow.translatesAutoresizingMaskIntoConstraints = false openRow.contentHorizontalAlignment = .leading openRow.isHidden = true openRow.accessibilityIdentifier = "link-open" openRow.addAction(UIAction { [weak self] _ in if let url = self?.opening { self?.onOpen?(url) } }, for: .primaryActionTriggered) addSubview(openRow) // The core's keys in its order, spread across the bar; a `gap` key parts the left keys from the right ones. let keys = words.bar.filter { $0.id != "keyboard" } if keys.contains(where: { $0.id == "gap" }) { stack.distribution = .fill stack.spacing = 2 for key in keys { if key.id == "gap" { let gap = UIView() gap.setContentHuggingPriority(.defaultLow - 1, for: .horizontal) stack.addArrangedSubview(gap) } else { stack.addArrangedSubview(button(key)) } } } else { for key in keys { stack.addArrangedSubview(button(key)) } } let openHeight = openRow.heightAnchor.constraint(equalToConstant: 0) let height = heightAnchor.constraint(equalToConstant: Self.height) self.openHeight = openHeight self.height = height NSLayoutConstraint.activate([ height, openRow.topAnchor.constraint(equalTo: topAnchor), openRow.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 14), openRow.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -14), openHeight, stack.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 6), stack.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -6), stack.topAnchor.constraint(equalTo: openRow.bottomAnchor), stack.bottomAnchor.constraint(equalTo: bottomAnchor), offersScroll.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 10), offersScroll.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -10), offersScroll.topAnchor.constraint(equalTo: stack.topAnchor), offersScroll.bottomAnchor.constraint(equalTo: stack.bottomAnchor), offers.leadingAnchor.constraint(equalTo: offersScroll.contentLayoutGuide.leadingAnchor), offers.trailingAnchor.constraint(equalTo: offersScroll.contentLayoutGuide.trailingAnchor), offers.topAnchor.constraint(equalTo: offersScroll.contentLayoutGuide.topAnchor), offers.bottomAnchor.constraint(equalTo: offersScroll.contentLayoutGuide.bottomAnchor), offers.heightAnchor.constraint(equalTo: offersScroll.frameLayoutGuide.heightAnchor), ]) restyle(look) } required init?(coder: NSCoder) { fatalError("init(coder:) is not used") } func restyle(_ look: WritingLook) { self.look = look backgroundColor = look.theme.bar for button in buttons { button.tintColor = look.theme.ink } formatButton?.tintColor = formatting ? look.theme.accent : look.theme.ink openRow.tintColor = look.theme.link } private func button(_ key: WritingKey) -> UIButton { let button = UIButton(configuration: .plain(), primaryAction: UIAction { [weak self] _ in self?.onKey?(key) }) wear(key, on: button, look: look) button.accessibilityIdentifier = "writing-bar-\(key.id)" button.widthAnchor.constraint(greaterThanOrEqualToConstant: 44).isActive = true button.setContentHuggingPriority(.defaultHigh, for: .horizontal) button.heightAnchor.constraint(equalToConstant: 44).isActive = true if key.id == "format" { formatButton = button } buttons.append(button) return button } /// Whether the panel stands in the keyboard's place: the format key shows the keyboard's key, lit. var formatting = false { didSet { guard let formatButton else { return } if let shown = formatting ? words.key("keyboard") : words.bar("format") { wear(shown, on: formatButton, look: look) } formatButton.tintColor = formatting ? look.theme.accent : look.theme.ink formatButton.accessibilityTraits = formatting ? [.button, .selected] : .button } } /// The tags offered for the tag being typed, in the keys' place; empty brings the keys back. func offer(_ tags: [WritingTagOffer]) { chips(tags.map { ("#" + $0.label, nil) }) { [weak self] i in self?.onOffer?(tags[i]) } } /// The notes a `[[` being typed may name, in the keys' place; empty brings the keys back. func offerLinks(_ notes: [WritingTagOffer]) { chips(notes.map { ($0.label, "notes") }) { [weak self] i in self?.onLinkOffer?(notes[i]) } } /// The link the cursor stands in (an address, or `note: