Skip to content

Truly independent web browser

UI/Qt: Spellcheck with NSSpellChecker on macOS

sideshowbarkerd1cab6ab23ec
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txt
index 5e0d2d0254f..8079a70ba18 100644
--- a/UI/Qt/CMakeLists.txt
+++ b/UI/Qt/CMakeLists.txt
@@ -81,12 +81,14 @@ if (APPLE)
LadybirdAccessibilityElement.mm
MacWindow.mm
Menu.mm
+ SpellCheckerMac.mm
WebContentViewAccessibility.mm
WebContentViewMac.mm
)
target_link_libraries(ladybird PRIVATE Qt::GuiPrivate "-framework ApplicationServices" "-framework Cocoa" "-framework IOSurface" "-framework QuartzCore")
set_source_files_properties(
LadybirdAccessibilityElement.mm
+ SpellCheckerMac.mm
WebContentViewAccessibility.mm
PROPERTIES COMPILE_FLAGS "-fobjc-arc"
)
diff --git a/UI/Qt/SpellCheckerMac.h b/UI/Qt/SpellCheckerMac.h
new file mode 100644
index 00000000000..9942fc03688
--- /dev/null
+++ b/UI/Qt/SpellCheckerMac.h
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/NonnullOwnPtr.h>
+#include <LibWebView/SpellChecker.h>
+
+namespace Ladybird {
+
+NonnullOwnPtr<WebView::SpellChecker> create_platform_spell_checker();
+
+}
diff --git a/UI/Qt/SpellCheckerMac.mm b/UI/Qt/SpellCheckerMac.mm
new file mode 100644
index 00000000000..54c7e1dc541
--- /dev/null
+++ b/UI/Qt/SpellCheckerMac.mm
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/AtomicRefCounted.h>
+#include <AK/HashMap.h>
+#include <AK/RefPtr.h>
+#include <LibCore/EventLoop.h>
+#include <UI/Qt/SpellCheckerMac.h>
+
+#import <AppKit/AppKit.h>
+
+namespace Ladybird {
+
+// NSString and our strings hold the same UTF-16 code units, lone surrogates included, so offsets carry over unchanged.
+static NSString* ns_string_from_utf16(Utf16String const& string)
+{
+ auto view = string.utf16_view();
+ Vector<unichar> code_units;
+ code_units.ensure_capacity(view.length_in_code_units());
+ for (size_t index = 0; index < view.length_in_code_units(); ++index)
+ code_units.unchecked_append(view.code_unit_at(index));
+ return [[NSString alloc] initWithCharacters:code_units.data() length:code_units.size()];
+}
+
+static Utf16String utf16_from_ns_string(NSString* string)
+{
+ char const* utf8 = [string UTF8String];
+ return Utf16String::from_utf8(StringView { utf8, strlen(utf8) });
+}
+
+// Answers come back on an arbitrary queue. The callbacks that take them stay on the main thread, keyed by request, so
+// only this atomically counted holder crosses threads.
+struct PendingChecks : public AtomicRefCounted<PendingChecks> {
+ HashMap<u64, Function<void(Vector<Web::SpellingError>)>> callbacks;
+ u64 next_id { 0 };
+};
+
+class MacSpellChecker final : public WebView::SpellChecker {
+public:
+ virtual DocumentTag create_document_tag() override
+ {
+ return [NSSpellChecker uniqueSpellDocumentTag];
+ }
+
+ virtual void close_document_tag(DocumentTag tag) override
+ {
+ [[NSSpellChecker sharedSpellChecker] closeSpellDocumentWithTag:tag];
+ }
+
+ virtual void check(DocumentTag tag, Utf16String const& text, Function<void(Vector<Web::SpellingError>)> on_complete) override
+ {
+ auto id = m_pending_checks->next_id++;
+ m_pending_checks->callbacks.set(id, move(on_complete));
+ RefPtr<PendingChecks> pending_checks = m_pending_checks;
+ RefPtr<Core::WeakEventLoopReference> event_loop = Core::EventLoop::current_weak();
+ auto* string = ns_string_from_utf16(text);
+ auto handler = ^(NSInteger, NSArray<NSTextCheckingResult*>* results, NSOrthography*, NSInteger) {
+ Vector<Web::SpellingError> errors;
+ for (NSTextCheckingResult* result in results) {
+ if (result.resultType != NSTextCheckingTypeSpelling)
+ continue;
+ errors.append({ static_cast<u32>(result.range.location), static_cast<u32>(result.range.length) });
+ }
+ auto strong_event_loop = event_loop->take();
+ if (!strong_event_loop)
+ return;
+ strong_event_loop->deferred_invoke([pending_checks, id, errors = move(errors)]() mutable {
+ if (auto callback = pending_checks->callbacks.take(id); callback.has_value())
+ (*callback)(move(errors));
+ });
+ };
+ [[NSSpellChecker sharedSpellChecker] requestCheckingOfString:string
+ range:NSMakeRange(0, string.length)
+ types:NSTextCheckingTypeSpelling
+ options:nil
+ inSpellDocumentWithTag:tag
+ completionHandler:handler];
+ }
+
+ virtual Vector<Utf16String> guesses(DocumentTag tag, Utf16String const& word) override
+ {
+ auto* string = ns_string_from_utf16(word);
+ NSArray<NSString*>* guesses = [[NSSpellChecker sharedSpellChecker] guessesForWordRange:NSMakeRange(0, string.length)
+ inString:string
+ language:nil
+ inSpellDocumentWithTag:tag];
+ Vector<Utf16String> result;
+ for (NSString* guess in guesses)
+ result.append(utf16_from_ns_string(guess));
+ return result;
+ }
+
+ virtual void ignore(DocumentTag tag, Utf16String const& word) override
+ {
+ [[NSSpellChecker sharedSpellChecker] ignoreWord:ns_string_from_utf16(word) inSpellDocumentWithTag:tag];
+ }
+
+ // NB: The system dictionary learns the word for every app, so the tab's tag plays no part.
+ virtual void learn(DocumentTag, Utf16String const& word) override
+ {
+ [[NSSpellChecker sharedSpellChecker] learnWord:ns_string_from_utf16(word)];
+ }
+
+private:
+ NonnullRefPtr<PendingChecks> m_pending_checks { adopt_ref(*new PendingChecks) };
+};
+
+NonnullOwnPtr<WebView::SpellChecker> create_platform_spell_checker()
+{
+ return make<MacSpellChecker>();
+}
+
+}
diff --git a/UI/Qt/main.cpp b/UI/Qt/main.cpp
index b0b6b7a5fc0..c76cb31aec3 100644
--- a/UI/Qt/main.cpp
+++ b/UI/Qt/main.cpp
@@ -28,6 +28,7 @@
# include <QSurfaceFormat>
# include <UI/Qt/ApplicationIcon.h>
# include <UI/Qt/MacWindow.h>
+# include <UI/Qt/SpellCheckerMac.h>
#endif
namespace Ladybird {
@@ -75,6 +76,9 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
#endif
auto app = TRY(Ladybird::Application::create(arguments));
+#if defined(AK_OS_MACOS)
+ app->set_spell_checker(Ladybird::create_platform_spell_checker());
+#endif
if (app->should_exit_after_profile_coordination()) {
outln("Opening in existing process");
return 0;