-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start implementing copy to all (#24)
- Loading branch information
Showing
17 changed files
with
256 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#![allow(unused)] | ||
|
||
use crate::render_util::item_context_menu; | ||
use nexus::imgui::{MenuItem, Ui}; | ||
use std::fmt; | ||
|
||
pub struct DynAction<T>(Option<Box<dyn FnMut(&mut T)>>); | ||
|
||
impl<T> DynAction<T> { | ||
pub const fn empty() -> Self { | ||
Self(None) | ||
} | ||
|
||
pub fn new(action: impl FnMut(&mut T) + 'static) -> Self { | ||
Self(Some(Box::new(action))) | ||
} | ||
|
||
pub fn try_new(action: Option<impl FnMut(&mut T) + 'static>) -> Self { | ||
match action { | ||
Some(action) => Self::new(action), | ||
None => Self::empty(), | ||
} | ||
} | ||
|
||
pub fn is_none(&self) -> bool { | ||
!self.is_some() | ||
} | ||
|
||
pub fn is_some(&self) -> bool { | ||
self.0.is_some() | ||
} | ||
|
||
pub fn perform(&mut self, value: &mut T) { | ||
if let Some(action) = self.0.as_mut() { | ||
action(value) | ||
} | ||
} | ||
|
||
pub fn set(&mut self, action: impl FnMut(&mut T) + 'static) { | ||
*self = Self::new(action); | ||
} | ||
|
||
pub fn map<O>(self, mut map: impl (FnMut(&mut O) -> &mut T) + 'static) -> DynAction<O> | ||
where | ||
T: 'static, | ||
{ | ||
DynAction::try_new( | ||
self.0 | ||
.map(|mut action| move |value: &mut O| action(map(value))), | ||
) | ||
} | ||
|
||
pub fn try_map<O>( | ||
self, | ||
mut try_map: impl FnMut(&mut O) -> Option<&mut T> + 'static, | ||
) -> DynAction<O> | ||
where | ||
T: 'static, | ||
{ | ||
DynAction::try_new(self.0.map(|mut action| { | ||
move |value: &mut O| { | ||
if let Some(inner) = try_map(value) { | ||
action(inner) | ||
} | ||
} | ||
})) | ||
} | ||
|
||
pub fn or(&mut self, other: Self) { | ||
if self.is_none() { | ||
*self = other; | ||
} | ||
} | ||
|
||
pub fn render_copy_all( | ||
&mut self, | ||
ui: &Ui, | ||
id: impl Into<String>, | ||
action: impl FnMut(&mut T) + 'static, | ||
) { | ||
item_context_menu(id, || { | ||
if MenuItem::new("Copy to all siblings").build(ui) { | ||
self.set(action); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
impl<T> fmt::Debug for DynAction<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
f.debug_tuple("DynAction") | ||
.field(&format_args!( | ||
"{:?}", | ||
self.0.as_ref().map(|inner| inner.as_ref() as *const _) | ||
)) | ||
.finish() | ||
} | ||
} | ||
|
||
/// Render a copy all context menu for the last item. | ||
/// | ||
/// Note: this uses the field name as popup id! | ||
#[macro_export] | ||
macro_rules! render_copy_field { | ||
($action:expr, $ui:expr, $self:ident . $field:ident) => {{ | ||
let value = $self.$field; | ||
$action.render_copy_all($ui, stringify!($field), move |other| { | ||
other.$field = value; | ||
}); | ||
}}; | ||
($action:expr, $ui:expr, *$field:ident) => {{ | ||
let value = *$field; | ||
$action.render_copy_all($ui, stringify!($field), move |other| { | ||
other.$field = value; | ||
}); | ||
}}; | ||
($action:expr, $ui:expr, $field:ident) => {{ | ||
$action.render_copy_all($ui, stringify!($field), move |other| { | ||
other.$field = $field; | ||
}); | ||
}}; | ||
} | ||
|
||
pub use render_copy_field; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.