bors-servo e51cf4186d Auto merge of #512 - Hupka:master, r=jrmuizel
Fixes #511: Add`NSPanel` trait to create NSPanels

Intention for PR described here: https://github.com/servo/core-foundation-rs/issues/511.

I didn't extend any of the examples to include `NSPanel` because I think that's not really worth it. Instead, here is an adapted version of the `hello_world.rs` example that prints out responses of the API for both, `NSWindow` and `NSPanel`.

  ```Rust
extern crate cocoa;

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

fn main() {
    unsafe {
        let _pool = NSAutoreleasePool::new(nil);

        let app = NSApp();
        app.setActivationPolicy_(NSApplicationActivationPolicyRegular);

        // create Menu Bar
        let menubar = NSMenu::new(nil).autorelease();
        let app_menu_item = NSMenuItem::new(nil).autorelease();
        menubar.addItem_(app_menu_item);
        app.setMainMenu_(menubar);

        // create Application menu
        let app_menu = NSMenu::new(nil).autorelease();
        let quit_prefix = NSString::alloc(nil).init_str("Quit ");
        let quit_title =
            quit_prefix.stringByAppendingString_(NSProcessInfo::processInfo(nil).processName());
        let quit_action = selector("terminate:");
        let quit_key = NSString::alloc(nil).init_str("q");
        let quit_item = NSMenuItem::alloc(nil)
            .initWithTitle_action_keyEquivalent_(quit_title, quit_action, quit_key)
            .autorelease();
        app_menu.addItem_(quit_item);
        app_menu_item.setSubmenu_(app_menu);

        // 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,
            )
            .autorelease();
        window.cascadeTopLeftFromPoint_(NSPoint::new(20., 20.));
        window.center();
        let title = NSString::alloc(nil).init_str("Hello World!");
        window.setTitle_(title);
        window.makeKeyAndOrderFront_(nil);

        // create Panel
        let panel = NSPanel::alloc(nil)
            .initWithContentRect_styleMask_backing_defer_(
                NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)),
                NSWindowStyleMask::NSTitledWindowMask,
                NSBackingStoreBuffered,
                NO,
            )
            .autorelease();
        panel.cascadeTopLeftFromPoint_(NSPoint::new(20., 20.));
        panel.center();
        let title = NSString::alloc(nil).init_str("Hello PANEL!");
        panel.setTitle_(title);
        panel.makeKeyAndOrderFront_(nil);

        // Specific behavior of NSWindow & NSPanel
        println!(
            "NSWindow can become main window: {}",
            window.canBecomeMainWindow()
        ); // returns true
        println!(
            "NSPanel can become main window? {}",
            panel.canBecomeMainWindow()
        ); // returns false

        // Test methods of NSPanel Trait
        println!(
            "BEFORE setting it: NSPanel is a floating panel: {}",
            panel.floatingPanel()
        ); // returns false
        println!(
            "BEFORE setting it: NSPanel becomes key only when needed: {}",
            panel.becomesKeyOnlyIfNeeded()
        ); // returns false

        panel.setFloatingPanel(true);
        panel.setBecomesKeyOnlyIfNeeded(true);

        println!(
            "AFTER: NSPanel is a floating panel: {}",
            panel.floatingPanel()
        ); // returns true
        println!(
            "AFTER: NSPanel becomes key only when needed: {}",
            panel.becomesKeyOnlyIfNeeded()
        ); // returns true

        let current_app = NSRunningApplication::currentApplication(nil);
        current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps);
        app.run();
    }
}
```
2022-06-09 20:04:04 -04:00
2022-02-07 21:47:39 +01:00
2022-02-16 12:09:01 +09:00
2020-07-17 22:48:21 -04:00
2013-04-04 19:01:35 -07:00
2013-04-03 18:18:52 -07:00
2013-04-03 18:18:52 -07:00

core-foundation-rs

Build Status

Compatibility

Targets macOS 10.7 by default.

To enable features added in macOS 10.8, set Cargo feature mac_os_10_8_features. To have both 10.8 features and 10.7 compatibility, also set mac_os_10_7_support. Setting both requires weak linkage, which is a nightly-only feature as of Rust 1.19.

For more experimental but more complete, generated bindings take a look at https://github.com/michaelwu/RustKit.

Contributing

If you wish to start contributing or even make a one-off change, simply submit a pull request with the code or documentation change and we'll go from there.

S
Description
No description provided
Readme 2.5 MiB
Languages
Rust 100%