Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
window: Add support for setting the app icon on the Mac.
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed May 11, 2016
1 parent b6a9f8c commit dd6c8b8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/api/cocoa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use std::str::from_utf8;
use std::sync::Mutex;
use std::ascii::AsciiExt;
use std::ops::Deref;
use std::path::PathBuf;

use events::ElementState::{Pressed, Released};
use events::Event::{Awakened, MouseInput, MouseMoved, ReceivedCharacter, KeyboardInput};
Expand Down Expand Up @@ -298,7 +299,8 @@ impl Window {
_ => ()
}

let app = match Window::create_app(pl_attribs.activation_policy) {
let app = match Window::create_app(pl_attribs.activation_policy,
win_attribs.icon.clone()) {
Some(app) => app,
None => { return Err(OsError(format!("Couldn't create NSApplication"))); },
};
Expand Down Expand Up @@ -363,13 +365,22 @@ impl Window {
Ok(window)
}

fn create_app(activation_policy: ActivationPolicy) -> Option<id> {
fn create_app(activation_policy: ActivationPolicy, icon_path: Option<PathBuf>) -> Option<id> {
unsafe {
let app = NSApp();
if app == nil {
None
} else {
app.setActivationPolicy_(activation_policy.into());
if let Some(icon_path) = icon_path {
if let Some(icon_path) = icon_path.to_str() {
let icon_path = NSString::alloc(nil).init_str(icon_path);
let icon = NSImage::alloc(nil).initByReferencingFile_(icon_path);
if icon.isValid() != NO {
app.setApplicationIconImage_(icon)
}
}
}
app.finishLaunching();
Some(app)
}
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub use native_monitor::NativeMonitorId;
use std::io;
#[cfg(not(target_os = "macos"))]
use std::cmp::Ordering;
use std::path::PathBuf;

mod api;
mod platform;
Expand Down Expand Up @@ -551,6 +552,12 @@ pub struct WindowAttributes {
/// (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/multipleTouchEnabled)
pub multitouch: bool,

/// A path to an icon for the window. This may not be supported on every windowing system.
/// If present, this path must reference a PNG file.
///
/// The default is `None`.
pub icon: Option<PathBuf>,

/// Parent Window.
///
/// The default is `None`.
Expand All @@ -570,6 +577,7 @@ impl Default for WindowAttributes {
transparent: false,
decorations: true,
multitouch: false,
icon: None,
parent: None,
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::vec_deque::IntoIter as VecDequeIter;
use std::default::Default;
use std::path::PathBuf;

use Api;
use ContextError;
Expand Down Expand Up @@ -202,6 +203,13 @@ impl<'a> WindowBuilder<'a> {
self
}

/// Sets the icon for the window. The supplied path must reference a PNG file.
#[inline]
pub fn with_icon(mut self, icon_path: PathBuf) -> WindowBuilder<'a> {
self.window.icon = Some(icon_path);
self
}

/// Sets the parent window
pub fn with_parent(mut self, parent: Option<WindowID>) -> WindowBuilder<'a> {
self.window.parent = parent;
Expand Down

0 comments on commit dd6c8b8

Please sign in to comment.