Skip to content

Commit

Permalink
Performance fix is selectable (#214)
Browse files Browse the repository at this point in the history
* Set isSelectable on tap as it uses a lot of time in init

* Introduced experimental flags for deferring isSelectable
  • Loading branch information
rajdeep authored Aug 15, 2023
1 parent 2376540 commit 0894de8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
8 changes: 7 additions & 1 deletion ExampleApp/ExampleApp/MasterViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//

import UIKit
import Proton

struct Navigation {
let title: String
Expand All @@ -32,7 +33,7 @@ struct NavigationItem {

class MasterViewController: UITableViewController {

let navigation = [
lazy var navigation = [
Navigation(title: "Basic features", items: [
NavigationItem(title: "Autogrowing Editor", viewController: { AutogrowingEditorViewExampleViewController() }),
]),
Expand All @@ -56,6 +57,11 @@ class MasterViewController: UITableViewController {
]),
]

required init?(coder: NSCoder) {
EditorView.experimentalFlags.isDeferredSelectable = true
super.init(coder: coder)
}

override func viewWillAppear(_ animated: Bool) {
clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
super.viewWillAppear(animated)
Expand Down
4 changes: 4 additions & 0 deletions Proton/Proton.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
1B183D8C23CEA90400AE83E5 /* EditorContentEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B183D8B23CEA90400AE83E5 /* EditorContentEncoding.swift */; };
1B183D8E23CEE9BA00AE83E5 /* AttributesEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B183D8D23CEE9BA00AE83E5 /* AttributesEncoding.swift */; };
1B183D9223CEEED900AE83E5 /* EditorContentEncoderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B183D9023CEEEC400AE83E5 /* EditorContentEncoderTests.swift */; };
1B1B36372A8B52390084C282 /* Experimental.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B1B36362A8B52390084C282 /* Experimental.swift */; };
1B1C3727244BE0D60028E1ED /* EditorViewContextTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B1C3726244BE0D60028E1ED /* EditorViewContextTests.swift */; };
1B238D6E2456A40200BF49D5 /* NullRichTextEditorContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B238D6D2456A40200BF49D5 /* NullRichTextEditorContext.swift */; };
1B2BC0D823CF17E300407DEE /* EditorContentTransformerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B2BC0D723CF17E300407DEE /* EditorContentTransformerTests.swift */; };
Expand Down Expand Up @@ -178,6 +179,7 @@
1B183D8B23CEA90400AE83E5 /* EditorContentEncoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorContentEncoding.swift; sourceTree = "<group>"; };
1B183D8D23CEE9BA00AE83E5 /* AttributesEncoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributesEncoding.swift; sourceTree = "<group>"; };
1B183D9023CEEEC400AE83E5 /* EditorContentEncoderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorContentEncoderTests.swift; sourceTree = "<group>"; };
1B1B36362A8B52390084C282 /* Experimental.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Experimental.swift; sourceTree = "<group>"; };
1B1C3726244BE0D60028E1ED /* EditorViewContextTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorViewContextTests.swift; sourceTree = "<group>"; };
1B238D6D2456A40200BF49D5 /* NullRichTextEditorContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NullRichTextEditorContext.swift; sourceTree = "<group>"; };
1B2BC0D723CF17E300407DEE /* EditorContentTransformerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorContentTransformerTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -493,6 +495,7 @@
1B82570C23C5686F0033A0A9 /* EditorViewContext.swift */,
1B8BE91A23C71CEA00353B17 /* EditorViewDelegate.swift */,
1B36192C2A68D89A00323BBF /* AggregateEditorViewDelegate.swift */,
1B1B36362A8B52390084C282 /* Experimental.swift */,
);
path = Editor;
sourceTree = "<group>";
Expand Down Expand Up @@ -1033,6 +1036,7 @@
1B82570923C5674E0033A0A9 /* EditorCommand.swift in Sources */,
1B91E7B926AA698A0002DF45 /* StrikethroughCommand.swift in Sources */,
1B82570D23C5686F0033A0A9 /* EditorViewContext.swift in Sources */,
1B1B36372A8B52390084C282 /* Experimental.swift in Sources */,
1B975AFF23CD454700EC410C /* RichTextViewContext.swift in Sources */,
1B45CDE823C05C0C001EB196 /* Focusable.swift in Sources */,
1B7C76AA2608A489006618AC /* FontTraitToggleCommand.swift in Sources */,
Expand Down
29 changes: 28 additions & 1 deletion Proton/Sources/Swift/Core/RichTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ class RichTextView: AutogrowingTextView {
weak var defaultTextFormattingProvider: DefaultTextFormattingProviding?
{
get { richTextStorage.defaultTextFormattingProvider }
set { richTextStorage.defaultTextFormattingProvider = newValue }
set {
richTextStorage.defaultTextFormattingProvider = newValue
// typingAttributes[.font] = defaultTypingAttributes[.font]
// typingAttributes[.paragraphStyle] = defaultTypingAttributes[.paragraphStyle]
// typingAttributes[.foregroundColor] = defaultTypingAttributes[.foregroundColor]
}
}

private lazy var placeholderLabel: UILabel = {
Expand Down Expand Up @@ -222,6 +227,18 @@ class RichTextView: AutogrowingTextView {

self.backgroundColor = defaultBackgroundColor
self.textColor = defaultTextColor
// self.typingAttributes = defaultTypingAttributes
}

private var _isSelectable = false
override var isSelectable: Bool {
get { return _isSelectable }
set {
_isSelectable = newValue
if EditorView.experimentalFlags.isDeferredSelectable == false {
super.isSelectable = newValue
}
}
}

var contentLength: Int {
Expand Down Expand Up @@ -511,9 +528,19 @@ class RichTextView: AutogrowingTextView {
func didTap(at location: CGPoint) {
context?.selectedTextView = self
let characterRange = rangeOfCharacter(at: location)
enableSelectable()
richTextViewDelegate?.richTextView(self, didTapAtLocation: location, characterRange: characterRange)
}

private func enableSelectable() {
guard EditorView.experimentalFlags.isDeferredSelectable else { return }
super.isSelectable = _isSelectable
becomeFirstResponder()
typingAttributes[.font] = defaultTypingAttributes[.font]
typingAttributes[.paragraphStyle] = defaultTypingAttributes[.paragraphStyle]
typingAttributes[.foregroundColor] = defaultTypingAttributes[.foregroundColor]
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: self)
Expand Down
3 changes: 3 additions & 0 deletions Proton/Sources/Swift/Editor/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ open class EditorView: UIView {
let context: RichTextViewContext
var needsAsyncTextResolution = false

public static var experimentalFlags = Experimental()


// Holds `attributedText` until Editor move to a window
// Setting attributed text without Editor being fully ready
// causes issues with cached bounds that shows up when rotating the device.
Expand Down
25 changes: 25 additions & 0 deletions Proton/Sources/Swift/Editor/Experimental.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Experimental.swift
// Proton
//
// Created by Rajdeep Kwatra on 15/8/2023.
// Copyright © 2023 Rajdeep Kwatra. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

public struct Experimental {
public var isDeferredSelectable = false
}

0 comments on commit 0894de8

Please sign in to comment.