DEVELOPMENT ENVIRONMENT

~liljamo/evenstar

ref: 57c6038d2e09735d1c277cfe558fd301393ce5c0 evenstar/src/app.rs -rw-r--r-- 6.8 KiB
57c6038dJonni Liljamo I lost the old commit history... 1 year, 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * 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;
    }
}