This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Cocoa instead of GTK3 on Mac OS X
- Loading branch information
Showing
9 changed files
with
155 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
extern crate msgbox; | ||
|
||
use msgbox::IconType; | ||
|
||
fn main() { | ||
msgbox::create("Hello Title", "Hello World!", msgbox::IconType::INFO); | ||
msgbox::create("Error", "Error occured at hello_world.rs:5.\nTerminating..", msgbox::IconType::ERROR); | ||
msgbox::create("Hello Title", "Hello World!", IconType::INFO); | ||
msgbox::create("Error", "Error occured at hello_world.rs:5.\nTerminating..", IconType::ERROR); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,44 @@ | ||
mod icon; | ||
pub use icon::IconType; | ||
|
||
/** | ||
* GTK+3 | ||
*/ | ||
#[cfg(target_os = "linux")] | ||
extern crate gtk; | ||
|
||
#[cfg(target_os = "linux")] | ||
mod linux; | ||
|
||
#[cfg(target_os = "linux")] | ||
pub use linux::create; | ||
pub use linux::*; | ||
|
||
/** | ||
* Cocoa | ||
*/ | ||
#[cfg(target_os = "macos")] | ||
#[macro_use] | ||
extern crate objc; | ||
|
||
#[cfg(target_os = "macos")] | ||
extern crate cocoa; | ||
|
||
#[cfg(target_os = "macos")] | ||
mod mac; | ||
mod macos; | ||
|
||
#[cfg(target_os = "macos")] | ||
pub use mac::create; | ||
pub use macos::*; | ||
|
||
/** | ||
* WinAPI | ||
*/ | ||
#[cfg(target_os = "windows")] | ||
extern crate user32; | ||
|
||
#[cfg(target_os = "windows")] | ||
extern crate winapi; | ||
|
||
#[cfg(target_os = "windows")] | ||
mod windows; | ||
#[cfg(target_os = "windows")] | ||
pub use windows::create; | ||
pub use windows::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use ::cocoa::base::{id, nil, class}; | ||
use ::cocoa::foundation::NSString; | ||
|
||
use icon::IconType; | ||
|
||
/** | ||
* cocoa-rs doesn't implement NSAlert yet (0.14.0) | ||
* Then implement it! | ||
* Someone would stub and implement all methods for NSAlert, and make it to the upstream? | ||
*/ | ||
|
||
/** | ||
* NSAlert.Style | ||
* https://developer.apple.com/documentation/appkit/nsalert.style | ||
*/ | ||
#[repr(u64)] | ||
#[derive(Clone, Copy, Debug, PartialEq)] | ||
pub enum NSAlertStyle { | ||
warning = 0, // Same visual as informational | ||
informational = 1, | ||
critical = 2 | ||
} | ||
|
||
/** | ||
* NSAlert | ||
* https://developer.apple.com/documentation/appkit/nsalert | ||
*/ | ||
pub trait NSAlert: Sized { | ||
unsafe fn alloc(_: Self) -> id { | ||
msg_send![class("NSAlert"), alloc] | ||
} | ||
|
||
unsafe fn init(self) -> id; | ||
unsafe fn autorelease(self) -> id; | ||
|
||
unsafe fn setAlertStyle(self, style: NSAlertStyle); | ||
unsafe fn setMessageText(self, messageText: id); | ||
unsafe fn setInformativeText(self, informativeText: id); | ||
unsafe fn addButton(self, withTitle: id); | ||
unsafe fn runModal(self) -> id; | ||
} | ||
|
||
impl NSAlert for id { | ||
unsafe fn init(self) -> id { | ||
msg_send![self, init] | ||
} | ||
|
||
unsafe fn autorelease(self) -> id { | ||
msg_send![self, autorelease] | ||
} | ||
|
||
unsafe fn setAlertStyle(self, alertStyle: NSAlertStyle) { | ||
msg_send![self, setAlertStyle: alertStyle] | ||
} | ||
|
||
unsafe fn setMessageText(self, messageText: id) { | ||
msg_send![self, setMessageText: messageText] | ||
} | ||
|
||
unsafe fn setInformativeText(self, informativeText: id) { | ||
msg_send![self, setInformativeText: informativeText] | ||
} | ||
|
||
unsafe fn addButton(self, withTitle: id) { | ||
msg_send![self, addButtonWithTitle: withTitle] | ||
} | ||
|
||
unsafe fn runModal(self) -> id { | ||
msg_send![self, runModal] | ||
} | ||
} | ||
|
||
pub fn create(title:&str, content:&str, icon_type:IconType) { | ||
let alert_style = match icon_type { | ||
IconType::ERROR => NSAlertStyle::critical, | ||
IconType::INFO => NSAlertStyle::informational, | ||
|
||
// AppKit doesn't support NSAlert without any icon | ||
IconType::NONE => NSAlertStyle::informational, | ||
}; | ||
|
||
unsafe { | ||
let alert = NSAlert::alloc(nil).init().autorelease(); | ||
alert.addButton(NSString::alloc(nil).init_str("OK")); | ||
alert.setMessageText(NSString::alloc(nil).init_str(title)); | ||
alert.setInformativeText(NSString::alloc(nil).init_str(content)); | ||
alert.setAlertStyle(alert_style); | ||
alert.runModal(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters