-
Notifications
You must be signed in to change notification settings - Fork 33
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
Replace libappindicator with ksni #201
Open
dfaust
wants to merge
3
commits into
tauri-apps:dev
Choose a base branch
from
dfaust:ksni
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2022-2022 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
#![allow(unused)] | ||
|
||
use tao::event_loop::{ControlFlow, EventLoopBuilder}; | ||
use tray_icon::{ | ||
menu::{ | ||
AboutMetadata, CheckMenuItem, IconMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem, | ||
Submenu, | ||
}, | ||
TrayIconBuilder, TrayIconEvent, | ||
}; | ||
|
||
fn main() { | ||
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/icon.png"); | ||
|
||
let event_loop = EventLoopBuilder::new().build(); | ||
|
||
let mut counter = 0; | ||
let tray_menu = Menu::new(); | ||
|
||
let counter_i = MenuItem::new(format!("Counter: {counter}"), true, None); | ||
tray_menu.append_items(&[&counter_i]); | ||
|
||
let mut tray_icon = None; | ||
|
||
let menu_channel = MenuEvent::receiver(); | ||
let tray_channel = TrayIconEvent::receiver(); | ||
|
||
event_loop.run(move |event, _, control_flow| { | ||
// We add delay of 16 ms (60fps) to event_loop to reduce cpu load. | ||
// Alternatively, you can set ControlFlow::Wait or use TrayIconEvent::set_event_handler, | ||
// see https://github.com/tauri-apps/tray-icon/issues/83#issuecomment-1697773065 | ||
*control_flow = ControlFlow::Poll; | ||
std::thread::sleep(std::time::Duration::from_millis(16)); | ||
|
||
if let tao::event::Event::NewEvents(tao::event::StartCause::Init) = event { | ||
let icon = load_tray_icon(std::path::Path::new(path)); | ||
|
||
// We create the icon once the event loop is actually running | ||
// to prevent issues like https://github.com/tauri-apps/tray-icon/issues/90 | ||
tray_icon = Some( | ||
TrayIconBuilder::new() | ||
.with_menu(Box::new(tray_menu.clone())) | ||
.with_tooltip("tao - awesome windowing lib") | ||
.with_icon(icon) | ||
.build() | ||
.unwrap(), | ||
); | ||
|
||
// We have to request a redraw here to have the icon actually show up. | ||
// Tao only exposes a redraw method on the Window so we use core-foundation directly. | ||
#[cfg(target_os = "macos")] | ||
unsafe { | ||
use core_foundation::runloop::{CFRunLoopGetMain, CFRunLoopWakeUp}; | ||
|
||
let rl = CFRunLoopGetMain(); | ||
CFRunLoopWakeUp(rl); | ||
} | ||
} | ||
|
||
counter += 1; | ||
counter_i.set_text(format!("Counter: {counter}")); | ||
}) | ||
} | ||
|
||
fn load_icon(path: &std::path::Path) -> (Vec<u8>, u32, u32) { | ||
let image = image::open(path) | ||
.expect("Failed to open icon path") | ||
.into_rgba8(); | ||
let (width, height) = image.dimensions(); | ||
let rgba = image.into_raw(); | ||
(rgba, width, height) | ||
} | ||
|
||
fn load_tray_icon(path: &std::path::Path) -> tray_icon::Icon { | ||
let (icon_rgba, icon_width, icon_height) = load_icon(path); | ||
tray_icon::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon") | ||
} | ||
|
||
fn load_menu_icon(path: &std::path::Path) -> muda::Icon { | ||
let (icon_rgba, icon_width, icon_height) = load_icon(path); | ||
muda::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon") | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason why this was changed to use thread sleep?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because otherwise the event loop won't wake up after the timeout, only when the user clicks the tray icon.
tauri-apps/tao#988