Finish un-arc-ing GuiHandler

This commit is contained in:
Michael Hübner 2025-03-04 14:25:35 +01:00
parent 2a77856cb8
commit b09ec887ab
9 changed files with 218 additions and 164 deletions

View file

@ -1,12 +1,12 @@
use crate::prelude::*;
use anyhow::{bail, Context, Result};
use anyhow::{Context, Result, bail};
use assetpath::AssetPath;
use super::validator::buttoninfo::{NeighbourDirection, NeighbourInfo};
use super::validator::gridinfo::GridInfo;
use super::validator::uiinfoelement::UiInfoElement;
use super::validator::validator::{handle_function_suffix, Validator};
use super::validator::validator::{Validator, handle_function_suffix};
use std::any::Any;
use std::collections::HashMap;
@ -22,7 +22,7 @@ pub struct GuiBuilder {
}
impl GuiBuilder {
pub fn new(gui_handler: &Arc<GuiHandler>, path: &AssetPath) -> Result<Arc<Self>> {
pub fn new(gui_handler: &mut GuiHandler, path: &AssetPath) -> Result<Arc<Self>> {
let validator = Validator::new(
#[cfg(feature = "audio")]
gui_handler,
@ -35,7 +35,7 @@ impl GuiBuilder {
)?))
}
pub fn from_str(gui_handler: &Arc<GuiHandler>, s: &str) -> Result<Arc<Self>> {
pub fn from_str(gui_handler: &mut GuiHandler, s: &str) -> Result<Arc<Self>> {
let validator = Validator::from_str(
#[cfg(feature = "audio")]
gui_handler,
@ -46,7 +46,7 @@ impl GuiBuilder {
}
pub fn merge<'a>(
gui_handler: &Arc<GuiHandler>,
gui_handler: &mut GuiHandler,
pathes: impl IntoIterator<Item = &'a AssetPath>,
) -> Result<Arc<Self>> {
let mut me = Self {
@ -85,7 +85,7 @@ impl GuiBuilder {
}
#[inline]
fn _new(gui_handler: &Arc<GuiHandler>, validator: Validator) -> Result<Self> {
fn _new(gui_handler: &mut GuiHandler, validator: Validator) -> Result<Self> {
let root = validator.root();
let mut ids = HashMap::new();
@ -178,7 +178,10 @@ impl GuiBuilder {
impl Functionality for GuiBuilder {
fn set_click_callbacks(
&self,
functions: Vec<(&str, Box<dyn Fn() -> Result<()> + Send + Sync>)>,
functions: Vec<(
&str,
Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>,
)>,
) -> Result<()> {
for (function_name, callback) in functions {
let suffix_less_function_name = handle_function_suffix(function_name);
@ -235,9 +238,9 @@ impl Visibility for GuiBuilder {
.unwrap_or(false)
}
fn set_visibility(&self, visibility: bool) -> Result<()> {
fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
for grid in &self.grids {
grid.set_visibility(visibility)?;
grid.set_visibility(gui_handler, visibility)?;
}
Ok(())
@ -259,7 +262,7 @@ impl GuiElementTraits for GuiBuilder {
}
impl TopGui for GuiBuilder {
fn decline(&self) -> Result<()> {
fn decline(&self, _gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(decline_callback) = self.decline_callback.read().unwrap().as_ref() {
decline_callback()?;
}
@ -267,11 +270,11 @@ impl TopGui for GuiBuilder {
Ok(())
}
fn next_tab(&self, _: bool) -> Result<()> {
fn next_tab(&self, _gui_handler: &mut GuiHandler, _: bool) -> Result<()> {
Ok(())
}
fn previous_tab(&self, _: bool) -> Result<()> {
fn previous_tab(&self, _gui_handler: &mut GuiHandler, _: bool) -> Result<()> {
Ok(())
}
}
@ -293,18 +296,18 @@ impl TopLevelGui for GuiBuilder {
Some(self)
}
fn enable(&self) -> Result<()> {
self.set_visibility(true)?;
fn enable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.set_visibility(gui_handler, true)?;
if let Some(button) = &self.default_select {
Button::select(button)?;
button.select(gui_handler)?;
}
Ok(())
}
fn disable(&self) -> Result<()> {
self.set_visibility(false)?;
fn disable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.set_visibility(gui_handler, false)?;
Ok(())
}
@ -332,7 +335,7 @@ impl_element!(MultiLineTextField);
// private
impl GuiBuilder {
fn create_tree(
gui_handler: &Arc<GuiHandler>,
gui_handler: &mut GuiHandler,
ids: &mut HashMap<String, UiElement>,
root_grid_info: &Arc<GridInfo>,
default_button: &mut Option<Arc<Button>>,
@ -340,7 +343,7 @@ impl GuiBuilder {
(reference_width, reference_height): (Option<u32>, Option<u32>),
layer: i32,
) -> Result<Arc<Grid>> {
let root_grid = Grid::try_from(root_grid_info, gui_handler, true)?;
let root_grid = Grid::try_from(gui_handler, root_grid_info, true)?;
let root_layer = root_grid_info.layer.unwrap_or(layer);
root_grid.set_layer(root_layer)?;
@ -348,11 +351,12 @@ impl GuiBuilder {
if let Some(ref_height) = reference_height {
root_grid
.framable
.set_reference_size(ref_width, ref_height)?;
.set_reference_size(gui_handler, ref_width, ref_height)?;
}
}
root_grid.set_frame(
gui_handler,
root_grid_info
.x_offset
.get()
@ -397,7 +401,7 @@ impl GuiBuilder {
}
fn create_child(
gui_handler: &Arc<GuiHandler>,
gui_handler: &mut GuiHandler,
child: &UiInfoElement,
grid: &Grid,
ids: &mut HashMap<String, UiElement>,
@ -407,11 +411,12 @@ impl GuiBuilder {
) -> Result<()> {
match child {
UiInfoElement::Button(button_info) => {
let button = Button::try_from(button_info, gui_handler)?;
let button = Button::try_from(gui_handler, button_info)?;
Self::insert_id(ids, &button_info.id, &button)?;
grid.attach(
gui_handler,
button.clone(),
button_info
.x_slot
@ -434,7 +439,7 @@ impl GuiBuilder {
Some(_) => {
return Err(anyhow::Error::msg(
"It is not allowed to select multiple UI elements",
))
));
}
None => *default_button = Some(button.clone()),
}
@ -446,6 +451,7 @@ impl GuiBuilder {
Self::insert_id(ids, &label_info.id, &label)?;
grid.attach(
gui_handler,
label.clone(),
label_info.x_slot.get().with_context(|| "x_slot of label")?,
label_info.y_slot.get().with_context(|| "y_slot of label")?,
@ -462,6 +468,7 @@ impl GuiBuilder {
GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?;
grid.attach(
gui_handler,
multi_line_label.clone(),
multi_line_label_info
.x_slot
@ -484,6 +491,7 @@ impl GuiBuilder {
GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?;
grid.attach(
gui_handler,
multi_line_text_field.clone(),
multi_line_text_field_info
.x_slot
@ -505,6 +513,7 @@ impl GuiBuilder {
Self::insert_id(ids, &text_field_info.id, &text_field)?;
grid.attach(
gui_handler,
text_field.clone(),
text_field_info
.x_slot
@ -526,6 +535,7 @@ impl GuiBuilder {
Self::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?;
grid.attach(
gui_handler,
icon.clone(),
icon_info.x_slot.get().with_context(|| "x_slot of icon")?,
icon_info.y_slot.get().with_context(|| "y_slot of icon")?,
@ -541,6 +551,7 @@ impl GuiBuilder {
Self::insert_id(ids, &progress_bar_info.id, &progress_bar)?;
grid.attach(
gui_handler,
progress_bar.clone(),
progress_bar_info
.x_slot
@ -557,11 +568,12 @@ impl GuiBuilder {
progress_bar.set_layer(layer)?;
}
UiInfoElement::Grid(grid_info) => {
let sub_grid = Grid::try_from(grid_info, gui_handler, false)?;
let sub_grid = Grid::try_from(gui_handler, grid_info, false)?;
Self::insert_id(ids, &grid_info.id, &sub_grid)?;
grid.attach(
gui_handler,
sub_grid.clone(),
grid_info.x_slot.get().with_context(|| "x_slot of grid")?,
grid_info.y_slot.get().with_context(|| "y_slot of grid")?,

View file

@ -5,7 +5,7 @@ use assetpath::AssetPath;
use super::validator::{
buttoninfo::NeighbourInfo,
uiinfoelement::UiInfoElement,
validator::{handle_function_suffix, Validator},
validator::{Validator, handle_function_suffix},
};
use crate::prelude::*;
use anyhow::Result;
@ -17,7 +17,7 @@ pub struct GuiSnippet {
}
impl GuiSnippet {
pub fn new(gui_handler: &Arc<GuiHandler>, path: &AssetPath) -> Result<Arc<Self>> {
pub fn new(gui_handler: &mut GuiHandler, path: &AssetPath) -> Result<Arc<Self>> {
let validator = Validator::new(
#[cfg(feature = "audio")]
&gui_handler,
@ -27,7 +27,7 @@ impl GuiSnippet {
Self::_new(gui_handler, validator)
}
pub fn from_str(gui_handler: &Arc<GuiHandler>, s: &str) -> Result<Arc<Self>> {
pub fn from_str(gui_handler: &mut GuiHandler, s: &str) -> Result<Arc<Self>> {
let validator = Validator::from_str(
#[cfg(feature = "audio")]
gui_handler,
@ -37,7 +37,7 @@ impl GuiSnippet {
Self::_new(gui_handler, validator)
}
fn _new(gui_handler: &Arc<GuiHandler>, validator: Validator) -> Result<Arc<Self>> {
fn _new(gui_handler: &mut GuiHandler, validator: Validator) -> Result<Arc<Self>> {
let root = validator.root();
let mut ids = HashMap::new();
@ -51,12 +51,12 @@ impl GuiSnippet {
}
let grid_info = &root.children[0];
let grid = Grid::try_from(grid_info, &gui_handler, false)?;
let grid = Grid::try_from(gui_handler, grid_info, false)?;
GuiBuilder::insert_id(&mut ids, &grid_info.id, &grid)?;
for child in grid_info.children.read().unwrap().iter() {
Self::create_child(&gui_handler, child, &grid, &mut ids, &mut custom_neighbours)?;
Self::create_child(gui_handler, child, &grid, &mut ids, &mut custom_neighbours)?;
}
GuiBuilder::connect_custom_neighbours(&ids, custom_neighbours)?;
@ -93,8 +93,8 @@ impl Visibility for GuiSnippet {
self.grid.visible()
}
fn set_visibility(&self, visibility: bool) -> Result<()> {
self.grid.set_visibility(visibility)
fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
self.grid.set_visibility(gui_handler, visibility)
}
}
@ -115,6 +115,7 @@ impl GuiElementTraits for GuiSnippet {
impl Gridable for GuiSnippet {
fn set_frame(
&self,
gui_handler: &mut GuiHandler,
x: i32,
y: i32,
w: u32,
@ -122,7 +123,8 @@ impl Gridable for GuiSnippet {
vert_align: VerticalAlign,
hori_align: HorizontalAlign,
) -> Result<()> {
self.grid.set_frame(x, y, w, h, vert_align, hori_align)
self.grid
.set_frame(gui_handler, x, y, w, h, vert_align, hori_align)
}
fn selectable(&self) -> Option<&Arc<Selectable>> {
@ -145,7 +147,10 @@ impl Gridable for GuiSnippet {
impl Functionality for GuiSnippet {
fn set_click_callbacks(
&self,
functions: Vec<(&str, Box<dyn Fn() -> Result<()> + Send + Sync>)>,
functions: Vec<(
&str,
Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>,
)>,
) -> Result<()> {
for (function_name, callback) in functions {
let suffix_less_function_name = handle_function_suffix(function_name);
@ -195,7 +200,7 @@ impl Functionality for GuiSnippet {
impl GuiSnippet {
fn create_child(
gui_handler: &Arc<GuiHandler>,
gui_handler: &mut GuiHandler,
child: &UiInfoElement,
grid: &Grid,
ids: &mut HashMap<String, UiElement>,
@ -203,11 +208,12 @@ impl GuiSnippet {
) -> Result<()> {
match child {
UiInfoElement::Button(button_info) => {
let button = Button::try_from(button_info, gui_handler)?;
let button = Button::try_from(gui_handler, button_info)?;
GuiBuilder::insert_id(ids, &button_info.id, &button)?;
grid.attach(
gui_handler,
button.clone(),
button_info.x_slot.get()?,
button_info.y_slot.get()?,
@ -223,6 +229,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &label_info.id, &label)?;
grid.attach(
gui_handler,
label,
label_info.x_slot.get()?,
label_info.y_slot.get()?,
@ -237,6 +244,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?;
grid.attach(
gui_handler,
multi_line_label,
multi_line_label_info.x_slot.get()?,
multi_line_label_info.y_slot.get()?,
@ -251,6 +259,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?;
grid.attach(
gui_handler,
multi_line_text_field,
multi_line_text_field_info.x_slot.get()?,
multi_line_text_field_info.y_slot.get()?,
@ -264,6 +273,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &text_field_info.id, &text_field)?;
grid.attach(
gui_handler,
text_field,
text_field_info.x_slot.get()?,
text_field_info.y_slot.get()?,
@ -277,6 +287,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?;
grid.attach(
gui_handler,
icon,
icon_info.x_slot.get()?,
icon_info.y_slot.get()?,
@ -290,6 +301,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &progress_bar_info.id, &progress_bar)?;
grid.attach(
gui_handler,
progress_bar,
progress_bar_info.x_slot.get()?,
progress_bar_info.y_slot.get()?,
@ -298,11 +310,12 @@ impl GuiSnippet {
)?;
}
UiInfoElement::Grid(grid_info) => {
let sub_grid = Grid::try_from(grid_info, gui_handler, false)?;
let sub_grid = Grid::try_from(gui_handler, grid_info, false)?;
GuiBuilder::insert_id(ids, &grid_info.id, &sub_grid)?;
grid.attach(
gui_handler,
sub_grid.clone(),
grid_info.x_slot.get()?,
grid_info.y_slot.get()?,

View file

@ -314,9 +314,10 @@ impl Button {
pub fn set_callback<F>(&self, callback: F)
where
F: Fn() -> Result<()> + Send + Sync + 'static,
F: Fn(&mut GuiHandler) -> Result<()> + Send + Sync + 'static,
{
self.click_executable.set_callback(move |_, _| callback());
self.click_executable
.set_callback(move |gui_handler: &mut GuiHandler, _: ()| callback(gui_handler));
}
pub fn set_select_callback<F>(&self, callback: F)
@ -324,7 +325,7 @@ impl Button {
F: Fn(bool) -> Result<()> + Send + Sync + 'static,
{
self.on_select_executable
.set_callback(|_m, select| callback(select));
.set_callback(move |_: &mut GuiHandler, select: bool| callback(select));
}
pub fn set_custom_callback<F>(&self, callback: F)
@ -555,7 +556,7 @@ impl Button {
fn create_clicked_changed_callback(button: Arc<Button>) {
let button_weak = Arc::downgrade(&button);
let clicked_changed = Box::new(move |gui_handler| {
let clicked_changed = Box::new(move |gui_handler: &mut GuiHandler| {
if let Some(button) = button_weak.upgrade() {
if button.clickable.clicked() {
button.hoverable.set_hovered(gui_handler, false)?;
@ -577,7 +578,7 @@ impl Button {
fn create_selected_changed_callback(button: Arc<Button>) {
let button_weak = Arc::downgrade(&button);
let selected_changed = move |(gui_handler, selected)| {
let selected_changed = move |gui_handler: &mut GuiHandler, selected: bool| {
if let Some(button) = button_weak.upgrade() {
if selected {
button.set_button_state(gui_handler, ButtonState::Selected)?;

View file

@ -337,10 +337,10 @@ impl Grid {
self.framable.allow_size_scale(gui_handler, false)
}
pub fn connect(
pub fn connect<'a>(
&self,
direction: ConnectDirection,
elements: impl IntoIterator<Item = (usize, &Arc<Selectable>)>,
elements: impl IntoIterator<Item = (usize, &'a Arc<Selectable>)>,
) -> Result<()> {
for (index, selectable) in elements {
match direction {
@ -499,8 +499,8 @@ impl Grid {
}
pub fn try_from(
grid_info: &GridInfo,
gui_handler: &mut GuiHandler,
grid_info: &GridInfo,
top_level: bool,
) -> Result<Arc<Self>> {
let grid = Grid::new(

View file

@ -9,8 +9,8 @@ pub trait TopLevelGui: Send + Sync {
fn top_gui(&self) -> Option<&dyn TopGui>;
fn elements(&self) -> Option<&HashMap<String, UiElement>>;
fn functionality(&self) -> Option<&dyn Functionality>;
fn enable(&self) -> Result<()>;
fn disable(&self) -> Result<()>;
fn enable(&self, gui_handler: &mut GuiHandler) -> Result<()>;
fn disable(&self, gui_handler: &mut GuiHandler) -> Result<()>;
}
pub fn any_to<T>(any: &dyn Any) -> Result<&T>
@ -26,7 +26,10 @@ where
pub trait Functionality {
fn set_click_callbacks(
&self,
callbacks: Vec<(&str, Box<dyn Fn() -> Result<()> + Send + Sync>)>,
callbacks: Vec<(
&str,
Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>,
)>,
) -> Result<()>;
fn set_select_callbacks(

View file

@ -1,19 +1,21 @@
/// A trait that is used by the gui handler as the target for input
use anyhow::Result;
use crate::prelude::*;
pub trait TopGui: Send + Sync {
/// Decline method which is executed on `InputMap::B` press
fn decline(&self) -> Result<()>;
fn decline(&self, gui_handler: &mut GuiHandler) -> Result<()>;
/// Method which is executed on `InputMap::RightButton` press
///
/// # Arguments
/// * `second_level` adds support for multiple tab layers, e.g. RB and RT press on controller
fn next_tab(&self, second_level: bool) -> Result<()>;
fn next_tab(&self, gui_handler: &mut GuiHandler, second_level: bool) -> Result<()>;
/// Method which is executed on `InputMap::LeftButton` press
/// ///
/// # Arguments
/// * `second_level` adds support for multiple tab layers, e.g. RB and RT press on controller
fn previous_tab(&self, second_level: bool) -> Result<()>;
fn previous_tab(&self, gui_handler: &mut GuiHandler, second_level: bool) -> Result<()>;
}

View file

@ -736,36 +736,44 @@ impl GuiHandler {
Ok(false)
}
pub fn decline_topgui(&self) -> Result<bool> {
pub fn decline_topgui(&self, gui_handler: &mut GuiHandler) -> Result<bool> {
// workaround for unwanted borrowing behaviour inside decline function
let opt_topgui = self.top_ui.as_ref().cloned();
if let Some(topgui) = opt_topgui {
topgui.decline()?;
topgui.decline(gui_handler)?;
return Ok(true);
}
Ok(false)
}
pub fn next_tab_topgui(&self, second_level: bool) -> Result<bool> {
pub fn next_tab_topgui(
&self,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<bool> {
// workaround for unwanted borrowing behaviour inside decline function
let opt_topgui = self.top_ui.as_ref().cloned();
if let Some(topgui) = opt_topgui {
topgui.next_tab(second_level)?;
topgui.next_tab(gui_handler, second_level)?;
return Ok(true);
}
Ok(false)
}
pub fn previous_tab_topgui(&self, second_level: bool) -> Result<bool> {
pub fn previous_tab_topgui(
&self,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<bool> {
// workaround for unwanted borrowing behaviour inside decline function
let opt_topgui = self.top_ui.as_ref().cloned();
if let Some(topgui) = opt_topgui {
topgui.previous_tab(second_level)?;
topgui.previous_tab(gui_handler, second_level)?;
return Ok(true);
}
@ -852,12 +860,12 @@ impl GuiHandler {
self.needs_update = true;
}
pub fn set_top_gui(&mut self, top_gui: impl Into<Option<Arc<dyn TopGui>>>) {
self.top_ui = top_gui.into();
pub fn set_top_gui(&mut self, top_gui: Option<Arc<dyn TopGui>>) {
self.top_ui = top_gui;
}
pub fn set_tooltip(&mut self, tooltip: impl Into<Option<Arc<dyn TopGui>>>) {
self.tooltip_ui = tooltip.into();
pub fn set_tooltip(&mut self, tooltip: Option<Arc<dyn TopGui>>) {
self.tooltip_ui = tooltip;
}
pub(crate) fn add_callback<F: FnOnce(&mut Self) -> Result<()> + Send + Sync + 'static>(

View file

@ -25,21 +25,22 @@ pub struct Keyboard {
mode: Arc<RwLock<KeyboardMode>>,
decline_callback: Arc<RwLock<Option<Box<dyn Fn() -> Result<()> + Send + Sync>>>>,
decline_callback: Arc<RwLock<Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>>>,
accept_callback: Arc<RwLock<Option<Box<dyn Fn(&dyn Any) -> Result<()> + Send + Sync>>>>,
elements: HashMap<String, UiElement>,
}
impl Keyboard {
pub fn new(gui_handler: &GuiHandler) -> Result<Arc<Self>> {
pub fn new(gui_handler: &mut GuiHandler) -> Result<Arc<Self>> {
let text_field_gui: Arc<GuiBuilder> =
GuiBuilder::from_str(gui_handler, include_str!("text_field.xml"))?;
let text_field: Arc<TextField> = text_field_gui.element("field")?;
let decline_callback: Arc<RwLock<Option<Box<dyn Fn() -> Result<()> + Send + Sync>>>> =
Arc::new(RwLock::new(None));
let decline_callback: Arc<
RwLock<Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>>,
> = Arc::new(RwLock::new(None));
let accept_callback: Arc<
RwLock<Option<Box<dyn Fn(&dyn Any) -> Result<()> + Send + Sync>>>,
@ -80,10 +81,12 @@ impl Keyboard {
}
fn setup(
gui_handler: &Arc<GuiHandler>,
gui_handler: &mut GuiHandler,
textfield: Arc<TextField>,
mode: &Arc<RwLock<KeyboardMode>>,
decline_callback: Arc<RwLock<Option<Box<dyn Fn() -> Result<()> + Send + Sync>>>>,
decline_callback: Arc<
RwLock<Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>>,
>,
accept_callback: Arc<RwLock<Option<Box<dyn Fn(&dyn Any) -> Result<()> + Send + Sync>>>>,
) -> Result<(Arc<GuiBuilder>, Arc<GuiBuilder>, Arc<GuiBuilder>)> {
let lower_case = GuiBuilder::from_str(gui_handler, include_str!("lower_case.xml"))?;
@ -190,9 +193,9 @@ impl Keyboard {
Self::set_text_callback(&specials, ".", textfield.clone())?;
Self::set_text_callback(&specials, "_", textfield.clone())?;
let back = Box::new(move || {
let back = Box::new(move |gui_handler: &mut GuiHandler| {
if let Some(callback) = decline_callback.read().unwrap().as_ref() {
(callback)()?;
(callback)(gui_handler)?;
}
Ok(())
@ -202,7 +205,7 @@ impl Keyboard {
let weak_textfield = Arc::downgrade(&textfield);
let weak_accept = Arc::downgrade(&accept_callback);
Box::new(move || {
Box::new(move |_: &mut GuiHandler| {
if let Some(textfield) = weak_textfield.upgrade() {
if let Some(accept_callback) = weak_accept.upgrade() {
if let Some(text) = textfield.text() {
@ -222,15 +225,15 @@ impl Keyboard {
let weak_lower = Arc::downgrade(&lower_case);
let weak_upper = Arc::downgrade(&upper_case);
Box::new(move || {
Box::new(move |gui_handler: &mut GuiHandler| {
if let Some(lower) = weak_lower.upgrade() {
if let Some(upper) = weak_upper.upgrade() {
let mut mode = mode.write().unwrap();
if let KeyboardMode::LowerCase = mode.deref() {
*mode = KeyboardMode::UpperCase;
lower.disable()?;
upper.enable()?;
lower.disable(gui_handler)?;
upper.enable(gui_handler)?;
}
}
}
@ -244,15 +247,15 @@ impl Keyboard {
let weak_upper = Arc::downgrade(&upper_case);
let weak_specials = Arc::downgrade(&specials);
Box::new(move || {
Box::new(move |gui_handler: &mut GuiHandler| {
if let Some(specials) = weak_specials.upgrade() {
if let Some(upper) = weak_upper.upgrade() {
let mut mode = mode.write().unwrap();
if let KeyboardMode::UpperCase = mode.deref() {
*mode = KeyboardMode::Specials;
upper.disable()?;
specials.enable()?;
upper.disable(gui_handler)?;
specials.enable(gui_handler)?;
}
}
}
@ -266,15 +269,15 @@ impl Keyboard {
let weak_lower = Arc::downgrade(&lower_case);
let weak_specials = Arc::downgrade(&specials);
Box::new(move || {
Box::new(move |gui_handler: &mut GuiHandler| {
if let Some(lower) = weak_lower.upgrade() {
if let Some(specials) = weak_specials.upgrade() {
let mut mode = mode.write().unwrap();
if let KeyboardMode::Specials = mode.deref() {
*mode = KeyboardMode::LowerCase;
specials.disable()?;
lower.enable()?;
specials.disable(gui_handler)?;
lower.enable(gui_handler)?;
}
}
}
@ -286,9 +289,9 @@ impl Keyboard {
let space_bar = {
let weak_textfield = Arc::downgrade(&textfield);
Box::new(move || {
Box::new(move |gui_handler: &mut GuiHandler| {
if let Some(text_field) = weak_textfield.upgrade() {
text_field.add_letter(' ')?;
text_field.add_letter(gui_handler, ' ')?;
}
Ok(())
@ -327,11 +330,11 @@ impl Keyboard {
let weak_textfield = Arc::downgrade(&textfield);
let weak_button = Arc::downgrade(&button_ref);
button_ref.set_callback(Box::new(move || {
button_ref.set_callback(Box::new(move |gui_handler: &mut GuiHandler| {
if let Some(textfield) = weak_textfield.upgrade() {
if let Some(button) = weak_button.upgrade() {
if let Some(text) = button.text()? {
textfield.add_letter(text.as_bytes()[0] as char)?;
textfield.add_letter(gui_handler, text.as_bytes()[0] as char)?;
}
}
}
@ -344,22 +347,22 @@ impl Keyboard {
}
impl TopGui for Keyboard {
fn decline(&self) -> Result<()> {
fn decline(&self, gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(callback) = self.decline_callback.read().unwrap().as_ref() {
(callback)()?;
(callback)(gui_handler)?;
}
Ok(())
}
fn next_tab(&self, _: bool) -> Result<()> {
fn next_tab(&self, _gui_handler: &mut GuiHandler, _: bool) -> Result<()> {
Ok(())
}
fn previous_tab(&self, second_level: bool) -> Result<()> {
fn previous_tab(&self, gui_handler: &mut GuiHandler, second_level: bool) -> Result<()> {
// abuse event
if !second_level {
self.text_field.remove_last()?;
self.text_field.remove_last(gui_handler)?;
}
Ok(())
@ -377,7 +380,7 @@ impl Visibility for Keyboard {
}
}
fn set_visibility(&self, visibility: bool) -> Result<()> {
fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
let mode = self.mode.read().unwrap();
let gui = match mode.deref() {
@ -387,11 +390,11 @@ impl Visibility for Keyboard {
};
if visibility {
gui.enable()?;
self.text_field_gui.enable()?;
gui.enable(gui_handler)?;
self.text_field_gui.enable(gui_handler)?;
} else {
gui.disable()?;
self.text_field_gui.disable()?;
gui.disable(gui_handler)?;
self.text_field_gui.disable(gui_handler)?;
}
Ok(())
@ -429,15 +432,15 @@ impl TopLevelGui for Keyboard {
Some(self)
}
fn enable(&self) -> Result<()> {
self.set_visibility(true)?;
self.text_field.focus_input()?;
fn enable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.set_visibility(gui_handler, true)?;
self.text_field.focus_input(gui_handler)?;
Ok(())
}
fn disable(&self) -> Result<()> {
self.set_visibility(false)?;
fn disable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.set_visibility(gui_handler, false)?;
Ok(())
}
@ -446,7 +449,10 @@ impl TopLevelGui for Keyboard {
impl Functionality for Keyboard {
fn set_click_callbacks(
&self,
callbacks: Vec<(&str, Box<dyn Fn() -> Result<()> + Send + Sync>)>,
callbacks: Vec<(
&str,
Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>,
)>,
) -> Result<()> {
for (name, callback) in callbacks {
if name == "decline" {

View file

@ -7,19 +7,21 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock, Weak};
pub trait FutureStateChange: Fn() -> Result<()> + Send + Sync {
pub trait FutureStateChange: Fn(&mut GuiHandler) -> Result<()> + Send + Sync {
fn clone_box<'a>(&self) -> Box<dyn 'a + FutureStateChange>
where
Self: 'a;
fn as_fn(&self) -> &(dyn Fn() -> Result<()> + Send + Sync)
fn as_fn(&self) -> &(dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync)
where
Self: Sized,
{
self
}
fn as_static(&'static self) -> &'static (dyn Fn() -> Result<()> + Send + Sync + 'static)
fn as_static(
&'static self,
) -> &'static (dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync + 'static)
where
Self: Sized,
{
@ -27,7 +29,7 @@ pub trait FutureStateChange: Fn() -> Result<()> + Send + Sync {
}
}
impl<F: Fn() -> Result<()> + Clone + Send + Sync> FutureStateChange for F {
impl<F: Fn(&mut GuiHandler) -> Result<()> + Clone + Send + Sync> FutureStateChange for F {
fn clone_box<'a>(&self) -> Box<dyn 'a + FutureStateChange>
where
Self: 'a,
@ -47,12 +49,12 @@ struct State {
top_level_gui: Arc<dyn TopLevelGui>,
on_activate: RwLock<Option<Box<dyn Fn() -> Result<()> + Send + Sync>>>,
on_deactivate: RwLock<Option<Box<dyn Fn() -> Result<()> + Send + Sync>>>,
on_activate: RwLock<Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>>,
on_deactivate: RwLock<Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>>,
next_tab: RwLock<Option<Box<dyn Fn() -> Result<()> + Send + Sync>>>,
previous_tab: RwLock<Option<Box<dyn Fn() -> Result<()> + Send + Sync>>>,
decline: RwLock<Option<Box<dyn Fn() -> Result<()> + Send + Sync>>>,
next_tab: RwLock<Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>>,
previous_tab: RwLock<Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>>,
decline: RwLock<Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>>,
}
/// Opaque handle for a State
@ -148,16 +150,21 @@ impl GetElement<ProgressBar> for StateHandle {
/// Update type
pub enum StateUpdateType<'a> {
/// Updates the callback which is executed on next tab event
NextTab(Option<Box<dyn Fn() -> Result<()> + Send + Sync>>),
NextTab(Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>),
/// Updates the callback which is executed on previous tab event
PreviousTab(Option<Box<dyn Fn() -> Result<()> + Send + Sync>>),
PreviousTab(Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>),
/// Updates the callback which is executed on decline event
Decline(Option<Box<dyn Fn() -> Result<()> + Send + Sync>>),
Decline(Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>),
/// Updates callbacks by identifier
ClickCallbacks(Vec<(&'a str, Box<dyn Fn() -> Result<()> + Send + Sync>)>),
ClickCallbacks(
Vec<(
&'a str,
Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>,
)>,
),
/// Updates callbacks by identifier
SelectCallbacks(Vec<(&'a str, Box<CustomCallback<bool, ()>>)>),
@ -169,10 +176,10 @@ pub enum StateUpdateType<'a> {
VecCallbacks(Vec<(&'a str, Box<dyn Fn(&dyn Any) -> Result<()> + Send + Sync>)>),
/// Updates the callback which is executed when this state gets activated on state change
OnActivate(Option<Box<dyn Fn() -> Result<()> + Send + Sync>>),
OnActivate(Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>),
/// Updates the callback which is executed when this state gets deactivated on state change
OnDeactivate(Option<Box<dyn Fn() -> Result<()> + Send + Sync>>),
OnDeactivate(Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>),
}
/// Type of the state to be created
@ -204,8 +211,6 @@ impl<'a, T: TopLevelGui + 'static> From<T> for CreationType<'a> {
/// Collection and handler for your UI (basically a state machine)
pub struct States {
gui_handler: Arc<GuiHandler>,
states: HashMap<String, Arc<State>>,
current_state: Arc<Mutex<Option<Arc<State>>>>,
@ -215,10 +220,8 @@ pub struct States {
impl States {
/// Creates an empty States struct
pub fn new(gui_handler: Arc<GuiHandler>) -> Result<Self> {
pub fn new() -> Result<Self> {
Ok(States {
gui_handler,
states: HashMap::new(),
current_state: Arc::new(Mutex::new(None)),
@ -246,12 +249,13 @@ impl States {
/// Adds a single state
pub fn add_state<'a>(
&mut self,
gui_handler: &mut GuiHandler,
id: &str,
creation_type: impl Into<CreationType<'a>>,
) -> Result<()> {
self.states.insert(
id.to_string(),
State::new(&self.gui_handler, id, creation_type.into())?,
State::new(gui_handler, id, creation_type.into())?,
);
Ok(())
@ -270,15 +274,15 @@ impl States {
/// # Arguments
///
/// * `id` - Set state with the given identifier or None
pub fn set_state<'b>(&self, id: impl Into<Option<&'b str>>) -> Result<()> {
pub fn set_state<'b>(
&self,
gui_handler: &mut GuiHandler,
id: impl Into<Option<&'b str>>,
) -> Result<()> {
Self::_set_state(
id.into().map(|id| self.get_state(id)).transpose()?,
&mut *self.current_state.lock().unwrap(),
if self.control_top_gui {
Some(self.gui_handler.clone())
} else {
None
},
gui_handler,
self.log_state_change,
)
}
@ -286,7 +290,7 @@ impl States {
fn _set_state(
state: Option<Arc<State>>,
current: &mut Option<Arc<State>>,
gui_handler: Option<Arc<GuiHandler>>,
gui_handler: &mut GuiHandler,
logging: bool,
) -> Result<()> {
if let Some(old_state) = current {
@ -298,17 +302,14 @@ impl States {
}
// execute deactivate on old state
old_state.deactivate()?;
old_state.deactivate(gui_handler)?;
}
// set new state, either no state or requested state
match state {
Some(state) => {
state.activate()?;
if let Some(gui_handler) = gui_handler {
gui_handler.set_top_gui(Some(state.clone()));
}
state.activate(gui_handler)?;
gui_handler.set_top_gui(Some(state.clone()));
if logging {
println!("Change UI State to {}", state.name);
@ -317,9 +318,7 @@ impl States {
*current = Some(state);
}
None => {
if let Some(gui_handler) = gui_handler {
gui_handler.set_top_gui(None);
}
gui_handler.set_top_gui(None);
if logging {
println!("Change UI State to None");
@ -353,19 +352,14 @@ impl States {
let state: Option<Arc<State>> = id.into().map(|id| self.get_state(id)).transpose()?;
let weak_state = state.map(|s| Arc::downgrade(&s));
let weak_current_state = Arc::downgrade(&self.current_state);
let gui_handler = if self.control_top_gui {
Some(Arc::downgrade(&self.gui_handler))
} else {
None
};
let logging = self.log_state_change;
Ok(Box::new(move || {
Ok(Box::new(move |gui_handler: &mut GuiHandler| {
if let Some(current) = weak_current_state.upgrade() {
Self::_set_state(
weak_state.as_ref().map(|w| w.upgrade()).flatten(),
&mut *current.lock().unwrap(),
gui_handler.as_ref().map(|h| h.upgrade()).flatten(),
gui_handler,
logging,
)?;
}
@ -377,7 +371,7 @@ impl States {
impl State {
fn new<'a>(
gui_handler: &Arc<GuiHandler>,
gui_handler: &mut GuiHandler,
name: &str,
creation_type: CreationType<'a>,
) -> Result<Arc<Self>> {
@ -435,56 +429,71 @@ impl State {
}
}
fn activate(&self) -> Result<()> {
self.top_level_gui.enable()?;
fn activate(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.top_level_gui.enable(gui_handler)?;
if let Some(activate) = self.on_activate.read().unwrap().as_ref() {
(activate)()?;
(activate)(gui_handler)?;
}
Ok(())
}
fn deactivate(&self) -> Result<()> {
self.top_level_gui.disable()?;
fn deactivate(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.top_level_gui.disable(gui_handler)?;
if let Some(deactivate) = self.on_deactivate.read().unwrap().as_ref() {
(deactivate)()?;
(deactivate)(gui_handler)?;
}
Ok(())
}
fn set_on_activate(&self, on_activate: Option<Box<dyn Fn() -> Result<()> + Send + Sync>>) {
fn set_on_activate(
&self,
on_activate: Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>,
) {
*self.on_activate.write().unwrap() = on_activate;
}
fn set_on_deactivate(&self, on_deactivate: Option<Box<dyn Fn() -> Result<()> + Send + Sync>>) {
fn set_on_deactivate(
&self,
on_deactivate: Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>,
) {
*self.on_deactivate.write().unwrap() = on_deactivate;
}
fn set_previous_tab(&self, previous_tab: Option<Box<dyn Fn() -> Result<()> + Send + Sync>>) {
fn set_previous_tab(
&self,
previous_tab: Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>,
) {
*self.previous_tab.write().unwrap() = previous_tab;
}
fn set_next_tab(&self, next_tab: Option<Box<dyn Fn() -> Result<()> + Send + Sync>>) {
fn set_next_tab(
&self,
next_tab: Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>,
) {
*self.next_tab.write().unwrap() = next_tab;
}
fn set_decline(&self, decline: Option<Box<dyn Fn() -> Result<()> + Send + Sync>>) {
fn set_decline(
&self,
decline: Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>,
) {
*self.decline.write().unwrap() = decline;
}
}
impl TopGui for State {
fn decline(&self) -> Result<()> {
fn decline(&self, gui_handler: &mut GuiHandler) -> Result<()> {
match self.decline.read().unwrap().as_ref() {
Some(decline) => {
(decline)()?;
(decline)(gui_handler)?;
}
None => {
if let Some(top_gui) = self.top_level_gui.top_gui() {
top_gui.decline()?;
top_gui.decline(gui_handler)?;
}
}
}
@ -492,14 +501,14 @@ impl TopGui for State {
Ok(())
}
fn next_tab(&self, second_level: bool) -> Result<()> {
fn next_tab(&self, gui_handler: &mut GuiHandler, second_level: bool) -> Result<()> {
match self.next_tab.read().unwrap().as_ref() {
Some(next_tab) => {
(next_tab)()?;
(next_tab)(gui_handler)?;
}
None => {
if let Some(top_gui) = self.top_level_gui.top_gui() {
top_gui.next_tab(second_level)?;
top_gui.next_tab(gui_handler, second_level)?;
}
}
}
@ -507,14 +516,14 @@ impl TopGui for State {
Ok(())
}
fn previous_tab(&self, second_level: bool) -> Result<()> {
fn previous_tab(&self, gui_handler: &mut GuiHandler, second_level: bool) -> Result<()> {
match self.previous_tab.read().unwrap().as_ref() {
Some(previous_tab) => {
(previous_tab)()?;
(previous_tab)(gui_handler)?;
}
None => {
if let Some(top_gui) = self.top_level_gui.top_gui() {
top_gui.previous_tab(second_level)?;
top_gui.previous_tab(gui_handler, second_level)?;
}
}
}