Fixes #1911 - Automatically focus the composer when a hardware keyboards is connected (#2343)

This commit is contained in:
Stefan Ceriu
2024-01-17 10:06:31 +02:00
committed by GitHub
parent 88e2f655aa
commit b573fe5b9b
4 changed files with 25 additions and 3 deletions

View File

@@ -16,6 +16,7 @@
import Combine
import Foundation
import GameKit
import SwiftUI
import WysiwygComposer
@@ -100,6 +101,8 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
.store(in: &cancellables)
setupMentionsHandling(mentionDisplayHelper: mentionDisplayHelper)
focusComposerIfHardwareKeyboardConnected()
}
// MARK: - Public
@@ -361,6 +364,23 @@ 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 {
MXLog.info("Hardware keyboard is connected")
state.bindings.composerFocused = true
}
NotificationCenter.default.addObserver(self, selector: #selector(hardwareKeyboardDidConnect), name: .GCKeyboardDidConnect, object: nil)
#endif
}
@objc private func hardwareKeyboardDidConnect(_ notification: Notification) {
MXLog.info("Did connect hardware keyboard")
state.bindings.composerFocused = true
}
}
private extension LinkAction {

View File

@@ -190,6 +190,9 @@ struct ComposerToolbar: View {
.onChange(of: composerFocused) { newValue in
context.composerFocused = newValue
}
.onAppear {
composerFocused = context.composerFocused
}
}
private var placeholder: String {

View File

@@ -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)
@@ -77,7 +76,6 @@ struct MessageComposer: View {
alignment: .top)
.tint(.compound.iconAccentTertiary)
.padding(.vertical, 10)
.focused($focused)
.onAppear {
onAppearAction()
}

1
changelog.d/1911.feature Normal file
View File

@@ -0,0 +1 @@
Automatically focus the composer when a hardware keyboards is connected