Skip to content

Commit

Permalink
Fixes #1911 - Automatically focus the composer when a hardware keyboa…
Browse files Browse the repository at this point in the history
…rds is connected
  • Loading branch information
stefanceriu committed Jan 16, 2024
1 parent b21194b commit cf1222c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import Combine
import Foundation
import GameKit
import SwiftUI
import WysiwygComposer

Expand Down Expand Up @@ -100,8 +101,10 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
.store(in: &cancellables)

setupMentionsHandling(mentionDisplayHelper: mentionDisplayHelper)

focusComposerIfHardwareKeyboardConnected()
}

// MARK: - Public

override func process(viewAction: ComposerToolbarViewAction) {
Expand Down Expand Up @@ -361,6 +364,22 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
private func removeLinks() {
wysiwygViewModel.applyLinkOperation(.removeLinks)
}

private func focusComposerIfHardwareKeyboardConnected() {
// The simulator always detects the hardware keyboard as connected
#if !targetEnvironment(simulator)
if GCKeyboard.coalesced != nil {
state.bindings.composerFocused = true
}

NotificationCenter.default.addObserver(self, selector: #selector(hardwareKeyboardDidConnect), name: .GCKeyboardDidConnect, object: nil)
#endif
}

@objc private func hardwareKeyboardDidConnect(_ notification: Notification) {
MXLog.error("Hardware keyboard did connect")
state.bindings.composerFocused = true
}
}

private extension LinkAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ struct ComposerToolbar: View {
.onChange(of: composerFocused) { newValue in
context.composerFocused = newValue
}
.onAppear {
composerFocused = context.composerFocused
}
}

private var placeholder: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ struct MessageComposer: View {
let replyCancellationAction: () -> Void
let editCancellationAction: () -> Void
let onAppearAction: () -> Void
@FocusState private var focused: Bool

@State private var composerTranslation: CGFloat = 0
private let composerShape = RoundedRectangle(cornerRadius: 21, style: .circular)
Expand Down Expand Up @@ -77,7 +76,6 @@ struct MessageComposer: View {
alignment: .top)
.tint(.compound.iconAccentTertiary)
.padding(.vertical, 10)
.focused($focused)
.onAppear {
onAppearAction()
}
Expand Down
1 change: 1 addition & 0 deletions changelog.d/1911.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Automatically focus the composer when a hardware keyboards is connected

0 comments on commit cf1222c

Please sign in to comment.