|
|
|
|
@@ -1,3 +1,10 @@
|
|
|
|
|
/*
|
|
|
|
|
* advotracker - Hotline tackingtool for Advocats
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2020 Ralf Zerres <ralf.zerres@networkx.de>
|
|
|
|
|
* SPDX-License-Identifier: (0BSD or MIT)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use orbtk::prelude::*;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
@@ -34,163 +41,163 @@ impl BaseState for PolicyListState {}
|
|
|
|
|
impl PolicyListState {
|
|
|
|
|
/// Sets a new action.
|
|
|
|
|
pub fn action(&mut self, action: Action) {
|
|
|
|
|
self.action = action.into();
|
|
|
|
|
self.action = action.into();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// If policy data element 'name' is empty, disable the add button
|
|
|
|
|
/// otherwise otherwise enabled it.
|
|
|
|
|
fn adjust_add_button_enabled(&self, text_box: Entity, ctx: &mut Context) {
|
|
|
|
|
/* Old syntax
|
|
|
|
|
if ctx.get_widget(text_box).get::<String>("name").is_empty() {
|
|
|
|
|
ctx.get_widget(self.add_button).set("enabled", false);
|
|
|
|
|
} else {
|
|
|
|
|
ctx.get_widget(self.add_button).set("enabled", true);
|
|
|
|
|
}
|
|
|
|
|
ctx.get_widget(self.add_button).update_theme_by_state(true);
|
|
|
|
|
*/
|
|
|
|
|
/* Old syntax
|
|
|
|
|
if ctx.get_widget(text_box).get::<String>("name").is_empty() {
|
|
|
|
|
ctx.get_widget(self.add_button).set("enabled", false);
|
|
|
|
|
} else {
|
|
|
|
|
ctx.get_widget(self.add_button).set("enabled", true);
|
|
|
|
|
}
|
|
|
|
|
ctx.get_widget(self.add_button).update_theme_by_state(true);
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if TextBox::get(ctx.get_widget(text_box)).text().is_empty() {
|
|
|
|
|
Button::get(ctx.child("add_button")).set_enabled(false);
|
|
|
|
|
} else {
|
|
|
|
|
Button::get(ctx.child("add_button")).set_enabled(true);
|
|
|
|
|
}
|
|
|
|
|
// new syntax not implemented yet
|
|
|
|
|
ctx.get_widget(self.add_button).update_theme_by_state(true);
|
|
|
|
|
if TextBox::get(ctx.get_widget(text_box)).text().is_empty() {
|
|
|
|
|
Button::get(ctx.child("add_button")).set_enabled(false);
|
|
|
|
|
} else {
|
|
|
|
|
Button::get(ctx.child("add_button")).set_enabled(true);
|
|
|
|
|
}
|
|
|
|
|
// new syntax not implemented yet
|
|
|
|
|
ctx.get_widget(self.add_button).update_theme_by_state(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// update number of available policy list entries.
|
|
|
|
|
fn adjust_count(&self, ctx: &mut Context) {
|
|
|
|
|
let policy_list_count = ctx.widget().get::<PolicyList>(PROP_POLICY_LIST).len();
|
|
|
|
|
ctx.widget().set(PROP_POLICY_LIST_COUNT, policy_list_count);
|
|
|
|
|
let policy_list_count = ctx.widget().get::<PolicyList>(PROP_POLICY_LIST).len();
|
|
|
|
|
ctx.widget().set(PROP_POLICY_LIST_COUNT, policy_list_count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Creates a new policy data list.
|
|
|
|
|
fn new_entry(&self, name: String, registry: &mut Registry, ctx: &mut Context) {
|
|
|
|
|
ctx.widget()
|
|
|
|
|
.get_mut::<PolicyList>(PROP_POLICY_LIST)
|
|
|
|
|
.push(PolicyList::new(name));
|
|
|
|
|
self.adjust_count(ctx);
|
|
|
|
|
self.save(registry, ctx);
|
|
|
|
|
ctx.widget()
|
|
|
|
|
.get_mut::<PolicyList>(PROP_POLICY_LIST)
|
|
|
|
|
.push(PolicyList::new(name));
|
|
|
|
|
self.adjust_count(ctx);
|
|
|
|
|
self.save(registry, ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// opens a given policy list name.
|
|
|
|
|
fn open_policy_list(&self, index: usize, ctx: &mut Context) {
|
|
|
|
|
ctx.get_widget(self.text_box)
|
|
|
|
|
.set("text", String16::from(""));
|
|
|
|
|
ctx.get_widget(self.policydata_view)
|
|
|
|
|
.set("list_index", Some(index));
|
|
|
|
|
self.navigate(self.policydata_view, ctx);
|
|
|
|
|
ctx.get_widget(self.text_box)
|
|
|
|
|
.set("text", String16::from(""));
|
|
|
|
|
ctx.get_widget(self.policydata_view)
|
|
|
|
|
.set("list_index", Some(index));
|
|
|
|
|
self.navigate(self.policydata_view, ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// removes a policy list.
|
|
|
|
|
fn remove_entry(&self, index: usize, registry: &mut Registry, ctx: &mut Context) {
|
|
|
|
|
ctx.widget()
|
|
|
|
|
.get_mut::<PolicyList>(PROP_POLICY_LIST)
|
|
|
|
|
.remove(index);
|
|
|
|
|
self.adjust_count(ctx);
|
|
|
|
|
self.save(registry, ctx);
|
|
|
|
|
ctx.widget()
|
|
|
|
|
.get_mut::<PolicyList>(PROP_POLICY_LIST)
|
|
|
|
|
.remove(index);
|
|
|
|
|
self.adjust_count(ctx);
|
|
|
|
|
self.save(registry, ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Change status of given text box to edit mode.
|
|
|
|
|
fn set_entry(&self, text_box: Entity, ctx: &mut Context) {
|
|
|
|
|
if *ctx.get_widget(text_box).get::<bool>("focused") {
|
|
|
|
|
ctx.get_widget(text_box).set("enabled", false);
|
|
|
|
|
ctx.push_event_by_window(FocusEvent::RemoveFocus(text_box));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if *ctx.get_widget(text_box).get::<bool>("focused") {
|
|
|
|
|
ctx.get_widget(text_box).set("enabled", false);
|
|
|
|
|
ctx.push_event_by_window(FocusEvent::RemoveFocus(text_box));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(old_focused_element) = ctx.window().get::<Global>("global").focused_widget {
|
|
|
|
|
ctx.push_event_by_window(FocusEvent::RemoveFocus(old_focused_element));
|
|
|
|
|
}
|
|
|
|
|
if let Some(old_focused_element) = ctx.window().get::<Global>("global").focused_widget {
|
|
|
|
|
ctx.push_event_by_window(FocusEvent::RemoveFocus(old_focused_element));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.get_widget(text_box).set("enabled", true);
|
|
|
|
|
ctx.get_widget(text_box).set("enabled", true);
|
|
|
|
|
|
|
|
|
|
// select all
|
|
|
|
|
ctx.get_widget(text_box)
|
|
|
|
|
.get_mut::<TextSelection>("text_selection")
|
|
|
|
|
.start_index = 0;
|
|
|
|
|
ctx.get_widget(text_box)
|
|
|
|
|
.get_mut::<TextSelection>("text_selection")
|
|
|
|
|
.length = ctx.get_widget(text_box).get::<String16>("name").len();
|
|
|
|
|
ctx.push_event_by_window(FocusEvent::RequestFocus(text_box));
|
|
|
|
|
// select all
|
|
|
|
|
ctx.get_widget(text_box)
|
|
|
|
|
.get_mut::<TextSelection>("text_selection")
|
|
|
|
|
.start_index = 0;
|
|
|
|
|
ctx.get_widget(text_box)
|
|
|
|
|
.get_mut::<TextSelection>("text_selection")
|
|
|
|
|
.length = ctx.get_widget(text_box).get::<String16>("name").len();
|
|
|
|
|
ctx.push_event_by_window(FocusEvent::RequestFocus(text_box));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl State for PolicyListState {
|
|
|
|
|
fn init(&mut self, registry: &mut Registry, ctx: &mut Context) {
|
|
|
|
|
self.text_box = ctx
|
|
|
|
|
.entity_of_child(ID_POLICY_LIST_TEXT_BOX)
|
|
|
|
|
.expect("PolicyListState.init: Child 'Text box' not found.");
|
|
|
|
|
self.add_button = ctx
|
|
|
|
|
.entity_of_child(ID_POLICY_LIST_ADD_BUTTON)
|
|
|
|
|
.expect("PolicyListState.init: Child 'Add button' not found.");
|
|
|
|
|
self.items_widget = ctx
|
|
|
|
|
.entity_of_child(ID_POLICY_LIST_ITEMS_WIDGET)
|
|
|
|
|
.expect("PolicyListState.init: Child 'Items widget' not found.");
|
|
|
|
|
self.policydata_view = (*ctx.widget()
|
|
|
|
|
.get::<u32>("policy_list_view")).into();
|
|
|
|
|
self.text_box = ctx
|
|
|
|
|
.entity_of_child(ID_POLICY_LIST_TEXT_BOX)
|
|
|
|
|
.expect("PolicyListState.init: Child 'Text box' not found.");
|
|
|
|
|
self.add_button = ctx
|
|
|
|
|
.entity_of_child(ID_POLICY_LIST_ADD_BUTTON)
|
|
|
|
|
.expect("PolicyListState.init: Child 'Add button' not found.");
|
|
|
|
|
self.items_widget = ctx
|
|
|
|
|
.entity_of_child(ID_POLICY_LIST_ITEMS_WIDGET)
|
|
|
|
|
.expect("PolicyListState.init: Child 'Items widget' not found.");
|
|
|
|
|
self.policydata_view = (*ctx.widget()
|
|
|
|
|
.get::<u32>("policy_list_view")).into();
|
|
|
|
|
|
|
|
|
|
if let Ok(policy_list_name) = registry
|
|
|
|
|
.get::<Settings>("settings")
|
|
|
|
|
.load::<PolicyList>(PROP_POLICY_LIST)
|
|
|
|
|
{
|
|
|
|
|
ctx.widget().set(PROP_POLICY_LIST, policy_list_name);
|
|
|
|
|
}
|
|
|
|
|
if let Ok(policy_list_name) = registry
|
|
|
|
|
.get::<Settings>("settings")
|
|
|
|
|
.load::<PolicyList>(PROP_POLICY_LIST)
|
|
|
|
|
{
|
|
|
|
|
ctx.widget().set(PROP_POLICY_LIST, policy_list_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.adjust_count(ctx);
|
|
|
|
|
self.adjust_count(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update(&mut self, registry: &mut Registry, ctx: &mut Context) {
|
|
|
|
|
// clear focus on focus moved
|
|
|
|
|
if self.last_focused != ctx.window().get::<Global>("global").focused_widget {
|
|
|
|
|
if let Some(last_focused) = self.last_focused {
|
|
|
|
|
ctx.get_widget(last_focused).set("focused", false);
|
|
|
|
|
ctx.get_widget(last_focused)
|
|
|
|
|
.set("visibility", Visibility::Collapsed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// clear focus on focus moved
|
|
|
|
|
if self.last_focused != ctx.window().get::<Global>("global").focused_widget {
|
|
|
|
|
if let Some(last_focused) = self.last_focused {
|
|
|
|
|
ctx.get_widget(last_focused).set("focused", false);
|
|
|
|
|
ctx.get_widget(last_focused)
|
|
|
|
|
.set("visibility", Visibility::Collapsed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(action) = self.action {
|
|
|
|
|
match action {
|
|
|
|
|
Action::InputTextChanged(text_box) => {
|
|
|
|
|
self.adjust_add_button_enabled(text_box, ctx);
|
|
|
|
|
}
|
|
|
|
|
Action::NewEntry(entity) => {
|
|
|
|
|
if let Some(name) = self.fetch_text(ctx, entity) {
|
|
|
|
|
self.new_entry(name, registry, ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Action::RemoveEntry(index) => {
|
|
|
|
|
self.remove_entry(index, registry, ctx);
|
|
|
|
|
}
|
|
|
|
|
Action::TextChanged(entity, index) => {
|
|
|
|
|
let text: String16 = ctx.get_widget(entity).clone("text");
|
|
|
|
|
if let Some(action) = self.action {
|
|
|
|
|
match action {
|
|
|
|
|
Action::InputTextChanged(text_box) => {
|
|
|
|
|
self.adjust_add_button_enabled(text_box, ctx);
|
|
|
|
|
}
|
|
|
|
|
Action::NewEntry(entity) => {
|
|
|
|
|
if let Some(name) = self.fetch_text(ctx, entity) {
|
|
|
|
|
self.new_entry(name, registry, ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Action::RemoveEntry(index) => {
|
|
|
|
|
self.remove_entry(index, registry, ctx);
|
|
|
|
|
}
|
|
|
|
|
Action::TextChanged(entity, index) => {
|
|
|
|
|
let text: String16 = ctx.get_widget(entity).clone("text");
|
|
|
|
|
|
|
|
|
|
if let Some(header) = ctx
|
|
|
|
|
.widget()
|
|
|
|
|
.get_mut::<PolicyList>("policy_list")
|
|
|
|
|
.get_mut(index)
|
|
|
|
|
{
|
|
|
|
|
header.name = text.to_string();
|
|
|
|
|
}
|
|
|
|
|
if let Some(header) = ctx
|
|
|
|
|
.widget()
|
|
|
|
|
.get_mut::<PolicyList>("policy_list")
|
|
|
|
|
.get_mut(index)
|
|
|
|
|
{
|
|
|
|
|
header.name = text.to_string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.save(registry, ctx);
|
|
|
|
|
}
|
|
|
|
|
Action::SetEntry(text_box) => {
|
|
|
|
|
self.last_focused = Some(text_box);
|
|
|
|
|
self.set_entry(text_box, ctx);
|
|
|
|
|
}
|
|
|
|
|
Action::RemoveFocus(text_box) => {
|
|
|
|
|
self.last_focused = None;
|
|
|
|
|
ctx.push_event_by_window(FocusEvent::RemoveFocus(text_box));
|
|
|
|
|
}
|
|
|
|
|
Action::OpenPolicyList(index) => {
|
|
|
|
|
self.open_policy_list(index, ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.save(registry, ctx);
|
|
|
|
|
}
|
|
|
|
|
Action::SetEntry(text_box) => {
|
|
|
|
|
self.last_focused = Some(text_box);
|
|
|
|
|
self.set_entry(text_box, ctx);
|
|
|
|
|
}
|
|
|
|
|
Action::RemoveFocus(text_box) => {
|
|
|
|
|
self.last_focused = None;
|
|
|
|
|
ctx.push_event_by_window(FocusEvent::RemoveFocus(text_box));
|
|
|
|
|
}
|
|
|
|
|
Action::OpenPolicyList(index) => {
|
|
|
|
|
self.open_policy_list(index, ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.action = None;
|
|
|
|
|
self.action = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|