Write config gui

This commit is contained in:
hodasemi 2019-11-22 19:02:27 +01:00
parent fa0ba18d12
commit 3741ad1f29
9 changed files with 564 additions and 0 deletions

2
.gitignore vendored
View file

@ -1,4 +1,6 @@
/target
**/*.rs.bk
*.glade~
Cargo.lock

82
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,82 @@
{
// 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 'service'",
"cargo": {
"args": [
"build",
"--bin=service",
"--package=macroboard"
],
"filter": {
"name": "service",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'service'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=service",
"--package=macroboard"
],
"filter": {
"name": "service",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'gui'",
"cargo": {
"args": [
"build",
"--bin=gui",
"--package=macroboard"
],
"filter": {
"name": "gui",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'gui'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=gui",
"--package=macroboard"
],
"filter": {
"name": "gui",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

19
.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,19 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run Gui",
"type": "shell",
"command": "cargo run --bin gui",
"problemMatcher": []
},
{
"label": "Run Service",
"type": "shell",
"command": "cargo run --bin service",
"problemMatcher": []
}
]
}

View file

@ -4,8 +4,17 @@ version = "0.1.0"
authors = ["hodasemi <michaelh.95@t-online.de>"]
edition = "2018"
[[bin]]
name = "service"
path = "src/service.rs"
[[bin]]
name = "gui"
path = "src/gui.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serial = "*"
gtk = { version = "*", features = ["v3_24"] }
utilities = { git = "http://dimov.cloud:80/hodasemi/Context.git" }

270
macro_ui.glade Normal file
View file

@ -0,0 +1,270 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="MainWindow">
<property name="can_focus">False</property>
<property name="window_position">center-always</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="margin_top">10</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="row_spacing">5</property>
<property name="column_spacing">5</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">10</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
<property name="label" translatable="yes">Button 1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">10</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
<property name="label" translatable="yes">Button 2</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">10</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
<property name="label" translatable="yes">Button 3</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">10</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
<property name="label" translatable="yes">Button 4</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">10</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
<property name="label" translatable="yes">Button 5</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">4</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">10</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
<property name="label" translatable="yes">Button 6</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">5</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">10</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
<property name="label" translatable="yes">Button 7</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">6</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">10</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
<property name="label" translatable="yes">Button 8</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">7</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="ButtonCommand1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">False</property>
<property name="xalign">0.5</property>
<property name="caps_lock_warning">False</property>
<property name="placeholder_text" translatable="yes">Command not set</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="ButtonCommand2">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">False</property>
<property name="xalign">0.5</property>
<property name="caps_lock_warning">False</property>
<property name="placeholder_text" translatable="yes">Command not set</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="ButtonCommand3">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">False</property>
<property name="xalign">0.5</property>
<property name="caps_lock_warning">False</property>
<property name="placeholder_text" translatable="yes">Command not set</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="ButtonCommand4">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">False</property>
<property name="xalign">0.5</property>
<property name="caps_lock_warning">False</property>
<property name="placeholder_text" translatable="yes">Command not set</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">3</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="ButtonCommand5">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">False</property>
<property name="xalign">0.5</property>
<property name="caps_lock_warning">False</property>
<property name="placeholder_text" translatable="yes">Command not set</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">4</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="ButtonCommand6">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">False</property>
<property name="xalign">0.5</property>
<property name="caps_lock_warning">False</property>
<property name="placeholder_text" translatable="yes">Command not set</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">5</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="ButtonCommand7">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">False</property>
<property name="xalign">0.5</property>
<property name="caps_lock_warning">False</property>
<property name="placeholder_text" translatable="yes">Command not set</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">6</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="ButtonCommand8">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">False</property>
<property name="xalign">0.5</property>
<property name="caps_lock_warning">False</property>
<property name="placeholder_text" translatable="yes">Command not set</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">7</property>
</packing>
</child>
<child>
<object class="GtkButton" id="SaveButton">
<property name="label" translatable="yes">Save</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="is_focus">True</property>
<property name="receives_default">True</property>
<property name="margin_left">20</property>
<property name="margin_right">20</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">8</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</interface>

113
src/gui.rs Normal file
View file

@ -0,0 +1,113 @@
use gtk;
use gtk::prelude::*;
use gtk::{Builder, Window};
use utilities::prelude::*;
use std::fs;
use std::path::Path;
mod shared;
use shared::config::{Config, COMMAND_COUNT};
fn main() -> VerboseResult<()> {
gtk::init().map_err(|_| "failed to initialize GTK")?;
let builder = Builder::new_from_string(include_str!("../macro_ui.glade"));
setup_gui(builder)?;
gtk::main();
Ok(())
}
fn setup_gui(builder: Builder) -> VerboseResult<()> {
let config = load_config()?;
apply_config(&builder, &config)?;
setup_save(&builder)?;
let window: Window = builder
.get_object("MainWindow")
.ok_or("failed getting main window")?;
window.show_all();
// close event
window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(false)
});
Ok(())
}
fn load_config() -> VerboseResult<Config> {
let home_dir = std::env::var("HOME").map_err(|_| "failed getting home directory")?;
if !Path::new(&format!("{}/.config", home_dir)).exists() {
fs::create_dir(format!("{}/.config", home_dir))?;
}
if !Path::new(&format!("{}/.config/MacroBoard", home_dir)).exists() {
fs::create_dir(format!("{}/.config/MacroBoard", home_dir))?;
}
Config::open(&format!("{}/.config/MacroBoard/commands.cfg", home_dir))
}
fn setup_save(builder: &Builder) -> VerboseResult<()> {
let mut text_fields = Vec::with_capacity(COMMAND_COUNT);
for i in 0..COMMAND_COUNT {
let command_text_field: gtk::Entry = builder
.get_object(&format!("ButtonCommand{}", i + 1))
.ok_or(format!("Failed getting Entry for command {}", i + 1))?;
text_fields.push(command_text_field);
}
let save_button: gtk::Button = builder
.get_object("SaveButton")
.ok_or("Failed getting save button")?;
save_button.connect_clicked(move |_| {
let mut config = Config::default();
for i in 0..COMMAND_COUNT {
let command = text_fields[i].get_buffer().get_text();
config.commands[i] = Some(command);
}
let home_dir = match std::env::var("HOME") {
Ok(var) => var,
Err(_) => {
println!("failed getting home directory");
return;
}
};
if let Err(err) = config.save(&format!("{}/.config/MacroBoard/commands.cfg", home_dir)) {
println!("{}", err);
}
});
Ok(())
}
fn apply_config(builder: &Builder, config: &Config) -> VerboseResult<()> {
for i in 0..COMMAND_COUNT {
if let Some(command) = &config.commands[i] {
let command_text_field: gtk::Entry = builder
.get_object(&format!("ButtonCommand{}", i + 1))
.ok_or(format!("Failed getting Entry for command {}", i + 1))?;
command_text_field.get_buffer().set_text(command);
}
}
Ok(())
}

68
src/shared/config.rs Normal file
View file

@ -0,0 +1,68 @@
use utilities::prelude::*;
pub const COMMAND_COUNT: usize = 8;
const COMMANDS_TAG: &str = "Commands";
const COMMANDS: [&str; COMMAND_COUNT] = [
"command_1",
"command_2",
"command_3",
"command_4",
"command_5",
"command_6",
"command_7",
"command_8",
];
pub struct Config {
pub commands: [Option<String>; COMMAND_COUNT],
}
impl Config {
pub fn open(path: &str) -> VerboseResult<Config> {
let mut config = Config::default();
let config_loader = match ConfigHandler::read_config(path) {
Ok(config_loader) => config_loader,
Err(_) => return Ok(config),
};
if let Some(commands) = config_loader.get(COMMANDS_TAG) {
for i in 0..COMMAND_COUNT {
if let Some(command) = commands.get(COMMANDS[i]) {
config.commands[i] = Some(command.to_value()?);
}
}
}
Ok(config)
}
pub fn save(&self, path: &str) -> VerboseResult<()> {
let fields = (0..COMMAND_COUNT)
.collect::<Vec<usize>>()
.iter()
.map(|i| {
(
COMMANDS[*i],
match &self.commands[*i] {
Some(command) => Value::from_value(command),
None => Value::empty(),
},
)
})
.collect();
let data = &[(COMMANDS_TAG, fields)];
ConfigHandler::write_config(path, data)
}
}
impl Default for Config {
fn default() -> Config {
Config {
commands: Default::default(),
}
}
}

1
src/shared/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod config;