Truly independent web browser
LibWebView+Hub+UI/Qt: Ask the UI's spell checker from the Hub
Andreas Klingc834cb0f9861
diff --git a/Libraries/LibWebView/SpellChecker.h b/Libraries/LibWebView/SpellChecker.hindex 782a659579f..0013637f184 100644--- a/Libraries/LibWebView/SpellChecker.h+++ b/Libraries/LibWebView/SpellChecker.h@@ -31,7 +31,9 @@ public:// Asynchronous; on_complete runs on the main thread.virtual void check(DocumentTag, Utf16String const& text, Function<void(Vector<Web::SpellingError>)> on_complete) = 0;- virtual Vector<Utf16String> guesses(DocumentTag, Utf16String const& word) = 0;+ // Asynchronous; on_complete runs on the main thread.+ virtual void guesses(DocumentTag, Utf16String const& word, Function<void(Vector<Utf16String>)> on_complete) = 0;+virtual void ignore(DocumentTag, Utf16String const& word) = 0;virtual void learn(DocumentTag, Utf16String const& word) = 0;diff --git a/Libraries/LibWebView/TestSpellChecker.cpp b/Libraries/LibWebView/TestSpellChecker.cppindex ceb79d5ca02..dcd229b8349 100644--- a/Libraries/LibWebView/TestSpellChecker.cpp+++ b/Libraries/LibWebView/TestSpellChecker.cpp@@ -126,9 +126,9 @@ void TestSpellChecker::suggest(DocumentTag, Utf16String const& text, u32 caret_o});}-Vector<Utf16String> TestSpellChecker::guesses(DocumentTag, Utf16String const& word)+void TestSpellChecker::guesses(DocumentTag, Utf16String const& word, Function<void(Vector<Utf16String>)> on_complete){- return guesses_for(word.utf16_view());+ on_complete(guesses_for(word.utf16_view()));}void TestSpellChecker::ignore(DocumentTag tag, Utf16String const& word)diff --git a/Libraries/LibWebView/TestSpellChecker.h b/Libraries/LibWebView/TestSpellChecker.hindex e3dc5ca43ed..008077bd0f8 100644--- a/Libraries/LibWebView/TestSpellChecker.h+++ b/Libraries/LibWebView/TestSpellChecker.h@@ -18,7 +18,7 @@ public:virtual DocumentTag create_document_tag() override;virtual void close_document_tag(DocumentTag) override;virtual void check(DocumentTag, Utf16String const& text, Function<void(Vector<Web::SpellingError>)> on_complete) override;- virtual Vector<Utf16String> guesses(DocumentTag, Utf16String const& word) override;+ virtual void guesses(DocumentTag, Utf16String const& word, Function<void(Vector<Utf16String>)> on_complete) override;virtual void ignore(DocumentTag, Utf16String const& word) override;virtual void learn(DocumentTag, Utf16String const& word) override;virtual bool learns_for_every_tag() const override { return false; }diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex 92d3f065b00..78865c90bd7 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -4901,15 +4901,25 @@ void ViewImplementation::reset_spell_checking()}}-void ViewImplementation::update_context_menu_misspelling(Optional<Utf16String> misspelled_word)+void ViewImplementation::update_context_menu_misspelling(Optional<Utf16String> misspelled_word, Function<void()> on_complete){auto* spell_checker = Application::the().spell_checker();m_context_menu_misspelled_word = spell_checker ? move(misspelled_word) : OptionalNone {};m_spelling_guesses.clear();- if (m_context_menu_misspelled_word.has_value())- m_spelling_guesses = spell_checker->guesses(spell_document_tag(), *m_context_menu_misspelled_word);- if (m_spelling_guesses.size() > MAX_SPELLING_GUESSES)- m_spelling_guesses.shrink(MAX_SPELLING_GUESSES);+ if (!m_context_menu_misspelled_word.has_value()) {+ on_complete();+ return;+ }++ auto request_id = m_context_menu_request_id;+ spell_checker->guesses(spell_document_tag(), *m_context_menu_misspelled_word, [weak_this = make_weak_ptr(), request_id, on_complete = move(on_complete)](Vector<Utf16String> guesses) {+ if (!weak_this || request_id != weak_this->m_context_menu_request_id)+ return;+ if (guesses.size() > MAX_SPELLING_GUESSES)+ guesses.shrink(MAX_SPELLING_GUESSES);+ weak_this->m_spelling_guesses = move(guesses);+ on_complete();+ });}void ViewImplementation::ignore_misspelling(Utf16String const& word)@@ -5025,41 +5035,45 @@ void ViewImplementation::did_request_page_context_menu(Badge<WebContentPage>, Werequest_text_fragment_url(text_fragment_generation_id);prepare_inspect_element(content_position);m_spelling_context_menu_page = requesting_page;- update_context_menu_misspelling(move(misspelled_word));auto request_id = ++m_context_menu_request_id;auto weak_this = make_weak_ptr();auto makes_link_to_highlight = text_fragment_generation_id.has_value();- request_context_menu_dictionary_lookup([weak_this, request_id, content_position, for_input_events_target, makes_link_to_highlight](auto const& lookup) {+ // NB: The menu lists the guesses for a misspelled word, so it waits for them.+ update_context_menu_misspelling(move(misspelled_word), [weak_this, request_id, content_position, for_input_events_target, makes_link_to_highlight] {if (!weak_this || request_id != weak_this->m_context_menu_request_id)return;-- auto open_menu = [weak_this, request_id, content_position, for_input_events_target, lookup, makes_link_to_highlight](Optional<String> selected_text) {+ weak_this->request_context_menu_dictionary_lookup([weak_this, request_id, content_position, for_input_events_target, makes_link_to_highlight](auto const& lookup) {if (!weak_this || request_id != weak_this->m_context_menu_request_id)return;- ContextMenuRequest request;- request.kind = ContextMenuKind::Page;- request.is_editable = for_input_events_target == Web::ContextMenuForInputEventsTarget::Yes;- request.selected_text = move(selected_text);- request.dictionary_lookup = lookup;- if (lookup.has_value() && weak_this->on_request_dictionary_lookup)- request.dictionary_lookup_position = weak_this->to_widget_position(lookup->baseline_origin.value_or(content_position));- else- request.dictionary_lookup.clear();- request.misspelled_word = weak_this->m_context_menu_misspelled_word;- request.spelling_guesses = weak_this->m_spelling_guesses;- request.makes_link_to_highlight = makes_link_to_highlight;- weak_this->send_context_menu_request(move(request), content_position);- };+ auto open_menu = [weak_this, request_id, content_position, for_input_events_target, lookup, makes_link_to_highlight](Optional<String> selected_text) {+ if (!weak_this || request_id != weak_this->m_context_menu_request_id)+ return;- if (lookup.has_value()) {- open_menu(lookup->text);- return;- }+ ContextMenuRequest request;+ request.kind = ContextMenuKind::Page;+ request.is_editable = for_input_events_target == Web::ContextMenuForInputEventsTarget::Yes;+ request.selected_text = move(selected_text);+ request.dictionary_lookup = lookup;+ if (lookup.has_value() && weak_this->on_request_dictionary_lookup)+ request.dictionary_lookup_position = weak_this->to_widget_position(lookup->baseline_origin.value_or(content_position));+ else+ request.dictionary_lookup.clear();+ request.misspelled_word = weak_this->m_context_menu_misspelled_word;+ request.spelling_guesses = weak_this->m_spelling_guesses;+ request.makes_link_to_highlight = makes_link_to_highlight;+ weak_this->send_context_menu_request(move(request), content_position);+ };++ if (lookup.has_value()) {+ open_menu(lookup->text);+ return;+ }- weak_this->selected_text_with_whitespace_collapsed()->when_resolved([open_menu = move(open_menu)](auto& selected_text) mutable {- open_menu(selected_text);+ weak_this->selected_text_with_whitespace_collapsed()->when_resolved([open_menu = move(open_menu)](auto& selected_text) mutable {+ open_menu(selected_text);+ });});});}diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex 1776fee0ea1..5da8a7c26d5 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -1127,7 +1127,7 @@ protected:RefPtr<WebContentPage> m_select_dropdown_page;static constexpr size_t MAX_SPELLING_GUESSES = 5;- void update_context_menu_misspelling(Optional<Utf16String> misspelled_word);+ void update_context_menu_misspelling(Optional<Utf16String> misspelled_word, Function<void()> on_complete);Optional<Utf16String> m_context_menu_misspelled_word;Vector<Utf16String> m_spelling_guesses;diff --git a/Services/Hub/Application.h b/Services/Hub/Application.hindex ccf2fe59178..90968bf789a 100644--- a/Services/Hub/Application.h+++ b/Services/Hub/Application.h@@ -60,6 +60,7 @@ public:void ui_did_start();virtual void notify_webdriver_window_created(String const& handle) override;void set_ui_info(Hub::UIInfo);+ Hub::UIInfo const* ui_info() const;StringView ui_mach_server_name() const { return m_ui_mach_server_name; }bool ui_has_location_source() const;bool ui_has_translator() const;@@ -73,8 +74,6 @@ public:private:explicit Application(ApplicationHost*);- Hub::UIInfo const* ui_info() const;-// ^WebView::Applicationvirtual Core::EventLoop& create_platform_event_loop() override;virtual void create_platform_arguments(Core::ArgsParser&) override;diff --git a/Services/Hub/CMakeLists.txt b/Services/Hub/CMakeLists.txtindex 8c4584e2d81..8cffb918bee 100644--- a/Services/Hub/CMakeLists.txt+++ b/Services/Hub/CMakeLists.txt@@ -27,12 +27,6 @@ target_include_directories(hubservice PRIVATE ${LADYBIRD_SOURCE_DIR}/Services/)target_link_libraries(hubservice PRIVATE LibCore LibGfx LibHTTP LibIPC LibRequests LibURL LibWebView hubipc)-if (APPLE)- target_sources(hubservice PRIVATE SpellCheckerMac.mm)- set_source_files_properties(SpellCheckerMac.mm PROPERTIES COMPILE_FLAGS "-fobjc-arc")- target_link_libraries(hubservice PRIVATE "-framework AppKit")-endif()-add_executable(Hub main.cpp)target_include_directories(Hub PRIVATE ${LADYBIRD_SOURCE_DIR}/Services/)target_link_libraries(Hub PRIVATE hubservice hubipc LibCore LibHTTP LibIPC LibMain LibRequests LibURL LibWebView)diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cppindex 150cc2680ee..32539cc624a 100644--- a/Services/Hub/ConnectionFromUI.cpp+++ b/Services/Hub/ConnectionFromUI.cpp@@ -306,6 +306,24 @@ void ConnectionFromUI::translator_translation_finished(u64 request_id, Optional<translator->did_translate(request_id, move(runs));}+void ConnectionFromUI::spell_checker_checked(u64 request_id, Vector<Web::SpellingError> errors)+{+ if (auto* spell_checker = SpellCheckerInUI::the())+ spell_checker->did_check(request_id, move(errors));+}++void ConnectionFromUI::spell_checker_guessed(u64 request_id, Vector<Utf16String> guesses)+{+ if (auto* spell_checker = SpellCheckerInUI::the())+ spell_checker->did_guess(request_id, move(guesses));+}++void ConnectionFromUI::spell_checker_suggested(u64 request_id, Optional<Web::WritingSuggestion> suggestion)+{+ if (auto* spell_checker = SpellCheckerInUI::the())+ spell_checker->did_suggest(request_id, move(suggestion));+}+void ConnectionFromUI::system_time_zone_changed(){WebView::WebContentClient::for_each_client([](WebView::WebContentClient& client) {diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.hindex 2d76b49eda9..ad1ffd64cd5 100644--- a/Services/Hub/ConnectionFromUI.h+++ b/Services/Hub/ConnectionFromUI.h@@ -118,6 +118,9 @@ private:virtual void translator_language_detected(u64 request_id, Optional<String> language) override;virtual void translator_target_languages_ready(u64 request_id, Vector<WebView::Translator::TargetLanguage> target_languages) override;virtual void translator_translation_finished(u64 request_id, Optional<Vector<Web::TranslationRun>> runs) override;+ virtual void spell_checker_checked(u64 request_id, Vector<Web::SpellingError> errors) override;+ virtual void spell_checker_guessed(u64 request_id, Vector<Utf16String> guesses) override;+ virtual void spell_checker_suggested(u64 request_id, Optional<Web::WritingSuggestion> suggestion) override;virtual void external_url_handler_resolved(u64 request_id, Optional<u64> handler_id, String application_name, bool is_ladybird) override;virtual void external_url_launched(u64 request_id, bool launched) override;virtual void cancel_download(u64 id) override;diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipcindex 404ac4e2bb2..843c4d82900 100644--- a/Services/Hub/HubClient.ipc+++ b/Services/Hub/HubClient.ipc@@ -15,7 +15,9 @@#include <LibURL/URL.h>#include <LibWebCommon/CSS/PreferredColorScheme.h>#include <LibWebCommon/Loader/NavigatorCompatibilityMode.h>+#include <LibWebCommon/Page/SpellingError.h>#include <LibWebCommon/Page/Translation.h>+#include <LibWebCommon/Page/WritingSuggestion.h>#include <LibWebCommon/WebView/DictionaryLookup.h>#include <LibWebCommon/HTML/FileFilter.h>#include <LibWebCommon/HTML/SelectItem.h>@@ -113,6 +115,9 @@ struct Hub::UIInfo {bool supports_picture_in_picture;bool has_location_source;bool has_translator;+ bool has_spell_checker;+ bool spell_checker_learns_for_every_tag;+ bool spell_checker_can_suggest;Optional<Core::GeolocationAuthorization> location_authorization;bool supports_selection_clipboard;bool supports_system_menu_bar;@@ -240,6 +245,12 @@ endpoint HubClienttranslator_close_session(u64 session_id) =|translator_translate(u64 request_id, u64 session_id, Web::TranslationUnit unit) =|translator_open_language_download_settings() =|+ spell_checker_close_document(i64 document_tag) =|+ spell_checker_check(u64 request_id, i64 document_tag, Utf16String text) =|+ spell_checker_guesses(u64 request_id, i64 document_tag, Utf16String word) =|+ spell_checker_ignore(i64 document_tag, Utf16String word) =|+ spell_checker_learn(i64 document_tag, Utf16String word) =|+ spell_checker_suggest(u64 request_id, i64 document_tag, Utf16String text, u32 caret_offset) =|app_focus_view_from_notification(u64 view_id) =|app_open_url_in_new_tab_from_notification(URL::URL url, Optional<u64> view_id, WebView::IsPrivate is_private) =|external_url_resolve_handler(u64 request_id, URL::URL url) =|diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipcindex 9458643f11a..54e1a236dda 100644--- a/Services/Hub/HubServer.ipc+++ b/Services/Hub/HubServer.ipc@@ -18,6 +18,8 @@#include <LibWebCommon/HTML/ActivateTab.h>#include <LibWebCommon/Loader/NavigatorCompatibilityMode.h>#include <LibWebCommon/HTML/VisibilityState.h>+#include <LibWebCommon/Page/SpellingError.h>+#include <LibWebCommon/Page/WritingSuggestion.h>#include <LibWebCommon/Clipboard/SystemClipboard.h>#include <LibWebView/HistoryStore.h>#include <LibWebView/ProcessSnapshot.h>@@ -186,6 +188,9 @@ endpoint HubServertranslator_language_detected(u64 request_id, Optional<String> language) =|translator_target_languages_ready(u64 request_id, Vector<WebView::Translator::TargetLanguage> target_languages) =|translator_translation_finished(u64 request_id, Optional<Vector<Web::TranslationRun>> runs) =|+ spell_checker_checked(u64 request_id, Vector<Web::SpellingError> errors) =|+ spell_checker_guessed(u64 request_id, Vector<Utf16String> guesses) =|+ spell_checker_suggested(u64 request_id, Optional<Web::WritingSuggestion> suggestion) =|external_url_handler_resolved(u64 request_id, Optional<u64> handler_id, String application_name, bool is_ladybird) =|external_url_launched(u64 request_id, bool launched) =|cancel_download(u64 id) =|diff --git a/Services/Hub/Proxies.cpp b/Services/Hub/Proxies.cppindex ba4b8950a2b..4ea4dbd338a 100644--- a/Services/Hub/Proxies.cpp+++ b/Services/Hub/Proxies.cpp@@ -259,6 +259,91 @@ void TranslatorInUI::open_language_download_settings()ui->async_translator_open_language_download_settings();}+static SpellCheckerInUI* s_spell_checker = nullptr;++SpellCheckerInUI::SpellCheckerInUI(bool learns_for_every_tag, bool can_suggest)+ : m_learns_for_every_tag(learns_for_every_tag)+ , m_can_suggest(can_suggest)+{+ s_spell_checker = this;+}++SpellCheckerInUI::~SpellCheckerInUI()+{+ s_spell_checker = nullptr;+}++SpellCheckerInUI* SpellCheckerInUI::the()+{+ return s_spell_checker;+}++// NB: The Hub never waits for the UI, so it picks the document's tag itself. The UI makes its own tag on first use.+WebView::SpellChecker::DocumentTag SpellCheckerInUI::create_document_tag()+{+ return m_next_document_tag++;+}++void SpellCheckerInUI::close_document_tag(DocumentTag tag)+{+ if (auto* ui = ConnectionFromUI::the())+ ui->async_spell_checker_close_document(tag);+}++void SpellCheckerInUI::check(DocumentTag tag, Utf16String const& text, Function<void(Vector<Web::SpellingError>)> on_complete)+{+ auto request_id = m_next_request_id++;+ m_pending_checks.set(request_id, move(on_complete));+ if (auto* ui = ConnectionFromUI::the())+ ui->async_spell_checker_check(request_id, tag, text);+}++void SpellCheckerInUI::did_check(u64 request_id, Vector<Web::SpellingError> errors)+{+ if (auto on_complete = m_pending_checks.take(request_id); on_complete.has_value())+ (*on_complete)(move(errors));+}++void SpellCheckerInUI::guesses(DocumentTag tag, Utf16String const& word, Function<void(Vector<Utf16String>)> on_complete)+{+ auto request_id = m_next_request_id++;+ m_pending_guesses.set(request_id, move(on_complete));+ if (auto* ui = ConnectionFromUI::the())+ ui->async_spell_checker_guesses(request_id, tag, word);+}++void SpellCheckerInUI::did_guess(u64 request_id, Vector<Utf16String> guesses)+{+ if (auto on_complete = m_pending_guesses.take(request_id); on_complete.has_value())+ (*on_complete)(move(guesses));+}++void SpellCheckerInUI::ignore(DocumentTag tag, Utf16String const& word)+{+ if (auto* ui = ConnectionFromUI::the())+ ui->async_spell_checker_ignore(tag, word);+}++void SpellCheckerInUI::learn(DocumentTag tag, Utf16String const& word)+{+ if (auto* ui = ConnectionFromUI::the())+ ui->async_spell_checker_learn(tag, word);+}++void SpellCheckerInUI::suggest(DocumentTag tag, Utf16String const& text, u32 caret_offset, Function<void(Optional<Web::WritingSuggestion>)> on_complete)+{+ auto request_id = m_next_request_id++;+ m_pending_suggestions.set(request_id, move(on_complete));+ if (auto* ui = ConnectionFromUI::the())+ ui->async_spell_checker_suggest(request_id, tag, text, caret_offset);+}++void SpellCheckerInUI::did_suggest(u64 request_id, Optional<Web::WritingSuggestion> suggestion)+{+ if (auto on_complete = m_pending_suggestions.take(request_id); on_complete.has_value())+ (*on_complete)(move(suggestion));+}+ExternalURLHandlerInUI::ExternalURLHandlerInUI(u64 handler_id, String application_name, bool is_ladybird): ExternalURLHandler(move(application_name), is_ladybird), m_handler_id(handler_id)diff --git a/Services/Hub/Proxies.h b/Services/Hub/Proxies.hindex ea4e3f65e5d..d1e9564a053 100644--- a/Services/Hub/Proxies.h+++ b/Services/Hub/Proxies.h@@ -14,6 +14,7 @@#include <LibWebView/ExternalURLHandler.h>#include <LibWebView/NotificationBackend.h>#include <LibWebView/PictureInPictureWindow.h>+#include <LibWebView/SpellChecker.h>#include <LibWebView/Translator.h>namespace Hub {@@ -127,6 +128,37 @@ private:SessionId m_next_session_id { 1 };};+class SpellCheckerInUI final : public WebView::SpellChecker {+public:+ SpellCheckerInUI(bool learns_for_every_tag, bool can_suggest);+ virtual ~SpellCheckerInUI() override;++ static SpellCheckerInUI* the();++ void did_check(u64 request_id, Vector<Web::SpellingError>);+ void did_guess(u64 request_id, Vector<Utf16String>);+ void did_suggest(u64 request_id, Optional<Web::WritingSuggestion>);++private:+ virtual DocumentTag create_document_tag() override;+ virtual void close_document_tag(DocumentTag) override;+ virtual void check(DocumentTag, Utf16String const& text, Function<void(Vector<Web::SpellingError>)> on_complete) override;+ virtual void guesses(DocumentTag, Utf16String const& word, Function<void(Vector<Utf16String>)> on_complete) override;+ virtual void ignore(DocumentTag, Utf16String const& word) override;+ virtual void learn(DocumentTag, Utf16String const& word) override;+ virtual bool learns_for_every_tag() const override { return m_learns_for_every_tag; }+ virtual bool can_suggest() const override { return m_can_suggest; }+ virtual void suggest(DocumentTag, Utf16String const& text, u32 caret_offset, Function<void(Optional<Web::WritingSuggestion>)> on_complete) override;++ HashMap<u64, Function<void(Vector<Web::SpellingError>)>> m_pending_checks;+ HashMap<u64, Function<void(Vector<Utf16String>)>> m_pending_guesses;+ HashMap<u64, Function<void(Optional<Web::WritingSuggestion>)>> m_pending_suggestions;+ u64 m_next_request_id { 1 };+ DocumentTag m_next_document_tag { 1 };+ bool m_learns_for_every_tag { true };+ bool m_can_suggest { false };+};+class ExternalURLHandlerInUI final : public WebView::ExternalURLHandler {public:ExternalURLHandlerInUI(u64 handler_id, String application_name, bool is_ladybird);diff --git a/Services/Hub/main.cpp b/Services/Hub/main.cppindex e090ceebc8c..afb60928e6c 100644--- a/Services/Hub/main.cpp+++ b/Services/Hub/main.cpp@@ -11,10 +11,6 @@#include <LibIPC/StartupTransport.h>#include <LibMain/Main.h>-#if defined(AK_OS_MACOS)-# include <Hub/SpellCheckerMac.h>-#endif-ErrorOr<int> ladybird_main(Main::Arguments arguments){AK::set_rich_debug_enabled(true);@@ -25,10 +21,6 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)return 0;}-#if defined(AK_OS_MACOS)- application->set_spell_checker(Hub::create_platform_spell_checker());-#endif-// Headless mode runs on its own, with no UI to serve.RefPtr<Hub::ConnectionFromUI> connection;if (!Hub::Application::browser_options().headless_mode.has_value()) {@@ -38,6 +30,8 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)// NB: Translating and finding the location need a run loop on macOS, and the UI is the process that has one.if (application->ui_has_translator())application->set_translator(make<Hub::TranslatorInUI>());+ if (auto const* info = application->ui_info(); info && info->has_spell_checker)+ application->set_spell_checker(make<Hub::SpellCheckerInUI>(info->spell_checker_learns_for_every_tag, info->spell_checker_can_suggest));Core::GeolocationProvider::set_provider_functions([]() -> ErrorOr<NonnullOwnPtr<Core::GeolocationProvider>> { return make<Hub::GeolocationProviderInUI>(); },[] { return Hub::Application::the().ui_has_location_source(); });diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cppindex 5c9e8758cc7..01ce8ce274a 100644--- a/UI/Qt/Application.cpp+++ b/UI/Qt/Application.cpp@@ -72,6 +72,7 @@# include <UI/Qt/ApplicationIcon.h># include <UI/Qt/MacWindow.h># include <UI/Qt/ScrollbarPreferencesMac.h>+# include <UI/Qt/SpellCheckerMac.h>#endif#if defined(LADYBIRD_HAS_TRANSLATOR)@@ -701,6 +702,9 @@ Hub::UIInfo Application::current_ui_info() constinfo.location_authorization = provider.value()->authorization();}info.has_translator = has_translator();+ info.has_spell_checker = m_spell_checker != nullptr;+ info.spell_checker_learns_for_every_tag = m_spell_checker && m_spell_checker->learns_for_every_tag();+ info.spell_checker_can_suggest = m_spell_checker && m_spell_checker->can_suggest();info.supports_selection_clipboard = supports_clipboard_type(ClipboardType::Selection);info.supports_system_menu_bar = supports_system_menu_bar();info.supports_vertical_tabs = supports_vertical_tabs();@@ -722,6 +726,9 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)# if defined(LADYBIRD_HAS_TRANSLATOR)m_translator = create_platform_translator();# endif+# if defined(AK_OS_MACOS)+ m_spell_checker = create_platform_spell_checker();+# endifIPC::MessageBuffer ui_info_buffer;IPC::Encoder encoder { ui_info_buffer };diff --git a/UI/Qt/Application.h b/UI/Qt/Application.hindex e6dd7839f4a..2d1eb70151e 100644--- a/UI/Qt/Application.h+++ b/UI/Qt/Application.h@@ -311,8 +311,10 @@ private:RefPtr<Hub::ConnectionFromUI> m_connection_from_ui;#endifWebView::Translator* translator() { return m_translator.ptr(); }+ WebView::SpellChecker* spell_checker() { return m_spell_checker.ptr(); }OwnPtr<WebView::Translator> m_translator;+ OwnPtr<WebView::SpellChecker> m_spell_checker;OwnPtr<WebView::Settings> m_settings;OwnPtr<BookmarkStore> m_bookmark_store;OwnPtr<SessionStore> m_session_store;diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txtindex 489a0c8075a..05cadf291e8 100644--- a/UI/Qt/CMakeLists.txt+++ b/UI/Qt/CMakeLists.txt@@ -148,6 +148,7 @@ if (APPLE)NativeTextEditingBindings.mmMenu.mmScrollbarPreferencesMac.mm+ SpellCheckerMac.mmSystemAppearanceMac.mmTranslateIconMac.mmWebContentViewAccessibility.mm@@ -156,6 +157,7 @@ if (APPLE)target_link_libraries(ladybird_impl PRIVATE Qt::GuiPrivate "-framework ApplicationServices" "-framework Cocoa" "-framework IOSurface" "-framework QuartzCore")set_source_files_properties(LadybirdAccessibilityElement.mm+ SpellCheckerMac.mmTranslateIconMac.mmTranslatorMac.mmWebContentViewAccessibility.mmdiff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cppindex 0c47a2c88b0..60eb8dc5ea3 100644--- a/UI/Qt/HubConnection.cpp+++ b/UI/Qt/HubConnection.cpp@@ -529,6 +529,67 @@ void HubConnection::translator_open_language_download_settings()translator->open_language_download_settings();}+// The Hub picks its own tag for each document, and the platform gets a tag of its own the first time one is used.+WebView::SpellChecker::DocumentTag HubConnection::spell_document_tag(WebView::SpellChecker& spell_checker, i64 document_tag)+{+ return m_spell_document_tags.ensure(document_tag, [&] { return spell_checker.create_document_tag(); });+}++void HubConnection::spell_checker_close_document(i64 document_tag)+{+ auto native_tag = m_spell_document_tags.take(document_tag);+ if (auto* spell_checker = Application::the().spell_checker(); spell_checker && native_tag.has_value())+ spell_checker->close_document_tag(*native_tag);+}++void HubConnection::spell_checker_check(u64 request_id, i64 document_tag, Utf16String text)+{+ auto* spell_checker = Application::the().spell_checker();+ if (!spell_checker) {+ async_spell_checker_checked(request_id, {});+ return;+ }+ spell_checker->check(spell_document_tag(*spell_checker, document_tag), text, [this, request_id](Vector<Web::SpellingError> errors) {+ async_spell_checker_checked(request_id, errors);+ });+}++void HubConnection::spell_checker_guesses(u64 request_id, i64 document_tag, Utf16String word)+{+ auto* spell_checker = Application::the().spell_checker();+ if (!spell_checker) {+ async_spell_checker_guessed(request_id, {});+ return;+ }+ spell_checker->guesses(spell_document_tag(*spell_checker, document_tag), word, [this, request_id](Vector<Utf16String> guesses) {+ async_spell_checker_guessed(request_id, guesses);+ });+}++void HubConnection::spell_checker_ignore(i64 document_tag, Utf16String word)+{+ if (auto* spell_checker = Application::the().spell_checker())+ spell_checker->ignore(spell_document_tag(*spell_checker, document_tag), word);+}++void HubConnection::spell_checker_learn(i64 document_tag, Utf16String word)+{+ if (auto* spell_checker = Application::the().spell_checker())+ spell_checker->learn(spell_document_tag(*spell_checker, document_tag), word);+}++void HubConnection::spell_checker_suggest(u64 request_id, i64 document_tag, Utf16String text, u32 caret_offset)+{+ auto* spell_checker = Application::the().spell_checker();+ if (!spell_checker) {+ async_spell_checker_suggested(request_id, {});+ return;+ }+ spell_checker->suggest(spell_document_tag(*spell_checker, document_tag), text, caret_offset, [this, request_id](Optional<Web::WritingSuggestion> suggestion) {+ async_spell_checker_suggested(request_id, suggestion);+ });+}+void HubConnection::app_focus_view_from_notification(u64 view_id){Application::the().focus_view_from_notification(view_id);diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.hindex 306a571d4b9..fbaa955a31a 100644--- a/UI/Qt/HubConnection.h+++ b/UI/Qt/HubConnection.h@@ -11,6 +11,7 @@#include <Hub/HubServerEndpoint.h>#include <LibCore/GeolocationProvider.h>#include <LibIPC/ConnectionToServer.h>+#include <LibWebView/SpellChecker.h>namespace Ladybird {@@ -54,6 +55,9 @@ private:HashMap<u64, Core::GeolocationProvider::WatchId> m_geolocation_watches;HashMap<u64, WebView::Translator::SessionId> m_translator_sessions;+ WebView::SpellChecker::DocumentTag spell_document_tag(WebView::SpellChecker&, i64 document_tag);+ HashMap<i64, WebView::SpellChecker::DocumentTag> m_spell_document_tags;+// The Hub owns the settings. The settings here are its last word, plus the changes it has yet to confirm.struct SettingsChange {u64 id { 0 };@@ -91,6 +95,12 @@ private:virtual void translator_close_session(u64 session_id) override;virtual void translator_translate(u64 request_id, u64 session_id, Web::TranslationUnit unit) override;virtual void translator_open_language_download_settings() override;+ virtual void spell_checker_close_document(i64 document_tag) override;+ virtual void spell_checker_check(u64 request_id, i64 document_tag, Utf16String text) override;+ virtual void spell_checker_guesses(u64 request_id, i64 document_tag, Utf16String word) override;+ virtual void spell_checker_ignore(i64 document_tag, Utf16String word) override;+ virtual void spell_checker_learn(i64 document_tag, Utf16String word) override;+ virtual void spell_checker_suggest(u64 request_id, i64 document_tag, Utf16String text, u32 caret_offset) override;virtual void app_focus_view_from_notification(u64 view_id) override;virtual void app_open_url_in_new_tab_from_notification(URL::URL url, Optional<u64> view_id, WebView::IsPrivate is_private) override;virtual void external_url_resolve_handler(u64 request_id, URL::URL url) override;diff --git a/Services/Hub/SpellCheckerMac.h b/UI/Qt/SpellCheckerMac.hsimilarity index 92%rename from Services/Hub/SpellCheckerMac.hrename to UI/Qt/SpellCheckerMac.hindex 7efee5cfcf8..9942fc03688 100644--- a/Services/Hub/SpellCheckerMac.h+++ b/UI/Qt/SpellCheckerMac.h@@ -9,7 +9,7 @@#include <AK/NonnullOwnPtr.h>#include <LibWebView/SpellChecker.h>-namespace Hub {+namespace Ladybird {NonnullOwnPtr<WebView::SpellChecker> create_platform_spell_checker();diff --git a/Services/Hub/SpellCheckerMac.mm b/UI/Qt/SpellCheckerMac.mmsimilarity index 97%rename from Services/Hub/SpellCheckerMac.mmrename to UI/Qt/SpellCheckerMac.mmindex cbf773c3872..132b8d05316 100644--- a/Services/Hub/SpellCheckerMac.mm+++ b/UI/Qt/SpellCheckerMac.mm@@ -7,9 +7,9 @@#include <AK/AtomicRefCounted.h>#include <AK/HashMap.h>#include <AK/RefPtr.h>-#include <Hub/SpellCheckerMac.h>#include <LibCore/EventLoop.h>#include <LibWebView/StringConversionsMacOS.h>+#include <UI/Qt/SpellCheckerMac.h>#import <AppKit/AppKit.h>@@ -20,7 +20,7 @@- (NSTextCheckingResult*)completionCandidateFromCandidates:(NSArray<NSTextCheckingResult*>*)candidates;@end-namespace Hub {+namespace Ladybird {using namespace WebView;@@ -81,7 +81,7 @@ public:completionHandler:handler];}- virtual Vector<Utf16String> guesses(DocumentTag tag, Utf16String const& word) override+ virtual void guesses(DocumentTag tag, Utf16String const& word, Function<void(Vector<Utf16String>)> on_complete) override{auto* string = utf16_string_to_ns_string(word);NSArray<NSString*>* guesses = [[NSSpellChecker sharedSpellChecker] guessesForWordRange:NSMakeRange(0, string.length)@@ -91,7 +91,7 @@ public:Vector<Utf16String> result;for (NSString* guess in guesses)result.append(ns_string_to_utf16_string(guess));- return result;+ on_complete(move(result));}virtual void ignore(DocumentTag tag, Utf16String const& word) override