Files
ReadViewSDK/Sources/RDEpubReaderView/EPUBUI/RDEPUBReaderBottomToolView.swift
T

139 lines
5.3 KiB
Swift

import UIKit
final class RDEPUBReaderBottomToolView: RDEPUBReaderToolView, RDEPUBReaderBottomToolViewProtocol {
var onShowTableOfContents: (() -> Void)?
var onShowBookmarks: (() -> Void)?
var onShowHighlights: (() -> Void)?
@available(*, deprecated, message: "Use the text selection menu to create annotations.")
var onAddHighlight: (() -> Void)?
var onShowSettings: (() -> Void)?
private let stackView: UIStackView = {
let view = UIStackView()
view.axis = .horizontal
view.distribution = .fillEqually
view.alignment = .fill
view.spacing = 16
return view
}()
private let chapterButton = RDEPUBReaderTintButton(type: .system)
private let bookmarksButton = RDEPUBReaderTintButton(type: .system)
/// 标注列表入口:查看全书已有划线 / 注释
private let annotationListButton = RDEPUBReaderTintButton(type: .system)
private let settingsButton = RDEPUBReaderTintButton(type: .system)
override init(frame: CGRect) {
super.init(frame: frame)
accessibilityIdentifier = "epub.reader.bottomToolbar"
addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(chapterButton)
stackView.addArrangedSubview(bookmarksButton)
stackView.addArrangedSubview(annotationListButton)
stackView.addArrangedSubview(settingsButton)
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
stackView.topAnchor.constraint(equalTo: topAnchor),
stackView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor)
])
configureButton(chapterButton, systemName: "list.bullet", fallbackTitle: "目录")
configureButton(bookmarksButton, systemName: "bookmark", fallbackTitle: "书签")
configureButton(annotationListButton, systemName: "note.text", fallbackTitle: "标注")
configureButton(settingsButton, systemName: "textformat.size", fallbackTitle: "设置")
chapterButton.accessibilityIdentifier = "epub.reader.toc"
bookmarksButton.accessibilityIdentifier = "epub.reader.bookmarks"
annotationListButton.accessibilityIdentifier = "epub.reader.highlights"
settingsButton.accessibilityIdentifier = "epub.reader.settings"
[chapterButton, bookmarksButton, annotationListButton, settingsButton].forEach { button in
button.heightAnchor.constraint(greaterThanOrEqualToConstant: 44).isActive = true
}
chapterButton.addTarget(self, action: #selector(chapterAction), for: .touchUpInside)
bookmarksButton.addTarget(self, action: #selector(bookmarksAction), for: .touchUpInside)
annotationListButton.addTarget(self, action: #selector(annotationListAction), for: .touchUpInside)
settingsButton.addTarget(self, action: #selector(settingsAction), for: .touchUpInside)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func lineFrame(in bounds: CGRect) -> CGRect {
CGRect(x: 0, y: 0, width: bounds.width, height: 0.5)
}
override func apply(theme: RDEPUBReaderTheme) {
super.apply(theme: theme)
[chapterButton, bookmarksButton, annotationListButton, settingsButton].forEach { button in
button.tintColor = theme.toolControlTextColor
button.setTitleColor(theme.toolControlTextColor, for: .normal)
}
}
func setHighlightsEnabled(_ isEnabled: Bool) {
annotationListButton.isEnabled = isEnabled
annotationListButton.alpha = isEnabled ? 1 : 0.45
}
@available(*, deprecated, message: "Use the text selection menu to create annotations.")
func setAddHighlightEnabled(_ isEnabled: Bool) {
// The built-in toolbar no longer exposes this action. Keep the method so
// custom toolbar integrations compiled against previous SDKs remain valid.
}
func setBookmarksEnabled(_ isEnabled: Bool) {
bookmarksButton.isEnabled = isEnabled
bookmarksButton.alpha = isEnabled ? 1 : 0.45
}
func updateVisibility(
showsTableOfContents: Bool,
allowsHighlights: Bool,
showsSettingsPanel: Bool
) {
chapterButton.isHidden = !showsTableOfContents
bookmarksButton.isHidden = false
annotationListButton.isHidden = !allowsHighlights
settingsButton.isHidden = !showsSettingsPanel
}
private func configureButton(_ button: UIButton, systemName: String, fallbackTitle: String) {
button.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
if #available(iOS 13.0, *), let image = UIImage(systemName: systemName) {
button.setImage(image.withRenderingMode(.alwaysTemplate), for: .normal)
} else {
button.setTitle(fallbackTitle, for: .normal)
}
}
@objc private func chapterAction() {
onShowTableOfContents?()
}
@objc private func bookmarksAction() {
onShowBookmarks?()
}
@objc private func annotationListAction() {
onShowHighlights?()
}
@objc private func settingsAction() {
onShowSettings?()
}
}