Files
otunes/apps/mobile/modules/player-navigation-menu/ios/PlayerNavigationMenuModule.swift
T

57 lines
1.9 KiB
Swift

import ExpoModulesCore
import UIKit
public class PlayerNavigationMenuModule: Module {
public func definition() -> ModuleDefinition {
Name("PlayerNavigationMenu")
View(PlayerNavigationMenuView.self) {
Events("onAction")
Prop("artist") { (view: PlayerNavigationMenuView, value: String) in
view.artist = value
}
Prop("album") { (view: PlayerNavigationMenuView, value: String) in
view.album = value
}
Prop("fontSize") { (view: PlayerNavigationMenuView, value: Double) in
view.fontSize = CGFloat(value)
}
}
}
}
final class PlayerNavigationMenuView: ExpoView {
let onAction = EventDispatcher()
var artist = "" { didSet { updateMenu(); setNeedsLayout() } }
var album = "" { didSet { updateMenu() } }
var fontSize: CGFloat = 20 { didSet { setNeedsLayout() } }
private let button = UIButton(type: .custom)
required init(appContext: AppContext? = nil) {
super.init(appContext: appContext)
button.showsMenuAsPrimaryAction = true
button.preferredMenuElementOrder = .fixed
button.accessibilityHint = "Opens album and artist options"
addSubview(button)
}
private func updateMenu() {
button.accessibilityLabel = artist
button.menu = UIMenu(children: [
UIAction(title: "Go to Album", subtitle: album, image: UIImage(systemName: "square.stack")) { [weak self] _ in
self?.onAction(["action": "go-album"])
},
UIAction(title: "Go to Artist", subtitle: artist, image: UIImage(systemName: "music.mic")) { [weak self] _ in
self?.onAction(["action": "go-artist"])
},
])
}
override func layoutSubviews() {
super.layoutSubviews()
// Anchor to the visible artist text, not the marquee's offscreen track.
let textWidth = (artist as NSString).size(withAttributes: [.font: UIFont.systemFont(ofSize: fontSize)]).width
button.frame = CGRect(x: 0, y: 0, width: min(ceil(textWidth), bounds.width), height: bounds.height)
}
}