Skip to content

Commit

Permalink
Font annotation fixes, added custom input accessory
Browse files Browse the repository at this point in the history
  • Loading branch information
michalrentka committed Aug 3, 2023
1 parent 78fe34c commit 43de4a1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Zotero.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@
B324278225C841A600567504 /* WsResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = B324278125C841A600567504 /* WsResponse.swift */; };
B3242CCD246ABBAF00D8748F /* AnnotationsConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3242CCC246ABBAF00D8748F /* AnnotationsConfig.swift */; };
B3243BC82A5EB2740033A7D6 /* HtmlAttributedStringConverterSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3243BC72A5EB2740033A7D6 /* HtmlAttributedStringConverterSpec.swift */; };
B325C40A2A7A8DE0008A2F11 /* CustomFreeTextAnnotationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B325C4092A7A8DE0008A2F11 /* CustomFreeTextAnnotationView.swift */; };
B325DBAC24374F4600EFF0F5 /* AppCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B325DBAB24374F4600EFF0F5 /* AppCoordinator.swift */; };
B325DBAD24375C7B00EFF0F5 /* UIViewController+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B305650E23FC051E003304F2 /* UIViewController+Extensions.swift */; };
B325DBAF24375D8D00EFF0F5 /* Conflict.swift in Sources */ = {isa = PBXBuildFile; fileRef = B325DBAE24375D8D00EFF0F5 /* Conflict.swift */; };
Expand Down Expand Up @@ -1445,6 +1446,7 @@
B324278125C841A600567504 /* WsResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WsResponse.swift; sourceTree = "<group>"; };
B3242CCC246ABBAF00D8748F /* AnnotationsConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnnotationsConfig.swift; sourceTree = "<group>"; };
B3243BC72A5EB2740033A7D6 /* HtmlAttributedStringConverterSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HtmlAttributedStringConverterSpec.swift; sourceTree = "<group>"; };
B325C4092A7A8DE0008A2F11 /* CustomFreeTextAnnotationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomFreeTextAnnotationView.swift; sourceTree = "<group>"; };
B325DBAB24374F4600EFF0F5 /* AppCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppCoordinator.swift; sourceTree = "<group>"; };
B325DBAE24375D8D00EFF0F5 /* Conflict.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Conflict.swift; sourceTree = "<group>"; };
B325DBB124375DAC00EFF0F5 /* ConflictResolution.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConflictResolution.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2739,6 +2741,7 @@
B340ECA5290FDC9F00EE920D /* AnnotationToolbarViewController.swift */,
B38B24B826E7664400BDD1BB /* AnnotationToolOptionsViewController.swift */,
B33AB487246D315F00490DDE /* CheckboxButton.swift */,
B325C4092A7A8DE0008A2F11 /* CustomFreeTextAnnotationView.swift */,
B3A95DA929194BDE00BCCF11 /* DashedView.swift */,
B3B953CD245981DA00FC96DB /* PDFDocumentViewController.swift */,
B34E3DEB28CF490700B747AE /* PDFExportSettingsView.swift */,
Expand Down Expand Up @@ -4644,6 +4647,7 @@
B3A47C3629015FCE00E7D90D /* TableOfContentsActionHandler.swift in Sources */,
B30565BE23FC051E003304F2 /* CreateItemFromDetailDbRequest.swift in Sources */,
B3593F77241A76E600760E20 /* CollectionEditActionHandler.swift in Sources */,
B325C40A2A7A8DE0008A2F11 /* CustomFreeTextAnnotationView.swift in Sources */,
B3BF7EE728A51BDC00A5A659 /* DeleteTagFromItemDbRequest.swift in Sources */,
B305661E23FC051E003304F2 /* DeleteGroupSyncAction.swift in Sources */,
B373877328FEA0C7004E5031 /* PDFSidebarViewController.swift in Sources */,
Expand Down
1 change: 0 additions & 1 deletion Zotero/Controllers/AnnotationConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ struct AnnotationConverter {

private static func freeTextAnnotation(from annotation: Annotation, color: UIColor, boundingBoxConverter: AnnotationBoundingBoxConverter) -> PSPDFKit.FreeTextAnnotation {
let text = PSPDFKit.FreeTextAnnotation(contents: annotation.comment)
text.fontName = "Helvetica"
text.color = color
text.fontSize = CGFloat(annotation.fontSize ?? 0)
text.setRotation(annotation.rotation ?? 0, updateBoundingBox: false)
Expand Down
3 changes: 2 additions & 1 deletion Zotero/Scenes/Detail/PDF/Models/DatabaseAnnotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ struct DatabaseAnnotation {
}

var rotation: UInt? {
return (self.item.fields.filter(.key(FieldKeys.Item.Annotation.Position.rotation)).first?.value).flatMap(UInt.init)
guard let rotation = (self.item.fields.filter(.key(FieldKeys.Item.Annotation.Position.rotation)).first?.value).flatMap(Double.init) else { return nil }
return UInt(round(rotation))
}

var sortIndex: String {
Expand Down
40 changes: 40 additions & 0 deletions Zotero/Scenes/Detail/PDF/Views/CustomFreeTextAnnotationView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// CustomFreeTextAnnotationView.swift
// Zotero
//
// Created by Michal Rentka on 02.08.2023.
// Copyright © 2023 Corporation for Digital Scholarship. All rights reserved.
//

import UIKit

import PSPDFKit
import PSPDFKitUI

protocol FreeTextInputDelegate: AnyObject {
func showColorPicker(sender: UIView)
func showFontSizePicker(sender: UIView)
func changeFontSize(size: UInt)
}

final class CustomFreeTextAnnotationView: FreeTextAnnotationView {
weak var delegate: FreeTextInputDelegate?

override func textViewForEditing() -> UITextView {
let textView = super.textViewForEditing()
textView.inputAccessoryView = FreeTextInputAccessory()
return textView
}
}

final class FreeTextInputAccessory: UIView {
init() {
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 44))
self.autoresizingMask = .flexibleWidth
self.backgroundColor = .systemBackground
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
18 changes: 18 additions & 0 deletions Zotero/Scenes/Detail/PDF/Views/PDFDocumentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ final class PDFDocumentViewController: UIViewController {
builder.overrideClass(PSPDFKit.NoteAnnotation.self, with: NoteAnnotation.self)
builder.overrideClass(PSPDFKit.SquareAnnotation.self, with: SquareAnnotation.self)
builder.overrideClass(PSPDFKit.UnderlineAnnotation.self, with: UnderlineAnnotation.self)
builder.overrideClass(FreeTextAnnotationView.self, with: CustomFreeTextAnnotationView.self)
}

let controller = PDFViewController(document: document, configuration: pdfConfiguration)
Expand Down Expand Up @@ -595,6 +596,10 @@ extension PDFDocumentViewController: PDFViewControllerDelegate {
if let annotation = annotations.first,
annotation.type == .freeText,
let annotationView = pageView.visibleAnnotationViews.first(where: { $0.annotation == annotation }) as? FreeTextAnnotationView {
if let annotationView = annotationView as? CustomFreeTextAnnotationView {
annotationView.delegate = self
}

// Free text annotation is being selected, check whether we should focus text view
guard annotation.key != nil, // don't focus new annotations, they are already focused
self.presentedViewController == nil // don't focus if annotation popup is visible
Expand Down Expand Up @@ -802,6 +807,19 @@ extension PDFDocumentViewController: AnnotationBoundingBoxConverter {
}
}

extension PDFDocumentViewController: FreeTextInputDelegate {
func showColorPicker(sender: UIView) {
}

func showFontSizePicker(sender: UIView) {
}

func changeFontSize(size: UInt) {
guard let key = self.viewModel.state.selectedAnnotation?.key else { return }
self.viewModel.process(action: .setFontSize(key: key, size: size))
}
}

final class SelectionView: UIView {
static let inset: CGFloat = 4.5 // 2.5 for border, 2 for padding

Expand Down

0 comments on commit 43de4a1

Please sign in to comment.