forked from readium/swift-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectableNavigator.swift
59 lines (50 loc) · 2.43 KB
/
SelectableNavigator.swift
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
//
// Copyright 2021 Readium Foundation. All rights reserved.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//
import Foundation
import R2Shared
import UIKit
/// A navigator supporting user selection.
public protocol SelectableNavigator: Navigator {
/// Currently selected content.
var currentSelection: Selection? { get }
/// Clears the current selection.
func clearSelection()
}
/// Represents a user content selection in a navigator.
///
/// In the case of a text selection, you can get its content using `locator.text.highlight`.
public struct Selection {
/// Location of the user selection in the `Publication`.
public let locator: Locator
/// Frame of the bounding rect for the selection, in the coordinate of the navigator view.
///
/// This is only useful in the context of a `VisualNavigator`.
public let frame: CGRect?
}
public protocol SelectableNavigatorDelegate: NavigatorDelegate {
/// Returns whether the default edit menu (`UIMenuController`) should be displayed for the given `selection`.
///
/// To implement a custom selection pop-up, return false and display your own view using `selection.frame`.
func navigator(_ navigator: SelectableNavigator, shouldShowMenuForSelection selection: Selection) -> Bool
/// Returns whether the given `action` should be visible in the edit menu of the given `selection`.
///
/// Implement this delegate method to validate the selection before showing a particular action. For example, making
/// sure the selected text is not too large for a definition look up:
///
/// public func navigator(_ navigator: SelectableNavigator, canPerformAction action: EditingAction, for selection: Selection) -> Bool {
/// switch action {
/// case .lookup:
/// return (selection.locator.text.highlight?.count ?? 0) <= 50
/// default:
/// return true
/// }
/// }
func navigator(_ navigator: SelectableNavigator, canPerformAction action: EditingAction, for selection: Selection) -> Bool
}
public extension SelectableNavigatorDelegate {
func navigator(_ navigator: SelectableNavigator, shouldShowMenuForSelection selection: Selection) -> Bool { true }
func navigator(_ navigator: SelectableNavigator, canPerformAction action: EditingAction, for selection: Selection) -> Bool { true }
}