Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prep 0.9 #49

Merged
merged 6 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rust-analyzer.cargo.features": [
"libappindicator"
]
}
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "tray-item"
version = "0.8.0"
version = "0.9.0"
authors = ["Edwin Svensson <[email protected]>"]
edition = "2018"
edition = "2021"
description = "Super simple API to make tray icons/menus on Windows, Mac & Linux"
homepage = "https://github.com/olback/tray-item-rs"
repository = "https://github.com/olback/tray-item-rs"
Expand All @@ -17,8 +17,8 @@ libappindicator = ["dep:libappindicator", "dep:gtk"]

[dependencies]
ksni = { version = "0.2.0", optional = true }
libappindicator = { version = "0.7", optional = true } # Tray icon
gtk = { version = "0.15", optional = true }
libappindicator = { version = "0.9", optional = true } # Tray icon
gtk = { version = "0.18", optional = true }

[target.'cfg(target_os="windows")'.dependencies]
padlock = "0.2"
Expand All @@ -30,13 +30,13 @@ features = [
"Win32_Graphics_Gdi",
"Win32_System_LibraryLoader",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging"
"Win32_UI_WindowsAndMessaging",
]

[target.'cfg(target_os="macos")'.dependencies]
cocoa = "0.24"
cocoa = "0.25"
objc = "0.2"
core-graphics = "0.22"
core-graphics = "0.23"
objc-foundation = "0.1"
objc_id = "0.1"
libc = "0.2"
Expand All @@ -47,4 +47,4 @@ opt-level = "z"
lto = true
incremental = false
codegen-units = 1
panic = "abort"
panic = "abort"
4 changes: 2 additions & 2 deletions examples/linux-embeded-icon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tray-item = { path="../../", features = [ "ksni" ] }
png = "0.16.8"
tray-item = { path = "../../", features = ["ksni"] }
png = "0.16"
12 changes: 6 additions & 6 deletions examples/linux-gresources/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
name = "linux-gresource-example"
version = "0.1.0"
edition = "2021"
build="build.rs"
build = "build.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tray-item = { path="../../", features = [ "libappindicator" ] }
gtk = "0.15"
gio = "0.15"
glib = "0.15"
tray-item = { path = "../../", features = ["libappindicator"] }
gtk = "0.18"
gio = "0.18"
glib = "0.18"

[build-dependencies]
gio = "0.15"
glib-build-tools = "0.18"
12 changes: 5 additions & 7 deletions examples/linux-gresources/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
extern crate gio;

fn main() {
gio::compile_resources(
"../resources",
"../resources/tray-icon.xml",
"compiled.gresource",
);
glib_build_tools::compile_resources(
&["../resources"],
"../resources/tray-icon.xml",
"compiled.gresource",
);
}
8 changes: 4 additions & 4 deletions examples/linux-gresources/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ fn main() {
Ok(Message::Quit) => {
gtk::main_quit();
println!("Quit!");
glib::Continue(false)
glib::ControlFlow::Break
}
Ok(Message::Green) => {
println!("Green!");
tray.set_icon(IconSource::Resource("/another-name-from-rc-file"))
.unwrap();
glib::Continue(true)
glib::ControlFlow::Continue
}
Ok(Message::Red) => {
println!("Red!");
tray.set_icon(IconSource::Resource("/name-of-icon-in-rc-file"))
.unwrap();
glib::Continue(true)
glib::ControlFlow::Continue
}
_ => {
println!("Default!");
glib::Continue(true)
glib::ControlFlow::Continue
}
});

Expand Down
5 changes: 2 additions & 3 deletions examples/linux-named-resource/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tray-item = { path="../../", features = [ "libappindicator" ] }
gtk = "0.15"

tray-item = { path = "../../", features = ["libappindicator"] }
gtk = "0.18"
2 changes: 2 additions & 0 deletions examples/windows-edit-menu-items/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/

12 changes: 12 additions & 0 deletions examples/windows-edit-menu-items/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "windows-edit-menu-items"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tray-item = { path = "../../" }

[build-dependencies]
windres = "*"
5 changes: 5 additions & 0 deletions examples/windows-edit-menu-items/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use windres::Build;

fn main() {
Build::new().compile("tray-example.rc").unwrap();
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
81 changes: 81 additions & 0 deletions examples/windows-edit-menu-items/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use std::sync::mpsc;
use tray_item::{IconSource, TrayItem};

enum Message {
Quit,
ChangeIcon,
Hello,
}

enum Icon {
Red,
Green,
}

impl Icon {
fn resource(&self) -> IconSource {
match self {
Self::Red => IconSource::Resource("another-name-from-rc-file"),
Self::Green => IconSource::Resource("name-of-icon-in-rc-file"),
}
}
}

fn main() {
let mut tray = TrayItem::new(
"Tray Example",
Icon::Green.resource(),
)
.unwrap();

let label_id = tray.inner_mut().add_label_with_id("Tray Label").unwrap();

tray.inner_mut().add_separator().unwrap();

let (tx, rx) = mpsc::sync_channel(1);

let hello_tx = tx.clone();
tray.add_menu_item("Hello!", move || {
hello_tx.send(Message::Hello).unwrap();
})
.unwrap();

let color_tx = tx.clone();
let color_id = tray.inner_mut().add_menu_item_with_id("Change to Red", move || {
color_tx.send(Message::ChangeIcon).unwrap();
})
.unwrap();
let mut current_icon = Icon::Green;

tray.inner_mut().add_separator().unwrap();

let quit_tx = tx.clone();
tray.add_menu_item("Quit", move || {
quit_tx.send(Message::Quit).unwrap();
})
.unwrap();

loop {
match rx.recv() {
Ok(Message::Quit) => {
println!("Quit");
break;
}
Ok(Message::ChangeIcon) => {
let (next_icon, next_message) = match current_icon {
Icon::Red => (Icon::Green, "Change to Red"),
Icon::Green => (Icon::Red, "Change to Green"),
};
current_icon = next_icon;

tray.inner_mut().set_menu_item_label(next_message, color_id).unwrap();
tray.set_icon(current_icon.resource())
.unwrap();
},
Ok(Message::Hello) => {
tray.inner_mut().set_label("Hi there!", label_id).unwrap();
},
_ => {}
}
}
}
3 changes: 3 additions & 0 deletions examples/windows-edit-menu-items/tray-example.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
aa-exe-icon ICON "icons/app-icon.ico"
name-of-icon-in-rc-file ICON "icons/icon-green.ico"
another-name-from-rc-file ICON "icons/icon-red.ico"
73 changes: 38 additions & 35 deletions src/api/linux_ksni/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use crate::{TIError, IconSource};
use crate::{IconSource, TIError};
use ksni::{menu::StandardItem, Handle, Icon};
use std::sync::Arc;

enum TrayItem {
Label(String),
MenuItem {
label: String,
action: Arc<dyn Fn() + Send + Sync + 'static>
action: Arc<dyn Fn() + Send + Sync + 'static>,
},
Separator
Separator,
}

struct Tray {
title: String,
icon: IconSource,
actions: Vec<TrayItem>
actions: Vec<TrayItem>,
}

pub struct TrayItemLinux {
tray: Handle<Tray>
tray: Handle<Tray>,
}

impl ksni::Tray for Tray {
Expand All @@ -33,44 +33,51 @@ impl ksni::Tray for Tray {
fn icon_name(&self) -> String {
match &self.icon {
IconSource::Resource(name) => name.to_string(),
IconSource::Data{..} => String::new(),
IconSource::Data { .. } => String::new(),
}
}

fn icon_pixmap(&self) -> Vec<Icon> {
match &self.icon {
IconSource::Resource(_) => vec![],
IconSource::Data{data, height, width} => {
IconSource::Data {
data,
height,
width,
} => {
vec![Icon {
width: *height,
height: *width,
data: data.clone()
data: data.clone(),
}]
},
}
}
}

fn menu(&self) -> Vec<ksni::MenuItem<Self>> {
self.actions.iter().map(|item| match item {
TrayItem::Label(label) => StandardItem {
label: label.clone(),
enabled: false,
..Default::default()
}
.into(),
TrayItem::MenuItem { label, action } => {
let action = action.clone();
StandardItem {
self.actions
.iter()
.map(|item| match item {
TrayItem::Label(label) => StandardItem {
label: label.clone(),
activate: Box::new(move |_| {
action();
}),
enabled: false,
..Default::default()
}
.into()
}
TrayItem::Separator => ksni::MenuItem::Separator,
}).collect()
.into(),
TrayItem::MenuItem { label, action } => {
let action = action.clone();
StandardItem {
label: label.clone(),
activate: Box::new(move |_| {
action();
}),
..Default::default()
}
.into()
}
TrayItem::Separator => ksni::MenuItem::Separator,
})
.collect()
}
}

Expand All @@ -79,21 +86,17 @@ impl TrayItemLinux {
let svc = ksni::TrayService::new(Tray {
title: title.to_string(),
icon,
actions: vec![]
actions: vec![],
});

let handle = svc.handle();
svc.spawn();

Ok(Self {
tray: handle
})
Ok(Self { tray: handle })
}

pub fn set_icon(&mut self, icon: IconSource) -> Result<(), TIError> {
self.tray.update(|tray| {
tray.icon = icon.clone()
});
self.tray.update(|tray| tray.icon = icon.clone());

Ok(())
}
Expand All @@ -110,14 +113,14 @@ impl TrayItemLinux {
where
F: Fn() -> () + Send + Sync + 'static,
{
let action = Arc::new(move ||{
let action = Arc::new(move || {
cb();
});

self.tray.update(move |tray| {
tray.actions.push(TrayItem::MenuItem {
label: label.to_string(),
action: action.clone()
action: action.clone(),
});
});
Ok(())
Expand Down
Loading
Loading