Skip to content

Commit e51cf41

Browse files
authored
Auto merge of #512 - Hupka:master, r=jrmuizel
Fixes #511: Add`NSPanel` trait to create NSPanels Intention for PR described here: #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(); } } ```
2 parents 5143684 + 01169af commit e51cf41

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

cocoa/src/appkit.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,44 @@ impl NSWindow for id {
18021802
// TODO: Constraint-Based Layouts
18031803
}
18041804

1805+
pub trait NSPanel: Sized {
1806+
unsafe fn alloc(_: Self) -> id {
1807+
msg_send![class!(NSPanel), alloc]
1808+
}
1809+
1810+
// NSPanel subclasses NSWindow, hence we only add the added methods
1811+
// https://developer.apple.com/documentation/appkit/nspanel
1812+
unsafe fn setBecomesKeyOnlyIfNeeded(self, becomesKeyOnlyIfNeeded: BOOL);
1813+
unsafe fn becomesKeyOnlyIfNeeded(self) -> BOOL;
1814+
unsafe fn setFloatingPanel(self, floatingPanel: BOOL);
1815+
unsafe fn floatingPanel(self) -> BOOL;
1816+
unsafe fn setWorksWhenModal(self, worksWithPanel: BOOL);
1817+
}
1818+
1819+
impl NSPanel for id {
1820+
// NSPanel subclasses NSWindow, hence we only add the added methods
1821+
// https://developer.apple.com/documentation/appkit/nspanel
1822+
unsafe fn setBecomesKeyOnlyIfNeeded(self, becomesKeyOnlyIfNeeded: BOOL) {
1823+
msg_send![self, setBecomesKeyOnlyIfNeeded: becomesKeyOnlyIfNeeded]
1824+
}
1825+
1826+
unsafe fn becomesKeyOnlyIfNeeded(self) -> BOOL {
1827+
msg_send![self, becomesKeyOnlyIfNeeded]
1828+
}
1829+
1830+
unsafe fn setFloatingPanel(self, floatingPanel: BOOL) {
1831+
msg_send![self, setFloatingPanel: floatingPanel]
1832+
}
1833+
1834+
unsafe fn floatingPanel(self) -> BOOL {
1835+
msg_send![self, isFloatingPanel]
1836+
}
1837+
1838+
unsafe fn setWorksWhenModal(self, worksWhenModal: BOOL) {
1839+
msg_send![self, setWorksWhenModal: worksWhenModal]
1840+
}
1841+
}
1842+
18051843
#[repr(i64)]
18061844
#[derive(Clone, Copy, Debug, PartialEq)]
18071845
pub enum NSModalResponse {

0 commit comments

Comments
 (0)