widgets/configuration: break out configuration functionality
* handle widget `configuration` in dedicated subdir * adopt constants to handle widget id's Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
@@ -15,6 +15,12 @@ pub static STYLE_BUTTON_MENU: &str = "button_menu";
|
||||
pub static STYLE_STACK_MENU: &str = "stack_menu";
|
||||
|
||||
// Widget IDs (DCES: Entity[id] => [Component1, .. , Component<n>] -> data or state)
|
||||
pub static ID_CONFIGURATION_FORM: &str = "configuration_form";
|
||||
pub static ID_CONFIGURATION_HEADER: &str = "configuration_header";
|
||||
pub static ID_CONFIGURATION_LABEL_CONFIG_FILE: &str = "configuration_label_config_file";
|
||||
pub static ID_CONFIGURATION_CONFIG_FILE: &str = "configuration_config_file";
|
||||
pub static ID_CONFIGURATION_LABEL_LANGUAGE_ID: &str = "configuration_label_language_id";
|
||||
pub static ID_CONFIGURATION_LANGUAGE_ID: &str = "configuration_language_id";
|
||||
|
||||
pub static ID_LOCALIZATION_FORM: &str = "localization_form";
|
||||
pub static ID_LOCALIZATION_HEADER: &str = "localization_header";
|
||||
|
||||
73
advotracker/src/widgets/configuration/configuration_state.rs
Normal file
73
advotracker/src/widgets/configuration/configuration_state.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* advotracker - Hotline tackingtool for Advocats
|
||||
*
|
||||
* Copyright 2020 Ralf Zerres <ralf.zerres@networkx.de>
|
||||
* SPDX-License-Identifier: (0BSD or MIT)
|
||||
*/
|
||||
|
||||
use orbtk::prelude::*;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::widgets::configuration::configuration_view::ConfigurationView;
|
||||
|
||||
/// Valid `actions` that are handled as state changes in the `Configuration` widget.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ConfigurationAction {
|
||||
SaveConfiguration,
|
||||
LoadConfiguration,
|
||||
}
|
||||
|
||||
/// Define valid configuration data.
|
||||
/// This structure is serialized and saved inside the OS dependent settings file.
|
||||
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||
struct ConfigurationData(
|
||||
pub String,
|
||||
pub String
|
||||
);
|
||||
|
||||
/// Valid `structures` that are handled inside the state of the `Configuration` widget.
|
||||
#[derive(Debug, Default, AsAny)]
|
||||
pub struct ConfigurationState {}
|
||||
|
||||
impl State for ConfigurationState {
|
||||
fn messages(
|
||||
&mut self,
|
||||
mut messages: MessageReader,
|
||||
registry: &mut Registry,
|
||||
ctx: &mut Context<'_>,
|
||||
) {
|
||||
for message in messages.read::<ConfigurationAction>() {
|
||||
match message {
|
||||
ConfigurationAction::LoadConfiguration => registry
|
||||
.get::<Settings>("settings")
|
||||
.load_async::<ConfigurationData>("configuration_data".to_string(), ctx.entity()),
|
||||
ConfigurationAction::SaveConfiguration => {
|
||||
let configuration_file: String = ConfigurationView::configuration_file_clone(&ctx.widget());
|
||||
let language_id: String = ConfigurationView::language_id_clone(&ctx.widget());
|
||||
registry.get::<Settings>("settings").save_async(
|
||||
"configuration_data".to_string(),
|
||||
ConfigurationData(
|
||||
configuration_file,
|
||||
language_id
|
||||
),
|
||||
ctx.entity(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// save result
|
||||
for message in messages.read::<SettingsResult<()>>() {
|
||||
println!("Result {:?}", message);
|
||||
}
|
||||
|
||||
// load result
|
||||
for message in messages.read::<SettingsResult<ConfigurationData>>() {
|
||||
if let Ok(data) = message {
|
||||
ConfigurationView::configuration_file_set(&mut ctx.widget(), data.0);
|
||||
ConfigurationView::language_id_set(&mut ctx.widget(), data.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
139
advotracker/src/widgets/configuration/configuration_view.rs
Normal file
139
advotracker/src/widgets/configuration/configuration_view.rs
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* advotracker - Hotline tackingtool for Advocats
|
||||
*
|
||||
* Copyright 2020 Ralf Zerres <ralf.zerres@networkx.de>
|
||||
* SPDX-License-Identifier: (0BSD or MIT)
|
||||
*/
|
||||
|
||||
use orbtk::prelude::*;
|
||||
|
||||
use crate::{
|
||||
data::constants::*,
|
||||
widgets::configuration::configuration_state::{ConfigurationAction, ConfigurationState},
|
||||
};
|
||||
|
||||
// Macro that initializes the widget structures/variables for our view
|
||||
widget!(
|
||||
ConfigurationView<ConfigurationState> {
|
||||
configuration_file: String,
|
||||
language_id: String
|
||||
}
|
||||
);
|
||||
|
||||
/// The template implementation of the configuration view
|
||||
/// All GUI elements are styled using the "style" attribute referencing to a ron based css
|
||||
impl Template for ConfigurationView {
|
||||
fn template(self, id: Entity, ctx: &mut BuildContext<'_>) -> Self {
|
||||
self.child(
|
||||
Grid::new()
|
||||
.id(ID_CONFIGURATION_FORM)
|
||||
.margin(4)
|
||||
.columns(
|
||||
Columns::create()
|
||||
.push(120)
|
||||
.push(12)
|
||||
.push("auto")
|
||||
)
|
||||
.rows(Rows::create()
|
||||
.push("auto")
|
||||
.push(4)
|
||||
.push("auto")
|
||||
.push(4)
|
||||
.push("auto")
|
||||
.push(12)
|
||||
.push("auto"),
|
||||
)
|
||||
.child(
|
||||
TextBlock::new()
|
||||
.id(ID_CONFIGURATION_HEADER)
|
||||
.h_align("start")
|
||||
.attach(Grid::column(0))
|
||||
.attach(Grid::row(0))
|
||||
.attach(Grid::column_span(3))
|
||||
.text("Configuration settings")
|
||||
.style("header")
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
TextBlock::new()
|
||||
.id(ID_CONFIGURATION_CONFIG_FILE)
|
||||
.attach(Grid::column(0))
|
||||
.attach(Grid::row(2))
|
||||
.v_align("center")
|
||||
.h_align("end")
|
||||
.text("Configuration file")
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
TextBox::new()
|
||||
.id(ID_CONFIGURATION_CONFIG_FILE)
|
||||
.attach(Grid::column(2))
|
||||
.attach(Grid::row(2))
|
||||
.text(("configuration_file", id))
|
||||
.water_mark("Filename...")
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
TextBlock::new()
|
||||
.id(ID_CONFIGURATION_LABEL_LANGUAGE_ID)
|
||||
.attach(Grid::column(0))
|
||||
.attach(Grid::row(4))
|
||||
.v_align("center")
|
||||
.h_align("end")
|
||||
.text("Language Id")
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
TextBox::new()
|
||||
.id(ID_CONFIGURATION_LANGUAGE_ID)
|
||||
.attach(Grid::column(2))
|
||||
.attach(Grid::row(4))
|
||||
.text(("language_id", id))
|
||||
.water_mark("Locale Identifier...")
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
Grid::new()
|
||||
.h_align("center")
|
||||
.attach(Grid::column(0))
|
||||
.attach(Grid::row(6))
|
||||
.attach(Grid::column_span(3))
|
||||
.columns(
|
||||
Columns::create()
|
||||
.push("auto")
|
||||
.push(8)
|
||||
.push("auto")
|
||||
)
|
||||
.rows(Rows::create()
|
||||
.push("auto")
|
||||
)
|
||||
.child(
|
||||
Button::new()
|
||||
.style("button_single_content")
|
||||
.attach(Grid::column(0))
|
||||
.attach(Grid::row(0))
|
||||
.text("load")
|
||||
.on_click(move |ctx, _| {
|
||||
ctx.send_message(ConfigurationAction::LoadConfiguration, id);
|
||||
true
|
||||
})
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
Button::new()
|
||||
.text("save")
|
||||
.style("button_single_content")
|
||||
.attach(Grid::column(2))
|
||||
.attach(Grid::row(0))
|
||||
.on_click(move |ctx, _| {
|
||||
ctx.send_message(ConfigurationAction::SaveConfiguration, id);
|
||||
true
|
||||
})
|
||||
.build(ctx),
|
||||
)
|
||||
.build(ctx),
|
||||
)
|
||||
.build(ctx),
|
||||
)
|
||||
}
|
||||
}
|
||||
12
advotracker/src/widgets/configuration/mod.rs
Normal file
12
advotracker/src/widgets/configuration/mod.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* advotracker - Hotline tackingtool for Advocats
|
||||
*
|
||||
* Copyright 2020 Ralf Zerres <ralf.zerres@networkx.de>
|
||||
* SPDX-License-Identifier: (0BSD or MIT)
|
||||
*/
|
||||
|
||||
/// The configuration state
|
||||
pub mod configuration_state;
|
||||
|
||||
/// The configuration view
|
||||
pub mod configuration_view;
|
||||
@@ -11,6 +11,7 @@ use crate::{
|
||||
data::structures::PolicyCheck,
|
||||
widgets::policycheck::policycheck_view::PolicycheckView,
|
||||
widgets::localization::localization_view::LocalizationView,
|
||||
widgets::configuration::configuration_view::ConfigurationView,
|
||||
};
|
||||
|
||||
// [START] views
|
||||
@@ -61,190 +62,3 @@ impl Template for MainView {
|
||||
//.child(policycheck_view)
|
||||
}
|
||||
}
|
||||
|
||||
widget!(
|
||||
ConfigurationView<ConfigurationState> {
|
||||
configuration_file: String,
|
||||
language_id: String
|
||||
}
|
||||
);
|
||||
|
||||
impl Template for ConfigurationView {
|
||||
fn template(self, id: Entity, ctx: &mut BuildContext<'_>) -> Self {
|
||||
self.child(
|
||||
Grid::new()
|
||||
.margin(4)
|
||||
.columns(
|
||||
Columns::create()
|
||||
.push(120)
|
||||
.push(12)
|
||||
.push("auto")
|
||||
)
|
||||
.rows(Rows::create()
|
||||
.push("auto")
|
||||
.push(4)
|
||||
.push("auto")
|
||||
.push(4)
|
||||
.push("auto")
|
||||
.push(12)
|
||||
.push("auto"),
|
||||
)
|
||||
.child(
|
||||
TextBlock::new()
|
||||
.h_align("start")
|
||||
.attach(Grid::column(0))
|
||||
.attach(Grid::row(0))
|
||||
.attach(Grid::column_span(3))
|
||||
.text("Configuration settings")
|
||||
.style("header")
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
TextBlock::new()
|
||||
.attach(Grid::column(0))
|
||||
.attach(Grid::row(2))
|
||||
.v_align("center")
|
||||
.h_align("end")
|
||||
.text("Configuration file")
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
TextBox::new()
|
||||
.attach(Grid::column(2))
|
||||
.attach(Grid::row(2))
|
||||
.text(("configuration_file", id))
|
||||
.water_mark("Filename...")
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
TextBlock::new()
|
||||
.attach(Grid::column(0))
|
||||
.attach(Grid::row(4))
|
||||
.v_align("center")
|
||||
.h_align("end")
|
||||
.text("Language Id")
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
TextBox::new()
|
||||
.attach(Grid::column(2))
|
||||
.attach(Grid::row(4))
|
||||
.text(("language_id", id))
|
||||
.water_mark("Locale Identifier...")
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
Grid::new()
|
||||
.h_align("center")
|
||||
.attach(Grid::column(0))
|
||||
.attach(Grid::row(6))
|
||||
.attach(Grid::column_span(3))
|
||||
.columns(
|
||||
Columns::create()
|
||||
.push("auto")
|
||||
.push(8)
|
||||
.push("auto")
|
||||
)
|
||||
.rows(Rows::create()
|
||||
.push("auto")
|
||||
)
|
||||
.child(
|
||||
Button::new()
|
||||
.style("button_single_content")
|
||||
.attach(Grid::column(0))
|
||||
.attach(Grid::row(0))
|
||||
.text("load")
|
||||
.on_click(move |ctx, _| {
|
||||
ctx.send_message(ConfigurationAction::LoadConfiguration, id);
|
||||
true
|
||||
})
|
||||
.build(ctx),
|
||||
)
|
||||
.child(
|
||||
Button::new()
|
||||
.text("save")
|
||||
.style("button_single_content")
|
||||
.attach(Grid::column(2))
|
||||
.attach(Grid::row(0))
|
||||
.on_click(move |ctx, _| {
|
||||
ctx.send_message(ConfigurationAction::SaveConfiguration, id);
|
||||
true
|
||||
})
|
||||
.build(ctx),
|
||||
)
|
||||
.build(ctx),
|
||||
)
|
||||
.build(ctx),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// [END] views
|
||||
|
||||
// [START] states
|
||||
|
||||
|
||||
#[derive(Debug, Default, AsAny)]
|
||||
struct ConfigurationState {}
|
||||
|
||||
impl State for ConfigurationState {
|
||||
fn messages(
|
||||
&mut self,
|
||||
mut messages: MessageReader,
|
||||
registry: &mut Registry,
|
||||
ctx: &mut Context<'_>,
|
||||
) {
|
||||
for message in messages.read::<ConfigurationAction>() {
|
||||
match message {
|
||||
ConfigurationAction::LoadConfiguration => registry
|
||||
.get::<Settings>("settings")
|
||||
.load_async::<ConfigurationData>("configuration_data".to_string(), ctx.entity()),
|
||||
ConfigurationAction::SaveConfiguration => {
|
||||
let configuration_file: String = ConfigurationView::configuration_file_clone(&ctx.widget());
|
||||
let language_id: String = ConfigurationView::language_id_clone(&ctx.widget());
|
||||
registry.get::<Settings>("settings").save_async(
|
||||
"configuration_data".to_string(),
|
||||
ConfigurationData(
|
||||
configuration_file,
|
||||
language_id
|
||||
),
|
||||
ctx.entity(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// save result
|
||||
for message in messages.read::<SettingsResult<()>>() {
|
||||
println!("Result {:?}", message);
|
||||
}
|
||||
|
||||
// load result
|
||||
for message in messages.read::<SettingsResult<ConfigurationData>>() {
|
||||
if let Ok(data) = message {
|
||||
ConfigurationView::configuration_file_set(&mut ctx.widget(), data.0);
|
||||
ConfigurationView::language_id_set(&mut ctx.widget(), data.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// [END] states
|
||||
|
||||
// [START] Configuration data
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum ConfigurationAction {
|
||||
SaveConfiguration,
|
||||
LoadConfiguration,
|
||||
}
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ConfigurationData(
|
||||
pub String,
|
||||
pub String
|
||||
);
|
||||
|
||||
// [END] Configuration data
|
||||
|
||||
@@ -5,7 +5,10 @@
|
||||
* SPDX-License-Identifier: (0BSD or MIT)
|
||||
*/
|
||||
|
||||
/// global helper functions
|
||||
/// The configuration widget.
|
||||
pub mod configuration;
|
||||
|
||||
/// The global helper functions.
|
||||
pub mod global_state;
|
||||
|
||||
/// The localization widget.
|
||||
|
||||
Reference in New Issue
Block a user