1
1
extern crate cocoa;
2
2
3
- use cocoa:: base:: { selector, id, nil, NO } ;
4
-
5
- use cocoa:: foundation:: { NSRect , NSPoint , NSSize , NSAutoreleasePool , NSProcessInfo ,
6
- NSString } ;
7
- use cocoa:: appkit:: { NSApp , NSColor , NSColorSpace , NSApplication , NSApplicationActivationPolicyRegular ,
8
- NSMenu , NSMenuItem , NSWindowStyleMask , NSBackingStoreType , NSWindow ,
9
- NSRunningApplication , NSApplicationActivateIgnoringOtherApps } ;
3
+ use cocoa:: base:: { id, nil, selector, NO } ;
10
4
5
+ use cocoa:: appkit:: {
6
+ NSApp , NSApplication , NSApplicationActivateIgnoringOtherApps ,
7
+ NSApplicationActivationPolicyRegular , NSBackingStoreType , NSColor , NSColorSpace , NSMenu ,
8
+ NSMenuItem , NSRunningApplication , NSWindow , NSWindowStyleMask ,
9
+ } ;
10
+ use cocoa:: foundation:: { NSAutoreleasePool , NSPoint , NSProcessInfo , NSRect , NSSize , NSString } ;
11
11
12
12
fn main ( ) {
13
13
unsafe {
14
14
// Create the app.
15
15
let app = create_app ( ) ;
16
-
16
+
17
17
// Create some colors
18
18
let clear = NSColor :: clearColor ( nil) ;
19
19
let black = NSColor :: colorWithRed_green_blue_alpha_ ( nil, 0.0 , 0.0 , 0.0 , 1.0 ) ;
20
20
let srgb_red = NSColor :: colorWithSRGBRed_green_blue_alpha_ ( nil, 1.0 , 0.0 , 0.0 , 1.0 ) ;
21
21
let device_green = NSColor :: colorWithDeviceRed_green_blue_alpha_ ( nil, 0.0 , 1.0 , 0.0 , 1.0 ) ;
22
- let display_p3_blue = NSColor :: colorWithDisplayP3Red_green_blue_alpha_ ( nil, 0.0 , 0.0 , 1.0 , 1.0 ) ;
23
- let calibrated_cyan = NSColor :: colorWithCalibratedRed_green_blue_alpha_ ( nil, 0.0 , 1.0 , 1.0 , 1.0 ) ;
22
+ let display_p3_blue =
23
+ NSColor :: colorWithDisplayP3Red_green_blue_alpha_ ( nil, 0.0 , 0.0 , 1.0 , 1.0 ) ;
24
+ let calibrated_cyan =
25
+ NSColor :: colorWithCalibratedRed_green_blue_alpha_ ( nil, 0.0 , 1.0 , 1.0 , 1.0 ) ;
24
26
25
27
// Create windows with different color types.
26
28
let _win_clear = create_window ( NSString :: alloc ( nil) . init_str ( "clear" ) , clear) ;
27
29
let _win_black = create_window ( NSString :: alloc ( nil) . init_str ( "black" ) , black) ;
28
30
let _win_srgb_red = create_window ( NSString :: alloc ( nil) . init_str ( "srgb_red" ) , srgb_red) ;
29
- let _win_device_green = create_window ( NSString :: alloc ( nil) . init_str ( "device_green" ) , device_green) ;
30
- let _win_display_p3_blue = create_window ( NSString :: alloc ( nil) . init_str ( "display_p3_blue" ) , display_p3_blue) ;
31
- let _win_calibrated_cyan = create_window ( NSString :: alloc ( nil) . init_str ( "calibrated_cyan" ) , calibrated_cyan) ;
31
+ let _win_device_green =
32
+ create_window ( NSString :: alloc ( nil) . init_str ( "device_green" ) , device_green) ;
33
+ let _win_display_p3_blue = create_window (
34
+ NSString :: alloc ( nil) . init_str ( "display_p3_blue" ) ,
35
+ display_p3_blue,
36
+ ) ;
37
+ let _win_calibrated_cyan = create_window (
38
+ NSString :: alloc ( nil) . init_str ( "calibrated_cyan" ) ,
39
+ calibrated_cyan,
40
+ ) ;
32
41
33
42
// Extract component values from a color.
34
43
// NOTE: some components will raise an exception if the color is not
@@ -42,44 +51,52 @@ fn main() {
42
51
println ! ( "hueComponent: {:?}" , my_color. hueComponent( ) ) ;
43
52
println ! ( "saturationComponent: {:?}" , my_color. saturationComponent( ) ) ;
44
53
println ! ( "brightnessComponent: {:?}" , my_color. brightnessComponent( ) ) ;
45
-
54
+
46
55
// Changing color spaces.
47
- let my_color_cmyk_cs = my_color. colorUsingColorSpace_ ( NSColorSpace :: deviceCMYKColorSpace ( nil) ) ;
56
+ let my_color_cmyk_cs =
57
+ my_color. colorUsingColorSpace_ ( NSColorSpace :: deviceCMYKColorSpace ( nil) ) ;
48
58
println ! ( "blackComponent: {:?}" , my_color_cmyk_cs. blackComponent( ) ) ;
49
59
println ! ( "cyanComponent: {:?}" , my_color_cmyk_cs. cyanComponent( ) ) ;
50
- println ! ( "magentaComponent: {:?}" , my_color_cmyk_cs. magentaComponent( ) ) ;
60
+ println ! (
61
+ "magentaComponent: {:?}" ,
62
+ my_color_cmyk_cs. magentaComponent( )
63
+ ) ;
51
64
println ! ( "yellowComponent: {:?}" , my_color_cmyk_cs. yellowComponent( ) ) ;
52
65
53
66
// Getting NSColorSpace name.
54
67
let cs = NSColorSpace :: genericGamma22GrayColorSpace ( nil) ;
55
68
let cs_name = cs. localizedName ( ) ;
56
69
let cs_name_bytes = cs_name. UTF8String ( ) as * const u8 ;
57
- let cs_name_string = std:: str:: from_utf8 ( std:: slice:: from_raw_parts ( cs_name_bytes, cs_name. len ( ) ) ) . unwrap ( ) ;
70
+ let cs_name_string =
71
+ std:: str:: from_utf8 ( std:: slice:: from_raw_parts ( cs_name_bytes, cs_name. len ( ) ) ) . unwrap ( ) ;
58
72
println ! ( "NSColorSpace: {:?}" , cs_name_string) ;
59
73
60
74
// Creating an NSColorSpace from CGColorSpaceRef.
61
75
let cg_cs = cs. CGColorSpace ( ) ;
62
76
let cs = NSColorSpace :: alloc ( nil) . initWithCGColorSpace_ ( cg_cs) ;
63
77
let cs_name = cs. localizedName ( ) ;
64
78
let cs_name_bytes = cs_name. UTF8String ( ) as * const u8 ;
65
- let cs_name_string = std:: str:: from_utf8 ( std:: slice:: from_raw_parts ( cs_name_bytes, cs_name. len ( ) ) ) . unwrap ( ) ;
79
+ let cs_name_string =
80
+ std:: str:: from_utf8 ( std:: slice:: from_raw_parts ( cs_name_bytes, cs_name. len ( ) ) ) . unwrap ( ) ;
66
81
println ! ( "initWithCGColorSpace_: {:?}" , cs_name_string) ;
67
82
68
83
app. run ( ) ;
69
84
}
70
85
}
71
86
72
87
unsafe fn create_window ( title : id , color : id ) -> id {
73
- let window = NSWindow :: alloc ( nil) . initWithContentRect_styleMask_backing_defer_ (
74
- NSRect :: new ( NSPoint :: new ( 0. , 0. ) , NSSize :: new ( 200. , 200. ) ) ,
75
- NSWindowStyleMask :: NSTitledWindowMask |
76
- NSWindowStyleMask :: NSClosableWindowMask |
77
- NSWindowStyleMask :: NSResizableWindowMask |
78
- NSWindowStyleMask :: NSMiniaturizableWindowMask |
79
- NSWindowStyleMask :: NSUnifiedTitleAndToolbarWindowMask ,
80
- NSBackingStoreType :: NSBackingStoreBuffered ,
81
- NO
82
- ) . autorelease ( ) ;
88
+ let window = NSWindow :: alloc ( nil)
89
+ . initWithContentRect_styleMask_backing_defer_ (
90
+ NSRect :: new ( NSPoint :: new ( 0. , 0. ) , NSSize :: new ( 200. , 200. ) ) ,
91
+ NSWindowStyleMask :: NSTitledWindowMask
92
+ | NSWindowStyleMask :: NSClosableWindowMask
93
+ | NSWindowStyleMask :: NSResizableWindowMask
94
+ | NSWindowStyleMask :: NSMiniaturizableWindowMask
95
+ | NSWindowStyleMask :: NSUnifiedTitleAndToolbarWindowMask ,
96
+ NSBackingStoreType :: NSBackingStoreBuffered ,
97
+ NO ,
98
+ )
99
+ . autorelease ( ) ;
83
100
84
101
window. cascadeTopLeftFromPoint_ ( NSPoint :: new ( 20. , 20. ) ) ;
85
102
window. setTitle_ ( title) ;
0 commit comments