Cleaning up
This commit is contained in:
parent
f4ca9770e1
commit
8c39ea6f94
|
@ -38,7 +38,7 @@ use objc::{class, msg_send, sel, sel_impl};
|
||||||
use objc::runtime::Object;
|
use objc::runtime::Object;
|
||||||
use objc_id::Id;
|
use objc_id::Id;
|
||||||
|
|
||||||
use crate::foundation::{id, nil, YES, BOOL, NSData, NSInteger, NSString, NSDictionary, NSNumber};
|
use crate::foundation::{id, nil, YES, NO, BOOL, NSData, NSString, NSDictionary, NSNumber};
|
||||||
|
|
||||||
mod value;
|
mod value;
|
||||||
pub use value::Value;
|
pub use value::Value;
|
||||||
|
@ -213,4 +213,49 @@ impl UserDefaults {
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a boolean value if the object stored for the specified key is managed by an
|
||||||
|
/// administrator. This is rarely used - mostly in managed environments, e.g a classroom.
|
||||||
|
///
|
||||||
|
/// For managed keys, the application should disable any user interface that allows the
|
||||||
|
/// user to modify the value of key.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use cacao::defaults::{UserDefaults, Value};
|
||||||
|
///
|
||||||
|
/// let mut defaults = UserDefaults::standard();
|
||||||
|
/// defaults.insert("test", Value::string("value"));
|
||||||
|
///
|
||||||
|
/// let value = defaults.is_forced_for_key("test");
|
||||||
|
/// assert_eq!(value, false);
|
||||||
|
/// ```
|
||||||
|
pub fn is_forced_for_key<K: AsRef<str>>(&self, key: K) -> bool {
|
||||||
|
let result: BOOL = unsafe {
|
||||||
|
let key = NSString::new(key.as_ref());
|
||||||
|
msg_send![&*self.0, objectIsForcedForKey:key.into_inner()]
|
||||||
|
};
|
||||||
|
|
||||||
|
match result {
|
||||||
|
YES => true,
|
||||||
|
NO => false,
|
||||||
|
_ => unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Blocks for any asynchronous updates to the defaults database and returns.
|
||||||
|
///
|
||||||
|
/// This method is legacy, likely unnecessary and shouldn't be used unless you know exactly why
|
||||||
|
/// you need it... and even then, you should double check it.
|
||||||
|
/// ```rust
|
||||||
|
/// use cacao::defaults::{UserDefaults, Value};
|
||||||
|
///
|
||||||
|
/// let mut defaults = UserDefaults::standard();
|
||||||
|
/// defaults.insert("test", Value::string("value"));
|
||||||
|
/// defaults.synchronize();
|
||||||
|
/// ```
|
||||||
|
pub fn synchronize(&self) {
|
||||||
|
unsafe {
|
||||||
|
let _: () = msg_send![&*self.0, synchronize];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use objc::{class, msg_send, sel, sel_impl};
|
use crate::foundation::{id, NSData, NSDictionary, NSString, NSNumber};
|
||||||
use objc_id::Id;
|
|
||||||
|
|
||||||
use crate::foundation::{id, YES, NO, NSData, NSInteger, NSDictionary, NSString};
|
|
||||||
|
|
||||||
/// Represents a Value that can be stored or queried with `UserDefaults`.
|
/// Represents a Value that can be stored or queried with `UserDefaults`.
|
||||||
///
|
///
|
||||||
|
@ -138,21 +135,15 @@ impl From<Value> for id {
|
||||||
// These currently work, but may not be exhaustive and should be looked over past the preview
|
// These currently work, but may not be exhaustive and should be looked over past the preview
|
||||||
// period.
|
// period.
|
||||||
fn from(value: Value) -> Self {
|
fn from(value: Value) -> Self {
|
||||||
unsafe {
|
|
||||||
match value {
|
match value {
|
||||||
Value::Bool(b) => msg_send![class!(NSNumber), numberWithBool:match b {
|
Value::Bool(b) => NSNumber::bool(b).into_inner(),
|
||||||
true => YES,
|
|
||||||
false => NO
|
|
||||||
}],
|
|
||||||
|
|
||||||
Value::String(s) => NSString::new(&s).into_inner(),
|
Value::String(s) => NSString::new(&s).into_inner(),
|
||||||
Value::Float(f) => msg_send![class!(NSNumber), numberWithDouble:f],
|
Value::Float(f) => NSNumber::float(f).into_inner(),
|
||||||
Value::Integer(i) => msg_send![class!(NSNumber), numberWithInteger:i as NSInteger],
|
Value::Integer(i) => NSNumber::integer(i).into_inner(),
|
||||||
Value::Data(data) => NSData::new(data).into_inner()
|
Value::Data(data) => NSData::new(data).into_inner()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<K> From<HashMap<K, Value>> for NSDictionary
|
impl<K> From<HashMap<K, Value>> for NSDictionary
|
||||||
where
|
where
|
||||||
|
@ -160,16 +151,13 @@ where
|
||||||
{
|
{
|
||||||
/// Translates a `HashMap` of `Value`s into an `NSDictionary`.
|
/// Translates a `HashMap` of `Value`s into an `NSDictionary`.
|
||||||
fn from(map: HashMap<K, Value>) -> Self {
|
fn from(map: HashMap<K, Value>) -> Self {
|
||||||
NSDictionary(unsafe {
|
let mut dictionary = NSDictionary::new();
|
||||||
let dictionary: id = msg_send![class!(NSMutableDictionary), new];
|
|
||||||
|
|
||||||
for (key, value) in map.into_iter() {
|
for (key, value) in map.into_iter() {
|
||||||
let k = NSString::new(key.as_ref());
|
let k = NSString::new(key.as_ref());
|
||||||
let v: id = value.into();
|
dictionary.insert(k, value.into());
|
||||||
let _: () = msg_send![dictionary, setObject:v forKey:k];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Id::from_ptr(dictionary)
|
dictionary
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
//! A wrapper type for `NSArray`. This is abstracted out as we need to use `NSArray` in a ton of
|
//! A wrapper type for `NSArray`.
|
||||||
|
//!
|
||||||
|
//! This is abstracted out as we need to use `NSArray` in a ton of
|
||||||
//! instances in this framework, and down the road I'd like to investigate using `CFArray` instead
|
//! instances in this framework, and down the road I'd like to investigate using `CFArray` instead
|
||||||
//! of `NSArray` (i.e, if the ObjC runtime is ever pulled or something - perhaps those types would
|
//! of `NSArray` (i.e, if the ObjC runtime is ever pulled or something - perhaps those types would
|
||||||
//! stick around).
|
//! stick around).
|
||||||
|
|
|
@ -5,7 +5,7 @@ use objc::{class, msg_send, sel, sel_impl};
|
||||||
use objc::runtime::Object;
|
use objc::runtime::Object;
|
||||||
use objc_id::Id;
|
use objc_id::Id;
|
||||||
|
|
||||||
use crate::foundation::id;
|
use crate::foundation::{id, NSString};
|
||||||
|
|
||||||
/// A wrapper for `NSDictionary`. Behind the scenes we actually wrap `NSMutableDictionary`, and
|
/// A wrapper for `NSDictionary`. Behind the scenes we actually wrap `NSMutableDictionary`, and
|
||||||
/// rely on Rust doing the usual borrow-checking guards that it does so well.
|
/// rely on Rust doing the usual borrow-checking guards that it does so well.
|
||||||
|
@ -25,6 +25,12 @@ impl NSDictionary {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn insert(&mut self, key: NSString, object: id) {
|
||||||
|
unsafe {
|
||||||
|
let _: () = msg_send![&*self.0, setObject:object forKey:key.into_inner()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn into_inner(mut self) -> id {
|
pub fn into_inner(mut self) -> id {
|
||||||
&mut *self.0
|
&mut *self.0
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,8 +21,7 @@ impl NSNumber {
|
||||||
NSNumber(unsafe {
|
NSNumber(unsafe {
|
||||||
Id::from_ptr(msg_send![class!(NSNumber), numberWithBool:match value {
|
Id::from_ptr(msg_send![class!(NSNumber), numberWithBool:match value {
|
||||||
true => YES,
|
true => YES,
|
||||||
false => NO,
|
false => NO
|
||||||
_ => unreachable!()
|
|
||||||
}])
|
}])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,6 @@ pub use url;
|
||||||
#[cfg(feature = "macos")]
|
#[cfg(feature = "macos")]
|
||||||
pub mod macos;
|
pub mod macos;
|
||||||
|
|
||||||
pub mod alert;
|
|
||||||
pub mod button;
|
pub mod button;
|
||||||
|
|
||||||
#[cfg(feature = "cloudkit")]
|
#[cfg(feature = "cloudkit")]
|
||||||
|
|
|
@ -1,5 +1,28 @@
|
||||||
//! A wrapper for `NSAlert`. Currently doesn't cover everything possible for this class, as it was
|
//! A wrapper for `NSAlert`.
|
||||||
//! built primarily for debugging uses. Feel free to extend via pull requests or something.
|
//!
|
||||||
|
//! This is housed inside `macos` as it's a useful tool for a few cases, but it doesn't match the
|
||||||
|
//! iOS API, so we make no guarantees about it being a universal control. In general this also
|
||||||
|
//! doesn't produce an amazing user experience, and you may want to shy away from using it.
|
||||||
|
//!
|
||||||
|
//! If you want to show a complex view in an alert-esque fashion, you may consider looking at
|
||||||
|
//! `Sheet`.
|
||||||
|
//!
|
||||||
|
//! ```rust
|
||||||
|
//! use cacao::macos::{App, AppDelegate, Alert};
|
||||||
|
//!
|
||||||
|
//! #[derive(Default)]
|
||||||
|
//! struct ExampleApp;
|
||||||
|
//!
|
||||||
|
//! impl AppDelegate {
|
||||||
|
//! fn did_finish_launching(&self) {
|
||||||
|
//!
|
||||||
|
//! }
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn main() {
|
||||||
|
//! App::new("com.alert.example", ExampleApp::default()).run()
|
||||||
|
//! }
|
||||||
|
//! ```
|
||||||
|
|
||||||
use objc_id::Id;
|
use objc_id::Id;
|
||||||
use objc::runtime::Object;
|
use objc::runtime::Object;
|
|
@ -15,7 +15,12 @@
|
||||||
//! of your app as a cross platform codebase, with the initial 10% being scaffolding code for the
|
//! of your app as a cross platform codebase, with the initial 10% being scaffolding code for the
|
||||||
//! platform (e.g, NSApplication vs UIApplication lifecycle).
|
//! platform (e.g, NSApplication vs UIApplication lifecycle).
|
||||||
|
|
||||||
pub mod app;
|
mod alert;
|
||||||
|
pub use alert::Alert;
|
||||||
|
|
||||||
|
mod app;
|
||||||
|
pub use app::*;
|
||||||
|
|
||||||
pub mod menu;
|
pub mod menu;
|
||||||
pub mod printing;
|
pub mod printing;
|
||||||
pub mod toolbar;
|
pub mod toolbar;
|
||||||
|
|
Loading…
Reference in a new issue