mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* iOS: Greek keyboard layout
A TestFlight tester asked for Greek. Adds KeyboardLanguage.greek ("el",
Ελληνικά). It matches the standard Greek layout and the system keyboard's
observed long-press options, as the iOS 27.2 simulator's own Greek keyboard
shows them (read through accessibility, every letter, 123 and #+= key held
for about 3 s with a real touch, the keyboard-provenance survey and
converter as since #2140):
- Rows: ερτυθιοπ´ / ασδφγηξκλ / ζχψωβνμ, nine, nine and seven keys. The
iPhone keyboard has no ; or ς key (; is on 123, ς on a held σ); its row 1
ends in the tonos key ´ (U+00B4) and sits over row 2.
- Tonos key: not a dead key. With real touches in Messages, ´ after an
unaccented vowel accents that vowel (α´ -> ά, Α´ -> Ά, ϊ´ -> ΐ, ϋ´ -> ΰ);
after anything else, an accented vowel included, it types ´ itself. No ¨
key: the diaeresis is only on held ι and υ. GreekTyping holds these rules.
- Final sigma: σ types ς while it ends the word and turns back into σ when
another letter follows ("ας", then α: "ασα"), as observed.
- letterHolds: ε έ; υ ύ ϋ ΰ; ι ί ϊ ΐ; ο ό; α ά; σ ς; η ή; κ ϗ; ω ώ; nothing
else, the tonos key included. In capitals the system offers nothing on Σ
and only Ί Ϊ / Ύ Ϋ on Ι / Υ, so AccentCatalog drops ς, ΐ, ΰ there for Greek.
- Pages: 123 has the ano teleia · where English has ; and ; where English
has ?; #+= has ? where English has |. Same key frames as English's. The
currency key is $, held as English's; a held " offers French's order.
- ; is Greek's question mark, so after "; " auto-capitalization turns shift
on and the strip offers sentence starters, as the system keyboard does.
Autocorrect uses iOS's el dictionary through the dictionary engine, as
Ukrainian does: no typing model. Greek letters are not Latin, so there is no
bilingual partner (#2143's check skips it). el/el-GR picks the layout
automatically (dictation language, then the iPhone's keyboards and
languages; Greek used to fall back to English), the space bar says
"Ελληνικά" while switching, and Switch dictation too dictates el-GR. Hex's
short next-word lists get Greek entries. Snapshot scenes take
--keyboard-layout el. FullKeyboardEngine stays under the 1,500-line cap: the
Greek hook lives in GreekTyping.swift and didEdit is no longer private.
Verified: the full HEXMobile suite passes (520 tests, 1 skipped) on an iPhone
17 Pro simulator (iOS 27.2), with new Greek cases in KeyboardLayoutTests
(rows, pages, frames, holds and capitals, automatic pick, no partner),
FullKeyboardEngineTests (typing καλημέρα with ´, final ς and its undoing,
ϊ´ -> ΐ, ´ after a consonant, switching) and ShiftStateMachineTests. Live in
Messages with the Hex keyboard: typed "Καλημέρα, τι κάνεις;" with the tonos
key (U+03AD) and a final ς (U+03C2), held ι and σ, and switched between
English and Ελληνικά on the space bar, in light and dark.
* iOS: Greek autocorrect restores a missing tonos
Greek typed without its accents is common ("καλημερα" for "καλημέρα"), and
a word lacking a tonos and a diaeresis ("αυπνια" for "αϋπνία"), or a tonos
and its final ς typed as σ ("λογοσ" for "λόγος"), was more than the one edit
autocorrect accepts. On the Greek layout, as on the Polish one (#2143), a
guess that differs from what was typed only by tonos, diaeresis or σ/ς now
costs what one missing Spanish accent does (0.15), plus 0.05 for each further
one. Other layouts keep the one-edit rule.
Verified: AutocorrectEngineTests pass on an iPhone 17 Pro simulator (iOS
27.2), with the new costs and fixes for καλημερα, Καλημερα, κανεις, αυπνια
and λογοσ (over "λόγοι", listed first), a word left alone, and no fix on
English; the full HEXMobile suite passes (520 tests, 1 skipped). In Messages
the simulator's checker corrected no Greek word ("καλημερα " stayed), as
with Polish, so the fixes run against a stub dictionary answering as an
iPhone's would; unverified on a physical iPhone.
* iOS: Greek dictation, and Lowercase writes a final ς
Greek dictation works through Apple Speech: macOS 27.2 lists el-GR for
DictationTranscriber only (not SpeechTranscriber), and say -v Melina clips
came back with every tonos and final ς, numbers as digits ("10 λεπτά",
"7:30") and no punctuation ("Καλημέρα τι κάνεις"). Parakeet is English-only,
so el-GR always goes to Apple Speech.
Lowercase lowered every capital sigma to σ: Swift's String.lowercased() maps
Σ without context, so "ΟΔΟΣ" became "οδοσ". It now uses Foundation's
lowercased(with: nil), Unicode's context-aware mapping, which writes ς at a
word's end ("οδος", "τους") and σ elsewhere or alone, and lowers Ά Έ Ή Ί Ό Ύ Ώ
Ϊ Ϋ. Other languages lower as before; desktop's to_lowercase already does this.
Tests: SpeechLanguagePickerTests picks el-GR from DictationTranscriber, by
hand and by a Greek iPhone's Automatic, never English. SpokenCleanupRulesTests
check Polished (capital, final stop), Casual, Verbatim and Lowercase (final ς,
accented capitals, a lone Σ, English unchanged). README notes the macOS run.
Verified: the full HEXMobile suite passes (520 tests, 1 skipped) on an iPhone
17 Pro simulator (iOS 27.2). In the simulator Hex picks Greek as the speech
model for a Greek iPhone and, with no speech models there, says "Greek isn't
available on this iPhone yet. Try English." rather than falling back to
Parakeet; dictating itself is unverified there.
* iOS: Greek callouts laid out as the system Greek keyboard's
#2178 lays English's and Spanish's long-press callouts out as the system
keyboard's (AccentCatalog.arrangement); other languages open from the key
toward the middle. Greek now has its observed arrangement too: every held
letter opens with the key's own on the left (ι ί ϊ ΐ, σ ς, κ ϗ, ...), even on
the right-hand keys, and a held " is „ “ ” « » ", as the iOS 27.2 simulator's
Greek keyboard showed them; its other symbols are English's. In capitals the
arrangement drops ς, ΐ and ΰ, as options(for:) already does, so Σ opens
nothing and Ι shows Ι Ί Ϊ. Rebased onto #2178, the capital filter now sits in
options(for:) beside its shared upper().
Verified: the full HEXMobile suite passes (530 tests, 1 skipped) on an iPhone
17 Pro simulator (iOS 27.2), with new KeyboardLayoutTests cases for the
Greek arrangements (ι lower and upper, κ, Σ none, ", $ as English's).
448 lines
19 KiB
Swift
448 lines
19 KiB
Swift
import UIKit
|
||
|
||
/// The options a held key offers after 400 ms: the language's letters on its layout
|
||
/// (`KeyboardLanguage.letterHolds(on:)`),
|
||
/// else the 123 and #+= pages' list, the system English keyboard's observed long-press options
|
||
/// (iOS 27.2 simulator) unless the language's differ (`KeyboardLanguage.symbolHolds`).
|
||
nonisolated enum AccentCatalog {
|
||
private static let symbols: [String: [String]] = [
|
||
"0": ["0", "°"],
|
||
"-": ["-", "–", "—", "•"],
|
||
"/": ["/", "\\"],
|
||
"$": ["$", "₩", "£", "¢", "€", "¥", "₽"],
|
||
"\"": ["\"", "”", "“", "„", "»", "«"],
|
||
".": [".", "…"],
|
||
"?": ["?", "¿"],
|
||
"!": ["!", "¡"],
|
||
"'": ["'", "’", "‘", "`"],
|
||
"&": ["&", "§"],
|
||
"%": ["%", "‰"],
|
||
"=": ["=", "≠", "≈"],
|
||
]
|
||
|
||
/// The options for a held key, base first; nil when it holds nothing.
|
||
static func options(
|
||
for base: String,
|
||
uppercase: Bool,
|
||
language: KeyboardLanguage = .english,
|
||
letters: KeyboardLetterLayout? = nil
|
||
) -> [String]? {
|
||
let key = base.lowercased()
|
||
guard var list = language.letterHolds(on: language.offered(letters))[key] ?? language.symbolHolds[key] ?? symbols[key] else {
|
||
return nil
|
||
}
|
||
if uppercase, language == .greek {
|
||
list.removeAll(where: greekWithoutCapitals.contains)
|
||
}
|
||
guard list.count > 1 else { return nil }
|
||
return uppercase ? list.map(upper) : list
|
||
}
|
||
|
||
/// Greek capitals: ς is Σ itself and ΐ ΰ have no capital of their own, so the system
|
||
/// keyboard's held Σ offers nothing, and Ι and Υ only Ί Ϊ and Ύ Ϋ.
|
||
private static let greekWithoutCapitals: Set<String> = ["ς", "ΐ", "ΰ"]
|
||
|
||
// ẞ, Unicode's capital ß; İ for the dotless ı, as the system keyboard shows it in capitals.
|
||
private static func upper(_ option: String) -> String {
|
||
switch option {
|
||
case "ß": "ẞ"
|
||
case "ı": "İ"
|
||
default: option.uppercased()
|
||
}
|
||
}
|
||
|
||
/// Where the system keyboard puts each option in its callout, left to right as screenshots
|
||
/// of every key held show them (iOS 27.2 simulator; `|` starts a second row below), for the
|
||
/// languages checked on screen. The callout opens with the key's own under the finger, where
|
||
/// Apple's is. Used only while it lists the same options as `options(for:)`.
|
||
private static let englishOrder: [String: String] = [
|
||
"e": "ë é e è ê ě ẽ ē ė ę",
|
||
"u": "ų ů ű ũ ü ú ù u û ǔ ū",
|
||
"i": "į ı ī ĩ ï í ì i î ǐ",
|
||
"o": "ő ō õ ø œ ǒ ö ó ò o ô",
|
||
"a": "æ ã å ā ă ą | a à á â ä ǎ",
|
||
"y": "ÿ ŷ ý y",
|
||
"w": "w ŵ",
|
||
"r": "r ř",
|
||
"t": "t ț ť þ",
|
||
"s": "s ß ş ș ś š",
|
||
"d": "d ď ð",
|
||
"g": "g ğ ġ",
|
||
"h": "h ħ",
|
||
"k": "ķ k",
|
||
"l": "ľ ļ ł l",
|
||
"z": "z ź ž ż",
|
||
"c": "c ç ć č ċ",
|
||
"n": "ň ņ ń ñ n",
|
||
"0": "° 0",
|
||
"-": "- – — •",
|
||
"/": "/ \\",
|
||
"$": "₽ ¥ € $ ¢ £ ₩",
|
||
"&": "& §",
|
||
"\"": "« » „ “ ” \"",
|
||
".": ". …",
|
||
"?": "? ¿",
|
||
"!": "! ¡",
|
||
"'": "` ‘ ’ '",
|
||
"%": "% ‰",
|
||
"=": "≈ ≠ =",
|
||
]
|
||
|
||
/// The Spanish keyboard's (letters missing hold nothing there), and English's symbols.
|
||
private static let spanishOrder: [String: String] = englishOrder.filter { $0.key.first?.isLetter != true }.merging([
|
||
"a": "a á à ä â ã å ą æ ā ª",
|
||
"e": "e é è ë ê ę ė ē",
|
||
"i": "ī į î ì ï í i",
|
||
"o": "º ō œ ø õ ô ò ó o ö",
|
||
"u": "ū û ù ü ú u",
|
||
"s": "s š",
|
||
"d": "d đ",
|
||
"l": "l·l l",
|
||
"c": "c ç ć č",
|
||
"n": "ń n",
|
||
"1": "1 1º 1ª", "2": "2 2º 2ª", "3": "3 3º 3ª", "4": "4 4º 4ª", "5": "5 5º 5ª",
|
||
"6": "6 6º 6ª", "7": "7 7º 7ª", "8": "8 8º 8ª", "9": "9ª 9 9º", "0": "0 0ª ° 0º",
|
||
]) { _, spanish in spanish }
|
||
|
||
/// The Greek keyboard's: each held letter opens from the key's own on the left, and its
|
||
/// held " differs from English's; every other symbol's is English's.
|
||
private static let greekOrder: [String: String] = englishOrder.filter { $0.key.first?.isLetter != true }.merging([
|
||
"ε": "ε έ", "υ": "υ ύ ϋ ΰ", "ι": "ι ί ϊ ΐ", "ο": "ο ό", "α": "α ά", "σ": "σ ς",
|
||
"η": "η ή", "κ": "κ ϗ", "ω": "ω ώ", "\"": "„ “ ” « » \"",
|
||
]) { _, greek in greek }
|
||
|
||
private static func displayOrder(for language: KeyboardLanguage) -> [String: String] {
|
||
switch language {
|
||
case .english: englishOrder
|
||
case .spanish: spanishOrder
|
||
case .greek: greekOrder
|
||
default: [:]
|
||
}
|
||
}
|
||
|
||
/// The options in the system keyboard's arrangement, when it's known for this key and
|
||
/// language; nil opens the list from the key toward the keyboard's middle.
|
||
static func arrangement(
|
||
for base: String,
|
||
uppercase: Bool,
|
||
language: KeyboardLanguage = .english,
|
||
letters: KeyboardLetterLayout? = nil
|
||
) -> AccentArrangement? {
|
||
let key = base.lowercased()
|
||
guard let order = displayOrder(for: language)[key],
|
||
let options = options(for: key, uppercase: false, language: language, letters: letters) else { return nil }
|
||
var rows = order.split(separator: "|").map { $0.split(separator: " ").map(String.init) }
|
||
let all = rows.flatMap { $0 }
|
||
guard all.count == options.count, Set(all) == Set(options) else { return nil }
|
||
if uppercase, language == .greek {
|
||
rows = rows.map { $0.filter { !greekWithoutCapitals.contains($0) } }.filter { !$0.isEmpty }
|
||
guard rows.joined().count > 1 else { return nil }
|
||
}
|
||
guard let baseRow = rows.firstIndex(where: { $0.contains(key) }),
|
||
let baseColumn = rows[baseRow].firstIndex(of: key) else { return nil }
|
||
let shown = uppercase ? rows.map { $0.map(upper) } : rows
|
||
return AccentArrangement(rows: shown, baseRow: baseRow, baseColumn: baseColumn)
|
||
}
|
||
}
|
||
|
||
|
||
/// A callout's options in rows (top first), and which one is the key's own.
|
||
nonisolated struct AccentArrangement: Equatable, Sendable {
|
||
var rows: [[String]]
|
||
var baseRow: Int
|
||
var baseColumn: Int
|
||
}
|
||
|
||
/// The long-press accent callout's geometry, as Apple's measures beside Hex's (iOS 27.2
|
||
/// simulators): a pill of cells 4 pt wider than the held key (36.3 pt on a 390 pt iPhone's
|
||
/// letters, 37.5 on 402, 51.7 on the 390's wide . , ? ! ' keys), 47 pt tall, 10.5 pt in from its sides and 10.35 pt from its top and
|
||
/// bottom, 3 pt above the key; the key's own option sits over the key, the pill pushed in to
|
||
/// stay 6.67 pt from the sides, cells narrowed if it can't fit. Row 1's has no room above it in
|
||
/// a third-party keyboard (Apple's rises into the app): it starts at the keyboard's top instead.
|
||
nonisolated struct AccentMenu: Equatable, Sendable {
|
||
static let cellHeight: CGFloat = 47
|
||
static let padding: CGFloat = 10.5
|
||
static let verticalPadding: CGFloat = 10.35
|
||
static let verticalGap: CGFloat = 3
|
||
static let cellGrowth: CGFloat = 4
|
||
static let sideInset: CGFloat = 6.67
|
||
static let cornerRadius: CGFloat = 20.8
|
||
static let highlightInset: CGFloat = 1.15
|
||
static let highlightRadius: CGFloat = 6.3
|
||
/// The skin-tone picker's geometry (`buildFixed`).
|
||
static let fixedCellHeight: CGFloat = 44
|
||
static let fixedPadding: CGFloat = 4
|
||
static let fixedGap: CGFloat = 6
|
||
|
||
let keyFrame: CGRect
|
||
let bubbleFrame: CGRect
|
||
/// Every option, row by row, left to right.
|
||
let options: [String]
|
||
let cellFrames: [CGRect]
|
||
var selectedIndex: Int
|
||
/// Laid out by `buildFixed`, and drawn the skin-tone picker's way.
|
||
var fixedCells = false
|
||
|
||
var selectedOption: String { options[selectedIndex] }
|
||
|
||
/// A list with the key's own option first (a language's): it opens toward the middle of the
|
||
/// keyboard, the key's own nearest the key.
|
||
static func build(
|
||
for keyFrame: CGRect,
|
||
rawOptions: [String],
|
||
in layoutWidth: CGFloat,
|
||
horizontalInset: CGFloat = 0,
|
||
cellWidth: CGFloat? = nil
|
||
) -> AccentMenu {
|
||
if let cellWidth {
|
||
return buildFixed(for: keyFrame, rawOptions: rawOptions, in: layoutWidth, horizontalInset: horizontalInset, cellWidth: cellWidth)
|
||
}
|
||
let opensLeft = keyFrame.midX > layoutWidth * 0.52
|
||
let ordered = opensLeft ? Array(rawOptions.reversed()) : rawOptions
|
||
return build(
|
||
for: keyFrame,
|
||
arrangement: AccentArrangement(rows: [ordered], baseRow: 0, baseColumn: opensLeft ? ordered.count - 1 : 0),
|
||
in: layoutWidth,
|
||
horizontalInset: horizontalInset
|
||
)
|
||
}
|
||
|
||
static func build(
|
||
for keyFrame: CGRect,
|
||
arrangement: AccentArrangement,
|
||
in layoutWidth: CGFloat,
|
||
horizontalInset: CGFloat = 0
|
||
) -> AccentMenu {
|
||
let minX = horizontalInset + min(sideInset, max(1, keyFrame.minX - horizontalInset))
|
||
let maxX = max(minX + 60, layoutWidth - horizontalInset - min(sideInset, max(1, layoutWidth - horizontalInset - keyFrame.maxX)))
|
||
let columns = CGFloat(max(1, arrangement.rows.map(\.count).max() ?? 1))
|
||
let rowCount = CGFloat(max(1, arrangement.rows.count))
|
||
let fullCell = keyFrame.width + cellGrowth
|
||
let cellW = min(fullCell, (maxX - minX - 2 * padding) / columns)
|
||
let bubbleW = columns * cellW + 2 * padding
|
||
let bubbleH = rowCount * cellHeight + 2 * verticalPadding
|
||
|
||
let baseOffset = padding + (CGFloat(arrangement.baseColumn) + 0.5) * cellW
|
||
var bubbleX = keyFrame.midX - baseOffset
|
||
bubbleX = max(minX, min(maxX - bubbleW, bubbleX))
|
||
let bubbleY = max(0, keyFrame.minY - verticalGap - bubbleH)
|
||
let bubbleFrame = CGRect(x: bubbleX, y: bubbleY, width: bubbleW, height: bubbleH)
|
||
|
||
var options: [String] = []
|
||
var cells: [CGRect] = []
|
||
var selected = 0
|
||
for (r, row) in arrangement.rows.enumerated() {
|
||
for (c, option) in row.enumerated() {
|
||
if r == arrangement.baseRow, c == arrangement.baseColumn { selected = options.count }
|
||
options.append(option)
|
||
cells.append(CGRect(
|
||
x: bubbleX + padding + CGFloat(c) * cellW,
|
||
y: bubbleY + verticalPadding + CGFloat(r) * cellHeight,
|
||
width: cellW,
|
||
height: cellHeight
|
||
))
|
||
}
|
||
}
|
||
return AccentMenu(keyFrame: keyFrame, bubbleFrame: bubbleFrame, options: options, cellFrames: cells, selectedIndex: selected)
|
||
}
|
||
|
||
/// The emoji panel's skin-tone picker, cells of its own width: its layout as it was.
|
||
private static func buildFixed(
|
||
for keyFrame: CGRect,
|
||
rawOptions: [String],
|
||
in layoutWidth: CGFloat,
|
||
horizontalInset: CGFloat,
|
||
cellWidth: CGFloat
|
||
) -> AccentMenu {
|
||
let minX = max(2, horizontalInset + 4)
|
||
let maxX = max(minX + 60, layoutWidth - horizontalInset - 4)
|
||
let availableWidth = maxX - minX
|
||
|
||
// Right-half keys reverse the list so the base letter sits under the thumb on the right
|
||
// and alternates fan inward toward the center of the keyboard.
|
||
let opensLeft = keyFrame.midX > layoutWidth * 0.52
|
||
let ordered = opensLeft ? Array(rawOptions.reversed()) : rawOptions
|
||
let defaultIndex = opensLeft ? max(0, ordered.count - 1) : 0
|
||
|
||
let count = CGFloat(max(1, ordered.count))
|
||
let idealCellW = cellWidth
|
||
let maxCellW = (availableWidth - 2 * fixedPadding) / count
|
||
let cellW = min(idealCellW, maxCellW)
|
||
let bubbleW = count * cellW + 2 * fixedPadding
|
||
let bubbleH = fixedCellHeight + 2 * fixedPadding
|
||
|
||
// Anchor the default cell over the pressed key, then clamp inside [minX, maxX - bubbleW].
|
||
let defaultCellCenterOffset = fixedPadding + (CGFloat(defaultIndex) + 0.5) * cellW
|
||
var bubbleX = keyFrame.midX - defaultCellCenterOffset
|
||
bubbleX = max(minX, min(maxX - bubbleW, bubbleX))
|
||
let bubbleY = max(1, keyFrame.minY - fixedGap - bubbleH)
|
||
let bubbleFrame = CGRect(x: bubbleX, y: bubbleY, width: bubbleW, height: bubbleH)
|
||
|
||
let cells = (0..<ordered.count).map { idx in
|
||
CGRect(
|
||
x: bubbleX + fixedPadding + CGFloat(idx) * cellW,
|
||
y: bubbleY + fixedPadding,
|
||
width: cellW,
|
||
height: fixedCellHeight
|
||
)
|
||
}
|
||
return AccentMenu(
|
||
keyFrame: keyFrame,
|
||
bubbleFrame: bubbleFrame,
|
||
options: ordered,
|
||
cellFrames: cells,
|
||
selectedIndex: defaultIndex,
|
||
fixedCells: true
|
||
)
|
||
}
|
||
|
||
/// The cell under a finger at `point`: its row by height (above or below the callout, the
|
||
/// nearest row), then the nearest cell in that row.
|
||
func index(for point: CGPoint) -> Int {
|
||
guard !cellFrames.isEmpty else { return 0 }
|
||
let rowTops = Array(Set(cellFrames.map(\.minY))).sorted()
|
||
var rowY = rowTops[0]
|
||
for top in rowTops where point.y >= top { rowY = top }
|
||
let row = cellFrames.indices.filter { cellFrames[$0].minY == rowY }
|
||
if point.x <= cellFrames[row[0]].minX { return row[0] }
|
||
for idx in row where point.x <= cellFrames[idx].maxX { return idx }
|
||
return row[row.count - 1]
|
||
}
|
||
|
||
/// The nearest cell along a one-row callout (or the base's row) for `x`.
|
||
func index(for x: CGFloat) -> Int {
|
||
index(for: CGPoint(x: x, y: cellFrames.indices.contains(selectedIndex) ? cellFrames[selectedIndex].midY : 0))
|
||
}
|
||
}
|
||
|
||
/// Floating callout view that renders an active `AccentMenu` over the key plane.
|
||
@MainActor
|
||
final class AccentCalloutView: UIView {
|
||
var palette = FullKeyboardPalette(isDark: false) { didSet { if palette != oldValue { setNeedsDisplay() } } }
|
||
private(set) var menu: AccentMenu?
|
||
/// The skin-tone picker's options' size (fixed cells).
|
||
var optionFontSize: CGFloat = 22 { didSet { setNeedsDisplay() } }
|
||
/// An option set apart from the rest by a hairline (an emoji's untoned form).
|
||
var separatedOption: String? { didSet { setNeedsDisplay() } }
|
||
|
||
override init(frame: CGRect) {
|
||
super.init(frame: frame)
|
||
isOpaque = false
|
||
backgroundColor = .clear
|
||
isUserInteractionEnabled = false
|
||
contentMode = .redraw
|
||
}
|
||
|
||
@available(*, unavailable)
|
||
required init?(coder: NSCoder) { fatalError() }
|
||
|
||
func show(_ menu: AccentMenu?) {
|
||
guard menu != self.menu else { return }
|
||
self.menu = menu
|
||
isHidden = menu == nil
|
||
setNeedsDisplay()
|
||
}
|
||
|
||
/// Apple's options are 21.5 pt, selected or not.
|
||
static let font = UIFont.systemFont(ofSize: 21.5, weight: .regular)
|
||
|
||
override func draw(_ rect: CGRect) {
|
||
guard let menu, let context = UIGraphicsGetCurrentContext() else { return }
|
||
if menu.fixedCells { return drawFixed(menu, in: context) }
|
||
let pill = UIBezierPath(roundedRect: menu.bubbleFrame, cornerRadius: AccentMenu.cornerRadius)
|
||
|
||
context.saveGState()
|
||
context.setShadow(offset: CGSize(width: 0, height: 1), blur: 6, color: palette.bubbleShadow.cgColor)
|
||
context.addPath(pill.cgPath)
|
||
context.setFillColor(palette.bubbleFill.cgColor)
|
||
context.fillPath()
|
||
context.restoreGState()
|
||
if palette.isDark {
|
||
// Apple's dark callout lightens toward its bottom, as its key pop-up does.
|
||
context.saveGState()
|
||
context.addPath(pill.cgPath)
|
||
context.clip()
|
||
let colors = [palette.bubbleFill.cgColor, UIColor(white: 0.33, alpha: 1).cgColor] as CFArray
|
||
if let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0, 1]) {
|
||
context.drawLinearGradient(
|
||
gradient,
|
||
start: CGPoint(x: 0, y: menu.bubbleFrame.minY),
|
||
end: CGPoint(x: 0, y: menu.bubbleFrame.maxY),
|
||
options: []
|
||
)
|
||
}
|
||
context.restoreGState()
|
||
}
|
||
|
||
for (idx, option) in menu.options.enumerated() {
|
||
let cell = menu.cellFrames[idx]
|
||
if idx == menu.selectedIndex {
|
||
let highlight = UIBezierPath(
|
||
roundedRect: cell.insetBy(dx: AccentMenu.highlightInset, dy: 0),
|
||
cornerRadius: AccentMenu.highlightRadius
|
||
)
|
||
context.saveGState()
|
||
context.addPath(highlight.cgPath)
|
||
context.setFillColor(palette.accentSelectFill.cgColor)
|
||
context.fillPath()
|
||
context.restoreGState()
|
||
}
|
||
let attributes: [NSAttributedString.Key: Any] = [.font: Self.font, .foregroundColor: palette.textBase]
|
||
let size = (option as NSString).size(withAttributes: attributes)
|
||
(option as NSString).draw(
|
||
at: CGPoint(x: cell.midX - size.width / 2, y: cell.midY - size.height / 2),
|
||
withAttributes: attributes
|
||
)
|
||
}
|
||
}
|
||
|
||
/// The skin-tone picker, drawn as it was.
|
||
private func drawFixed(_ menu: AccentMenu, in context: CGContext) {
|
||
let pill = UIBezierPath(roundedRect: menu.bubbleFrame, cornerRadius: 12)
|
||
|
||
context.saveGState()
|
||
context.setShadow(offset: CGSize(width: 0, height: 3), blur: 10, color: palette.popupShadow.cgColor)
|
||
context.addPath(pill.cgPath)
|
||
context.setFillColor(palette.popupFill.cgColor)
|
||
context.fillPath()
|
||
context.restoreGState()
|
||
|
||
let scale = max(1, traitCollection.displayScale)
|
||
context.saveGState()
|
||
context.addPath(pill.cgPath)
|
||
context.setLineWidth(1 / scale)
|
||
context.setStrokeColor(palette.popupBorder.cgColor)
|
||
context.strokePath()
|
||
context.restoreGState()
|
||
|
||
if let separatedOption, let idx = menu.options.firstIndex(of: separatedOption), menu.options.count > 1 {
|
||
let cell = menu.cellFrames[idx]
|
||
let x = idx == 0 ? cell.maxX : cell.minX
|
||
context.saveGState()
|
||
context.setFillColor(palette.separator.cgColor)
|
||
context.fill(CGRect(x: x - 0.5 / scale, y: cell.minY + 9, width: 1 / scale, height: cell.height - 18))
|
||
context.restoreGState()
|
||
}
|
||
for (idx, option) in menu.options.enumerated() {
|
||
let cell = menu.cellFrames[idx]
|
||
let isSelected = idx == menu.selectedIndex
|
||
if isSelected {
|
||
context.saveGState()
|
||
let highlight = UIBezierPath(roundedRect: cell.insetBy(dx: 1.5, dy: 2), cornerRadius: 8)
|
||
context.addPath(highlight.cgPath)
|
||
context.setFillColor(palette.actionFill.cgColor)
|
||
context.fillPath()
|
||
context.restoreGState()
|
||
}
|
||
let attributes: [NSAttributedString.Key: Any] = [
|
||
.font: UIFont.systemFont(ofSize: optionFontSize, weight: isSelected ? .medium : .regular),
|
||
.foregroundColor: isSelected ? UIColor.white : palette.textBase,
|
||
]
|
||
let size = (option as NSString).size(withAttributes: attributes)
|
||
(option as NSString).draw(
|
||
at: CGPoint(x: cell.midX - size.width / 2, y: cell.midY - size.height / 2),
|
||
withAttributes: attributes
|
||
)
|
||
}
|
||
}
|
||
}
|