Draw ui
This commit is contained in:
48
custom_consumption/src/app_config.rs
Normal file
48
custom_consumption/src/app_config.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use clap::Parser;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Solar system central backend
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
pub struct AppConfig {
|
||||
/// The path to the file to write to update the value
|
||||
#[clap(short, long, default_value = "/dev/shm/consumption.txt")]
|
||||
file_path: String,
|
||||
|
||||
/// Minimal value
|
||||
#[clap( long, default_value_t = -5000)]
|
||||
pub min: i64,
|
||||
|
||||
/// Maximal value
|
||||
#[clap(long, default_value_t = 5000)]
|
||||
pub max: i64,
|
||||
}
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref ARGS: AppConfig = {
|
||||
AppConfig::parse()
|
||||
};
|
||||
}
|
||||
|
||||
impl AppConfig {
|
||||
/// Get parsed command line arguments
|
||||
pub fn get() -> &'static AppConfig {
|
||||
&ARGS
|
||||
}
|
||||
|
||||
/// Path to the file to update the value
|
||||
pub fn file_path(&self) -> PathBuf {
|
||||
Path::new(&self.file_path).to_path_buf()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::app_config::AppConfig;
|
||||
|
||||
#[test]
|
||||
fn verify_cli() {
|
||||
use clap::CommandFactory;
|
||||
AppConfig::command().debug_assert()
|
||||
}
|
||||
}
|
1
custom_consumption/src/lib.rs
Normal file
1
custom_consumption/src/lib.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod app_config;
|
52
custom_consumption/src/main.rs
Normal file
52
custom_consumption/src/main.rs
Normal file
@ -0,0 +1,52 @@
|
||||
use custom_consumption::app_config::AppConfig;
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default().with_inner_size([220.0, 60.0]),
|
||||
..Default::default()
|
||||
};
|
||||
eframe::run_native(
|
||||
"Custom consumption",
|
||||
options,
|
||||
Box::new(|_cc| Box::<MyApp>::default()),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
struct MyApp {
|
||||
consumption: i64,
|
||||
}
|
||||
|
||||
impl Default for MyApp {
|
||||
fn default() -> Self {
|
||||
let mut prev_consumption = 42;
|
||||
|
||||
if AppConfig::get().file_path().is_file() {
|
||||
prev_consumption = std::fs::read_to_string(AppConfig::get().file_path())
|
||||
.unwrap_or("43".to_string())
|
||||
.parse()
|
||||
.unwrap_or(44)
|
||||
}
|
||||
|
||||
Self {
|
||||
consumption: prev_consumption,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for MyApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("Custom consumption");
|
||||
ui.add(
|
||||
egui::Slider::new(
|
||||
&mut self.consumption,
|
||||
AppConfig::get().min..=AppConfig::get().max,
|
||||
)
|
||||
.text("W"),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user