Skip to content

fix(example): upgrades window customization example from deprecated cocoa to objc2-app-kit #3406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions src/content/docs/learn/window-customization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ Remove the main window from the `tauri.conf.json` file:
}
```

Add `cocoa` crate to dependencies so that we can use it to call the macOS native API:
Add `objc2-app-kit` crate to dependencies so that we can use it to call the macOS native API:

```toml title="src-tauri/Cargo.toml"
[target."cfg(target_os = \"macos\")".dependencies]
cocoa = "0.26"
objc2-app-kit = "0.3.1"
```

Create the main window and change its background color:
Expand All @@ -270,20 +270,19 @@ Create the main window and change its background color:
// set background color only when building for macOS
#[cfg(target_os = "macos")]
{
use cocoa::appkit::{NSColor, NSWindow};
use cocoa::base::{id, nil};

let ns_window = window.ns_window().unwrap() as id;
unsafe {
let bg_color = NSColor::colorWithRed_green_blue_alpha_(
nil,
50.0 / 255.0,
158.0 / 255.0,
163.5 / 255.0,
1.0,
);
ns_window.setBackgroundColor_(bg_color);
}
use objc2_app_kit::{NSColor, NSWindow};

unsafe {
let ns_window: &NSWindow = &*window.ns_window().unwrap().cast();

let bg_color = NSColor::colorWithRed_green_blue_alpha(
50.0 / 255.0,
158.0 / 255.0,
163.5 / 255.0,
1.0,
);
ns_window.setBackgroundColor(Some(&bg_color));
}
}

Ok(())
Expand Down