Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: qdot/systray-rs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: snapview/systray-rs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 5 commits
  • 5 files changed
  • 1 contributor

Commits on Mar 1, 2017

  1. Copy the full SHA
    26f40fc View commit details
  2. Copy the full SHA
    5d69d63 View commit details

Commits on Mar 6, 2017

  1. Copy the full SHA
    46c5a40 View commit details

Commits on Jul 20, 2018

  1. Copy the full SHA
    e635450 View commit details

Commits on Dec 10, 2019

  1. Copy the full SHA
    8f4d433 View commit details
Showing with 88 additions and 12 deletions.
  1. +1 −2 Cargo.toml
  2. BIN examples/rust-logo.png
  3. +10 −1 examples/{systray-example.rs → trayicon.rs}
  4. +70 −8 src/api/cocoa/mod.rs
  5. +7 −1 src/lib.rs
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -5,15 +5,14 @@ authors = ["Kyle Machulis <kyle@machul.is>"]

[dependencies]
log = "*"
libc = "*"

[target.'cfg(target_os = "windows")'.dependencies]
kernel32-sys = { git = "https://github.com/application-developer-DA/winapi-rs.git", branch = "0.2" }
shell32-sys = { git = "https://github.com/application-developer-DA/winapi-rs.git", branch = "0.2" }
user32-sys = { git = "https://github.com/application-developer-DA/winapi-rs.git", branch = "0.2" }
winapi = { git = "https://github.com/application-developer-DA/winapi-rs.git", branch = "0.2" }

libc = "*"

[target.'cfg(target_os = "macos")'.dependencies]
objc = "*"
cocoa = "*"
Binary file added examples/rust-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion examples/systray-example.rs → examples/trayicon.rs
Original file line number Diff line number Diff line change
@@ -29,5 +29,14 @@ fn main() {

#[cfg(not(target_os = "windows"))]
fn main() {
panic!("Not implemented on this platform!");
let mut app;
match systray::Application::new() {
Ok(w) => app = w,
Err(e) => panic!("Can't create tray icon app!")
}

const ICON_BUFFER: &'static [u8] = include_bytes!("rust-logo.png");
let mut w = &mut app.window;
w.set_icon_from_buffer(ICON_BUFFER, 256, 256).unwrap();
w.wait_for_message();
}
78 changes: 70 additions & 8 deletions src/api/cocoa/mod.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,90 @@
//! Contains the implementation of the Mac OS X tray icon in the top bar.
use std;
use {SystrayError};

use cocoa::appkit::{NSApp, NSApplication, NSButton, NSImage, NSStatusBar, NSStatusItem,
NSSquareStatusItemLength};
use cocoa::base::{id, nil};
use cocoa::foundation::{NSData, NSSize, NSAutoreleasePool};

use SystrayError;

/// The generation representation of the Mac OS X application.
pub struct Window {
/// A mutable reference to the `NSApplication` instance of the currently running application.
application: id,
/// It seems that we have to use `NSAutoreleasePool` to prevent memory leaks.
autorelease_pool: id,
}

impl Window {
/// Creates a new instance of the `Window`.
pub fn new() -> Result<Window, SystrayError> {
Err(SystrayError::NotImplementedError)
Ok(Window {
application: unsafe { NSApp() },
autorelease_pool: unsafe { NSAutoreleasePool::new(nil) },
})
}

/// Closes the current application.
pub fn quit(&self) {
unimplemented!()
let _: () = unsafe { msg_send![self.application, terminate] };
}

/// Sets the tooltip (not available for this platfor).
pub fn set_tooltip(&self, _: &String) -> Result<(), SystrayError> {
unimplemented!()
Err(SystrayError::OsError("This operating system does not support tooltips for the tray \
items".to_owned()))
}

/// Adds an additional item to the tray icon menu.
pub fn add_menu_item<F>(&self, _: &String, _: F) -> Result<u32, SystrayError>
where F: std::ops::Fn(&Window) -> () + 'static
{
unimplemented!()
}
pub fn wait_for_message(&mut self) {
unimplemented!()

/// Sets the application icon displayed in the tray bar. Accepts a `buffer` to the underlying
/// image, you can pass even encoded PNG images here. Supports the same list of formats as
/// `NSImage`.
pub fn set_icon_from_buffer(&mut self, buffer: &'static [u8], _: u32, _: u32)
-> Result<(), SystrayError>
{
const ICON_WIDTH: f64 = 18.0;
const ICON_HEIGHT: f64 = 18.0;

let tray_entry = unsafe {
NSStatusBar::systemStatusBar(nil).statusItemWithLength_(NSSquareStatusItemLength)
.autorelease()
};

let nsdata = unsafe {
NSData::dataWithBytes_length_(nil,
buffer.as_ptr() as *const std::os::raw::c_void,
buffer.len() as u64).autorelease()
};
if nsdata == nil {
return Err(SystrayError::OsError("Could not create `NSData` out of the passed buffer"
.to_owned()));
}

let nsimage = unsafe { NSImage::initWithData_(NSImage::alloc(nil), nsdata).autorelease() };
if nsimage == nil {
return Err(SystrayError::OsError("Could not create `NSImage` out of the created \
`NSData` buffer".to_owned()));
}

unsafe {
let new_size = NSSize::new(ICON_WIDTH, ICON_HEIGHT);
let _: () = msg_send![nsimage, setSize:new_size];
tray_entry.button().setImage_(nsimage);
}

Ok(())
}
pub fn set_icon_from_buffer(&self, _: &[u8], _: u32, _: u32) -> Result<(), SystrayError> {
unimplemented!()

/// Starts the application event loop. Calling this function will block the current thread.
pub fn wait_for_message(&mut self) {
unsafe { self.application.run() };
}
}
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,13 @@ extern crate kernel32;
extern crate user32;
#[cfg(target_os = "windows")]
extern crate shell32;
#[cfg(target_os = "windows")]

#[cfg(target_os = "macos")]
extern crate cocoa;
#[cfg(target_os = "macos")]
#[macro_use]
extern crate objc;

extern crate libc;

pub mod api;