├── .gitignore ├── .gitmodules ├── Cargo.lock ├── Cargo.lock.license ├── Cargo.toml ├── LICENSES ├── Apache-2.0.txt └── MPL-2.0.txt ├── README.md ├── build.rs ├── cpp ├── helpers.cpp └── helpers.h ├── images ├── arrow-back.png ├── arrow-back.png.license ├── arrow-forward.png ├── arrow-forward.png.license ├── search.png └── search.png.license ├── qml ├── ServoToolbar.qml └── main.qml ├── shell.nix └── src ├── browser.rs ├── embedder.rs ├── events_loop.rs ├── main.rs ├── renderer.rs ├── servothread.rs ├── webview.rs └── windowheadless.rs /.gitignore: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | # SPDX-FileContributor: Andrew Hayzen 3 | # 4 | # SPDX-License-Identifier: MPL-2.0 5 | 6 | /target 7 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KDABLabs/cxx-qt-servo-webview/05399f6520bd44883907f74855cd80e9b86067f9/.gitmodules -------------------------------------------------------------------------------- /Cargo.lock.license: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | # SPDX-FileContributor: Andrew Hayzen 3 | # 4 | # SPDX-License-Identifier: MPL-2.0 5 | 6 | [package] 7 | name = "cxxqtservo" 8 | version = "0.1.0" 9 | edition = "2021" 10 | license = "MPL-2.0" 11 | 12 | [dependencies] 13 | cxx = "1.0.95" 14 | cxx-qt = "0.6" 15 | cxx-qt-lib = { version = "0.6", features = ["url"] } 16 | 17 | euclid = "0.22" 18 | gleam = "0.15" 19 | glow = "0.12.2" 20 | raw-window-handle = "0.5" 21 | url = "2.3" 22 | 23 | # Note that we cannot run cargo update on the lock file 24 | # instead copy the lock file from the servo repository 25 | # then cargo run will add the missing dependencies to the lock file 26 | libservo = { git = "https://github.com/servo/servo.git", rev = "3a3e76a935f92ce84c24496cfc46207cd46088f6" } 27 | servo-media = { git = "https://github.com/servo/media" } 28 | surfman = { version = "0.9", features = ["chains", "sm-angle", "sm-angle-default"] } 29 | 30 | [build-dependencies] 31 | cxx-qt-build = { version = "0.6", features = [ "link_qt_object_files" ] } 32 | cxx-qt-lib-headers = { version = "0.6" } 33 | 34 | [patch.crates-io] 35 | # Use a need to follow `main` of CXX-Qt version 36 | cxx-qt = { git = "https://github.com/kdab/cxx-qt.git", branch = "main" } 37 | cxx-qt-build = { git = "https://github.com/kdab/cxx-qt.git", branch = "main" } 38 | cxx-qt-gen = { git = "https://github.com/kdab/cxx-qt.git", branch = "main" } 39 | cxx-qt-macro = { git = "https://github.com/kdab/cxx-qt.git", branch = "main" } 40 | cxx-qt-lib = { git = "https://github.com/kdab/cxx-qt.git", branch = "main" } 41 | cxx-qt-lib-headers = { git = "https://github.com/kdab/cxx-qt.git", branch = "main" } 42 | qt-build-utils = { git = "https://github.com/kdab/cxx-qt.git", branch = "main" } 43 | 44 | # We need a patched surfman to avoid OpenGL errors 45 | surfman = { git = "https://github.com/ahayzen-kdab/surfman.git", branch = "patched-opengl-assert-0-9-1" } 46 | -------------------------------------------------------------------------------- /LICENSES/Apache-2.0.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 10 | 11 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 12 | 13 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 14 | 15 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 16 | 17 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 18 | 19 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 20 | 21 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 22 | 23 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 24 | 25 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 26 | 27 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 28 | 29 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 30 | 31 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 32 | 33 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 34 | 35 | (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and 36 | 37 | (b) You must cause any modified files to carry prominent notices stating that You changed the files; and 38 | 39 | (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 40 | 41 | (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 42 | 43 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 44 | 45 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 46 | 47 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 48 | 49 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 50 | 51 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 52 | 53 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 54 | 55 | END OF TERMS AND CONDITIONS 56 | 57 | APPENDIX: How to apply the Apache License to your work. 58 | 59 | To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. 60 | 61 | Copyright [yyyy] [name of copyright owner] 62 | 63 | Licensed under the Apache License, Version 2.0 (the "License"); 64 | you may not use this file except in compliance with the License. 65 | You may obtain a copy of the License at 66 | 67 | http://www.apache.org/licenses/LICENSE-2.0 68 | 69 | Unless required by applicable law or agreed to in writing, software 70 | distributed under the License is distributed on an "AS IS" BASIS, 71 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 72 | See the License for the specific language governing permissions and 73 | limitations under the License. 74 | -------------------------------------------------------------------------------- /LICENSES/MPL-2.0.txt: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at https://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | # Servo WebView for Qt using CXX-Qt 9 | 10 | KDAB has built a demo of using [CXX-Qt](https://github.com/KDAB/cxx-qt/) to expose a [Servo](https://servo.org/) in Rust as a component to Qt. 11 | 12 | ## Setup 13 | 14 | * Ensure that you have Qt installed and `qmake` in your `PATH`. 15 | * Ensure that you have the dependencies of `./mach boostrap` from the [https://github.com/servo/servo/](https://github.com/servo/servo/) repository. 16 | * Alternatively use the `shell.nix` by [installing Nix](https://nixos.org/download/) and then running `nix-shell`, it will take care of all dependencies automatically 17 | * Install the nightly compiler from February 18 | 19 | ```console 20 | $ rustup install nightly-2024-02-01 21 | $ rustup default nightly-2024-02-01 22 | ``` 23 | 24 | * Then run in release mode 25 | 26 | ```console 27 | $ cargo run --release 28 | ``` 29 | 30 | Note that this demo currently supports Wayland only, not X11. 31 | 32 | ## Debugging 33 | 34 | To make sure, that Qt picks the correct OpenGL driver, use the `QSG_INFO=1` variable. For hardware acceleration to work, the driver name should **not** contain `llvmpipe`. 35 | Note that on embedded hardware it might be necessary to force servo to use OpenGL ES. 36 | 37 | ### Nix 38 | 39 | If you using Nix and if not on NixOS, make sure to run the final executable with the `nixGLMesa` wrapper (or if not on Mesa, with the correct wrapper for your driver). This will make sure that the OpenGL drivers are passed through from the host system with [nixGL](https://github.com/nix-community/nixGL). 40 | 41 | ## Licensing 42 | 43 | This demo is Copyright (C) Klarälvdalens Datakonsult AB, and is available under 44 | the terms of the [MPL-2.0](https://github.com/KDABLabs/cxx-qt-servo-webview/blob/main/LICENSES/MPL-2.0.txt) license. 45 | 46 | Contact KDAB at to inquire about additional features or 47 | services related to this project. 48 | 49 | # About KDAB 50 | 51 | The KDAB Group is the global No.1 software consultancy for Qt, C++ and 52 | OpenGL applications across desktop, embedded and mobile platforms. 53 | 54 | The KDAB Group provides consulting and mentoring for developing Qt applications 55 | from scratch and in porting from all popular and legacy frameworks to Qt. 56 | We continue to help develop parts of Qt and are one of the major contributors 57 | to the Qt Project. We can give advanced or standard trainings anywhere 58 | around the globe on Qt as well as C++, OpenGL, 3D and more. 59 | 60 | Please visit to meet the people who write code like this. 61 | -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | use cxx_qt_build::{CxxQtBuilder, QmlModule}; 7 | 8 | fn main() { 9 | CxxQtBuilder::new() 10 | .qt_module("Network") 11 | .qt_module("Quick") 12 | .qt_module("OpenGL") 13 | .qml_module(QmlModule { 14 | uri: "com.kdab.servo", 15 | rust_files: &["src/webview.rs"], 16 | qml_files: &["qml/main.qml", "qml/ServoToolbar.qml"], 17 | qrc_files: &[ 18 | "images/arrow-back.png", 19 | "images/arrow-forward.png", 20 | "images/search.png", 21 | ], 22 | ..Default::default() 23 | }) 24 | .cc_builder(|cc| { 25 | cc.include("cpp"); 26 | cc.file("cpp/helpers.cpp"); 27 | println!("cargo:rerun-if-changed=cpp/helpers.cpp"); 28 | }) 29 | .file("src/renderer.rs") 30 | .qobject_header("cpp/helpers.h") 31 | .with_opts(cxx_qt_lib_headers::build_opts()) 32 | .build(); 33 | } 34 | -------------------------------------------------------------------------------- /cpp/helpers.cpp: -------------------------------------------------------------------------------- 1 | // clang-format off 2 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 3 | // clang-format on 4 | // SPDX-FileContributor: Andrew Hayzen 5 | // 6 | // SPDX-License-Identifier: MPL-2.0 7 | 8 | #include "helpers.h" 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | void 15 | blitFramebuffer(QOpenGLFramebufferObject* target, ::std::unique_ptr source) 16 | { 17 | Q_ASSERT(target != nullptr); 18 | Q_ASSERT(source != nullptr); 19 | QOpenGLFramebufferObject::blitFramebuffer(target, source.get()); 20 | } 21 | 22 | ::std::unique_ptr 23 | fboFromTexture(unsigned int texture_id, unsigned int texture_target, QSize size) 24 | { 25 | QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); 26 | 27 | auto fbo = ::std::make_unique(size); 28 | f->glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture_target, texture_id, 0); 29 | Q_ASSERT(fbo->isValid()); 30 | return fbo; 31 | } 32 | 33 | ::rust::isize 34 | qTouchEventPointCount(QTouchEvent const& event) 35 | { 36 | return static_cast<::rust::isize>(event.pointCount()); 37 | } 38 | 39 | QEventPoint const& 40 | qTouchEventPoint(QTouchEvent& event, ::rust::isize index) 41 | { 42 | return (event.point(static_cast(index))); 43 | } 44 | -------------------------------------------------------------------------------- /cpp/helpers.h: -------------------------------------------------------------------------------- 1 | // clang-format off 2 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 3 | // clang-format on 4 | // SPDX-FileContributor: Andrew Hayzen 5 | // 6 | // SPDX-License-Identifier: MPL-2.0 7 | 8 | #pragma once 9 | 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "rust/cxx.h" 18 | 19 | class QOpenGLFramebufferObject; 20 | 21 | // Alias for CXX, could use custom type in Rust to use type_id 22 | using QQuickFramebufferObjectRenderer = QQuickFramebufferObject::Renderer; 23 | 24 | // TODO: useful to add to cxx-qt-lib anyway for opaque types? 25 | template 26 | ::std::unique_ptr constructUniquePtr(Args... args) 27 | { 28 | return ::std::make_unique(args...); 29 | } 30 | 31 | void 32 | blitFramebuffer(QOpenGLFramebufferObject* target, ::std::unique_ptr source); 33 | 34 | ::std::unique_ptr 35 | fboFromTexture(unsigned int texture_id, unsigned int texture_target, QSize size); 36 | 37 | // Alias for QEventPoint::State 38 | // 39 | // TODO: if events were in cxx-qt-lib we wouldn't need this 40 | using QEventPointState = QEventPoint::State; 41 | 42 | // TODO: once qsizetype is in cxx-qt we could avoid this 43 | ::rust::isize 44 | qTouchEventPointCount(QTouchEvent const& event); 45 | 46 | QEventPoint const& 47 | qTouchEventPoint(QTouchEvent& event, ::rust::isize index); 48 | 49 | using QMouseEventButton = Qt::MouseButton; 50 | -------------------------------------------------------------------------------- /images/arrow-back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KDABLabs/cxx-qt-servo-webview/05399f6520bd44883907f74855cd80e9b86067f9/images/arrow-back.png -------------------------------------------------------------------------------- /images/arrow-back.png.license: -------------------------------------------------------------------------------- 1 | SPDX-FileCopyrightText: Google Material Design icons authors 2 | 3 | SPDX-License-Identifier: Apache-2.0 4 | -------------------------------------------------------------------------------- /images/arrow-forward.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KDABLabs/cxx-qt-servo-webview/05399f6520bd44883907f74855cd80e9b86067f9/images/arrow-forward.png -------------------------------------------------------------------------------- /images/arrow-forward.png.license: -------------------------------------------------------------------------------- 1 | SPDX-FileCopyrightText: Google Material Design icons authors 2 | 3 | SPDX-License-Identifier: Apache-2.0 4 | -------------------------------------------------------------------------------- /images/search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KDABLabs/cxx-qt-servo-webview/05399f6520bd44883907f74855cd80e9b86067f9/images/search.png -------------------------------------------------------------------------------- /images/search.png.license: -------------------------------------------------------------------------------- 1 | SPDX-FileCopyrightText: Google Material Design icons authors 2 | 3 | SPDX-License-Identifier: Apache-2.0 4 | -------------------------------------------------------------------------------- /qml/ServoToolbar.qml: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | import QtQuick 2.12 7 | import QtQuick.Controls 2.12 8 | import QtQuick.Layouts 1.12 9 | 10 | import com.kdab.servo 1.0 11 | 12 | ColumnLayout { 13 | id: root 14 | 15 | signal goBack() 16 | signal goForward() 17 | signal urlRequest(url requestedUrl) 18 | 19 | property alias canGoBack: backButton.enabled 20 | property alias canGoForward: forwardButton.enabled 21 | property url faviconUrl 22 | property url webViewUrl 23 | 24 | spacing: 0 25 | 26 | // Toolbar of controls for Servo 27 | RowLayout { 28 | Layout.preferredHeight: 64 29 | Layout.fillWidth: true 30 | spacing: 0 31 | 32 | component ServoButton: ToolButton { 33 | hoverEnabled: false 34 | Layout.alignment: Qt.AlignVCenter 35 | Layout.preferredHeight: 64 36 | Layout.preferredWidth: 64 37 | icon.height: 32 38 | icon.width: 32 39 | opacity: enabled ? 1.0 : 0.5 40 | } 41 | 42 | component ServoSeparator: Rectangle { 43 | color: Qt.rgba(0, 0, 0, 0.3) 44 | Layout.alignment: Qt.AlignVCenter 45 | Layout.preferredHeight: 24 46 | Layout.preferredWidth: 2 47 | } 48 | 49 | ServoButton { 50 | id: backButton 51 | icon.source: "../images/arrow-back.png" 52 | icon.height: 48 53 | icon.width: 48 54 | 55 | onClicked: root.goBack() 56 | } 57 | 58 | ServoButton { 59 | id: forwardButton 60 | icon.source: "../images/arrow-forward.png" 61 | icon.height: 48 62 | icon.width: 48 63 | 64 | onClicked: root.goForward() 65 | } 66 | 67 | Rectangle { 68 | Layout.alignment: Qt.AlignVCenter 69 | Layout.fillWidth: true 70 | Layout.preferredHeight: 48 71 | radius: height / 2 72 | color: Qt.rgba(0, 0, 0, 0.1) 73 | 74 | RowLayout { 75 | anchors.fill: parent 76 | anchors.leftMargin: 12 77 | anchors.rightMargin: 24 78 | spacing: 12 79 | 80 | Rectangle { 81 | Layout.preferredHeight: 32 82 | Layout.preferredWidth: 32 83 | Layout.alignment: Qt.AlignVCenter 84 | radius: 16 85 | 86 | Image { 87 | anchors.centerIn: parent 88 | height: 24 89 | source: root.faviconUrl === "https://localhost/emptyfavicon.ico" ? "" : root.faviconUrl 90 | sourceSize.height: 24 91 | sourceSize.width: 24 92 | width: 24 93 | } 94 | } 95 | 96 | TextField { 97 | id: textInputUrl 98 | font.pixelSize: 24 99 | Layout.alignment: Qt.AlignVCenter 100 | Layout.fillHeight: true 101 | Layout.fillWidth: true 102 | text: root.webViewUrl 103 | placeholderText: qsTr("Url...") 104 | background: Item {} 105 | 106 | onAccepted: { 107 | if (goButton.enabled) { 108 | goButton.clicked(); 109 | } 110 | } 111 | } 112 | } 113 | } 114 | 115 | ServoButton { 116 | id: goButton 117 | enabled: textInputUrl.text.length > 0 118 | icon.source: "../images/search.png" 119 | icon.height: 48 120 | icon.width: 48 121 | 122 | onClicked: root.urlRequest(textInputUrl.text) 123 | } 124 | } 125 | 126 | Rectangle { 127 | color: Qt.rgba(0, 0, 0, 0.3) 128 | Layout.fillWidth: true 129 | Layout.preferredHeight: 2 130 | } 131 | } -------------------------------------------------------------------------------- /qml/main.qml: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | import QtQuick 2.12 7 | import QtQuick.Controls 2.12 8 | import QtQuick.Layouts 1.12 9 | import QtQuick.Window 2.12 10 | 11 | import com.kdab.servo 1.0 12 | 13 | Window { 14 | id: root 15 | color: "white" 16 | title: webView.title 17 | visible: true 18 | height: 800 19 | width: 1280 20 | 21 | ColumnLayout { 22 | anchors.fill: parent 23 | spacing: 0 24 | 25 | ServoToolbar { 26 | id: toolbar 27 | canGoBack: webView.canGoBack 28 | canGoForward: webView.canGoForward 29 | faviconUrl: webView.faviconUrl 30 | webViewUrl: webView.url 31 | Layout.fillWidth: true 32 | 33 | onGoBack: webView.goBack() 34 | onGoForward: webView.goForward() 35 | onUrlRequest: (requestedUrl) => webView.url = requestedUrl 36 | } 37 | 38 | // Servo webview 39 | ServoWebView { 40 | id: webView 41 | Layout.fillHeight: true 42 | Layout.fillWidth: true 43 | url: "https://servo.org/" 44 | } 45 | } 46 | 47 | // Progress bar at the bottom overlaying the Servo WebView 48 | // so that we don't have a flicker when it's hidden as this doesn't cause a resize 49 | ProgressBar { 50 | anchors.bottom: parent.bottom 51 | anchors.left: parent.left 52 | anchors.right: parent.right 53 | height: 10 54 | indeterminate: true 55 | visible: webView.loading 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /shell.nix: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | # SPDX-FileContributor: Magnus Groß 3 | # 4 | # SPDX-License-Identifier: MPL-2.0 5 | 6 | with import {}; 7 | with import (fetchTarball "https://github.com/nix-community/nixGL/archive/489d6b095ab9d289fe11af0219a9ff00fe87c7c5.tar.gz") { enable32bits = false; }; 8 | let 9 | pkgs_gnumake_4_3 = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/6adf48f53d819a7b6e15672817fa1e78e5f4e84f.tar.gz") {}; 10 | llvmPackages = llvmPackages_14; # servo/servo#31059 11 | stdenv = stdenvAdapters.useMoldLinker llvmPackages.stdenv; 12 | in 13 | stdenv.mkDerivation { 14 | name = "cxx-qt-servo-webview"; 15 | 16 | buildInputs = [ 17 | fontconfig freetype libunwind 18 | xorg.libxcb 19 | xorg.libX11 20 | xorg.libXcursor 21 | xorg.libXrandr 22 | xorg.libXi 23 | xorg.xcbutilkeysyms 24 | xorg.libXinerama 25 | xcb-util-cursor 26 | libxkbcommon 27 | gst_all_1.gstreamer 28 | gst_all_1.gst-plugins-base 29 | gst_all_1.gst-plugins-bad 30 | rustup 31 | taplo 32 | llvmPackages.bintools 33 | llvmPackages.llvm 34 | llvmPackages.libclang 35 | udev 36 | cmake dbus gcc git pkg-config which llvm perl yasm m4 37 | pkgs_gnumake_4_3.gnumake # servo/mozjs#375 38 | libGL 39 | qt6.full 40 | stdenv.cc.cc.lib 41 | mold 42 | ]; 43 | LD_LIBRARY_PATH = lib.makeLibraryPath [zlib xorg.libXcursor xorg.libXrandr xorg.libXi libxkbcommon vulkan-loader stdenv.cc.cc]; 44 | LIBCLANG_PATH = "${llvmPackages.libclang.lib}/lib"; 45 | 46 | shellHook = '' 47 | # see https://github.com/servo/mozjs/blob/20f7934762a6a1d4751353c8d024a0185ba85547/shell.nix#L11-L16 48 | export AS="$CC -c" 49 | ''; 50 | } 51 | -------------------------------------------------------------------------------- /src/browser.rs: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | use servo::{ 7 | compositing::windowing::EmbedderEvent, embedder_traits::EmbedderMsg, 8 | TopLevelBrowsingContextId as WebViewId, 9 | }; 10 | use std::collections::HashMap; 11 | use std::vec::Drain; 12 | 13 | #[derive(Default)] 14 | pub struct WebView {} 15 | 16 | #[derive(Default)] 17 | pub(crate) struct QServoBrowserResponse { 18 | pub(crate) favicon_url: Option, 19 | pub(crate) present: Option, 20 | pub(crate) title: Option, 21 | pub(crate) loading: Option, 22 | pub(crate) url: Option, 23 | pub(crate) blocked_navigation_request: Option, 24 | pub(crate) can_go_back: Option, 25 | pub(crate) can_go_forward: Option, 26 | } 27 | 28 | #[derive(Default)] 29 | pub(crate) struct QServoBrowser { 30 | web_views: HashMap, 31 | event_queue: Vec, 32 | focused_webview_id: Option, 33 | } 34 | 35 | impl QServoBrowser { 36 | pub fn webview_id(&self) -> Option { 37 | self.focused_webview_id 38 | } 39 | 40 | pub fn get_events(&mut self) -> Vec { 41 | std::mem::take(&mut self.event_queue) 42 | } 43 | 44 | /// Returns true if the caller needs to manually present a new frame. 45 | /// 46 | /// TODO: does this move into the WebView? 47 | pub fn handle_servo_events( 48 | &mut self, 49 | events: Drain<'_, (Option, EmbedderMsg)>, 50 | navigation_allowed: bool, 51 | ) -> QServoBrowserResponse { 52 | let mut response = QServoBrowserResponse::default(); 53 | 54 | for (webview_id, msg) in events { 55 | match msg { 56 | // Do not allow for opening a new tab / window 57 | // Not handling this crashes the webview 58 | EmbedderMsg::AllowOpeningWebView(ipc) => { 59 | ipc.send(false).unwrap(); 60 | } 61 | EmbedderMsg::AllowNavigationRequest(pipeline_id, url) => { 62 | if let Some(_webview_id) = webview_id { 63 | self.event_queue 64 | .push(EmbedderEvent::AllowNavigationResponse( 65 | pipeline_id, 66 | navigation_allowed, 67 | )); 68 | 69 | let url = url.into_url(); 70 | if navigation_allowed { 71 | // There is a new URL 72 | response.url = Some(url); 73 | } else { 74 | response.blocked_navigation_request = Some(url); 75 | } 76 | } 77 | } 78 | EmbedderMsg::WebViewOpened(new_webview_id) => { 79 | self.web_views.insert(new_webview_id, WebView {}); 80 | self.event_queue 81 | .push(EmbedderEvent::FocusWebView(new_webview_id)); 82 | } 83 | EmbedderMsg::WebViewClosed(webview_id) => { 84 | self.web_views.remove(&webview_id); 85 | self.focused_webview_id = None; 86 | } 87 | EmbedderMsg::WebViewFocused(webview_id) => { 88 | self.focused_webview_id = Some(webview_id); 89 | } 90 | EmbedderMsg::WebViewBlurred => { 91 | self.focused_webview_id = None; 92 | } 93 | EmbedderMsg::ChangePageTitle(title) => { 94 | response.title = title; 95 | } 96 | EmbedderMsg::NewFavicon(url) => { 97 | response.favicon_url = Some(url.as_url().to_owned()); 98 | } 99 | EmbedderMsg::LoadStart => { 100 | response.loading = Some(true); 101 | } 102 | EmbedderMsg::LoadComplete => { 103 | response.loading = Some(false); 104 | } 105 | EmbedderMsg::ReadyToPresent => { 106 | response.present = Some(true); 107 | } 108 | EmbedderMsg::HistoryChanged(urls, position) => { 109 | response.url = Some(urls[position].as_url().to_owned()); 110 | response.can_go_back = Some(position > 0); 111 | response.can_go_forward = Some(position < (urls.len() - 1)); 112 | } 113 | // TODO: this is where page up/down or shortcuts are handled 114 | // EmbedderMsg::Keyboard(key_event) => {} 115 | _others => { 116 | println!("handle_servo_events: {:?}", _others); 117 | } 118 | } 119 | } 120 | 121 | response 122 | } 123 | 124 | pub fn push_event(&mut self, event: EmbedderEvent) { 125 | self.event_queue.push(event); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/embedder.rs: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | use servo::compositing::windowing::EmbedderMethods; 7 | use servo::embedder_traits::EventLoopWaker; 8 | 9 | // https://doc.servo.org/embedder_traits/trait.EventLoopWaker.html 10 | pub(crate) struct QServoEmbedder { 11 | event_loop_waker: Box, 12 | } 13 | 14 | impl QServoEmbedder { 15 | pub fn new(event_loop_waker: Box) -> Self { 16 | Self { event_loop_waker } 17 | } 18 | } 19 | 20 | // https://doc.servo.org/compositing/windowing/trait.EmbedderMethods.html 21 | impl EmbedderMethods for QServoEmbedder { 22 | fn create_event_loop_waker(&mut self) -> Box { 23 | self.event_loop_waker.clone() 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/events_loop.rs: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | use cxx_qt::CxxQtThread; 7 | use servo::embedder_traits::EventLoopWaker; 8 | 9 | use crate::webview::qobject::ServoWebView; 10 | 11 | pub(crate) struct QServoEventsLoopWaker { 12 | qt_loop: CxxQtThread, 13 | } 14 | 15 | impl QServoEventsLoopWaker { 16 | pub(crate) fn new(qt_loop: CxxQtThread) -> Self { 17 | Self { qt_loop } 18 | } 19 | } 20 | 21 | impl EventLoopWaker for QServoEventsLoopWaker { 22 | fn clone_box(&self) -> Box { 23 | Box::new(QServoEventsLoopWaker { 24 | qt_loop: self.qt_loop.clone(), 25 | }) 26 | } 27 | 28 | fn wake(&self) { 29 | println!("wake!"); 30 | self.qt_loop 31 | .queue(|qobject| { 32 | qobject.update(); 33 | }) 34 | .unwrap(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | use cxx_qt_lib::{QGuiApplication, QQmlApplicationEngine, QUrl}; 7 | 8 | mod browser; 9 | mod embedder; 10 | mod events_loop; 11 | mod renderer; 12 | mod servothread; 13 | mod webview; 14 | mod windowheadless; 15 | 16 | fn main() { 17 | // We need the OpenGL backend for QQuickFramebufferObject 18 | std::env::set_var("QSG_RHI_BACKEND", "opengl"); 19 | 20 | let mut app = QGuiApplication::new(); 21 | let mut engine = QQmlApplicationEngine::new(); 22 | 23 | if let Some(engine) = engine.as_mut() { 24 | engine.load(&QUrl::from("qrc:/qt/qml/com/kdab/servo/qml/main.qml")); 25 | } 26 | 27 | if let Some(app) = app.as_mut() { 28 | app.exec(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/renderer.rs: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | #[cxx_qt::bridge(cxx_file_stem = "servorenderer")] 7 | pub(crate) mod qobject { 8 | unsafe extern "C++" { 9 | include!(); 10 | type QQuickFramebufferObject; 11 | } 12 | 13 | unsafe extern "C++" { 14 | include!("helpers.h"); 15 | 16 | #[cxx_name = "constructUniquePtr"] 17 | fn qservo_renderer_unique_ptr() -> UniquePtr; 18 | } 19 | 20 | unsafe extern "C++" { 21 | include!(); 22 | type QOpenGLFramebufferObject; 23 | 24 | include!("helpers.h"); 25 | #[cxx_name = "blitFramebuffer"] 26 | unsafe fn blit_framebuffer( 27 | target: *mut QOpenGLFramebufferObject, 28 | source: UniquePtr, 29 | ); 30 | 31 | include!("cxx-qt-lib/qsize.h"); 32 | type QSize = cxx_qt_lib::QSize; 33 | 34 | #[cxx_name = "fboFromTexture"] 35 | fn fbo_from_texture( 36 | texture_id: u32, 37 | texture_target: u32, 38 | size: QSize, 39 | ) -> UniquePtr; 40 | } 41 | 42 | unsafe extern "RustQt" { 43 | #[base = "QQuickFramebufferObject::Renderer"] 44 | type QServoRenderer = super::QServoRendererRust; 45 | 46 | #[inherit] 47 | #[rust_name = "framebuffer_object"] 48 | fn framebufferObject(self: &QServoRenderer) -> *mut QOpenGLFramebufferObject; 49 | 50 | #[cxx_override] 51 | fn render(self: Pin<&mut QServoRenderer>); 52 | 53 | #[cxx_override] 54 | unsafe fn synchronize(self: Pin<&mut QServoRenderer>, item: *mut QQuickFramebufferObject); 55 | } 56 | 57 | impl cxx_qt::Constructor<()> for QServoRenderer {} 58 | } 59 | 60 | use crate::{ 61 | servothread::{QServoMessage, QServoThread}, 62 | webview::qobject::ServoWebView, 63 | }; 64 | use core::pin::Pin; 65 | use cxx_qt::{CxxQtType, Threading}; 66 | use cxx_qt_lib::{QSize, QUrl}; 67 | use euclid::Size2D; 68 | use servo::{compositing::windowing::EmbedderEvent, servo_url::ServoUrl}; 69 | use std::sync::mpsc::{self, Sender}; 70 | use surfman::{Context, Device}; 71 | 72 | #[derive(Default)] 73 | pub struct QServoRendererRust { 74 | size: QSize, 75 | url: QUrl, 76 | servo_sender: Option>, 77 | qt_gl: Option<(Device, Context)>, 78 | } 79 | 80 | impl Drop for QServoRendererRust { 81 | fn drop(&mut self) { 82 | self.servo_sender 83 | .as_ref() 84 | .unwrap() 85 | .send(QServoMessage::Quit) 86 | .unwrap(); 87 | 88 | if let Some((device, mut context)) = self.qt_gl.take() { 89 | device.destroy_context(&mut context).unwrap(); 90 | } 91 | } 92 | } 93 | 94 | impl qobject::QServoRenderer { 95 | pub(crate) fn new() -> cxx::UniquePtr { 96 | qobject::qservo_renderer_unique_ptr() 97 | } 98 | 99 | fn render(mut self: Pin<&mut Self>) { 100 | println!("render start"); 101 | 102 | // Ask to borrow a surface 103 | let (take_sender, take_receiver) = mpsc::sync_channel(0); 104 | let (recycle_sender, recycle_receiver) = mpsc::sync_channel(0); 105 | self.servo_sender 106 | .as_ref() 107 | .unwrap() 108 | .send(QServoMessage::BorrowSurface(take_sender, recycle_receiver)) 109 | .unwrap(); 110 | 111 | // Wait for the response from the background thread 112 | let surface = take_receiver.recv(); 113 | if let Ok(Some(surface)) = surface { 114 | // Find the target fbo 115 | let fbo_target = self.as_ref().framebuffer_object(); 116 | let size = self.as_ref().size.clone(); 117 | 118 | if let Some((ref mut device, ref mut context)) = self.as_mut().rust_mut().qt_gl.as_mut() 119 | { 120 | // Build a texture from the surface 121 | match device.create_surface_texture(context, surface) { 122 | Ok(texture) => { 123 | // Retrieve the texture info 124 | let object = device.surface_texture_object(&texture); 125 | let target = device.surface_gl_texture_target(); 126 | 127 | // Build a source FBO from the texture 128 | // 129 | // Note that this is a unique_ptr which is freed when bliting 130 | let fbo_source = qobject::fbo_from_texture(object, target, size); 131 | 132 | // Blit source FBO to the target FBO 133 | unsafe { qobject::blit_framebuffer(fbo_target, fbo_source) }; 134 | 135 | // Destory the texture and return the surface back to the background thread 136 | let surface = device.destroy_surface_texture(context, texture); 137 | recycle_sender.send(surface.ok()).unwrap(); 138 | } 139 | Err((_, surface)) => { 140 | // Return the surface back to the background thread 141 | recycle_sender.send(Some(surface)).unwrap(); 142 | } 143 | } 144 | } else { 145 | recycle_sender.send(Some(surface)).unwrap(); 146 | } 147 | } else { 148 | recycle_sender.send(None).unwrap(); 149 | } 150 | 151 | println!("render end"); 152 | } 153 | 154 | unsafe fn synchronize(mut self: Pin<&mut Self>, item: *mut qobject::QQuickFramebufferObject) { 155 | println!("sync start"); 156 | let webview_ptr = item as *mut ServoWebView; 157 | if let Some(webview_ref) = webview_ptr.as_mut() { 158 | let mut webview = Pin::new_unchecked(webview_ref); 159 | 160 | // Start the Servo worker thread if there isn't one 161 | if self.as_ref().servo_sender.is_none() { 162 | let qt_thread = webview.qt_thread(); 163 | let (servo_sender, servo_receiver) = mpsc::channel(); 164 | 165 | use surfman::platform::generic::multi; 166 | use surfman::platform::unix::wayland; 167 | let native_connection = wayland::connection::NativeConnection::current() 168 | .expect("Failed to bootstrap native connection"); 169 | let wayland_connection = unsafe { 170 | wayland::connection::Connection::from_native_connection(native_connection) 171 | .expect("Failed to bootstrap wayland connection") 172 | }; 173 | let connection = multi::connection::Connection::Default( 174 | multi::connection::Connection::Default(wayland_connection), 175 | ); 176 | 177 | // Pass in the original size of the window 178 | // this reduces the glitches where the size of the servo view is lost 179 | let size = webview.as_ref().size().to_size(); 180 | let size = Size2D::new(size.width() as u32, size.height() as u32); 181 | 182 | std::thread::spawn(move || { 183 | QServoThread::new(servo_receiver, qt_thread, connection, size).run() 184 | }); 185 | 186 | self.as_mut().rust_mut().servo_sender = Some(servo_sender); 187 | } 188 | 189 | // Check if we have a new URL 190 | let url = webview.as_ref().url().clone(); 191 | if url != self.url && url.is_valid() { 192 | self.as_mut().rust_mut().url = url; 193 | 194 | let new_url = url::Url::try_from(&self.url); 195 | if new_url.is_ok() { 196 | let servo_url = ServoUrl::from_url(new_url.unwrap()); 197 | self.as_ref() 198 | .servo_sender 199 | .as_ref() 200 | .unwrap() 201 | .send(QServoMessage::Url(servo_url)) 202 | .unwrap(); 203 | } 204 | } 205 | 206 | let size = webview.as_ref().size().to_size(); 207 | if size != self.size { 208 | self.as_mut().rust_mut().size = size; 209 | 210 | self.as_ref() 211 | .servo_sender 212 | .as_ref() 213 | .unwrap() 214 | .send(QServoMessage::Resize(Size2D::new( 215 | self.size.width(), 216 | self.size.height(), 217 | ))) 218 | .unwrap(); 219 | } 220 | 221 | if let Some(direction) = webview.as_mut().rust_mut().navigation_direction.take() { 222 | self.as_ref() 223 | .servo_sender 224 | .as_ref() 225 | .unwrap() 226 | .send(QServoMessage::Navigation(direction)) 227 | .unwrap(); 228 | } 229 | 230 | // Process any converted events from Qt 231 | let events: Vec = webview.as_mut().rust_mut().events.drain(..).collect(); 232 | for event in events.into_iter() { 233 | self.as_ref() 234 | .servo_sender 235 | .as_ref() 236 | .unwrap() 237 | .send(QServoMessage::RawEmbeddedEvent(event)) 238 | .unwrap(); 239 | } 240 | 241 | // Process any pending events 242 | let navigation_allowed = *webview.as_ref().navigation_allowed(); 243 | let (heartbeat_sender, heartbeat_receiver) = mpsc::sync_channel(0); 244 | self.as_ref() 245 | .servo_sender 246 | .as_ref() 247 | .unwrap() 248 | .send(QServoMessage::Heartbeat( 249 | heartbeat_sender, 250 | navigation_allowed, 251 | )) 252 | .unwrap(); 253 | // Wait for response, otherwise if we enter render() while the 254 | // heartbeat is running flickering can occur 255 | heartbeat_receiver.recv().unwrap(); 256 | } 257 | 258 | println!("sync end"); 259 | } 260 | } 261 | 262 | impl cxx_qt::Initialize for qobject::QServoRenderer { 263 | fn initialize(mut self: core::pin::Pin<&mut Self>) { 264 | use surfman::platform::generic::multi; 265 | use surfman::platform::unix::wayland; 266 | let native_connection = wayland::connection::NativeConnection::current() 267 | .expect("Failed to bootstrap native connection"); 268 | let wayland_connection = unsafe { 269 | wayland::connection::Connection::from_native_connection(native_connection) 270 | .expect("Failed to bootstrap wayland connection") 271 | }; 272 | let connection = multi::connection::Connection::Default( 273 | multi::connection::Connection::Default(wayland_connection), 274 | ); 275 | let adapter = connection 276 | .create_software_adapter() 277 | .expect("Failed to create adapter"); 278 | let device = connection 279 | .create_device(&adapter) 280 | .expect("Failed to bootstrap surfman device"); 281 | let native_context = { 282 | let current = wayland::context::NativeContext::current() 283 | .expect("Failed to bootstrap native context"); 284 | multi::context::NativeContext::Default(multi::context::NativeContext::Default(current)) 285 | }; 286 | let context = unsafe { 287 | device 288 | .create_context_from_native_context(native_context) 289 | .expect("Failed to bootstrap surfman context") 290 | }; 291 | 292 | self.as_mut().rust_mut().qt_gl = Some((device, context)); 293 | } 294 | } 295 | -------------------------------------------------------------------------------- /src/servothread.rs: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | use std::{ 7 | collections::HashMap, 8 | rc::Rc, 9 | sync::mpsc::{Receiver, SyncSender}, 10 | }; 11 | 12 | use cxx_qt::CxxQtThread; 13 | use cxx_qt_lib::{QString, QUrl}; 14 | use servo::{ 15 | compositing::windowing::{EmbedderEvent, WindowMethods}, 16 | embedder_traits::EventLoopWaker, 17 | euclid::Size2D, 18 | servo_geometry::DeviceIndependentPixel, 19 | servo_url::ServoUrl, 20 | style_traits::DevicePixel, 21 | Servo, TopLevelBrowsingContextId, 22 | }; 23 | use surfman::chains::SwapChainAPI; 24 | use surfman::{Connection, Surface}; 25 | use url::Url; 26 | 27 | use crate::{ 28 | browser::QServoBrowser, embedder::QServoEmbedder, events_loop::QServoEventsLoopWaker, 29 | webview::qobject::ServoWebView, windowheadless::QServoWindowHeadless, 30 | }; 31 | 32 | // #[derive(Debug)] 33 | pub(crate) enum QServoMessage { 34 | Navigation(i32), 35 | RawEmbeddedEvent(EmbedderEvent), 36 | Resize(Size2D), 37 | Url(ServoUrl), 38 | Heartbeat(SyncSender<()>, bool), 39 | BorrowSurface(SyncSender>, Receiver>), 40 | Quit, 41 | } 42 | 43 | unsafe impl Send for QServoMessage {} 44 | 45 | pub(crate) struct QServoThread { 46 | browser: QServoBrowser, 47 | browser_id: TopLevelBrowsingContextId, 48 | servo: Servo, 49 | receiver: Receiver, 50 | qt_thread: CxxQtThread, 51 | } 52 | 53 | impl QServoThread { 54 | pub(crate) fn new( 55 | receiver: Receiver, 56 | qt_thread: CxxQtThread, 57 | connection: Connection, 58 | size: Size2D, 59 | ) -> Self { 60 | let event_loop_waker = QServoEventsLoopWaker::new(qt_thread.clone()); 61 | let embedder = Box::new(QServoEmbedder::new(event_loop_waker.clone_box())); 62 | 63 | let window = Rc::new(QServoWindowHeadless::new(size, connection)); 64 | let user_agent = None; 65 | // The in-process interface to Servo. 66 | // 67 | // It does everything necessary to render the web, primarily 68 | // orchestrating the interaction between JavaScript, CSS layout, 69 | // rendering, and the client window. 70 | // 71 | // Clients create a `Servo` instance for a given reference-counted type 72 | // implementing `WindowMethods`, which is the bridge to whatever 73 | // application Servo is embedded in. Clients then create an event 74 | // loop to pump messages between the embedding application and 75 | // various browser components. 76 | let servo_data = Servo::new( 77 | embedder, 78 | window.clone(), 79 | user_agent, 80 | servo::compositing::CompositeTarget::Window, 81 | ); 82 | 83 | // Enable logging and store servo instance 84 | servo_data.servo.setup_logging(); 85 | 86 | // Initialise servo 87 | event_loop_waker.wake(); 88 | 89 | Self { 90 | browser_id: servo_data.browser_id, 91 | servo: servo_data.servo, 92 | browser: QServoBrowser::default(), 93 | receiver, 94 | qt_thread, 95 | } 96 | } 97 | 98 | pub(crate) fn run(&mut self) { 99 | let mut current_url = None; 100 | let mut favicons = HashMap::::new(); 101 | 102 | while let Ok(msg) = self.receiver.recv() { 103 | match msg { 104 | QServoMessage::Navigation(direction) => { 105 | let direction = if direction < 0 { 106 | servo::msg::constellation_msg::TraversalDirection::Back( 107 | direction.unsigned_abs() as usize, 108 | ) 109 | } else { 110 | servo::msg::constellation_msg::TraversalDirection::Forward( 111 | direction.unsigned_abs() as usize, 112 | ) 113 | }; 114 | 115 | self.browser 116 | .push_event(EmbedderEvent::Navigation(self.browser_id, direction)); 117 | } 118 | QServoMessage::RawEmbeddedEvent(event) => { 119 | self.browser.push_event(event); 120 | } 121 | QServoMessage::Resize(size) => { 122 | let surfman = self.servo.window().rendering_context(); 123 | surfman 124 | .resize(size.to_untyped().to_i32()) 125 | .expect("Failed to resize"); 126 | self.browser.push_event(EmbedderEvent::Resize); 127 | } 128 | QServoMessage::Url(url) => { 129 | // Don't update the url if this was the last url 130 | if Some(&url) == current_url.as_ref() { 131 | continue; 132 | } 133 | 134 | current_url = Some(url.clone()); 135 | 136 | // Open a new browser or load the url 137 | if let Some(webview_id) = self.browser.webview_id() { 138 | self.browser 139 | .push_event(EmbedderEvent::LoadUrl(webview_id, url)); 140 | } else { 141 | self.browser 142 | .push_event(EmbedderEvent::NewWebView(url, self.browser_id)); 143 | } 144 | } 145 | QServoMessage::BorrowSurface(sender, receiver) => { 146 | let surfman = self.servo.window().rendering_context(); 147 | let swap_chain = surfman.swap_chain().unwrap(); 148 | 149 | let surface = swap_chain.take_surface(); 150 | 151 | println!("sending surface: {}", surface.is_some()); 152 | sender.send(surface).unwrap(); 153 | 154 | println!("waiting for surface return"); 155 | let surface = receiver.recv().unwrap(); 156 | 157 | println!("returned surface, recycling"); 158 | if let Some(surface) = surface { 159 | swap_chain.recycle_surface(surface); 160 | } 161 | } 162 | QServoMessage::Heartbeat(sender, navigation_allowed) => { 163 | // Browser process servo events 164 | let mut need_present = false; 165 | let mut need_resize = false; 166 | 167 | { 168 | let mut servo_events = self.servo.get_events(); 169 | loop { 170 | let mut response = self 171 | .browser 172 | .handle_servo_events(servo_events, navigation_allowed); 173 | 174 | if let Some(url) = response.url.as_ref() { 175 | // If there is no favicon but we have found one previously 176 | // for this url then set it 177 | if response.favicon_url.is_none() { 178 | if let Some(favicon) = favicons.get(url) { 179 | response.favicon_url = Some(favicon.to_owned()); 180 | } 181 | } 182 | 183 | current_url = Some(ServoUrl::from_url(url.to_owned())); 184 | } 185 | 186 | // Store the favicon for the current url 187 | if let Some(favicon) = response.favicon_url.as_ref() { 188 | if let Some(current_url) = current_url.as_ref() { 189 | favicons.insert( 190 | current_url.as_url().to_owned(), 191 | favicon.to_owned(), 192 | ); 193 | } 194 | } 195 | 196 | // If there is a url but no known favicon then set an empty icon for now 197 | // so that when loading we don't show the old favicon 198 | if response.url.is_some() && response.favicon_url.is_none() { 199 | response.favicon_url = 200 | Some(Url::parse("https://localhost/emptyfavicon.ico").unwrap()); 201 | } 202 | 203 | // Handle the responses from browser events to Qt 204 | self.qt_thread 205 | .queue(move |mut webview| { 206 | if let Some(title) = response.title { 207 | webview.as_mut().set_title(QString::from(&title)); 208 | } 209 | if let Some(loading) = response.loading { 210 | webview.as_mut().set_loading(loading); 211 | } 212 | if let Some(favicon_url) = response.favicon_url { 213 | webview.as_mut().set_favicon_url(QUrl::from(&favicon_url)); 214 | } 215 | if let Some(url) = response.url { 216 | webview.as_mut().set_url(QUrl::from(&url)); 217 | } 218 | if let Some(url) = response.blocked_navigation_request { 219 | webview 220 | .as_mut() 221 | .blocked_navigation_request(QUrl::from(&url)); 222 | } 223 | if let Some(can_go_back) = response.can_go_back { 224 | webview.as_mut().set_can_go_back(can_go_back); 225 | } 226 | if let Some(can_go_forward) = response.can_go_forward { 227 | webview.as_mut().set_can_go_forward(can_go_forward); 228 | } 229 | }) 230 | .unwrap(); 231 | 232 | // Present when required 233 | need_present |= response.present.unwrap_or(false); 234 | 235 | // Servo process browser events 236 | let browser_events = self.browser.get_events(); 237 | need_resize |= self.servo.handle_events(browser_events); 238 | 239 | servo_events = self.servo.get_events(); 240 | // There could be more events so loop around if there are 241 | if servo_events.len() == 0 { 242 | break; 243 | } 244 | } 245 | } 246 | 247 | // If we have resized then we need to force a repaint synchornously 248 | // 249 | // This is the same as Present::Immediate in servoshell 250 | // Resizes are unusual in that we need to repaint synchronously. 251 | // TODO(servo#30049) can we replace this with the simpler Servo::recomposite? 252 | if need_resize { 253 | println!("repaint_synchronously"); 254 | self.servo.repaint_synchronously(); 255 | } 256 | 257 | // Instead we do this after recycle_surface as this avoids issues 258 | // with the surface becoming empty after resize 259 | // Present if we resized or need to present 260 | if need_present || need_resize { 261 | println!("present!"); 262 | self.servo.present(); 263 | } 264 | 265 | // If we are resizing then recomposite after the present 266 | // as we need to ensure that the front and back buffer 267 | // have both been recomposite / repaint_synchronously 268 | if need_resize { 269 | println!("recomposite"); 270 | self.servo.recomposite(); 271 | } 272 | 273 | // Indicate that we have completed the heartbeat 274 | sender.send(()).unwrap(); 275 | 276 | println!("heartbeat!"); 277 | } 278 | QServoMessage::Quit => break, 279 | } 280 | } 281 | 282 | println!("quiting!"); 283 | self.servo.handle_events(vec![EmbedderEvent::Quit]); 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /src/webview.rs: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | #[cxx_qt::bridge(cxx_file_stem = "servowebview")] 7 | pub(crate) mod qobject { 8 | unsafe extern "C++" { 9 | include!("cxx-qt-lib/qstring.h"); 10 | type QString = cxx_qt_lib::QString; 11 | 12 | include!("cxx-qt-lib/qsizef.h"); 13 | type QSizeF = cxx_qt_lib::QSizeF; 14 | 15 | include!("cxx-qt-lib/qurl.h"); 16 | type QUrl = cxx_qt_lib::QUrl; 17 | 18 | include!(); 19 | 20 | include!("helpers.h"); 21 | // TODO: describe this with a custom type 22 | type QQuickFramebufferObjectRenderer; 23 | 24 | #[cxx_name = "setMirrorVertically"] 25 | fn set_mirror_vertically(self: Pin<&mut ServoWebView>, enable: bool); 26 | } 27 | 28 | /// This enum specifies why the focus changed. It will be passed through QWidget::setFocus 29 | /// and can be retrieved in the QFocusEvent sent to the widget upon focus change. 30 | #[namespace = "Qt"] 31 | #[repr(i32)] 32 | enum FocusReason { 33 | /// A mouse action occurred. 34 | MouseFocusReason, 35 | /// The Tab key was pressed. 36 | TabFocusReason, 37 | /// A Backtab occurred. The input for this may include the Shift or Control keys; e.g. Shift+Tab. 38 | BacktabFocusReason, 39 | /// The window system made this window either active or inactive. 40 | ActiveWindowFocusReason, 41 | /// The application opened/closed a pop-up that grabbed/released the keyboard focus. 42 | PopupFocusReason, 43 | /// The user typed a label's buddy shortcut 44 | ShortcutFocusReason, 45 | /// The menu bar took focus. 46 | MenuBarFocusReason, 47 | /// Another reason, usually application-specific. 48 | OtherFocusReason, 49 | } 50 | 51 | #[namespace = "Qt"] 52 | unsafe extern "C++" { 53 | type FocusReason; 54 | } 55 | 56 | unsafe extern "RustQt" { 57 | #[qobject] 58 | #[base = "QQuickFramebufferObject"] 59 | #[qml_element] 60 | #[qproperty(bool, can_go_back)] 61 | #[qproperty(bool, can_go_forward)] 62 | #[qproperty(QUrl, favicon_url)] 63 | #[qproperty(bool, loading)] 64 | #[qproperty(bool, navigation_allowed)] 65 | #[qproperty(QString, title)] 66 | #[qproperty(QUrl, url)] 67 | type ServoWebView = super::QServoWebViewRust; 68 | 69 | #[cxx_override] 70 | #[cxx_name = "createRenderer"] 71 | fn create_renderer(self: &ServoWebView) -> *mut QQuickFramebufferObjectRenderer; 72 | 73 | #[inherit] 74 | #[cxx_name = "forceActiveFocus"] 75 | fn force_active_focus(self: Pin<&mut ServoWebView>, reason: FocusReason); 76 | 77 | #[inherit] 78 | fn size(self: &ServoWebView) -> QSizeF; 79 | 80 | #[inherit] 81 | #[cxx_name = "setAcceptedMouseButtons"] 82 | fn set_accepted_mouse_buttons(self: Pin<&mut ServoWebView>, buttons: QMouseEventButton); 83 | 84 | #[inherit] 85 | #[cxx_name = "setAcceptTouchEvents"] 86 | fn set_accept_touch_events(self: Pin<&mut ServoWebView>, enabled: bool); 87 | 88 | #[inherit] 89 | fn update(self: Pin<&mut ServoWebView>); 90 | 91 | #[qsignal] 92 | fn blocked_navigation_request(self: Pin<&mut ServoWebView>, blocked_url: QUrl); 93 | 94 | #[qinvokable] 95 | fn go_back(self: Pin<&mut ServoWebView>); 96 | 97 | #[qinvokable] 98 | fn go_forward(self: Pin<&mut ServoWebView>); 99 | } 100 | 101 | unsafe extern "C++" { 102 | include!("cxx-qt-lib/qpoint.h"); 103 | type QPoint = cxx_qt_lib::QPoint; 104 | include!("cxx-qt-lib/qpointf.h"); 105 | type QPointF = cxx_qt_lib::QPointF; 106 | 107 | type QWheelEvent; 108 | 109 | #[cxx_name = "pixelDelta"] 110 | fn pixel_delta(self: &QWheelEvent) -> QPoint; 111 | 112 | fn position(self: &QWheelEvent) -> QPointF; 113 | } 114 | 115 | #[repr(u8)] 116 | enum QEventPointState { 117 | Unknown = 0x00, 118 | Stationary = 0x04, 119 | Pressed = 0x01, 120 | Updated = 0x02, 121 | Released = 0x08, 122 | } 123 | 124 | unsafe extern "C++" { 125 | type QEventPointState; 126 | } 127 | 128 | unsafe extern "C++" { 129 | type QEventPoint; 130 | 131 | fn id(&self) -> i32; 132 | fn position(&self) -> QPointF; 133 | fn state(&self) -> QEventPointState; 134 | } 135 | 136 | unsafe extern "C++" { 137 | type QKeyEvent; 138 | 139 | #[cxx_name = "isAutoRepeat"] 140 | fn is_auto_repeat(&self) -> bool; 141 | 142 | fn key(&self) -> i32; 143 | 144 | fn text(&self) -> QString; 145 | } 146 | 147 | #[repr(i32)] 148 | enum QMouseEventButton { 149 | NoButton = 0, 150 | AllButtons = 0x07ffffff, 151 | LeftButton = 0x01, 152 | RightButton = 0x02, 153 | MiddleButton = 0x04, 154 | } 155 | 156 | unsafe extern "C++" { 157 | type QMouseEventButton; 158 | } 159 | 160 | unsafe extern "C++" { 161 | type QMouseEvent; 162 | 163 | fn button(&self) -> QMouseEventButton; 164 | fn position(&self) -> QPointF; 165 | } 166 | 167 | unsafe extern "C++" { 168 | type QTouchEvent; 169 | 170 | #[cxx_name = "qTouchEventPointCount"] 171 | fn qtouchevent_point_count(ptr: &QTouchEvent) -> isize; 172 | 173 | #[cxx_name = "qTouchEventPoint"] 174 | fn qtouchevent_point(ptr: Pin<&mut QTouchEvent>, i: isize) -> &QEventPoint; 175 | } 176 | 177 | unsafe extern "RustQt" { 178 | #[cxx_name = "keyPressEvent"] 179 | #[cxx_override] 180 | unsafe fn key_press_event(self: Pin<&mut ServoWebView>, event: *mut QKeyEvent); 181 | 182 | #[cxx_name = "keyReleaseEvent"] 183 | #[cxx_override] 184 | unsafe fn key_release_event(self: Pin<&mut ServoWebView>, event: *mut QKeyEvent); 185 | 186 | #[cxx_override] 187 | #[cxx_name = "mouseMoveEvent"] 188 | unsafe fn mouse_move_event(self: Pin<&mut ServoWebView>, event: *mut QMouseEvent); 189 | 190 | #[cxx_override] 191 | #[cxx_name = "mousePressEvent"] 192 | unsafe fn mouse_press_event(self: Pin<&mut ServoWebView>, event: *mut QMouseEvent); 193 | 194 | #[cxx_override] 195 | #[cxx_name = "mouseReleaseEvent"] 196 | unsafe fn mouse_release_event(self: Pin<&mut ServoWebView>, event: *mut QMouseEvent); 197 | 198 | #[cxx_override] 199 | #[cxx_name = "touchEvent"] 200 | unsafe fn touch_event(self: Pin<&mut ServoWebView>, event: *mut QTouchEvent); 201 | 202 | #[cxx_override] 203 | #[cxx_name = "wheelEvent"] 204 | unsafe fn wheel_event(self: Pin<&mut ServoWebView>, event: *mut QWheelEvent); 205 | } 206 | 207 | impl cxx_qt::Constructor<()> for ServoWebView {} 208 | impl cxx_qt::Threading for ServoWebView {} 209 | } 210 | 211 | use core::pin::Pin; 212 | use cxx_qt::CxxQtType; 213 | use cxx_qt_lib::{QPointF, QString, QUrl}; 214 | use euclid::Point2D; 215 | use qobject::{FocusReason, QEventPointState, QMouseEventButton}; 216 | use servo::{ 217 | compositing::windowing::{EmbedderEvent, MouseWindowEvent}, 218 | keyboard_types::{Code, Key, KeyState, KeyboardEvent, Location, Modifiers}, 219 | script_traits::{MouseButton, TouchEventType, TouchId}, 220 | }; 221 | use std::str::FromStr; 222 | 223 | use crate::renderer::qobject::QServoRenderer; 224 | 225 | impl qobject::QTouchEvent { 226 | fn point_count(&self) -> isize { 227 | qobject::qtouchevent_point_count(self) 228 | } 229 | 230 | fn point(self: Pin<&mut Self>, index: isize) -> &qobject::QEventPoint { 231 | qobject::qtouchevent_point(self, index) 232 | } 233 | } 234 | 235 | fn get_servo_code_from_scancode(code: i32) -> Code { 236 | // TODO: Map more codes 237 | use servo::keyboard_types::Code::*; 238 | match code { 239 | 0x01000000 => Escape, 240 | 0x01000001 => Tab, 241 | // 0x01000002 242 | 0x01000003 => Backspace, 243 | 0x01000004 => Enter, 244 | 0x01000005 => NumpadEnter, 245 | 0x01000006 => Insert, 246 | 0x01000007 => Delete, 247 | // ... 248 | 0x01000010 => Home, 249 | 0x01000011 => End, 250 | 0x01000012 => ArrowLeft, 251 | 0x01000013 => ArrowUp, 252 | 0x01000014 => ArrowRight, 253 | 0x01000015 => ArrowDown, 254 | 0x01000016 => PageUp, 255 | 0x01000017 => PageDown, 256 | // Shift 257 | 0x01000020 => ShiftLeft, 258 | // ... 259 | 0x01000030 => F1, 260 | 0x01000031 => F2, 261 | 0x01000032 => F3, 262 | 0x01000033 => F4, 263 | 0x01000034 => F5, 264 | 0x01000035 => F6, 265 | 0x01000036 => F7, 266 | 0x01000037 => F8, 267 | 0x01000038 => F9, 268 | 0x01000039 => F10, 269 | 0x0100003a => F11, 270 | 0x0100003b => F12, 271 | // ... 272 | 0x20 => Space, 273 | // ... 274 | 0x2c => Comma, 275 | // 0x2d 276 | 0x2e => Period, 277 | 0x2f => Slash, 278 | 0x30 => Digit0, 279 | 0x31 => Digit1, 280 | 0x32 => Digit2, 281 | 0x33 => Digit3, 282 | 0x34 => Digit4, 283 | 0x35 => Digit5, 284 | 0x36 => Digit6, 285 | 0x37 => Digit7, 286 | 0x38 => Digit8, 287 | 0x39 => Digit9, 288 | // 0x3a 289 | 0x3b => Semicolon, 290 | // ... 291 | 0x41 => KeyA, 292 | 0x42 => KeyB, 293 | 0x43 => KeyC, 294 | 0x44 => KeyD, 295 | 0x45 => KeyE, 296 | 0x46 => KeyF, 297 | 0x47 => KeyG, 298 | 0x48 => KeyH, 299 | 0x49 => KeyI, 300 | 0x4a => KeyJ, 301 | 0x4b => KeyK, 302 | 0x4c => KeyL, 303 | 0x4d => KeyM, 304 | 0x4e => KeyN, 305 | 0x4f => KeyO, 306 | 0x50 => KeyP, 307 | 0x51 => KeyQ, 308 | 0x52 => KeyR, 309 | 0x53 => KeyS, 310 | 0x54 => KeyT, 311 | 0x55 => KeyU, 312 | 0x56 => KeyV, 313 | 0x57 => KeyW, 314 | 0x58 => KeyX, 315 | 0x59 => KeyY, 316 | 0x5a => KeyZ, 317 | 0x5b => BracketLeft, 318 | 0x5c => Backslash, 319 | 0x5d => BracketRight, 320 | // ... 321 | // QuoteLeft 322 | 0x60 => Quote, 323 | // ... 324 | _ => Unidentified, 325 | } 326 | } 327 | 328 | pub struct QServoWebViewRust { 329 | can_go_back: bool, 330 | can_go_forward: bool, 331 | favicon_url: QUrl, 332 | loading: bool, 333 | title: QString, 334 | url: QUrl, 335 | pub(crate) events: Vec, 336 | press_position: Option, 337 | navigation_allowed: bool, 338 | pub(crate) navigation_direction: Option, 339 | } 340 | 341 | impl Default for QServoWebViewRust { 342 | fn default() -> Self { 343 | Self { 344 | can_go_back: false, 345 | can_go_forward: false, 346 | favicon_url: QUrl::default(), 347 | loading: false, 348 | title: QString::default(), 349 | url: QUrl::default(), 350 | events: vec![], 351 | press_position: None, 352 | navigation_allowed: true, 353 | navigation_direction: None, 354 | } 355 | } 356 | } 357 | 358 | impl qobject::ServoWebView { 359 | fn create_renderer(&self) -> *mut qobject::QQuickFramebufferObjectRenderer { 360 | QServoRenderer::new().into_raw() as *mut qobject::QQuickFramebufferObjectRenderer 361 | } 362 | 363 | fn go_back(mut self: Pin<&mut Self>) { 364 | self.as_mut().rust_mut().navigation_direction = Some(-1); 365 | self.as_mut().update(); 366 | } 367 | 368 | fn go_forward(mut self: Pin<&mut Self>) { 369 | self.as_mut().rust_mut().navigation_direction = Some(1); 370 | self.as_mut().update(); 371 | } 372 | 373 | fn key_event(mut self: Pin<&mut Self>, event: *mut qobject::QKeyEvent, state: KeyState) { 374 | if let Some(event) = unsafe { event.as_ref() } { 375 | let keyboard_event = KeyboardEvent { 376 | state, 377 | key: Key::from_str(&String::from(&event.text())).unwrap_or(Key::Unidentified), 378 | code: get_servo_code_from_scancode(event.key()), 379 | repeat: event.is_auto_repeat(), 380 | // TODO: handle these correctly 381 | location: Location::Standard, 382 | modifiers: Modifiers::empty(), 383 | is_composing: false, 384 | }; 385 | self.as_mut() 386 | .rust_mut() 387 | .events 388 | .push(EmbedderEvent::Keyboard(keyboard_event)); 389 | 390 | self.as_mut().update(); 391 | } 392 | } 393 | 394 | fn key_press_event(self: Pin<&mut Self>, event: *mut qobject::QKeyEvent) { 395 | self.key_event(event, KeyState::Down); 396 | } 397 | 398 | fn key_release_event(self: Pin<&mut Self>, event: *mut qobject::QKeyEvent) { 399 | self.key_event(event, KeyState::Up); 400 | } 401 | 402 | fn mouse_move_event(mut self: Pin<&mut Self>, event: *mut qobject::QMouseEvent) { 403 | if let Some(event) = unsafe { event.as_ref() } { 404 | let event_position = event.position(); 405 | let position = Point2D::new(event_position.x() as f32, event_position.y() as f32); 406 | self.as_mut() 407 | .rust_mut() 408 | .events 409 | .push(EmbedderEvent::MouseWindowMoveEventClass(position)); 410 | 411 | self.as_mut().update(); 412 | } 413 | } 414 | 415 | fn mouse_press_event(mut self: Pin<&mut Self>, event: *mut qobject::QMouseEvent) { 416 | if let Some(event) = unsafe { event.as_ref() } { 417 | let event_position = event.position(); 418 | let position = Point2D::new(event_position.x() as f32, event_position.y() as f32); 419 | let button = match event.button() { 420 | QMouseEventButton::LeftButton => MouseButton::Left, 421 | QMouseEventButton::RightButton => MouseButton::Right, 422 | QMouseEventButton::MiddleButton => MouseButton::Middle, 423 | _others => return, 424 | }; 425 | self.as_mut() 426 | .rust_mut() 427 | .events 428 | .push(EmbedderEvent::MouseWindowEventClass( 429 | MouseWindowEvent::MouseDown(button, position), 430 | )); 431 | 432 | // Store the event position so we can detect clicks 433 | self.as_mut().rust_mut().press_position = Some(event_position); 434 | 435 | // Ensure we have focus so that we receive key events 436 | self.as_mut() 437 | .force_active_focus(FocusReason::MouseFocusReason); 438 | 439 | self.as_mut().update(); 440 | } 441 | } 442 | 443 | fn mouse_release_event(mut self: Pin<&mut Self>, event: *mut qobject::QMouseEvent) { 444 | if let Some(event) = unsafe { event.as_ref() } { 445 | let event_position = event.position(); 446 | let position = Point2D::new(event_position.x() as f32, event_position.y() as f32); 447 | let button = match event.button() { 448 | QMouseEventButton::LeftButton => MouseButton::Left, 449 | QMouseEventButton::RightButton => MouseButton::Right, 450 | QMouseEventButton::MiddleButton => MouseButton::Middle, 451 | _others => return, 452 | }; 453 | self.as_mut() 454 | .rust_mut() 455 | .events 456 | .push(EmbedderEvent::MouseWindowEventClass( 457 | MouseWindowEvent::MouseUp(button, position), 458 | )); 459 | 460 | if let Some(press_position) = self.as_mut().rust_mut().press_position.take() { 461 | // If the press position is close to the release then assume a click 462 | let diff = press_position - event_position; 463 | let dist = (diff.x().powf(2.0) + diff.y().powf(2.0)).sqrt(); 464 | let dpi_factor = 1.0; // TODO: read DPI settings 465 | if dist < 10.0 * dpi_factor { 466 | self.as_mut() 467 | .rust_mut() 468 | .events 469 | .push(EmbedderEvent::MouseWindowEventClass( 470 | MouseWindowEvent::Click(button, position), 471 | )); 472 | } 473 | } 474 | 475 | // Ensure we have focus so that we receive key events 476 | self.as_mut() 477 | .force_active_focus(FocusReason::MouseFocusReason); 478 | 479 | self.as_mut().update(); 480 | } 481 | } 482 | 483 | fn touch_event(mut self: Pin<&mut Self>, event: *mut qobject::QTouchEvent) { 484 | if let Some(event) = unsafe { event.as_mut() } { 485 | let mut event = unsafe { Pin::new_unchecked(event) }; 486 | let points = event.as_ref().point_count(); 487 | if points == 0 { 488 | // Empty points in Qt means that touch events have been cancelled 489 | // emulate cancelling 4 ids as we have no id here 490 | for id in 0..3 { 491 | self.as_mut().rust_mut().events.push(EmbedderEvent::Touch( 492 | TouchEventType::Cancel, 493 | TouchId(id), 494 | Point2D::new(0 as f32, 0 as f32), 495 | )); 496 | } 497 | } else { 498 | for i in 0..points { 499 | let point = event.as_mut().point(i); 500 | let position = point.position(); 501 | let position = Point2D::new(position.x() as f32, position.y() as f32); 502 | let phase = match point.state() { 503 | QEventPointState::Unknown | QEventPointState::Stationary => continue, 504 | QEventPointState::Pressed => TouchEventType::Down, 505 | QEventPointState::Updated => TouchEventType::Move, 506 | QEventPointState::Released => TouchEventType::Up, 507 | _others => continue, 508 | }; 509 | self.as_mut().rust_mut().events.push(EmbedderEvent::Touch( 510 | phase, 511 | TouchId(point.id()), 512 | position, 513 | )); 514 | } 515 | } 516 | 517 | // Ensure we have focus so that we receive key events 518 | self.as_mut() 519 | .force_active_focus(FocusReason::MouseFocusReason); 520 | 521 | self.as_mut().update(); 522 | } 523 | } 524 | 525 | fn wheel_event(mut self: Pin<&mut Self>, event: *mut qobject::QWheelEvent) { 526 | if let Some(event) = unsafe { event.as_ref() } { 527 | // TODO: consider angle_delta 528 | // https://doc.qt.io/qt-6/qwheelevent.html#angleDelta 529 | let pixel_delta = event.pixel_delta(); 530 | let position = event.position(); 531 | 532 | self.as_mut().rust_mut().events.push(EmbedderEvent::Wheel( 533 | servo::script_traits::WheelDelta { 534 | x: pixel_delta.x() as f64, 535 | y: pixel_delta.y() as f64, 536 | z: 0.0, 537 | mode: servo::script_traits::WheelMode::DeltaPixel, 538 | }, 539 | euclid::Point2D::new(position.x() as f32, position.y() as f32), 540 | )); 541 | 542 | // Scroll events snap to the major axis of movement, with vertical 543 | // preferred over horizontal. 544 | let (dx, dy) = if pixel_delta.y().abs() > pixel_delta.x().abs() { 545 | (0.0, pixel_delta.y() as f32) 546 | } else { 547 | (pixel_delta.x() as f32, 0.0) 548 | }; 549 | 550 | let scroll_location = 551 | servo::webrender_api::ScrollLocation::Delta(euclid::Vector2D::new(dx, dy)); 552 | 553 | self.as_mut().rust_mut().events.push( 554 | servo::compositing::windowing::EmbedderEvent::Scroll( 555 | scroll_location, 556 | euclid::Point2D::new(position.x() as i32, position.y() as i32), 557 | // TODO: consider scrolling phase 558 | // https://doc.qt.io/qt-6/qwheelevent.html#phase 559 | servo::script_traits::TouchEventType::Move, 560 | ), 561 | ); 562 | 563 | self.as_mut().update(); 564 | } 565 | } 566 | } 567 | 568 | impl cxx_qt::Initialize for qobject::ServoWebView { 569 | fn initialize(mut self: Pin<&mut Self>) { 570 | // TODO: we don't support QFlags so just enable everything for now 571 | self.as_mut() 572 | .set_accepted_mouse_buttons(QMouseEventButton::AllButtons); 573 | 574 | self.as_mut().set_accept_touch_events(true); 575 | self.as_mut().set_mirror_vertically(true); 576 | 577 | // When the URL changes trigger QQuickFramebufferObject::update 578 | // which then triggers QQuickFramebufferObject::Renderer::synchronize 579 | self.on_url_changed(|qobject| { 580 | qobject.update(); 581 | }) 582 | .release(); 583 | } 584 | } 585 | -------------------------------------------------------------------------------- /src/windowheadless.rs: -------------------------------------------------------------------------------- 1 | // SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company 2 | // SPDX-FileContributor: Andrew Hayzen 3 | // 4 | // SPDX-License-Identifier: MPL-2.0 5 | 6 | use std::cell::Cell; 7 | 8 | use servo::{ 9 | compositing::windowing::{AnimationState, EmbedderCoordinates, WindowMethods}, 10 | euclid::{Point2D, Rect, Scale, Size2D}, 11 | rendering_context::RenderingContext, 12 | servo_geometry::DeviceIndependentPixel, 13 | }; 14 | use surfman::{Connection, SurfaceType}; 15 | 16 | pub(crate) struct QServoWindowHeadless { 17 | animation_state: Cell, 18 | rendering_context: RenderingContext, 19 | } 20 | 21 | impl QServoWindowHeadless { 22 | pub fn new(size: Size2D, connection: Connection) -> Self { 23 | // Initialize surfman 24 | let adapter = connection 25 | .create_software_adapter() 26 | .expect("Failed to create adapter"); 27 | let size = size.to_untyped().to_i32(); 28 | let surface_type = SurfaceType::Generic { size }; 29 | let rendering_context = RenderingContext::create(&connection, &adapter, surface_type) 30 | .expect("Failed to create WR surfman"); 31 | 32 | Self { 33 | rendering_context, 34 | animation_state: Cell::new(AnimationState::Idle), 35 | } 36 | } 37 | } 38 | 39 | impl WindowMethods for QServoWindowHeadless { 40 | fn get_coordinates(&self) -> EmbedderCoordinates { 41 | let size = self 42 | .rendering_context 43 | .context_surface_info() 44 | .unwrap_or(None) 45 | .map(|info| Size2D::from_untyped(info.size)) 46 | .unwrap_or(Size2D::new(0, 0)); 47 | let origin = Point2D::origin(); 48 | EmbedderCoordinates { 49 | hidpi_factor: Scale::new(1.0), 50 | screen: size, 51 | screen_avail: size, 52 | window: (size, origin), 53 | framebuffer: size, 54 | viewport: Rect::new(origin, size), 55 | } 56 | } 57 | 58 | fn set_animation_state(&self, state: AnimationState) { 59 | self.animation_state.set(state); 60 | } 61 | 62 | fn rendering_context(&self) -> RenderingContext { 63 | self.rendering_context.clone() 64 | } 65 | } 66 | --------------------------------------------------------------------------------