update
This commit is contained in:
parent
6fbc41065a
commit
b5b7b25dfa
File diff suppressed because it is too large
Load Diff
|
|
@ -4,8 +4,7 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
eframe = "0.29.1"
|
|
||||||
egui = "0.29.1"
|
|
||||||
tao = "0.30.3"
|
|
||||||
tray-icon = "0.19.0"
|
tray-icon = "0.19.0"
|
||||||
image = "0.25"
|
image = "0.25"
|
||||||
|
notan = { version = "0.12.1", features = ["egui"] }
|
||||||
|
anyhow = "1.0.89"
|
||||||
|
|
|
||||||
133
src/main.rs
133
src/main.rs
|
|
@ -1,67 +1,96 @@
|
||||||
// Copyright 2022-2022 Tauri Programme within The Commons Conservancy
|
use notan::draw::DrawConfig;
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
use notan::egui::{self, *};
|
||||||
// SPDX-License-Identifier: MIT
|
use notan::prelude::*;
|
||||||
|
use tray_icon::menu::MenuEventReceiver;
|
||||||
#![allow(unused)]
|
use tray_icon::TrayIcon;
|
||||||
|
|
||||||
use tao::event_loop::{ControlFlow, EventLoopBuilder};
|
|
||||||
use tray_icon::{
|
use tray_icon::{
|
||||||
menu::{AboutMetadata, Menu, MenuEvent, MenuItem, PredefinedMenuItem},
|
menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem},
|
||||||
TrayIconBuilder, TrayIconEvent,
|
TrayIconBuilder,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
#[derive(AppState)]
|
||||||
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/icon.png");
|
struct State {
|
||||||
|
tray_icon: Option<TrayIcon>,
|
||||||
|
quit_i: MenuItem,
|
||||||
|
menu_channel: MenuEventReceiver,
|
||||||
|
}
|
||||||
|
|
||||||
let event_loop = EventLoopBuilder::new().build();
|
#[notan_main]
|
||||||
|
fn main() -> Result<(), String> {
|
||||||
|
let win = WindowConfig::new()
|
||||||
|
.set_vsync(true)
|
||||||
|
.set_lazy_loop(true)
|
||||||
|
.set_high_dpi(true);
|
||||||
|
|
||||||
|
notan::init_with(init)
|
||||||
|
.add_config(win)
|
||||||
|
.add_config(EguiConfig)
|
||||||
|
.add_config(DrawConfig)
|
||||||
|
.update(update)
|
||||||
|
.draw(draw)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(app: &mut App, state: &mut State) {
|
||||||
|
if let Ok(event) = state.menu_channel.try_recv() {
|
||||||
|
if event.id == state.quit_i.id() {
|
||||||
|
state.tray_icon.take();
|
||||||
|
app.exit();
|
||||||
|
}
|
||||||
|
println!("{event:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins) {
|
||||||
|
let mut output = plugins.egui(|ctx| {
|
||||||
|
egui::SidePanel::left("side_panel").show(ctx, |ui| {
|
||||||
|
ui.heading("Egui Plugin Example");
|
||||||
|
|
||||||
|
ui.separator();
|
||||||
|
if ui.button("Quit").clicked() {
|
||||||
|
app.exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.separator();
|
||||||
|
ui.label("Welcome to a basic example of how to use Egui with notan.");
|
||||||
|
|
||||||
|
ui.separator();
|
||||||
|
ui.label("Check the source code to learn more about how it works");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
output.clear_color(Color::BLACK);
|
||||||
|
gfx.render(&output);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init(_gfx: &mut Graphics) -> State {
|
||||||
|
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/icon.png");
|
||||||
|
|
||||||
let tray_menu = Menu::new();
|
let tray_menu = Menu::new();
|
||||||
|
|
||||||
let test = MenuItem::new("Test", true, None);
|
let test = MenuItem::new("Test", true, None);
|
||||||
let quit_i = MenuItem::new("Quit", true, None);
|
let quit_i = MenuItem::new("Quit", true, None);
|
||||||
tray_menu.append_items(&[&test, &PredefinedMenuItem::separator(), &quit_i]);
|
tray_menu
|
||||||
|
.append_items(&[&test, &PredefinedMenuItem::separator(), &quit_i])
|
||||||
|
.unwrap();
|
||||||
|
let icon = load_icon(std::path::Path::new(path));
|
||||||
|
|
||||||
let mut tray_icon = None;
|
let tray_icon = Some(
|
||||||
|
TrayIconBuilder::new()
|
||||||
|
.with_menu(Box::new(tray_menu.clone()))
|
||||||
|
.with_tooltip("Workspace")
|
||||||
|
.with_icon(icon)
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
let menu_channel = MenuEvent::receiver();
|
let menu_channel = MenuEvent::receiver().clone();
|
||||||
let tray_channel = TrayIconEvent::receiver();
|
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
State {
|
||||||
*control_flow = ControlFlow::Wait;
|
menu_channel,
|
||||||
|
tray_icon,
|
||||||
if let tao::event::Event::NewEvents(tao::event::StartCause::Init) = event {
|
quit_i,
|
||||||
let icon = load_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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(event) = menu_channel.try_recv() {
|
|
||||||
if event.id == quit_i.id() {
|
|
||||||
tray_icon.take();
|
|
||||||
*control_flow = ControlFlow::Exit;
|
|
||||||
}
|
|
||||||
println!("{event:?}");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_icon(path: &std::path::Path) -> tray_icon::Icon {
|
fn load_icon(path: &std::path::Path) -> tray_icon::Icon {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue