/*
* This file is part of Evenstar.
* Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
*
* See LICENSE for licensing information.
*/
use eframe::{egui, epi};
use serde::{Serialize, Deserialize};
use serde_json;
use dirs::home_dir;
use rfd::FileDialog;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LinkEntry {
pub id: u16,
pub name: String,
pub desctiption: String,
pub link: String,
}
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct Evenstar {
links: Vec<LinkEntry>,
#[serde(skip)]
adding_link: bool,
#[serde(skip)]
add_link: String,
#[serde(skip)]
add_name: String,
#[serde(skip)]
add_description: String,
}
impl Default for Evenstar {
fn default() -> Self {
Self {
links: Vec::new(),
adding_link: false,
add_link: "".to_owned(),
add_name: "".to_owned(),
add_description: "".to_owned(),
}
}
}
impl epi::App for Evenstar {
fn name(&self) -> &str {
"Evenstar"
}
/// Called once before the first frame.
fn setup(
&mut self,
_ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
) {
// Load previous app state (if any).
if let Some(storage) = _storage {
*self = epi::get_value(storage, epi::APP_KEY).unwrap_or_default()
}
}
/// Called by the frame work to save state before shutdown.
fn save(&mut self, storage: &mut dyn epi::Storage) {
epi::set_value(storage, epi::APP_KEY, self);
}
/// Called each time the UI needs repainting, which may be many times per second.
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
let Self { links, adding_link, add_link, add_name, add_description } = self;
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| {
if ui.button("Quit").clicked() {
frame.quit();
}
});
ui.menu_button("Import / Export", |ui| {
if ui.button("Import").clicked() {
let user_downloads = format!("{}/Downloads", home_dir().unwrap().into_os_string().into_string().unwrap());
let file_path = FileDialog::new()
.set_title("Import Evenstar links")
.set_directory(user_downloads)
.add_filter("JSON", &["json"])
.pick_file();
if let Some(file_path) = file_path {
let file_path = file_path.to_str().unwrap();
let file_contents = std::fs::read_to_string(file_path).unwrap();
let new_links: Vec<LinkEntry> = serde_json::from_str(&file_contents).unwrap();
*links = new_links.clone();
}
}
if ui.button("Export").clicked() {
let json = serde_json::to_string(&links);
let user_downloads = format!("{}/Downloads", home_dir().unwrap().into_os_string().into_string().unwrap());
let file_path = FileDialog::new()
.set_title("Export Evenstar links")
.set_directory(user_downloads)
.add_filter("JSON", &["json"])
.save_file();
std::fs::write(file_path.unwrap().into_os_string().into_string().unwrap(), json.unwrap())
.expect("Failed to write to file");
}
});
});
});
egui::TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| {
ui.horizontal(|ui| {
egui::warn_if_debug_build(ui);
});
});
egui::SidePanel::left("side_panel").show(ctx, |ui| {
ui.heading("Side Panel");
if ui.button("Add Link").clicked() {
*adding_link = true;
}
});
egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's
egui::ScrollArea::vertical().show(ui, |ui| {
egui::Grid::new("link_grid").striped(true).show(ui, |ui| {
for link in links.clone() {
ui.label(link.id.to_string());
ui.hyperlink_to(link.name.to_string(), link.link.to_string());
ui.label(link.desctiption.to_string());
if ui.button("Delete").clicked() {
links.remove(link.id as usize);
reindex(links);
}
ui.end_row();
}
});
});
});
if *adding_link {
egui::Window::new("Add Link").show(ctx, |ui| {
ui.horizontal(|ui| {
ui.label("Link: ");
ui.text_edit_singleline(add_link);
});
ui.horizontal(|ui| {
ui.label("Name: ");
ui.text_edit_singleline(add_name);
});
ui.horizontal(|ui| {
ui.label("Description: ");
ui.text_edit_singleline(add_description);
});
ui.horizontal(|ui| {
if ui.button("Cancel").clicked() {
*add_link = "".to_owned();
*add_name = "".to_owned();
*add_description = "".to_owned();
self.adding_link = false;
}
if ui.button("Add").clicked() {
self.links.push(LinkEntry {
id: self.links.len() as u16,
name: add_name.clone(),
desctiption: add_description.clone(),
link: add_link.clone(),
});
*add_link = "".to_owned();
*add_name = "".to_owned();
*add_description = "".to_owned();
self.adding_link = false;
}
});
});
}
}
}
pub fn reindex(links: &mut Vec<LinkEntry>) {
for (i, link) in links.iter_mut().enumerate() {
link.id = i as u16;
}
}