Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address bar refactoring #1610

Merged
merged 14 commits into from
Sep 14, 2023
Merged

Address bar refactoring #1610

merged 14 commits into from
Sep 14, 2023

Conversation

mallexxx
Copy link
Collaborator

@mallexxx mallexxx commented Sep 8, 2023

Task/Issue URL: https://app.asana.com/0/1177771139624306/1205421610519119/f
BSK PR: duckduckgo/BrowserServicesKit#499
iOS PR: duckduckgo/iOS#1989

Description:

  • refactored String NSRange helpers to use designated functions instead of UTF16 view
  • refactored AddressBarTextField for better structure and readability, added comments
  • added Undo Manager support (cmd+z)
  • Fixed opt+delete word forward deletion corrupting suffix
  • Fixed opt+arrows by-word navigation
  • Hopefully fixed a crash

Steps to test this PR:

  1. Test address bar input, autocompletion selection for search terms, urls and bookmarks, performing search and editing url
  2. Test opt+arrows for by-word navigation, test opt+backspace, opt+delete(fn+backspace) by word deletion
  3. Test Undo/Redo for typing, entering words or separators, autocompleted suggestions and choosing autocompletion
  4. Switching between tabs should keep the value and reset the Undo Manger
  5. Validate testing steps from subtasks of https://app.asana.com/0/0/1205449500218048/f

Internal references:

Pull Request Review Checklist
Software Engineering Expectations
Technical Design Template
Pull Request Documentation

@mallexxx mallexxx requested a review from tomasstrba September 8, 2023 10:31
@mallexxx mallexxx changed the title Alex/address bar refactoring Address bar refactoring Sep 8, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Sep 8, 2023

Warnings
⚠️ PR has more than 500 lines of code changing. Consider splitting into smaller PRs if possible.

Generated by 🚫 dangerJS against 7ee3bfc

override func paste(_ sender: Any?) {
// Fixes an issue when url-name instead of url is pasted
if let url = NSPasteboard.general.url {
super.pasteAsPlainText(url.absoluteString)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed extra controlTextDidChange call: paste/pasteAsPlainText already calls controlTextDidChange

@available(macOS 12.0, *)
override var textLayoutManager: NSTextLayoutManager? {
if let textLayoutManager = super.textLayoutManager,
!(textLayoutManager.textSelectionNavigation is AddressBarTextSelectionNavigation) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from macOS 12 TextKit uses new architecture, need to override TextSelectionNavigation to adjust by-word text selection


// MARK: - Exclude “ – Search with DuckDuckGo” suffix from selection range

override func setSelectedRanges(_ ranges: [NSValue], affinity: NSSelectionAffinity, stillSelecting stillSelectingFlag: Bool) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final editor selection adjustment. disables putting cursor in the Suffix

breakUndoCoalescingIfNeeded(for: InputType(string))

addressBar.textView(self, userTypedString: string, at: replacementRange.location == NSNotFound ? self.selectedRange() : replacementRange) {
super.insertText(string, replacementRange: replacementRange)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment in textView:userTypedString:at: - using callback to set temporary flags for handleTextDidChange


// MARK: - Moving selection by word

@available(macOS, deprecated: 12.0, message: "Move this logic to AddressBarTextSelectionNavigation")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving this logic here until we drop Big Sur


override var allowsUndo: Bool {
get {
!(addressBar?.isUndoDisabled ?? false) && super.allowsUndo
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disable adding undo action when the text field is set directly e.g. for suggestion autocompletion

defer {
lastInputType = inputType
}
switch (lastInputType, inputType) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"merges" undo actions of the same type into coalesced actions. When entering separators the coalescing is broken to avoid undoing the whole user input but only undo the last typed word

import Combine
import Common
import BrowserServicesKit

protocol AddressBarTextFieldDelegate: AnyObject {

func adressBarTextField(_ addressBarTextField: AddressBarTextField, didChangeValue value: AddressBarTextField.Value)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value is already published so removed this to avoid using 2 patterns and only observe the published property

Copy link
Contributor

@tomasstrba tomasstrba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this is a great improvement! 🏅 The user experience of the address bar is so much better now.

In terms of code, I don't have any comment. The logic is pretty complicated so the codebase just reflects that. I am missing some unit tests, but since you work with subclasses of views, I don't think it's even possible to reasonable unit test anything here.

I only have one concert but it doesn't have to be correct. Do you feel like performance of address bar is lower with this change? For example, when typing, it feels slower. If not, then feel free to merge! LGTM!

@tomasstrba tomasstrba assigned mallexxx and unassigned tomasstrba Sep 13, 2023
@mallexxx
Copy link
Collaborator Author

mallexxx commented Sep 14, 2023

@tomasstrba, I‘ve profiled text editing - nothing really stood out to me except Sharing Menu updating on each hotkey press - which eats some time but it‘s not a regression and to be handled in a separate PR. TextView inner machinery takes much more CPU time than all our code.
There is some slowness on launch - because of keychain migration - but this was targeted in another PR

Screenshot 2023-09-14 at 10 16 31

Screenshot 2023-09-14 at 10 14 34

@mallexxx mallexxx merged commit b3229e5 into develop Sep 14, 2023
@mallexxx mallexxx deleted the alex/address-bar-refactoring branch September 14, 2023 10:18
samsymons added a commit that referenced this pull request Sep 14, 2023
# By Alexey Martemyanov (3) and Dominik Kapusta (1)
# Via GitHub
* develop:
  disable flaky testWhenAutoconsentDisabled_promptIsDisplayed (#1619)
  Prevent crash on database init failure (#1617)
  Address bar refactoring (#1610)
  Switch Sync environment to production (#1622)

# Conflicts:
#	DuckDuckGo/AppDelegate/AppDelegate.swift
samsymons added a commit that referenced this pull request Sep 17, 2023
# By Alexey Martemyanov (8) and others
# Via GitHub
* develop:
  Clean up login titles at insert and update + migration (#1605)
  Changes for DBP internal release (#1621)
  Fix NetP builds in the DBP target (#1627)
  Put Sparkle behind a new SPARKLE flag (#1616)
  prevent WKFullScreenWindowController crashing after `close` call (#1618)
  Use Development environment for Sync by default in Debug builds (#1625)
  disable flaky testWhenAutoconsentDisabled_promptIsDisplayed (#1619)
  Prevent crash on database init failure (#1617)
  Address bar refactoring (#1610)
  Switch Sync environment to production (#1622)
  Fixes startup options reset (#1581)
  perform Email Keychain migration only once (#1600)
  Fix NetP embedding in DBP builds (#1614)
  Bump version to 1.56.0 (58)
  Update embedded files
  fix Package.resolved
  New deployment target set to 11.4 (#1592)

# Conflicts:
#	DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
#	DuckDuckGo/Main/Main.swift
samsymons added a commit that referenced this pull request Sep 18, 2023
# By Alexey Martemyanov (7) and others
* release/1.57.0: (24 commits)
  Bump version to 1.57.0 (59)
  Update embedded files
  Remove sandbox entitlement from NetP login item (#1629)
  Make sure Catalina build is in the appcast (#1632)
  implement onboarding experiment (#1575)
  fix bug where when in case of error address shown address bar is wrong (#1604)
  Update BSK for iOS phased rollout feature flag (#1623)
  Autofill update for iOS de-duplication of prompted logins (#1612)
  Tool for uploading release files to s3 (#1561)
  fix Safari import not working (#1630)
  Refactor Sharing menu, fix QLPreviewPanel warnings (#1624)
  Clean up login titles at insert and update + migration (#1605)
  Changes for DBP internal release (#1621)
  Fix NetP builds in the DBP target (#1627)
  Put Sparkle behind a new SPARKLE flag (#1616)
  prevent WKFullScreenWindowController crashing after `close` call (#1618)
  Use Development environment for Sync by default in Debug builds (#1625)
  disable flaky testWhenAutoconsentDisabled_promptIsDisplayed (#1619)
  Prevent crash on database init failure (#1617)
  Address bar refactoring (#1610)
  ...

# Conflicts:
#	DuckDuckGo/ContentBlocker/macos-config.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants