frontend: base_state: global methods implemented as a trait

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2020-06-19 17:51:21 +02:00
parent 4ae06a3db0
commit 86059ccebd

View File

@@ -0,0 +1,43 @@
use orbtk::prelude::*;
use crate::{data::PolicyList, keys::*};
/// Provides generic methods to handle states of datatypes (e.g. used in `PolicyList`).
pub trait BaseState {
/// Navigates to the given entity.
fn navigate(&self, to: Entity, ctx: &mut Context) {
if let Some(old_focused_element) = ctx.window().get::<Global>("global").focused_widget {
let mut old_focused_element = ctx.get_widget(old_focused_element);
old_focused_element.set("focused", false);
old_focused_element.update_theme_by_state(false);
}
ctx.window().get_mut::<Global>("global").focused_widget = None;
ctx.widget().set("visibility", Visibility::Collapsed);
ctx.get_widget(to).set("visibility", Visibility::Visible);
}
/// Fetches the text of a widget.
fn fetch_text(&self, ctx: &mut Context, entity: Entity) -> Option<String> {
let mut widget = ctx.get_widget(entity);
let entry = widget.get_mut::<String>("text");
if entry.is_empty() {
return None;
}
let copy = entry.to_string();
entry.clear();
Some(copy)
}
// Save the data.
fn save(&self, registry: &mut Registry, ctx: &mut Context) {
registry
.get::<Settings>("settings")
.save(
PROP_POLICY_LIST,
ctx.widget().get::<PolicyList>(PROP_POLICY_LIST),
)
.unwrap();
}
}