Skip to content

Format #495

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

Closed
wants to merge 1 commit into from
Closed
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
710 changes: 490 additions & 220 deletions cocoa-foundation/src/foundation.rs

Large diffs are not rendered by default.

49 changes: 27 additions & 22 deletions cocoa-foundation/tests/foundation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ extern crate cocoa_foundation;
#[cfg(test)]
mod foundation {
mod nsstring {
use cocoa_foundation::foundation::NSString;
use cocoa_foundation::base::nil;
use cocoa_foundation::foundation::NSString;
use std::slice;
use std::str;

Expand All @@ -17,8 +17,8 @@ mod foundation {
unsafe {
let built = NSString::alloc(nil).init_str(expected);
let bytes = built.UTF8String() as *const u8;
let objc_string = str::from_utf8(slice::from_raw_parts(bytes, built.len()))
.unwrap();
let objc_string =
str::from_utf8(slice::from_raw_parts(bytes, built.len())).unwrap();
assert_eq!(objc_string.len(), expected.len());
assert_eq!(objc_string, expected);
}
Expand All @@ -30,8 +30,8 @@ mod foundation {
unsafe {
let built = NSString::alloc(nil).init_str(expected);
let bytes = built.UTF8String() as *const u8;
let objc_string = str::from_utf8(slice::from_raw_parts(bytes, built.len()))
.unwrap();
let objc_string =
str::from_utf8(slice::from_raw_parts(bytes, built.len())).unwrap();
assert_eq!(objc_string.len(), expected.len());
assert_eq!(objc_string, expected);
}
Expand All @@ -56,18 +56,18 @@ mod foundation {
let built_to_append = NSString::alloc(nil).init_str(to_append);
let append_string = built.stringByAppendingString_(built_to_append);
let bytes = append_string.UTF8String() as *const u8;
let objc_string = str::from_utf8(slice::from_raw_parts(bytes, append_string.len()))
.unwrap();
let objc_string =
str::from_utf8(slice::from_raw_parts(bytes, append_string.len())).unwrap();
assert_eq!(objc_string, expected);
}
}
}

mod nsfastenumeration {
use std::str;
use std::slice;
use cocoa_foundation::foundation::{NSString, NSFastEnumeration};
use cocoa_foundation::base::{id, nil};
use cocoa_foundation::foundation::{NSFastEnumeration, NSString};
use std::slice;
use std::str;

#[test]
fn test_iter() {
Expand All @@ -76,7 +76,8 @@ mod foundation {
let separator = NSString::alloc(nil).init_str(" ");
let components: id = msg_send![string, componentsSeparatedByString: separator];

let combined = components.iter()
let combined = components
.iter()
.map(|s| {
let bytes = s.UTF8String() as *const u8;
str::from_utf8(slice::from_raw_parts(bytes, s.len())).unwrap()
Expand Down Expand Up @@ -108,9 +109,10 @@ mod foundation {

mod nsdictionary {
use block::ConcreteBlock;
use cocoa_foundation::foundation::{NSArray, NSComparisonResult, NSDictionary, NSFastEnumeration,
NSString};
use cocoa_foundation::base::{id, nil};
use cocoa_foundation::foundation::{
NSArray, NSComparisonResult, NSDictionary, NSFastEnumeration, NSString,
};

#[test]
fn test_get() {
Expand All @@ -132,8 +134,8 @@ mod foundation {
let keys = vec!["a", "b", "c", "d", "e", "f"];
let objects = vec!["1", "2", "3", "4", "5", "6"];
unsafe {
use std::{slice, str};
use std::cmp::{Ord, Ordering};
use std::{slice, str};

let keys_raw_vec = keys.clone().into_iter().map(&mkstr).collect::<Vec<_>>();
let objs_raw_vec = objects.clone().into_iter().map(&mkstr).collect::<Vec<_>>();
Expand All @@ -152,26 +154,29 @@ mod foundation {
unsafe {
let (bytes0, len0) = (s0.UTF8String() as *const u8, s0.len());
let (bytes1, len1) = (s1.UTF8String() as *const u8, s1.len());
let (s0, s1) = (str::from_utf8(slice::from_raw_parts(bytes0, len0)).unwrap(),
str::from_utf8(slice::from_raw_parts(bytes1, len1)).unwrap());
let (s0, s1) = (
str::from_utf8(slice::from_raw_parts(bytes0, len0)).unwrap(),
str::from_utf8(slice::from_raw_parts(bytes1, len1)).unwrap(),
);
let (c0, c1) = (s0.chars().next().unwrap(), s1.chars().next().unwrap());
c0.cmp(&c1)
}
}

// First test cocoa sorting...
let mut comparator = ConcreteBlock::new(|s0: id, s1: id| {
match compare_function(&s0, &s1) {
let mut comparator =
ConcreteBlock::new(|s0: id, s1: id| match compare_function(&s0, &s1) {
Ordering::Less => NSComparisonResult::NSOrderedAscending,
Ordering::Equal => NSComparisonResult::NSOrderedSame,
Ordering::Greater => NSComparisonResult::NSOrderedDescending,
}
});
});

let associated_iter = keys.iter().zip(objects.iter());
for (k_id, (k, v)) in dict.keysSortedByValueUsingComparator_(&mut *comparator)
for (k_id, (k, v)) in dict
.keysSortedByValueUsingComparator_(&mut *comparator)
.iter()
.zip(associated_iter) {
.zip(associated_iter)
{
assert!(k_id.isEqualToString(k));
let v_id = dict.objectForKey_(k_id);
assert!(v_id.isEqualToString(v));
Expand Down
73 changes: 45 additions & 28 deletions cocoa/examples/color.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
extern crate cocoa;

use cocoa::base::{selector, id, nil, NO};

use cocoa::foundation::{NSRect, NSPoint, NSSize, NSAutoreleasePool, NSProcessInfo,
NSString};
use cocoa::appkit::{NSApp, NSColor, NSColorSpace, NSApplication, NSApplicationActivationPolicyRegular,
NSMenu, NSMenuItem, NSWindowStyleMask, NSBackingStoreType, NSWindow,
NSRunningApplication, NSApplicationActivateIgnoringOtherApps};
use cocoa::base::{id, nil, selector, NO};

use cocoa::appkit::{
NSApp, NSApplication, NSApplicationActivateIgnoringOtherApps,
NSApplicationActivationPolicyRegular, NSBackingStoreType, NSColor, NSColorSpace, NSMenu,
NSMenuItem, NSRunningApplication, NSWindow, NSWindowStyleMask,
};
use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSProcessInfo, NSRect, NSSize, NSString};

fn main() {
unsafe {
// Create the app.
let app = create_app();

// Create some colors
let clear = NSColor::clearColor(nil);
let black = NSColor::colorWithRed_green_blue_alpha_(nil, 0.0, 0.0, 0.0, 1.0);
let srgb_red = NSColor::colorWithSRGBRed_green_blue_alpha_(nil, 1.0, 0.0, 0.0, 1.0);
let device_green = NSColor::colorWithDeviceRed_green_blue_alpha_(nil, 0.0, 1.0, 0.0, 1.0);
let display_p3_blue = NSColor::colorWithDisplayP3Red_green_blue_alpha_(nil, 0.0, 0.0, 1.0, 1.0);
let calibrated_cyan = NSColor::colorWithCalibratedRed_green_blue_alpha_(nil, 0.0, 1.0, 1.0, 1.0);
let display_p3_blue =
NSColor::colorWithDisplayP3Red_green_blue_alpha_(nil, 0.0, 0.0, 1.0, 1.0);
let calibrated_cyan =
NSColor::colorWithCalibratedRed_green_blue_alpha_(nil, 0.0, 1.0, 1.0, 1.0);

// Create windows with different color types.
let _win_clear = create_window(NSString::alloc(nil).init_str("clear"), clear);
let _win_black = create_window(NSString::alloc(nil).init_str("black"), black);
let _win_srgb_red = create_window(NSString::alloc(nil).init_str("srgb_red"), srgb_red);
let _win_device_green = create_window(NSString::alloc(nil).init_str("device_green"), device_green);
let _win_display_p3_blue = create_window(NSString::alloc(nil).init_str("display_p3_blue"), display_p3_blue);
let _win_calibrated_cyan = create_window(NSString::alloc(nil).init_str("calibrated_cyan"), calibrated_cyan);
let _win_device_green =
create_window(NSString::alloc(nil).init_str("device_green"), device_green);
let _win_display_p3_blue = create_window(
NSString::alloc(nil).init_str("display_p3_blue"),
display_p3_blue,
);
let _win_calibrated_cyan = create_window(
NSString::alloc(nil).init_str("calibrated_cyan"),
calibrated_cyan,
);

// Extract component values from a color.
// NOTE: some components will raise an exception if the color is not
Expand All @@ -42,44 +51,52 @@ fn main() {
println!("hueComponent: {:?}", my_color.hueComponent());
println!("saturationComponent: {:?}", my_color.saturationComponent());
println!("brightnessComponent: {:?}", my_color.brightnessComponent());

// Changing color spaces.
let my_color_cmyk_cs = my_color.colorUsingColorSpace_(NSColorSpace::deviceCMYKColorSpace(nil));
let my_color_cmyk_cs =
my_color.colorUsingColorSpace_(NSColorSpace::deviceCMYKColorSpace(nil));
println!("blackComponent: {:?}", my_color_cmyk_cs.blackComponent());
println!("cyanComponent: {:?}", my_color_cmyk_cs.cyanComponent());
println!("magentaComponent: {:?}", my_color_cmyk_cs.magentaComponent());
println!(
"magentaComponent: {:?}",
my_color_cmyk_cs.magentaComponent()
);
println!("yellowComponent: {:?}", my_color_cmyk_cs.yellowComponent());

// Getting NSColorSpace name.
let cs = NSColorSpace::genericGamma22GrayColorSpace(nil);
let cs_name = cs.localizedName();
let cs_name_bytes = cs_name.UTF8String() as *const u8;
let cs_name_string = std::str::from_utf8(std::slice::from_raw_parts(cs_name_bytes, cs_name.len())).unwrap();
let cs_name_string =
std::str::from_utf8(std::slice::from_raw_parts(cs_name_bytes, cs_name.len())).unwrap();
println!("NSColorSpace: {:?}", cs_name_string);

// Creating an NSColorSpace from CGColorSpaceRef.
let cg_cs = cs.CGColorSpace();
let cs = NSColorSpace::alloc(nil).initWithCGColorSpace_(cg_cs);
let cs_name = cs.localizedName();
let cs_name_bytes = cs_name.UTF8String() as *const u8;
let cs_name_string = std::str::from_utf8(std::slice::from_raw_parts(cs_name_bytes, cs_name.len())).unwrap();
let cs_name_string =
std::str::from_utf8(std::slice::from_raw_parts(cs_name_bytes, cs_name.len())).unwrap();
println!("initWithCGColorSpace_: {:?}", cs_name_string);

app.run();
}
}

unsafe fn create_window(title: id, color: id) -> id {
let window = NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_(
NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)),
NSWindowStyleMask::NSTitledWindowMask |
NSWindowStyleMask::NSClosableWindowMask |
NSWindowStyleMask::NSResizableWindowMask |
NSWindowStyleMask::NSMiniaturizableWindowMask |
NSWindowStyleMask::NSUnifiedTitleAndToolbarWindowMask,
NSBackingStoreType::NSBackingStoreBuffered,
NO
).autorelease();
let window = NSWindow::alloc(nil)
.initWithContentRect_styleMask_backing_defer_(
NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)),
NSWindowStyleMask::NSTitledWindowMask
| NSWindowStyleMask::NSClosableWindowMask
| NSWindowStyleMask::NSResizableWindowMask
| NSWindowStyleMask::NSMiniaturizableWindowMask
| NSWindowStyleMask::NSUnifiedTitleAndToolbarWindowMask,
NSBackingStoreType::NSBackingStoreBuffered,
NO,
)
.autorelease();

window.cascadeTopLeftFromPoint_(NSPoint::new(20., 20.));
window.setTitle_(title);
Expand Down
64 changes: 42 additions & 22 deletions cocoa/examples/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ extern crate core_graphics;
#[macro_use]
extern crate objc;

use cocoa::base::{selector, nil, NO, id};
use cocoa::foundation::{NSRect, NSPoint, NSSize, NSAutoreleasePool, NSProcessInfo,
NSString, NSUInteger};
use cocoa::appkit::{NSApp, NSApplication, NSApplicationActivationPolicyRegular, NSWindow,
NSBackingStoreBuffered, NSMenu, NSMenuItem, NSWindowStyleMask,
NSRunningApplication, NSApplicationActivateIgnoringOtherApps,
NSWindowCollectionBehavior, NSApplicationPresentationOptions};
use cocoa::appkit::{
NSApp, NSApplication, NSApplicationActivateIgnoringOtherApps,
NSApplicationActivationPolicyRegular, NSApplicationPresentationOptions, NSBackingStoreBuffered,
NSMenu, NSMenuItem, NSRunningApplication, NSWindow, NSWindowCollectionBehavior,
NSWindowStyleMask,
};
use cocoa::base::{id, nil, selector, NO};
use cocoa::foundation::{
NSAutoreleasePool, NSPoint, NSProcessInfo, NSRect, NSSize, NSString, NSUInteger,
};

use core_graphics::display::CGDisplay;

use objc::runtime::{Object, Sel};
use objc::declare::ClassDecl;
use objc::runtime::{Object, Sel};

fn main() {
unsafe {
Expand Down Expand Up @@ -47,7 +50,12 @@ fn main() {
let superclass = class!(NSObject);
let mut decl = ClassDecl::new("MyWindowDelegate", superclass).unwrap();

extern fn will_use_fillscreen_presentation_options(_: &Object, _: Sel, _: id, _: NSUInteger) -> NSUInteger {
extern "C" fn will_use_fillscreen_presentation_options(
_: &Object,
_: Sel,
_: id,
_: NSUInteger,
) -> NSUInteger {
// Set initial presentation options for fullscreen
let options = NSApplicationPresentationOptions::NSApplicationPresentationFullScreen
| NSApplicationPresentationOptions::NSApplicationPresentationHideDock
Expand All @@ -56,20 +64,28 @@ fn main() {
options.bits()
}

extern fn window_entering_fullscreen(_: &Object, _: Sel, _: id) {
extern "C" fn window_entering_fullscreen(_: &Object, _: Sel, _: id) {
// Reset HideDock and HideMenuBar settings during/after we entered fullscreen.
let options = NSApplicationPresentationOptions::NSApplicationPresentationHideDock | NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar;
let options = NSApplicationPresentationOptions::NSApplicationPresentationHideDock
| NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar;
unsafe {
NSApp().setPresentationOptions_(options);
}
}

decl.add_method(sel!(window:willUseFullScreenPresentationOptions:),
will_use_fillscreen_presentation_options as extern fn(&Object, Sel, id, NSUInteger) -> NSUInteger);
decl.add_method(sel!(windowWillEnterFullScreen:),
window_entering_fullscreen as extern fn(&Object, Sel, id));
decl.add_method(sel!(windowDidEnterFullScreen:),
window_entering_fullscreen as extern fn(&Object, Sel, id));
decl.add_method(
sel!(window:willUseFullScreenPresentationOptions:),
will_use_fillscreen_presentation_options
as extern "C" fn(&Object, Sel, id, NSUInteger) -> NSUInteger,
);
decl.add_method(
sel!(windowWillEnterFullScreen:),
window_entering_fullscreen as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(windowDidEnterFullScreen:),
window_entering_fullscreen as extern "C" fn(&Object, Sel, id),
);

let delegate_class = decl.register();
let delegate_object = msg_send![delegate_class, new];
Expand All @@ -78,10 +94,12 @@ fn main() {
let display = CGDisplay::main();
let size = NSSize::new(display.pixels_wide() as _, display.pixels_high() as _);
let window = NSWindow::alloc(nil)
.initWithContentRect_styleMask_backing_defer_(NSRect::new(NSPoint::new(0., 0.), size),
NSWindowStyleMask::NSTitledWindowMask,
NSBackingStoreBuffered,
NO)
.initWithContentRect_styleMask_backing_defer_(
NSRect::new(NSPoint::new(0., 0.), size),
NSWindowStyleMask::NSTitledWindowMask,
NSBackingStoreBuffered,
NO,
)
.autorelease();
window.setDelegate_(delegate_object);
let title = NSString::alloc(nil).init_str("Fullscreen!");
Expand All @@ -90,7 +108,9 @@ fn main() {

let current_app = NSRunningApplication::currentApplication(nil);
current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps);
window.setCollectionBehavior_(NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenPrimary);
window.setCollectionBehavior_(
NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenPrimary,
);
window.toggleFullScreen_(nil);
app.run();
}
Expand Down
24 changes: 13 additions & 11 deletions cocoa/examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
extern crate cocoa;

use cocoa::base::{selector, nil, NO};
use cocoa::foundation::{NSRect, NSPoint, NSSize, NSAutoreleasePool, NSProcessInfo,
NSString};
use cocoa::appkit::{NSApp, NSApplication, NSApplicationActivationPolicyRegular, NSWindow,
NSBackingStoreBuffered, NSMenu, NSMenuItem, NSWindowStyleMask,
NSRunningApplication, NSApplicationActivateIgnoringOtherApps};
use cocoa::appkit::{
NSApp, NSApplication, NSApplicationActivateIgnoringOtherApps,
NSApplicationActivationPolicyRegular, NSBackingStoreBuffered, NSMenu, NSMenuItem,
NSRunningApplication, NSWindow, NSWindowStyleMask,
};
use cocoa::base::{nil, selector, NO};
use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSProcessInfo, NSRect, NSSize, NSString};

fn main() {
unsafe {
Expand Down Expand Up @@ -35,11 +36,12 @@ fn main() {

// create Window
let window = NSWindow::alloc(nil)
.initWithContentRect_styleMask_backing_defer_(NSRect::new(NSPoint::new(0., 0.),
NSSize::new(200., 200.)),
NSWindowStyleMask::NSTitledWindowMask,
NSBackingStoreBuffered,
NO)
.initWithContentRect_styleMask_backing_defer_(
NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)),
NSWindowStyleMask::NSTitledWindowMask,
NSBackingStoreBuffered,
NO,
)
.autorelease();
window.cascadeTopLeftFromPoint_(NSPoint::new(20., 20.));
window.center();
Expand Down
Loading