macOS: Handle input source change notifications in QCocoaInputContext

Instead of going via QNSView. Also add some logging.

Pick-to: 6.2
Change-Id: Iabed7511572ef22597651efa8047f06227b28533
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Tor Arne Vestbø 2021-08-18 20:53:13 +02:00
parent 4ee20e3a2f
commit f9beb3c4eb
6 changed files with 26 additions and 26 deletions

View File

@ -72,6 +72,7 @@ Q_DECLARE_LOGGING_CATEGORY(lcQpaWindow)
Q_DECLARE_LOGGING_CATEGORY(lcQpaDrawing)
Q_DECLARE_LOGGING_CATEGORY(lcQpaMouse)
Q_DECLARE_LOGGING_CATEGORY(lcQpaKeys)
Q_DECLARE_LOGGING_CATEGORY(lcQpaInputMethods)
Q_DECLARE_LOGGING_CATEGORY(lcQpaScreen)
Q_DECLARE_LOGGING_CATEGORY(lcQpaApplication)
Q_DECLARE_LOGGING_CATEGORY(lcQpaClipboard)

View File

@ -59,6 +59,7 @@ Q_LOGGING_CATEGORY(lcQpaWindow, "qt.qpa.window");
Q_LOGGING_CATEGORY(lcQpaDrawing, "qt.qpa.drawing");
Q_LOGGING_CATEGORY(lcQpaMouse, "qt.qpa.input.mouse", QtCriticalMsg);
Q_LOGGING_CATEGORY(lcQpaKeys, "qt.qpa.input.keys", QtCriticalMsg);
Q_LOGGING_CATEGORY(lcQpaInputMethods, "qt.qpa.input.methods")
Q_LOGGING_CATEGORY(lcQpaScreen, "qt.qpa.screen", QtCriticalMsg);
Q_LOGGING_CATEGORY(lcQpaApplication, "qt.qpa.application");
Q_LOGGING_CATEGORY(lcQpaClipboard, "qt.qpa.clipboard")

View File

@ -44,6 +44,8 @@
#include <QtCore/QLocale>
#include <QtCore/QPointer>
#include <QtCore/private/qcore_mac_p.h>
QT_BEGIN_NAMESPACE
class QCocoaInputContext : public QPlatformInputContext
@ -67,6 +69,7 @@ private Q_SLOTS:
private:
QPointer<QWindow> mWindow;
QLocale m_locale;
QMacNotificationObserver m_inputSourceObserver;
};
QT_END_NAMESPACE

View File

@ -85,6 +85,13 @@ QCocoaInputContext::QCocoaInputContext()
, mWindow(QGuiApplication::focusWindow())
{
QMetaObject::invokeMethod(this, "connectSignals", Qt::QueuedConnection);
m_inputSourceObserver = QMacNotificationObserver(nil,
NSTextInputContextKeyboardSelectionDidChangeNotification, [&]() {
qCDebug(lcQpaInputMethods) << "Text input source changed";
updateLocale();
});
updateLocale();
}
@ -146,18 +153,21 @@ void QCocoaInputContext::focusObjectChanged(QObject *focusObject)
void QCocoaInputContext::updateLocale()
{
TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
CFArrayRef languages = (CFArrayRef) TISGetInputSourceProperty(source, kTISPropertyInputSourceLanguages);
if (CFArrayGetCount(languages) > 0) {
CFStringRef langRef = (CFStringRef)CFArrayGetValueAtIndex(languages, 0);
QString name = QString::fromCFString(langRef);
QLocale locale(name);
if (m_locale != locale) {
m_locale = locale;
emitLocaleChanged();
}
QCFType<TISInputSourceRef> source = TISCopyCurrentKeyboardInputSource();
NSArray *languages = static_cast<NSArray*>(TISGetInputSourceProperty(source,
kTISPropertyInputSourceLanguages));
qCDebug(lcQpaInputMethods) << "Input source supports" << languages;
if (!languages.count)
return;
QString language = QString::fromNSString(languages.firstObject);
QLocale locale(language);
if (m_locale != locale) {
qCDebug(lcQpaInputMethods) << "Reporting new locale" << locale;
m_locale = locale;
emitLocaleChanged();
}
CFRelease(source);
}
QT_END_NAMESPACE

View File

@ -116,7 +116,6 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSViewMouseMoveHelper);
@end
@interface QNSView (ComplexText) <NSTextInputClient>
- (void)textInputContextKeyboardSelectionDidChangeNotification:(NSNotification *)textInputContextKeyboardSelectionDidChangeNotification;
@end
@implementation QNSView {
@ -159,11 +158,6 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSViewMouseMoveHelper);
[self initDrawing];
[self initMouse];
[self registerDragTypes];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textInputContextKeyboardSelectionDidChangeNotification:)
name:NSTextInputContextKeyboardSelectionDidChangeNotification
object:nil];
}
return self;
}

View File

@ -265,13 +265,4 @@
return nil;
}
- (void)textInputContextKeyboardSelectionDidChangeNotification:(NSNotification *)textInputContextKeyboardSelectionDidChangeNotification
{
Q_UNUSED(textInputContextKeyboardSelectionDidChangeNotification);
if (([NSApp keyWindow] == self.window) && self.window.firstResponder == self) {
if (QCocoaInputContext *ic = qobject_cast<QCocoaInputContext *>(QCocoaIntegration::instance()->inputContext()))
ic->updateLocale();
}
}
@end