[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 { ... }
}
๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead

Required Methodsยง

Source

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

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead
Source

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

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead
Source

unsafe fn UTF8String(self) -> *const i8

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead
Source

unsafe fn len(self) -> usize

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead
Source

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

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead
Source

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

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead

Provided Methodsยง

Source

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

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

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

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead
Sourceยง

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

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead
Sourceยง

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

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead
Sourceยง

unsafe fn len(self) -> usize

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead
Sourceยง

unsafe fn UTF8String(self) -> *const i8

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead
Sourceยง

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

๐Ÿ‘ŽDeprecated: use the objc2-foundation crate instead

Implementorsยง