Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/warpui_extras/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license.workspace = true
default = ["secure_storage", "user_preferences", "user_preferences-file"]
secure_storage = ["dep:security-framework", "dep:secret-service", "dep:ouroboros"]
test-util = []
user_preferences = ["dep:cocoa", "dep:objc", "dep:gloo-storage"]
user_preferences = ["dep:cocoa", "dep:objc", "dep:gloo-storage", "dep:core-foundation"]
user_preferences-file = ["user_preferences", "dep:serde", "dep:serde_json"]
user_preferences-toml = ["user_preferences", "dep:serde", "dep:serde_json", "dep:toml_edit"]

Expand All @@ -32,6 +32,7 @@ tempfile.workspace = true

[target.'cfg(target_os = "macos")'.dependencies]
cocoa = { workspace = true, optional = true }
core-foundation = { workspace = true, optional = true }
objc = { workspace = true, optional = true }
security-framework = { version = "2.0.0", optional = true }

Expand Down
18 changes: 18 additions & 0 deletions crates/warpui_extras/src/user_preferences/user_defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#![allow(deprecated)]

use cocoa::base::{id, nil};
use core_foundation::base::{CFGetTypeID, CFTypeRef, TCFType};
use core_foundation::boolean::CFBoolean;
use objc::{class, msg_send, rc::StrongPtr, sel, sel_impl};

/// A user preferences store backed by macOS user defaults (`NSUserDefaults`).
Expand Down Expand Up @@ -61,6 +63,22 @@ impl super::UserPreferences for UserDefaultsPreferencesStorage {
fn read_value(&self, key: &str) -> Result<Option<String>, super::Error> {
unsafe {
let key = util::make_nsstring(key);

// Check for NSNumber-typed booleans before calling stringForKey:. When a value
// is written via `defaults write -bool false`, it is stored as NSNumber(BOOL).
// stringForKey: coerces that to "0"/"1", which serde_json cannot parse as bool
// and silently falls back to the setting's default. Return canonical JSON instead.
//
// BOOL is toll-free bridged with the kCFBooleanTrue/kCFBooleanFalse singletons,
// so CFGetTypeID is the only safe way to discriminate a real BOOL from a
// char-valued NSNumber (which shares ObjC encoding "c" on x86_64). Char-valued
// and other numeric NSNumbers carry CFNumber's type ID and fall through.
let raw: id = msg_send![*self.user_defaults, objectForKey: *key];
if raw != nil && CFGetTypeID(raw as CFTypeRef) == CFBoolean::type_id() {
let b: bool = msg_send![raw, boolValue];
return Ok(Some(b.to_string()));
}

let value: id = msg_send![*self.user_defaults, stringForKey: *key];
if value != nil {
Ok(Some(
Expand Down