[go: up one dir, main page]

Trait NSString

Source
pub trait NSString: Sized {
    // Required methods
    unsafe fn stringByAppendingString_(self, other: *mut Object) -> *mut Object;
    unsafe fn init_str(self, string: &str) -> Self;
    unsafe fn UTF8String(self) -> *const i8;
    unsafe fn len(self) -> usize;
    unsafe fn isEqualToString(self, string: &str) -> bool;
    unsafe fn substringWithRange(self, range: NSRange) -> *mut Object;

    // Provided method
    unsafe fn alloc(_: Self) -> *mut Object { ... }
}

Required Methods§

Source

unsafe fn stringByAppendingString_(self, other: *mut Object) -> *mut Object

Source

unsafe fn init_str(self, string: &str) -> Self

Source

unsafe fn UTF8String(self) -> *const i8

Source

unsafe fn len(self) -> usize

Source

unsafe fn isEqualToString(self, string: &str) -> bool

Source

unsafe fn substringWithRange(self, range: NSRange) -> *mut Object

Provided Methods§

Source

unsafe fn alloc(_: Self) -> *mut Object

Examples found in repository?
examples/tab_view.rs (line 18)
10fn main() {
11    unsafe {
12        // create a tab View
13        let tab_view = NSTabView::new(nil)
14            .initWithFrame_(NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)));
15
16        // create a tab view item
17        let tab_view_item =
18            NSTabViewItem::new(nil).initWithIdentifier_(NSString::alloc(nil).init_str("TabView1"));
19
20        tab_view_item.setLabel_(NSString::alloc(nil).init_str("Tab view item 1"));
21        tab_view.addTabViewItem_(tab_view_item);
22
23        // create a second tab view item
24        let tab_view_item2 =
25            NSTabViewItem::new(nil).initWithIdentifier_(NSString::alloc(nil).init_str("TabView2"));
26
27        tab_view_item2.setLabel_(NSString::alloc(nil).init_str("Tab view item 2"));
28        tab_view.addTabViewItem_(tab_view_item2);
29
30        // Create the app and set the content.
31        let app = create_app(NSString::alloc(nil).init_str("Tab View"), tab_view);
32        app.run();
33    }
34}
35
36unsafe fn create_app(title: id, content: id) -> id {
37    let _pool = NSAutoreleasePool::new(nil);
38
39    let app = NSApp();
40    app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
41
42    // create Menu Bar
43    let menubar = NSMenu::new(nil).autorelease();
44    let app_menu_item = NSMenuItem::new(nil).autorelease();
45    menubar.addItem_(app_menu_item);
46    app.setMainMenu_(menubar);
47
48    // create Application menu
49    let app_menu = NSMenu::new(nil).autorelease();
50    let quit_prefix = NSString::alloc(nil).init_str("Quit ");
51    let quit_title =
52        quit_prefix.stringByAppendingString_(NSProcessInfo::processInfo(nil).processName());
53    let quit_action = selector("terminate:");
54    let quit_key = NSString::alloc(nil).init_str("q");
55    let quit_item = NSMenuItem::alloc(nil)
56        .initWithTitle_action_keyEquivalent_(quit_title, quit_action, quit_key)
57        .autorelease();
58    app_menu.addItem_(quit_item);
59    app_menu_item.setSubmenu_(app_menu);
60
61    // create Window
62    let window = NSWindow::alloc(nil)
63        .initWithContentRect_styleMask_backing_defer_(
64            NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)),
65            NSWindowStyleMask::NSTitledWindowMask
66                | NSWindowStyleMask::NSClosableWindowMask
67                | NSWindowStyleMask::NSResizableWindowMask
68                | NSWindowStyleMask::NSMiniaturizableWindowMask
69                | NSWindowStyleMask::NSUnifiedTitleAndToolbarWindowMask,
70            NSBackingStoreType::NSBackingStoreBuffered,
71            NO,
72        )
73        .autorelease();
74    window.cascadeTopLeftFromPoint_(NSPoint::new(20., 20.));
75    window.center();
76
77    window.setTitle_(title);
78    window.makeKeyAndOrderFront_(nil);
79
80    window.setContentView_(content);
81    let current_app = NSRunningApplication::currentApplication(nil);
82    current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps);
83
84    app
85}
More examples
Hide additional examples
examples/hello_world.rs (line 24)
9fn main() {
10    unsafe {
11        let _pool = NSAutoreleasePool::new(nil);
12
13        let app = NSApp();
14        app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
15
16        // create Menu Bar
17        let menubar = NSMenu::new(nil).autorelease();
18        let app_menu_item = NSMenuItem::new(nil).autorelease();
19        menubar.addItem_(app_menu_item);
20        app.setMainMenu_(menubar);
21
22        // create Application menu
23        let app_menu = NSMenu::new(nil).autorelease();
24        let quit_prefix = NSString::alloc(nil).init_str("Quit ");
25        let quit_title =
26            quit_prefix.stringByAppendingString_(NSProcessInfo::processInfo(nil).processName());
27        let quit_action = selector("terminate:");
28        let quit_key = NSString::alloc(nil).init_str("q");
29        let quit_item = NSMenuItem::alloc(nil)
30            .initWithTitle_action_keyEquivalent_(quit_title, quit_action, quit_key)
31            .autorelease();
32        app_menu.addItem_(quit_item);
33        app_menu_item.setSubmenu_(app_menu);
34
35        // create Window
36        let window = NSWindow::alloc(nil)
37            .initWithContentRect_styleMask_backing_defer_(
38                NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)),
39                NSWindowStyleMask::NSTitledWindowMask,
40                NSBackingStoreBuffered,
41                NO,
42            )
43            .autorelease();
44        window.cascadeTopLeftFromPoint_(NSPoint::new(20., 20.));
45        window.center();
46        let title = NSString::alloc(nil).init_str("Hello World!");
47        window.setTitle_(title);
48        window.makeKeyAndOrderFront_(nil);
49        let current_app = NSRunningApplication::currentApplication(nil);
50        current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps);
51        app.run();
52    }
53}
examples/nsvisualeffectview_blur.rs (line 28)
12fn main() {
13    unsafe {
14        // Create the app.
15        let _pool = NSAutoreleasePool::new(nil);
16
17        let app = NSApp();
18        app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
19
20        // create Menu Bar
21        let menubar = NSMenu::new(nil).autorelease();
22        let app_menu_item = NSMenuItem::new(nil).autorelease();
23        menubar.addItem_(app_menu_item);
24        app.setMainMenu_(menubar);
25
26        // create Application menu
27        let app_menu = NSMenu::new(nil).autorelease();
28        let quit_prefix = NSString::alloc(nil).init_str("Quit ");
29        let quit_title =
30            quit_prefix.stringByAppendingString_(NSProcessInfo::processInfo(nil).processName());
31        let quit_action = selector("terminate:");
32        let quit_key = NSString::alloc(nil).init_str("q");
33        let quit_item = NSMenuItem::alloc(nil)
34            .initWithTitle_action_keyEquivalent_(quit_title, quit_action, quit_key)
35            .autorelease();
36        app_menu.addItem_(quit_item);
37        app_menu_item.setSubmenu_(app_menu);
38
39        // Create some colors
40        let clear = NSColor::clearColor(nil);
41
42        // Create windows with different color types.
43        let window = NSWindow::alloc(nil)
44            .initWithContentRect_styleMask_backing_defer_(
45                NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)),
46                NSWindowStyleMask::NSTitledWindowMask
47                    | NSWindowStyleMask::NSClosableWindowMask
48                    | NSWindowStyleMask::NSResizableWindowMask
49                    | NSWindowStyleMask::NSMiniaturizableWindowMask
50                    | NSWindowStyleMask::NSUnifiedTitleAndToolbarWindowMask,
51                NSBackingStoreType::NSBackingStoreBuffered,
52                NO,
53            )
54            .autorelease();
55
56        window.cascadeTopLeftFromPoint_(NSPoint::new(20., 20.));
57        window.setTitle_(NSString::alloc(nil).init_str("NSVisualEffectView_blur"));
58        window.setBackgroundColor_(clear);
59        window.makeKeyAndOrderFront_(nil);
60
61        //NSVisualEffectView blur
62        let ns_view = window.contentView();
63        let bounds = NSView::bounds(ns_view);
64        let blurred_view =
65            NSVisualEffectView::initWithFrame_(NSVisualEffectView::alloc(nil), bounds);
66        blurred_view.autorelease();
67
68        blurred_view.setMaterial_(NSVisualEffectMaterial::HudWindow);
69        blurred_view.setBlendingMode_(NSVisualEffectBlendingMode::BehindWindow);
70        blurred_view.setState_(NSVisualEffectState::FollowsWindowActiveState);
71        blurred_view.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable);
72
73        let _: () = msg_send![ns_view, addSubview: blurred_view positioned: NSWindowOrderingMode::NSWindowBelow relativeTo: 0];
74
75        app.run();
76    }
77}
examples/color.rs (line 26)
10fn main() {
11    unsafe {
12        // Create the app.
13        let app = create_app();
14
15        // Create some colors
16        let clear = NSColor::clearColor(nil);
17        let black = NSColor::colorWithRed_green_blue_alpha_(nil, 0.0, 0.0, 0.0, 1.0);
18        let srgb_red = NSColor::colorWithSRGBRed_green_blue_alpha_(nil, 1.0, 0.0, 0.0, 1.0);
19        let device_green = NSColor::colorWithDeviceRed_green_blue_alpha_(nil, 0.0, 1.0, 0.0, 1.0);
20        let display_p3_blue =
21            NSColor::colorWithDisplayP3Red_green_blue_alpha_(nil, 0.0, 0.0, 1.0, 1.0);
22        let calibrated_cyan =
23            NSColor::colorWithCalibratedRed_green_blue_alpha_(nil, 0.0, 1.0, 1.0, 1.0);
24
25        // Create windows with different color types.
26        let _win_clear = create_window(NSString::alloc(nil).init_str("clear"), clear);
27        let _win_black = create_window(NSString::alloc(nil).init_str("black"), black);
28        let _win_srgb_red = create_window(NSString::alloc(nil).init_str("srgb_red"), srgb_red);
29        let _win_device_green =
30            create_window(NSString::alloc(nil).init_str("device_green"), device_green);
31        let _win_display_p3_blue = create_window(
32            NSString::alloc(nil).init_str("display_p3_blue"),
33            display_p3_blue,
34        );
35        let _win_calibrated_cyan = create_window(
36            NSString::alloc(nil).init_str("calibrated_cyan"),
37            calibrated_cyan,
38        );
39
40        // Extract component values from a color.
41        // NOTE: some components will raise an exception if the color is not
42        // in the correct NSColorSpace. Refer to Apple's documentation for details.
43        // https://developer.apple.com/documentation/appkit/nscolor?language=objc
44        let my_color = NSColor::colorWithRed_green_blue_alpha_(nil, 0.25, 0.75, 0.5, 0.25);
45        println!("alphaComponent: {:?}", my_color.alphaComponent());
46        println!("redComponent: {:?}", my_color.redComponent());
47        println!("greenComponent: {:?}", my_color.greenComponent());
48        println!("blueComponent: {:?}", my_color.blueComponent());
49        println!("hueComponent: {:?}", my_color.hueComponent());
50        println!("saturationComponent: {:?}", my_color.saturationComponent());
51        println!("brightnessComponent: {:?}", my_color.brightnessComponent());
52
53        // Changing color spaces.
54        let my_color_cmyk_cs =
55            my_color.colorUsingColorSpace_(NSColorSpace::deviceCMYKColorSpace(nil));
56        println!("blackComponent: {:?}", my_color_cmyk_cs.blackComponent());
57        println!("cyanComponent: {:?}", my_color_cmyk_cs.cyanComponent());
58        println!(
59            "magentaComponent: {:?}",
60            my_color_cmyk_cs.magentaComponent()
61        );
62        println!("yellowComponent: {:?}", my_color_cmyk_cs.yellowComponent());
63
64        // Getting NSColorSpace name.
65        let cs = NSColorSpace::genericGamma22GrayColorSpace(nil);
66        let cs_name = cs.localizedName();
67        let cs_name_bytes = cs_name.UTF8String() as *const u8;
68        let cs_name_string =
69            std::str::from_utf8(std::slice::from_raw_parts(cs_name_bytes, cs_name.len())).unwrap();
70        println!("NSColorSpace: {:?}", cs_name_string);
71
72        // Creating an NSColorSpace from CGColorSpaceRef.
73        let cg_cs = cs.CGColorSpace();
74        let cs = NSColorSpace::alloc(nil).initWithCGColorSpace_(cg_cs);
75        let cs_name = cs.localizedName();
76        let cs_name_bytes = cs_name.UTF8String() as *const u8;
77        let cs_name_string =
78            std::str::from_utf8(std::slice::from_raw_parts(cs_name_bytes, cs_name.len())).unwrap();
79        println!("initWithCGColorSpace_: {:?}", cs_name_string);
80
81        app.run();
82    }
83}
84
85unsafe fn create_window(title: id, color: id) -> id {
86    let window = NSWindow::alloc(nil)
87        .initWithContentRect_styleMask_backing_defer_(
88            NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)),
89            NSWindowStyleMask::NSTitledWindowMask
90                | NSWindowStyleMask::NSClosableWindowMask
91                | NSWindowStyleMask::NSResizableWindowMask
92                | NSWindowStyleMask::NSMiniaturizableWindowMask
93                | NSWindowStyleMask::NSUnifiedTitleAndToolbarWindowMask,
94            NSBackingStoreType::NSBackingStoreBuffered,
95            NO,
96        )
97        .autorelease();
98
99    window.cascadeTopLeftFromPoint_(NSPoint::new(20., 20.));
100    window.setTitle_(title);
101    window.setBackgroundColor_(color);
102    window.makeKeyAndOrderFront_(nil);
103    window
104}
105
106unsafe fn create_app() -> id {
107    let _pool = NSAutoreleasePool::new(nil);
108
109    let app = NSApp();
110    app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
111
112    // create Menu Bar
113    let menubar = NSMenu::new(nil).autorelease();
114    let app_menu_item = NSMenuItem::new(nil).autorelease();
115    menubar.addItem_(app_menu_item);
116    app.setMainMenu_(menubar);
117
118    // create Application menu
119    let app_menu = NSMenu::new(nil).autorelease();
120    let quit_prefix = NSString::alloc(nil).init_str("Quit ");
121    let quit_title =
122        quit_prefix.stringByAppendingString_(NSProcessInfo::processInfo(nil).processName());
123    let quit_action = selector("terminate:");
124    let quit_key = NSString::alloc(nil).init_str("q");
125    let quit_item = NSMenuItem::alloc(nil)
126        .initWithTitle_action_keyEquivalent_(quit_title, quit_action, quit_key)
127        .autorelease();
128    app_menu.addItem_(quit_item);
129    app_menu_item.setSubmenu_(app_menu);
130
131    let current_app = NSRunningApplication::currentApplication(nil);
132    current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps);
133
134    app
135}
examples/fullscreen.rs (line 33)
18fn main() {
19    unsafe {
20        let _pool = NSAutoreleasePool::new(nil);
21
22        let app = NSApp();
23        app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
24
25        // create Menu Bar
26        let menubar = NSMenu::new(nil).autorelease();
27        let app_menu_item = NSMenuItem::new(nil).autorelease();
28        menubar.addItem_(app_menu_item);
29        app.setMainMenu_(menubar);
30
31        // create Application menu
32        let app_menu = NSMenu::new(nil).autorelease();
33        let quit_prefix = NSString::alloc(nil).init_str("Quit ");
34        let quit_title =
35            quit_prefix.stringByAppendingString_(NSProcessInfo::processInfo(nil).processName());
36        let quit_action = selector("terminate:");
37        let quit_key = NSString::alloc(nil).init_str("q");
38        let quit_item = NSMenuItem::alloc(nil)
39            .initWithTitle_action_keyEquivalent_(quit_title, quit_action, quit_key)
40            .autorelease();
41        app_menu.addItem_(quit_item);
42        app_menu_item.setSubmenu_(app_menu);
43
44        // Create NSWindowDelegate
45        let superclass = class!(NSObject);
46        let mut decl = ClassDecl::new("MyWindowDelegate", superclass).unwrap();
47
48        extern "C" fn will_use_fillscreen_presentation_options(
49            _: &Object,
50            _: Sel,
51            _: id,
52            _: NSUInteger,
53        ) -> NSUInteger {
54            // Set initial presentation options for fullscreen
55            let options = NSApplicationPresentationOptions::NSApplicationPresentationFullScreen
56                | NSApplicationPresentationOptions::NSApplicationPresentationHideDock
57                | NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar
58                | NSApplicationPresentationOptions::NSApplicationPresentationDisableProcessSwitching;
59            options.bits()
60        }
61
62        extern "C" fn window_entering_fullscreen(_: &Object, _: Sel, _: id) {
63            // Reset HideDock and HideMenuBar settings during/after we entered fullscreen.
64            let options = NSApplicationPresentationOptions::NSApplicationPresentationHideDock
65                | NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar;
66            unsafe {
67                NSApp().setPresentationOptions_(options);
68            }
69        }
70
71        decl.add_method(
72            sel!(window:willUseFullScreenPresentationOptions:),
73            will_use_fillscreen_presentation_options
74                as extern "C" fn(&Object, Sel, id, NSUInteger) -> NSUInteger,
75        );
76        decl.add_method(
77            sel!(windowWillEnterFullScreen:),
78            window_entering_fullscreen as extern "C" fn(&Object, Sel, id),
79        );
80        decl.add_method(
81            sel!(windowDidEnterFullScreen:),
82            window_entering_fullscreen as extern "C" fn(&Object, Sel, id),
83        );
84
85        let delegate_class = decl.register();
86        let delegate_object = msg_send![delegate_class, new];
87
88        // create Window
89        let display = CGDisplay::main();
90        let size = NSSize::new(display.pixels_wide() as _, display.pixels_high() as _);
91        let window = NSWindow::alloc(nil)
92            .initWithContentRect_styleMask_backing_defer_(
93                NSRect::new(NSPoint::new(0., 0.), size),
94                NSWindowStyleMask::NSTitledWindowMask,
95                NSBackingStoreBuffered,
96                NO,
97            )
98            .autorelease();
99        window.setDelegate_(delegate_object);
100        let title = NSString::alloc(nil).init_str("Fullscreen!");
101        window.setTitle_(title);
102        window.makeKeyAndOrderFront_(nil);
103
104        let current_app = NSRunningApplication::currentApplication(nil);
105        current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps);
106        window.setCollectionBehavior_(
107            NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenPrimary,
108        );
109        window.toggleFullScreen_(nil);
110        app.run();
111    }
112}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl NSString for *mut Object

Source§

unsafe fn isEqualToString(self, other: &str) -> bool

Source§

unsafe fn stringByAppendingString_(self, other: *mut Object) -> *mut Object

Source§

unsafe fn init_str(self, string: &str) -> *mut Object

Source§

unsafe fn len(self) -> usize

Source§

unsafe fn UTF8String(self) -> *const i8

Source§

unsafe fn substringWithRange(self, range: NSRange) -> *mut Object

Implementors§