More macOS fixes, introduce RUST code

This commit is contained in:
PJB3005
2025-12-27 01:44:57 +01:00
parent 602d7833a1
commit a1dddf6af1
13 changed files with 529 additions and 2 deletions

View File

@@ -54,6 +54,7 @@ namespace Robust.Client.WebView.Cef
protected override void OnRegisterCustomSchemes(CefSchemeRegistrar registrar)
{
// NOTE: KEEP IN SYNC WITH RUST CODE!
registrar.AddCustomScheme("res", CefSchemeOptions.Secure | CefSchemeOptions.Standard);
registrar.AddCustomScheme("usr", CefSchemeOptions.Secure | CefSchemeOptions.Standard);
}

View File

@@ -45,6 +45,7 @@ namespace Robust.Client.WebView.Cef
_localization.GetString("cmd-flushcookies-help"),
(_, _, _) => CefCookieManager.GetGlobal(null).FlushStore(null));
#if !MACOS
string subProcessName;
if (OperatingSystem.IsWindows())
subProcessName = "Robust.Client.WebView.exe";
@@ -53,7 +54,6 @@ namespace Robust.Client.WebView.Cef
else
throw new NotSupportedException("Unsupported platform for CEF!");
#if !MACOS
var subProcessPath = Path.Combine(BasePath, subProcessName);
var cefResourcesPath = LocateCefResources();
_sawmill.Debug($"Subprocess path: {subProcessPath}, resources: {cefResourcesPath}");
@@ -80,12 +80,15 @@ namespace Robust.Client.WebView.Cef
return 0;
});
// Needed to implement CefAppProtocol on our NSApplication.
NativeLibrary.Load("robust_native_webview", typeof(WebViewManagerCef).Assembly, null);
#endif
var settings = new CefSettings()
{
WindowlessRenderingEnabled = true, // So we can render to our UI controls.
ExternalMessagePump = false, // Unsure, honestly. TODO CEF: Research this?
ExternalMessagePump = true,
NoSandbox = true, // Not disabling the sandbox crashes CEF.
#if !MACOS
BrowserSubprocessPath = subProcessPath,

1
native/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
target/

357
native/Cargo.lock generated Normal file
View File

@@ -0,0 +1,357 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anyhow"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
[[package]]
name = "bitflags"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
[[package]]
name = "block2"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5"
dependencies = [
"objc2",
]
[[package]]
name = "cc"
version = "1.2.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35900b6c8d709fb1d854671ae27aeaa9eec2f8b01b364e1619a40da3e6fe2afe"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cef"
version = "141.6.0+141.0.11"
dependencies = [
"cef-dll-sys",
"libloading",
"objc2",
"windows-sys",
]
[[package]]
name = "cef-dll-sys"
version = "141.6.0+141.0.11"
dependencies = [
"anyhow",
"cmake",
"download-cef",
]
[[package]]
name = "cef-helper"
version = "0.1.0"
dependencies = [
"cef",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "cmake"
version = "0.1.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7caa3f9de89ddbe2c607f4101924c5abec803763ae9534e4f4d7d8f84aa81f0"
dependencies = [
"cc",
]
[[package]]
name = "dispatch2"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89a09f22a6c6069a18470eb92d2298acf25463f14256d24778e1230d789a2aec"
dependencies = [
"bitflags",
"objc2",
]
[[package]]
name = "download-cef"
version = "2.2.0"
dependencies = [
"thiserror",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52051878f80a721bb68ebfbc930e07b65ba72f2da88968ea5c06fd6ca3d3a127"
[[package]]
name = "libc"
version = "0.2.177"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
[[package]]
name = "libloading"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
dependencies = [
"cfg-if",
"windows-link",
]
[[package]]
name = "objc2"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7c2599ce0ec54857b29ce62166b0ed9b4f6f1a70ccc9a71165b6154caca8c05"
dependencies = [
"objc2-encode",
]
[[package]]
name = "objc2-app-kit"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d49e936b501e5c5bf01fda3a9452ff86dc3ea98ad5f283e1455153142d97518c"
dependencies = [
"bitflags",
"block2",
"libc",
"objc2",
"objc2-cloud-kit",
"objc2-core-data",
"objc2-core-foundation",
"objc2-core-graphics",
"objc2-core-image",
"objc2-core-text",
"objc2-core-video",
"objc2-foundation",
"objc2-quartz-core",
]
[[package]]
name = "objc2-cloud-kit"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73ad74d880bb43877038da939b7427bba67e9dd42004a18b809ba7d87cee241c"
dependencies = [
"bitflags",
"objc2",
"objc2-foundation",
]
[[package]]
name = "objc2-core-data"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b402a653efbb5e82ce4df10683b6b28027616a2715e90009947d50b8dd298fa"
dependencies = [
"bitflags",
"objc2",
"objc2-foundation",
]
[[package]]
name = "objc2-core-foundation"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536"
dependencies = [
"bitflags",
"dispatch2",
"objc2",
]
[[package]]
name = "objc2-core-graphics"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e022c9d066895efa1345f8e33e584b9f958da2fd4cd116792e15e07e4720a807"
dependencies = [
"bitflags",
"dispatch2",
"objc2",
"objc2-core-foundation",
"objc2-io-surface",
]
[[package]]
name = "objc2-core-image"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5d563b38d2b97209f8e861173de434bd0214cf020e3423a52624cd1d989f006"
dependencies = [
"objc2",
"objc2-foundation",
]
[[package]]
name = "objc2-core-text"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0cde0dfb48d25d2b4862161a4d5fcc0e3c24367869ad306b0c9ec0073bfed92d"
dependencies = [
"bitflags",
"objc2",
"objc2-core-foundation",
"objc2-core-graphics",
]
[[package]]
name = "objc2-core-video"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d425caf1df73233f29fd8a5c3e5edbc30d2d4307870f802d18f00d83dc5141a6"
dependencies = [
"bitflags",
"objc2",
"objc2-core-foundation",
"objc2-core-graphics",
"objc2-io-surface",
]
[[package]]
name = "objc2-encode"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33"
[[package]]
name = "objc2-foundation"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3e0adef53c21f888deb4fa59fc59f7eb17404926ee8a6f59f5df0fd7f9f3272"
dependencies = [
"bitflags",
"objc2",
"objc2-core-foundation",
]
[[package]]
name = "objc2-io-surface"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "180788110936d59bab6bd83b6060ffdfffb3b922ba1396b312ae795e1de9d81d"
dependencies = [
"bitflags",
"objc2",
"objc2-core-foundation",
]
[[package]]
name = "objc2-quartz-core"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96c1358452b371bf9f104e21ec536d37a650eb10f7ee379fff67d2e08d537f1f"
dependencies = [
"bitflags",
"objc2",
"objc2-foundation",
]
[[package]]
name = "proc-macro2"
version = "1.0.103"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
dependencies = [
"proc-macro2",
]
[[package]]
name = "robust-apphost"
version = "0.1.0"
dependencies = [
"cef",
]
[[package]]
name = "robust-native-webview"
version = "0.1.0"
dependencies = [
"cc",
"cef",
"objc2",
"objc2-app-kit",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "syn"
version = "2.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f17c7e013e88258aa9543dcbe81aca68a667a9ac37cd69c9fbc07858bfe0e2f"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "2.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "2.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]

9
native/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[workspace]
resolver = "3"
members = ["cef-helper", "robust-apphost", "robust-native-webview"]
[workspace.dependencies]
cef = { path = "../../../cef-rs/cef", default-features = false }
objc2 = "0.6.3"
objc2-app-kit = "0.3.2"
cc = "1.0"

View File

@@ -0,0 +1,7 @@
[package]
name = "cef-helper"
version = "0.1.0"
edition = "2024"
[dependencies]
cef = { workspace = true, default-features = false }

View File

@@ -0,0 +1,41 @@
use std::ptr;
use cef::{App, ImplApp, ImplSchemeRegistrar, SchemeRegistrar, WrapApp, rc::Rc, wrap_app};
fn main() {
#[cfg(target_os = "macos")]
let _loader = {
let loader =
cef::library_loader::LibraryLoader::new(&std::env::current_exe().unwrap(), true);
assert!(loader.load());
loader
};
cef::api_hash(cef::sys::CEF_API_VERSION_14100, 0);
let args = cef::args::Args::new();
let main_args = args.as_main_args();
let mut app = DemoApp::new();
let ret = cef::execute_process(Some(main_args), Some(&mut app), ptr::null_mut());
std::process::exit(ret)
}
// CefSchemeOptions
const SCHEME_STANDARD: i32 = 1 << 0;
const SCHEME_SECURE: i32 = 1 << 3;
wrap_app! {
struct DemoApp {
}
impl App {
fn on_register_custom_schemes(&self, registrar: Option<&mut SchemeRegistrar>) {
let registrar = registrar.unwrap();
// NOTE: KEEP IN SYNC WITH C# CODE!
registrar.add_custom_scheme(Some(&"usr".into()), SCHEME_SECURE | SCHEME_STANDARD);
registrar.add_custom_scheme(Some(&"res".into()), SCHEME_SECURE | SCHEME_STANDARD);
}
}
}

View File

@@ -0,0 +1,10 @@
[package]
name = "robust-apphost"
version = "0.1.0"
edition = "2024"
[features]
cef_sandbox = ["dep:cef"]
[dependencies]
cef = { workspace = true, default-features = false, features = ["sandbox"], optional = true }

View File

@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@@ -0,0 +1,15 @@
[package]
name = "robust-native-webview"
version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["cdylib"]
[dependencies]
cef = { workspace = true, default-features = false }
objc2.workspace = true
objc2-app-kit.workspace = true
[build-dependencies]
cc = { workspace = true }

View File

@@ -0,0 +1,18 @@
use std::{env, path::PathBuf};
fn main() {
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or("".into());
let cef_path = PathBuf::from(env::var_os("CEF_PATH").expect("CEF_PATH is not set"));
if target_os == "macos" {
cc::Build::new()
.include(cef_path)
.cpp(true)
.flag("--std=c++17")
.file("src/mac_application.mm")
.compile("mac_application");
println!("cargo::rerun-if-changed=src/mac_application.mm");
println!("cargo::rustc-link-lib=framework=Cocoa");
}
}

View File

@@ -0,0 +1,10 @@
#[cfg(target_os = "macos")]
unsafe extern "C" {
fn get_swizzled_idiot();
}
// Not actually called but I need a chain of reference so that the objc code is compiled in.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rt_native_webview_init() {
unsafe { get_swizzled_idiot() };
}

View File

@@ -0,0 +1,52 @@
#import <Cocoa/Cocoa.h>
#include <objc/runtime.h>
#include <cstdio>
#include "include/cef_application_mac.h"
// "inspired" by https://github.com/chromiumembedded/java-cef/blob/2caef5a994d85e86de670b35cf77950bcedba7bb/native/util_mac.mm
// And also just what I could find on the internet.
// This is global. There can only ever be one NSApplication anyways.
bool g_handling_send_event = false;
@interface NSApplication (RobustApplication) <CefAppProtocol>
- (BOOL)isHandlingSendEvent;
- (void)setHandlingSendEvent:(BOOL)handlingSendEvent;
- (void)_swizzled_sendEvent:(NSEvent*)event;
@end
@implementation NSApplication (RobustApplication)
+ (void)load {
Method original = class_getInstanceMethod(self, @selector(sendEvent));
Method swizzled = class_getInstanceMethod(self, @selector(_swizzled_sendEvent));
method_exchangeImplementations(original, swizzled);
}
- (BOOL)isHandlingSendEvent {
return g_handling_send_event;
}
- (void)setHandlingSendEvent:(BOOL)handlingSendEvent {
printf("setHandlingSendEvent: %i\n", (int) handlingSendEvent);
g_handling_send_event = handlingSendEvent;
}
- (void)_swizzled_sendEvent:(NSEvent*)event {
printf("send it");
CefScopedSendingEvent sendingEventScoper;
[self _swizzled_sendEvent:event];
}
@end
extern "C" {
void get_swizzled_idiot() {
NSApplication* app = [NSApplication sharedApplication];
[app isHandlingSendEvent];
}
}