Compare commits

...

2 Commits

Author SHA1 Message Date
Steve Biedermann 94bcff177e update 2024-10-08 17:55:54 +02:00
Steve Biedermann 8a07184fd2 update 2024-10-08 17:04:06 +02:00
4 changed files with 3156 additions and 115 deletions

45
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'workspace'",
"cargo": {
"args": [
"build",
"--bin=workspace",
"--package=workspace"
],
"filter": {
"name": "workspace",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'workspace'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=workspace",
"--package=workspace"
],
"filter": {
"name": "workspace",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

2992
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,5 +6,13 @@ edition = "2021"
[dependencies] [dependencies]
tray-icon = "0.19.0" tray-icon = "0.19.0"
image = "0.25" image = "0.25"
notan = { version = "0.12.1", features = ["egui"] } notan = { version = "0.12.1", features = ["audio", "clipboard", "drop_files", "egui", "extra", "links", "notan_extra", "save_file", "serde", "text"] }
anyhow = "1.0.89" anyhow = "1.0.89"
notan_extra = "0.12.1"
tokio = { version = "1.40.0", features = ["full"] }
indexmap = { version = "2.6.0", features = ["serde"] }
serde = { version = "1.0.210", features = ["derive"] }
rsn = "0.1.0"
known-folders = "1.2.0"
eframe = "0.29.1"
egui = "0.29.1"

View File

@ -1,61 +1,183 @@
use notan::draw::DrawConfig; #![allow(dead_code)]
use notan::egui::{self, *}; #![allow(unused_imports)]
use notan::prelude::*;
use tray_icon::menu::MenuEventReceiver; use std::path::PathBuf;
use tray_icon::TrayIcon; use std::str::FromStr;
use indexmap::IndexSet;
use notan::{
draw::DrawConfig,
egui::{self, *},
extra::FpsLimit,
log::warn,
prelude::*,
Event,
};
use serde::{Deserialize, Serialize};
use tokio::process::Command;
use tray_icon::{ use tray_icon::{
menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem}, menu::{Menu, MenuEvent, MenuEventReceiver, MenuItem, PredefinedMenuItem},
TrayIconBuilder, MouseButton, TrayIcon, TrayIconBuilder, TrayIconEvent, TrayIconEventReceiver,
}; };
#[derive(AppState)] #[derive(Serialize, Deserialize, Hash, PartialEq, Eq)]
struct State { struct LogFile {
name: String,
path: PathBuf,
}
struct TrayData {
tray_icon: Option<TrayIcon>, tray_icon: Option<TrayIcon>,
quit_i: MenuItem, quit_i: MenuItem,
menu_channel: MenuEventReceiver, menu_channel: MenuEventReceiver,
tray_channel: TrayIconEventReceiver,
} }
#[notan_main] impl Default for TrayData {
fn main() -> Result<(), String> { fn default() -> Self {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/icon.png");
let tray_menu = Menu::new();
let test = MenuItem::new("Test", true, None);
let quit_i = MenuItem::new("Quit", true, None);
tray_menu
.append_items(&[&test, &PredefinedMenuItem::separator(), &quit_i])
.unwrap();
let icon = load_icon(std::path::Path::new(path));
let tray_icon = Some(
TrayIconBuilder::new()
.with_menu_on_left_click(false)
.with_menu(Box::new(tray_menu.clone()))
.with_tooltip("Workspace")
.with_icon(icon)
.build()
.unwrap(),
);
let menu_channel = MenuEvent::receiver().clone();
let tray_channel = TrayIconEvent::receiver().clone();
TrayData {
menu_channel,
tray_icon,
quit_i,
tray_channel,
}
}
}
#[derive(AppState, Serialize, Deserialize)]
struct State {
#[serde(skip)]
tray_data: TrayData,
log_history: IndexSet<LogFile>,
}
impl State {
fn save(&mut self) {
let folder = known_folders::get_known_folder_path(known_folders::KnownFolder::LocalAppData)
.unwrap()
.join("workspace");
std::fs::create_dir_all(&folder).unwrap();
std::fs::write(folder.join("data.rsn"), rsn::to_string(self)).unwrap();
}
}
#[tokio::main]
async fn main() -> Result<(), String> {
let win = WindowConfig::new() let win = WindowConfig::new()
.set_vsync(true) .set_vsync(false)
.set_lazy_loop(true) .set_lazy_loop(true)
.set_transparent(true)
.set_high_dpi(true); .set_high_dpi(true);
notan::init_with(init) notan::init_with(init)
.add_config(win) .add_config(win)
.add_config(EguiConfig) .add_config(EguiConfig)
.add_config(DrawConfig) .add_config(DrawConfig)
.add_plugin(FpsLimit::new(60))
.update(update) .update(update)
.event(event)
.draw(draw) .draw(draw)
.build() .build()
} }
fn update(app: &mut App, state: &mut State) { fn update(app: &mut App, state: &mut State) {
if let Ok(event) = state.menu_channel.try_recv() { if let Ok(event) = MenuEvent::receiver().try_recv() {
if event.id == state.quit_i.id() { if event.id == state.tray_data.quit_i.id() {
state.tray_icon.take(); state.tray_data.tray_icon.take();
app.exit(); app.exit();
} }
println!("{event:?}");
} }
} }
fn draw(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins) { fn event(_app: &mut App, _assets: &mut Assets, state: &mut State, evt: Event) {
match evt {
Event::Exit => {}
Event::Drop(file) => {
if file.name.ends_with(".log") {
state.log_history.shift_insert(
0,
LogFile {
name: file.name.clone(),
path: file
.path
.unwrap_or_else(|| PathBuf::from_str(&file.name).unwrap()),
},
);
state.save();
}
}
_ => {}
}
}
fn draw(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut State) {
let mut output = plugins.egui(|ctx| { let mut output = plugins.egui(|ctx| {
egui::SidePanel::left("side_panel").show(ctx, |ui| { egui::SidePanel::left("side_panel").show(ctx, |ui| {
ui.heading("Egui Plugin Example"); ui.collapsing("Logs", |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
ui.separator(); let mut to_swap = None;
if ui.button("Quit").clicked() { let mut to_delete = None;
app.exit(); let history_len = state.log_history.len();
} for (i, log) in state.log_history.iter().enumerate() {
let path_text = log.path.to_string_lossy().to_string();
ui.separator(); let label = ui
ui.label("Welcome to a basic example of how to use Egui with notan."); .add(Label::new(&log.name).sense(Sense::click()))
.on_hover_text(&path_text);
ui.separator(); label.context_menu(|ui| {
ui.label("Check the source code to learn more about how it works"); if ui.button("Open in Notepad").clicked() {
_ = Command::new("notepad.exe").arg(&path_text).spawn();
ui.close_menu();
}
if ui.button("Remove").clicked() {
to_delete = Some(i);
ui.close_menu();
}
});
if label.clicked() {
_ = Command::new("C:\\tools\\fast-log-viewer\\fast_log_viewer.exe")
.arg(path_text)
.spawn();
to_swap = Some(i);
}
if i < history_len - 1 {
ui.separator();
}
}
if let Some(i) = to_swap {
if let Some(value) = state.log_history.swap_remove_index(i) {
state.log_history.shift_insert(0, value);
}
state.save();
}
if let Some(i) = to_delete {
_ = state.log_history.shift_remove_index(i);
state.save();
}
})
})
}); });
}); });
@ -64,33 +186,27 @@ fn draw(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins) {
} }
fn init(_gfx: &mut Graphics) -> State { fn init(_gfx: &mut Graphics) -> State {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/icon.png"); let data = known_folders::get_known_folder_path(known_folders::KnownFolder::LocalAppData)
.unwrap()
.join("workspace")
.join("data.rsn");
let tray_menu = Menu::new(); let mut state = None;
if data.exists() {
let test = MenuItem::new("Test", true, None); match std::fs::read_to_string(data) {
let quit_i = MenuItem::new("Quit", true, None); Ok(data) => {
tray_menu state = Some(rsn::from_str(&data).unwrap());
.append_items(&[&test, &PredefinedMenuItem::separator(), &quit_i]) }
.unwrap(); Err(err) => {
let icon = load_icon(std::path::Path::new(path)); warn!("Couldn't read data file: {}", err);
}
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().clone();
State {
menu_channel,
tray_icon,
quit_i,
} }
state.unwrap_or_else(|| State {
tray_data: TrayData::default(),
log_history: IndexSet::new(),
})
} }
fn load_icon(path: &std::path::Path) -> tray_icon::Icon { fn load_icon(path: &std::path::Path) -> tray_icon::Icon {