#![allow(deprecated)]
use std::any::TypeId;
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::hash::Hash;
use std::rc::Rc;
use std::time::Duration;
use waft_embedder::{Canvas, Color, Matrix4, Offset, Paint, RRect, Rect, TextDirection};
use waft_foundation::{
App, Handle, HandleId, Listenable, ListenableObject, Listener, OptionRetained, Ref, Retained,
Timer, ValueChanged,
};
use waft_gestures::{
GestureLongPressCallback, GestureLongPressUpCallback, GestureTapCallback,
GestureTapCancelCallback, GestureTapDownCallback, GestureTapUpCallback, PointerEnterEvent,
PointerExitEvent, TapDownDetails, TapUpDetails,
};
use waft_painting::{BorderRadius, BoxShape, ShapeBorder, ShapeBorderRef, get_as_translation};
use waft_rendering::{AnyRenderBox, HitTestBehavior};
use waft_services::MouseCursorRef;
use waft_widgets::{
ActionRef, Actions, ActivateIntent, AutomaticKeepAliveClientMixin,
AutomaticKeepAliveClientMixinData, BuildContext, ButtonActivateIntent, CallbackAction,
DefaultSelectionStyle, Directionality, Feedback, Focus, FocusHighlightMode, FocusManager,
FocusNodeRef, GestureDetector, HighlightModeListener, InheritedWidget, IntoWidget, KeyRef,
MediaQuery, MouseRegion, NavigationMode, Semantics, State, StateData, StatefulWidget,
StatelessWidget, WidgetRef, WidgetState, WidgetStateMouseCursor, WidgetStateProperty,
WidgetStatePropertyRef, WidgetStates, debug_check_has_directionality,
};
use crate::debug::debug_check_has_material;
use crate::ink_highlight::InkHighlight;
use crate::material::{
AnyInkFeature, AnyMaterialInkController, InkFeatureLeaf, InkFeatureLeafData, InkFeatureObject,
Material, RectCallback,
};
use crate::material_state::MaterialStatesController;
use crate::theme::Theme;
pub struct InteractiveInkFeatureData {
color: Color,
custom_border: Option<ShapeBorderRef>,
}
impl InteractiveInkFeatureData {
pub fn new(color: Color, custom_border: Option<ShapeBorderRef>) -> InteractiveInkFeatureData {
InteractiveInkFeatureData {
color,
custom_border,
}
}
pub(crate) fn custom_border_ref(&self) -> Option<&dyn ShapeBorder> {
self.custom_border.as_deref()
}
}
pub trait InteractiveInkFeatureLeafData: InkFeatureLeafData {
fn interactive_ink_feature(&self) -> &InteractiveInkFeatureData;
fn interactive_ink_feature_mut(&mut self) -> &mut InteractiveInkFeatureData;
}
pub trait InteractiveInkFeatureLeaf: InkFeatureLeaf + InteractiveInkFeatureLeafData {
fn confirm(self: Handle<Self>, app: &mut App) {
let _ = (self, app);
}
fn cancel(self: Handle<Self>, app: &mut App) {
let _ = (self, app);
}
}
pub trait InteractiveInkFeatureObject: Sized + 'static {
fn as_interactive_ink_feature(self: Handle<Self>) -> AnyInteractiveInkFeature;
}
pub type InteractiveInkFeatureRef = Retained<AnyInteractiveInkFeature>;
waft_foundation::erased_handle!(
AnyInteractiveInkFeature: InteractiveInkFeatureLeaf
=> InteractiveInkFeatureObject::as_interactive_ink_feature
);
impl<F: InteractiveInkFeatureLeaf> InteractiveInkFeatureObject for F {
fn as_interactive_ink_feature(self: Handle<Self>) -> AnyInteractiveInkFeature {
AnyInteractiveInkFeature {
id: self.id(),
vtable: const { &InteractiveInkFeatureVTable::of::<F>() },
}
}
}
struct InteractiveInkFeatureVTable {
type_name: fn() -> &'static str,
as_ink_feature: fn(HandleId) -> AnyInkFeature,
confirm: fn(&mut App, HandleId),
cancel: fn(&mut App, HandleId),
color: fn(&App, HandleId) -> Color,
set_color: fn(&mut App, HandleId, Color),
custom_border: fn(&App, HandleId) -> Option<ShapeBorderRef>,
set_custom_border: fn(&mut App, HandleId, Option<ShapeBorderRef>),
}
fn resolve<T: 'static>(id: HandleId) -> Handle<T> {
Handle::from_id(id)
}
impl InteractiveInkFeatureVTable {
const fn of<F: InteractiveInkFeatureLeaf>() -> InteractiveInkFeatureVTable {
InteractiveInkFeatureVTable {
type_name: std::any::type_name::<F>,
as_ink_feature: |id| resolve::<F>(id).as_ink_feature(),
confirm: |app, id| F::confirm(resolve(id), app),
cancel: |app, id| F::cancel(resolve(id), app),
color: |app, id| InteractiveInkFeature::color(resolve::<F>(id), app),
set_color: |app, id, value| {
InteractiveInkFeature::set_color(resolve::<F>(id), app, value)
},
custom_border: |app, id| InteractiveInkFeature::custom_border(resolve::<F>(id), app),
set_custom_border: |app, id, value| {
InteractiveInkFeature::set_custom_border(resolve::<F>(id), app, value)
},
}
}
}
#[derive(Clone, Copy)]
pub struct AnyInteractiveInkFeature {
id: HandleId,
vtable: &'static InteractiveInkFeatureVTable,
}
impl fmt::Debug for AnyInteractiveInkFeature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}({:?})", (self.vtable.type_name)(), self.id)
}
}
impl AnyInteractiveInkFeature {
pub fn id(self) -> HandleId {
self.id
}
pub fn downcast<T: 'static>(self, app: &App) -> Option<Handle<T>> {
app.handle::<T>(self.id)
}
pub fn as_ink_feature(self) -> AnyInkFeature {
(self.vtable.as_ink_feature)(self.id)
}
pub fn confirm(self, app: &mut App) {
(self.vtable.confirm)(app, self.id);
}
pub fn cancel(self, app: &mut App) {
(self.vtable.cancel)(app, self.id);
}
pub fn dispose(self, app: &mut App) {
self.as_ink_feature().dispose(app);
}
pub fn color(self, app: &App) -> Color {
(self.vtable.color)(app, self.id)
}
pub fn set_color(self, app: &mut App, value: Color) {
(self.vtable.set_color)(app, self.id, value);
}
pub fn custom_border(self, app: &App) -> Option<ShapeBorderRef> {
(self.vtable.custom_border)(app, self.id)
}
pub fn set_custom_border(self, app: &mut App, value: Option<ShapeBorderRef>) {
(self.vtable.set_custom_border)(app, self.id, value);
}
}
pub struct InteractiveInkFeature;
impl InteractiveInkFeature {
pub fn color<F: InteractiveInkFeatureLeaf>(this: Handle<F>, app: &App) -> Color {
app.get(this).interactive_ink_feature().color
}
pub fn set_color<F: InteractiveInkFeatureLeaf>(this: Handle<F>, app: &mut App, value: Color) {
if value == InteractiveInkFeature::color(this, app) {
return;
}
app.get_mut(this).interactive_ink_feature_mut().color = value;
crate::material::InkFeature::controller(this, app).mark_needs_paint(app);
}
pub fn custom_border<F: InteractiveInkFeatureLeaf>(
this: Handle<F>,
app: &App,
) -> Option<ShapeBorderRef> {
app.get(this)
.interactive_ink_feature()
.custom_border
.clone()
}
pub fn set_custom_border<F: InteractiveInkFeatureLeaf>(
this: Handle<F>,
app: &mut App,
value: Option<ShapeBorderRef>,
) {
if value == app.get(this).interactive_ink_feature().custom_border {
return;
}
app.get_mut(this)
.interactive_ink_feature_mut()
.custom_border = value;
crate::material::InkFeature::controller(this, app).mark_needs_paint(app);
}
#[allow(clippy::too_many_arguments)]
pub fn paint_ink_circle(
app: &App,
canvas: &mut Canvas,
transform: &Matrix4,
paint: &Paint,
center: Offset,
radius: f64,
text_direction: Option<TextDirection>,
custom_border: Option<&dyn ShapeBorder>,
border_radius: BorderRadius,
clip_callback: Option<&RectCallback>,
) {
let origin_offset = get_as_translation(*transform);
canvas.save();
match origin_offset {
None => canvas.concat(transform),
Some(origin_offset) => {
canvas.translate(origin_offset.dx() as f32, origin_offset.dy() as f32)
}
}
if let Some(clip_callback) = clip_callback {
let rect = clip_callback(app);
if let Some(custom_border) = custom_border {
let path = custom_border.get_outer_path(rect, text_direction);
canvas.clip_path(
path.as_valo(),
path.fill_type,
waft_embedder::ClipOp::Intersect,
true,
);
} else if border_radius != BorderRadius::ZERO {
clip_rrect(canvas, rrect_from_rect_and_corners(rect, border_radius));
} else {
canvas.clip_rect(rect, waft_embedder::ClipOp::Intersect, true);
}
}
canvas.draw_circle(center, radius as f32, paint);
canvas.restore();
}
}
pub(crate) fn rrect_from_rect_and_corners(rect: Rect, border_radius: BorderRadius) -> RRect {
RRect::from_rect_and_corners(
rect,
border_radius.top_left,
border_radius.top_right,
border_radius.bottom_right,
border_radius.bottom_left,
)
}
pub(crate) fn clip_rrect(canvas: &mut Canvas, rrect: RRect) {
canvas.clip_rrect_radii_elliptical(
rrect.outer_rect(),
waft_embedder::rrect_radii_elliptical(rrect),
waft_embedder::ClipOp::Intersect,
true,
);
}
pub trait InteractiveInkFeatureFactory: fmt::Debug {
#[allow(clippy::too_many_arguments)]
fn create(
&self,
app: &mut App,
controller: AnyMaterialInkController,
reference_box: AnyRenderBox,
position: Offset,
color: Color,
text_direction: TextDirection,
contained_ink_well: bool,
rect_callback: Option<RectCallback>,
border_radius: Option<BorderRadius>,
custom_border: Option<ShapeBorderRef>,
radius: Option<f64>,
on_removed: Option<Listener>,
) -> InteractiveInkFeatureRef;
}
pub type InteractiveInkFeatureFactoryRef = Rc<dyn InteractiveInkFeatureFactory>;
impl PartialEq for dyn InteractiveInkFeatureFactory {
fn eq(&self, other: &dyn InteractiveInkFeatureFactory) -> bool {
std::ptr::addr_eq(
self as *const dyn InteractiveInkFeatureFactory,
other as *const dyn InteractiveInkFeatureFactory,
)
}
}
#[derive(Debug)]
struct ParentInkResponseProvider {
state: Handle<InkResponseState>,
child: WidgetRef,
}
impl ParentInkResponseProvider {
fn maybe_of(app: &mut App, context: BuildContext) -> Option<Handle<InkResponseState>> {
context
.depend_on_inherited_widget_of_exact_type::<ParentInkResponseProvider>(app)
.map(|provider| provider.state)
}
}
impl InheritedWidget for ParentInkResponseProvider {
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &ParentInkResponseProvider) -> bool {
self.state != old_widget.state
}
}
pub type GetRectCallback = Rc<dyn Fn(&App, AnyRenderBox) -> Option<RectCallback>>;
pub type CheckContext = Rc<dyn Fn(&App, BuildContext) -> bool>;
#[derive(Clone)]
pub struct InkResponse {
pub key: Option<KeyRef>,
pub child: Option<WidgetRef>,
pub on_tap: Option<GestureTapCallback>,
pub on_tap_down: Option<GestureTapDownCallback>,
pub on_tap_up: Option<GestureTapUpCallback>,
pub on_tap_cancel: Option<GestureTapCallback>,
pub on_double_tap: Option<GestureTapCallback>,
pub on_long_press: Option<GestureLongPressCallback>,
pub on_long_press_up: Option<GestureLongPressUpCallback>,
pub on_secondary_tap: Option<GestureTapCallback>,
pub on_secondary_tap_down: Option<GestureTapDownCallback>,
pub on_secondary_tap_up: Option<GestureTapUpCallback>,
pub on_secondary_tap_cancel: Option<GestureTapCallback>,
pub on_tertiary_tap_down: Option<GestureTapDownCallback>,
pub on_tertiary_tap_up: Option<GestureTapUpCallback>,
pub on_tertiary_tap_cancel: Option<GestureTapCancelCallback>,
pub on_highlight_changed: Option<ValueChanged<bool>>,
pub on_hover: Option<ValueChanged<bool>>,
pub mouse_cursor: Option<MouseCursorRef>,
pub contained_ink_well: bool,
pub highlight_shape: BoxShape,
pub radius: Option<f64>,
pub border_radius: Option<BorderRadius>,
pub custom_border: Option<ShapeBorderRef>,
pub focus_color: Option<Color>,
pub hover_color: Option<Color>,
pub highlight_color: Option<Color>,
pub overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>,
pub splash_color: Option<Color>,
pub splash_factory: Option<InteractiveInkFeatureFactoryRef>,
pub enable_feedback: bool,
pub exclude_from_semantics: bool,
pub on_focus_change: Option<ValueChanged<bool>>,
pub autofocus: bool,
pub focus_node: Option<FocusNodeRef>,
pub can_request_focus: bool,
pub states_controller: Option<Ref<MaterialStatesController>>,
pub hover_duration: Option<Duration>,
pub get_rect_callback: GetRectCallback,
pub debug_check_context: CheckContext,
}
impl fmt::Debug for InkResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("InkResponse")
.field("contained_ink_well", &self.contained_ink_well)
.field("highlight_shape", &self.highlight_shape)
.field("child", &self.child)
.finish_non_exhaustive()
}
}
impl Default for InkResponse {
fn default() -> InkResponse {
InkResponse {
key: None,
child: None,
on_tap: None,
on_tap_down: None,
on_tap_up: None,
on_tap_cancel: None,
on_double_tap: None,
on_long_press: None,
on_long_press_up: None,
on_secondary_tap: None,
on_secondary_tap_down: None,
on_secondary_tap_up: None,
on_secondary_tap_cancel: None,
on_tertiary_tap_down: None,
on_tertiary_tap_up: None,
on_tertiary_tap_cancel: None,
on_highlight_changed: None,
on_hover: None,
mouse_cursor: None,
contained_ink_well: false,
highlight_shape: BoxShape::Circle,
radius: None,
border_radius: None,
custom_border: None,
focus_color: None,
hover_color: None,
highlight_color: None,
overlay_color: None,
splash_color: None,
splash_factory: None,
enable_feedback: true,
exclude_from_semantics: false,
on_focus_change: None,
autofocus: false,
focus_node: None,
can_request_focus: true,
states_controller: None,
hover_duration: None,
get_rect_callback: Rc::new(|_app, _reference_box| None),
debug_check_context: Rc::new(InkResponse::debug_check_context),
}
}
}
macro_rules! ink_response_setters {
($name:ident, $($field:ident: $ty:ty),* $(,)?) => {
$(
#[doc = concat!("Dart `", stringify!($name), "(", stringify!($field), ":)`.")]
pub fn $field(mut self, $field: $ty) -> $name {
self.ink_response_mut().$field = Some($field);
self
}
)*
};
}
macro_rules! ink_response_value_setters {
($name:ident, $($field:ident: $ty:ty),* $(,)?) => {
$(
#[doc = concat!("Dart `", stringify!($name), "(", stringify!($field), ":)`.")]
pub fn $field(mut self, $field: $ty) -> $name {
self.ink_response_mut().$field = $field;
self
}
)*
};
}
macro_rules! ink_response_shared_setters {
($name:ident) => {
ink_response_setters!(
$name,
key: KeyRef,
on_tap: GestureTapCallback,
on_tap_down: GestureTapDownCallback,
on_tap_up: GestureTapUpCallback,
on_tap_cancel: GestureTapCallback,
on_double_tap: GestureTapCallback,
on_long_press: GestureLongPressCallback,
on_long_press_up: GestureLongPressUpCallback,
on_secondary_tap: GestureTapCallback,
on_secondary_tap_up: GestureTapUpCallback,
on_secondary_tap_down: GestureTapDownCallback,
on_secondary_tap_cancel: GestureTapCallback,
on_tertiary_tap_up: GestureTapUpCallback,
on_tertiary_tap_down: GestureTapDownCallback,
on_tertiary_tap_cancel: GestureTapCancelCallback,
on_highlight_changed: ValueChanged<bool>,
on_hover: ValueChanged<bool>,
mouse_cursor: MouseCursorRef,
radius: f64,
border_radius: BorderRadius,
focus_color: Color,
hover_color: Color,
highlight_color: Color,
overlay_color: WidgetStatePropertyRef<Option<Color>>,
splash_color: Color,
splash_factory: InteractiveInkFeatureFactoryRef,
on_focus_change: ValueChanged<bool>,
focus_node: FocusNodeRef,
states_controller: Ref<MaterialStatesController>,
hover_duration: Duration,
);
ink_response_value_setters!(
$name,
enable_feedback: bool,
exclude_from_semantics: bool,
can_request_focus: bool,
autofocus: bool,
);
#[doc = concat!("Dart `", stringify!($name), "(child:)`.")]
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> $name {
self.ink_response_mut().child = Some(child.into_widget());
self
}
#[doc = concat!("Dart `", stringify!($name), "(customBorder:)`.")]
pub fn custom_border(mut self, custom_border: impl Into<ShapeBorderRef>) -> $name {
self.ink_response_mut().custom_border = Some(custom_border.into());
self
}
pub fn override_get_rect_callback(
mut self,
get_rect_callback: impl Fn(&App, AnyRenderBox) -> Option<RectCallback> + 'static,
) -> $name {
self.ink_response_mut().get_rect_callback = Rc::new(get_rect_callback);
self
}
pub fn override_debug_check_context(
mut self,
debug_check_context: impl Fn(&App, BuildContext) -> bool + 'static,
) -> $name {
self.ink_response_mut().debug_check_context = Rc::new(debug_check_context);
self
}
};
}
impl InkResponse {
pub fn new() -> InkResponse {
InkResponse::default()
}
fn ink_response_mut(&mut self) -> &mut InkResponse {
self
}
ink_response_shared_setters!(InkResponse);
ink_response_value_setters!(
InkResponse,
contained_ink_well: bool,
highlight_shape: BoxShape,
);
pub fn get_rect_callback(
&self,
app: &App,
reference_box: AnyRenderBox,
) -> Option<RectCallback> {
(self.get_rect_callback)(app, reference_box)
}
pub fn debug_check_context(app: &App, context: BuildContext) -> bool {
debug_assert!(debug_check_has_material(app, context));
debug_assert!(debug_check_has_directionality(
app, context, None, None, None
));
true
}
pub(crate) fn build_state_widget(&self, app: &mut App, context: BuildContext) -> WidgetRef {
let parent_state = ParentInkResponseProvider::maybe_of(app, context);
InkResponseStateWidget {
response: self.clone(),
parent_state,
}
.into_widget()
}
}
impl StatelessWidget for InkResponse {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
self.build_state_widget(app, context)
}
}
#[derive(Debug)]
struct InkResponseStateWidget {
response: InkResponse,
parent_state: Option<Handle<InkResponseState>>,
}
impl StatefulWidget for InkResponseStateWidget {
type State = InkResponseState;
fn create_state(&self) -> InkResponseState {
InkResponseState {
state: StateData::new(),
automatic_keep_alive_client: AutomaticKeepAliveClientMixinData::new(),
splashes: None,
current_splash: None,
hovering: false,
highlights: HashMap::new(),
action_map: HashMap::new(),
internal_states_controller: None,
active_children: Vec::new(),
activation_timer: None,
has_focus: false,
focus_highlight_mode_listener: None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum HighlightType {
Pressed,
Hover,
Focus,
}
struct InkResponseState {
state: StateData<InkResponseStateWidget>,
automatic_keep_alive_client: AutomaticKeepAliveClientMixinData,
splashes: Option<HashSet<InteractiveInkFeatureRef>>,
current_splash: Option<InteractiveInkFeatureRef>,
hovering: bool,
highlights: HashMap<HighlightType, Option<Ref<InkHighlight>>>,
action_map: HashMap<TypeId, ActionRef>,
internal_states_controller: Option<Ref<MaterialStatesController>>,
active_children: Vec<Handle<InkResponseState>>,
activation_timer: Option<Timer>,
has_focus: bool,
focus_highlight_mode_listener: Option<HighlightModeListener>,
}
impl InkResponseState {
const ACTIVATION_DURATION: Duration = Duration::from_millis(100);
fn highlights_exist(self: Handle<Self>, app: &App) -> bool {
app.get(self).highlights.values().any(Option::is_some)
}
fn mark_child_ink_response_pressed(
self: Handle<Self>,
app: &mut App,
child_state: Handle<InkResponseState>,
value: bool,
) {
let last_any_pressed = self.any_child_ink_response_pressed(app);
let active_children = &mut app.get_mut(self).active_children;
if value {
active_children.push(child_state);
} else if let Some(index) = active_children
.iter()
.position(|child| *child == child_state)
{
active_children.remove(index);
}
let now_any_pressed = self.any_child_ink_response_pressed(app);
if now_any_pressed != last_any_pressed
&& let Some(parent_state) = self.widget(app).parent_state
{
parent_state.mark_child_ink_response_pressed(app, self, now_any_pressed);
}
}
fn any_child_ink_response_pressed(self: Handle<Self>, app: &App) -> bool {
!app.get(self).active_children.is_empty()
}
fn activate_on_intent(self: Handle<Self>, app: &mut App) {
if let Some(timer) = app.get_mut(self).activation_timer.take() {
timer.cancel(app);
}
let context = self.context(app);
self.start_new_splash(app, None, Some(context));
if let Some(current_splash) = app.get_mut(self).current_splash.take() {
current_splash.confirm(app);
}
if let Some(on_tap) = self.widget(app).response.on_tap.clone() {
if self.widget(app).response.enable_feedback {
Feedback::for_tap(app, context);
}
on_tap.call(app);
}
let timer = Timer::new(
app,
Self::ACTIVATION_DURATION,
Listener::new(move |app| {
self.update_highlight(app, HighlightType::Pressed, false, true);
}),
);
app.get_mut(self).activation_timer = Some(timer);
}
fn simulate_tap(self: Handle<Self>, app: &mut App) {
let context = self.context(app);
self.start_new_splash(app, None, Some(context));
self.handle_tap(app);
}
fn simulate_long_press(self: Handle<Self>, app: &mut App) {
let context = self.context(app);
self.start_new_splash(app, None, Some(context));
self.handle_long_press(app);
}
fn handle_states_controller_change(self: Handle<Self>, app: &mut App) {
self.set_state(app, |_state| {});
}
fn states_controller(self: Handle<Self>, app: &App) -> Handle<MaterialStatesController> {
self.widget(app)
.response
.states_controller
.handle()
.or(app.get(self).internal_states_controller.handle())
.expect("created in init_states_controller")
}
fn states(self: Handle<Self>, app: &App) -> WidgetStates {
app.get(self.states_controller(app)).value().clone()
}
fn init_states_controller(self: Handle<Self>, app: &mut App) {
if self.widget(app).response.states_controller.is_none() {
let controller = MaterialStatesController::new(app, None);
app.get_mut(self).internal_states_controller = Some(controller);
}
let enabled = self.enabled(app);
let states_controller = self.states_controller(app);
states_controller.update(app, WidgetState::Disabled, !enabled);
states_controller.add_listener(
app,
Listener::handle_method(self, Self::handle_states_controller_change),
);
}
fn get_fade_duration_for_type(
self: Handle<Self>,
app: &App,
r#type: HighlightType,
) -> Duration {
match r#type {
HighlightType::Pressed => Duration::from_millis(200),
HighlightType::Hover | HighlightType::Focus => self
.widget(app)
.response
.hover_duration
.unwrap_or(Duration::from_millis(50)),
}
}
fn update_highlight(
self: Handle<Self>,
app: &mut App,
r#type: HighlightType,
value: bool,
call_on_hover: bool,
) {
let highlight = app
.get(self)
.highlights
.get(&r#type)
.and_then(|highlight| highlight.handle());
let handle_ink_removal = Listener::new(move |app| {
debug_assert!(
app.get(self)
.highlights
.get(&r#type)
.and_then(|highlight| highlight.handle())
.is_some()
);
app.get_mut(self).highlights.insert(r#type, None);
self.update_keep_alive(app);
});
let states_controller = self.states_controller(app);
match r#type {
HighlightType::Pressed => {
states_controller.update(app, WidgetState::Pressed, value);
}
HighlightType::Hover => {
if call_on_hover {
states_controller.update(app, WidgetState::Hovered, value);
}
}
HighlightType::Focus => {
}
}
if r#type == HighlightType::Pressed
&& let Some(parent_state) = self.widget(app).parent_state
{
parent_state.mark_child_ink_response_pressed(app, self, value);
}
if value == highlight.is_some_and(|highlight| highlight.active(app)) {
return;
}
if value {
match highlight {
None => {
let response = self.widget(app).response.clone();
let states = self.states(app);
let context = self.context(app);
let resolved_overlay_color = response
.overlay_color
.as_ref()
.and_then(|overlay_color| overlay_color.resolve(&states))
.unwrap_or_else(|| match r#type {
HighlightType::Pressed => response
.highlight_color
.unwrap_or_else(|| Theme::of(app, context).highlight_color),
HighlightType::Focus => response
.focus_color
.unwrap_or_else(|| Theme::of(app, context).focus_color),
HighlightType::Hover => response
.hover_color
.unwrap_or_else(|| Theme::of(app, context).hover_color),
});
let reference_box = context
.find_render_object(app)
.and_then(|render_object| render_object.as_box())
.expect("an ink response has a render box");
let enabled = self.enabled(app);
let controller = Material::of(app, context);
let rect_callback = response.get_rect_callback(app, reference_box);
let text_direction = Directionality::of(app, context);
let fade_duration = self.get_fade_duration_for_type(app, r#type);
let created = InkHighlight::new(
app,
controller,
reference_box,
if enabled {
resolved_overlay_color
} else {
resolved_overlay_color.with_alpha(0)
},
text_direction,
response.highlight_shape,
response.radius,
response.border_radius,
response.custom_border.clone(),
rect_callback,
Some(handle_ink_removal),
fade_duration,
);
app.get_mut(self).highlights.insert(r#type, Some(created));
self.update_keep_alive(app);
}
Some(highlight) => highlight.activate(app),
}
} else {
highlight
.expect("an active highlight exists")
.deactivate(app);
}
debug_assert!(
value
== app
.get(self)
.highlights
.get(&r#type)
.and_then(|highlight| highlight.handle())
.is_some_and(|highlight| highlight.active(app))
);
match r#type {
HighlightType::Pressed => {
if let Some(on_highlight_changed) =
self.widget(app).response.on_highlight_changed.clone()
{
on_highlight_changed(app, value);
}
}
HighlightType::Hover => {
if call_on_hover && let Some(on_hover) = self.widget(app).response.on_hover.clone()
{
on_hover(app, value);
}
}
HighlightType::Focus => {}
}
}
fn update_highlights_and_splashes(self: Handle<Self>, app: &mut App) {
let custom_border = self.widget(app).response.custom_border.clone();
let highlights: Vec<Handle<InkHighlight>> = app
.get(self)
.highlights
.values()
.flatten()
.map(Retained::handle)
.collect();
for highlight in highlights {
InteractiveInkFeature::set_custom_border(highlight, app, custom_border.clone());
}
if let Some(current_splash) = app.get(self).current_splash.handle() {
current_splash.set_custom_border(app, custom_border.clone());
}
let splashes: Vec<AnyInteractiveInkFeature> = app
.get(self)
.splashes
.iter()
.flatten()
.map(Retained::handle)
.collect();
for ink_feature in splashes {
ink_feature.set_custom_border(app, custom_border.clone());
}
}
fn create_splash(
self: Handle<Self>,
app: &mut App,
global_position: Offset,
) -> InteractiveInkFeatureRef {
let context = self.context(app);
let ink_controller = Material::of(app, context);
let reference_box = context
.find_render_object(app)
.and_then(|render_object| render_object.as_box())
.expect("an ink response has a render box");
let position = reference_box.global_to_local(app, global_position, None);
let response = self.widget(app).response.clone();
let states = self.states(app);
let color = response
.overlay_color
.as_ref()
.and_then(|overlay_color| overlay_color.resolve(&states))
.or(response.splash_color)
.unwrap_or_else(|| Theme::of(app, context).splash_color);
let rect_callback = if response.contained_ink_well {
response.get_rect_callback(app, reference_box)
} else {
None
};
let border_radius = response.border_radius;
let custom_border = response.custom_border.clone();
let splash: Rc<std::cell::Cell<Option<AnyInteractiveInkFeature>>> = Rc::default();
let on_removed = Listener::new({
let splash = Rc::clone(&splash);
move |app| {
let splash = splash.get();
if let Some(splashes) = &mut app.get_mut(self).splashes {
debug_assert!(splash.is_some_and(|splash| splashes.contains(&splash)));
if let Some(splash) = splash {
splashes.remove(&splash);
}
if app.get(self).current_splash.handle() == splash {
app.get_mut(self).current_splash = None;
}
self.update_keep_alive(app);
} }
});
let splash_factory = response
.splash_factory
.clone()
.unwrap_or_else(|| Theme::of(app, context).splash_factory.clone());
let text_direction = Directionality::of(app, context);
let created = splash_factory.create(
app,
ink_controller,
reference_box,
position,
color,
text_direction,
response.contained_ink_well,
rect_callback,
border_radius,
custom_border,
response.radius,
Some(on_removed),
);
splash.set(Some(created.handle()));
created
}
fn handle_focus_highlight_mode_change(
self: Handle<Self>,
app: &mut App,
_mode: FocusHighlightMode,
) {
if !self.mounted(app) {
return;
}
self.update_focus_highlights(app);
self.set_state(app, |_state| {});
}
fn should_show_focus(self: Handle<Self>, app: &mut App) -> bool {
let context = self.context(app);
match MediaQuery::maybe_navigation_mode_of(app, context) {
Some(NavigationMode::Traditional) | None => {
self.enabled(app) && app.get(self).has_focus
}
Some(NavigationMode::Directional) => app.get(self).has_focus,
}
}
fn update_focus_highlights(self: Handle<Self>, app: &mut App) {
let show_focus = match FocusManager::instance(app).highlight_mode(app) {
FocusHighlightMode::Touch => false,
FocusHighlightMode::Traditional => self.should_show_focus(app),
};
self.update_highlight(app, HighlightType::Focus, show_focus, true);
}
fn handle_focus_update(self: Handle<Self>, app: &mut App, has_focus: bool) {
app.get_mut(self).has_focus = has_focus;
self.states_controller(app)
.update(app, WidgetState::Focused, has_focus);
self.update_focus_highlights(app);
if let Some(on_focus_change) = self.widget(app).response.on_focus_change.clone() {
on_focus_change(app, has_focus);
}
}
fn handle_any_tap_down(self: Handle<Self>, app: &mut App, details: &TapDownDetails) {
if self.any_child_ink_response_pressed(app) {
return;
}
self.start_new_splash(app, Some(details), None);
}
fn handle_tap_down(self: Handle<Self>, app: &mut App, details: TapDownDetails) {
self.handle_any_tap_down(app, &details);
if let Some(on_tap_down) = self.widget(app).response.on_tap_down.clone() {
on_tap_down(app, details);
}
}
fn handle_tap_up(self: Handle<Self>, app: &mut App, details: TapUpDetails) {
if let Some(on_tap_up) = self.widget(app).response.on_tap_up.clone() {
on_tap_up(app, details);
}
}
fn handle_secondary_tap_down(self: Handle<Self>, app: &mut App, details: TapDownDetails) {
self.handle_any_tap_down(app, &details);
if let Some(on_secondary_tap_down) = self.widget(app).response.on_secondary_tap_down.clone()
{
on_secondary_tap_down(app, details);
}
}
fn handle_secondary_tap_up(self: Handle<Self>, app: &mut App, details: TapUpDetails) {
if let Some(on_secondary_tap_up) = self.widget(app).response.on_secondary_tap_up.clone() {
on_secondary_tap_up(app, details);
}
}
fn handle_tertiary_tap_down(self: Handle<Self>, app: &mut App, details: TapDownDetails) {
self.handle_any_tap_down(app, &details);
if let Some(on_tertiary_tap_down) = self.widget(app).response.on_tertiary_tap_down.clone() {
on_tertiary_tap_down(app, details);
}
}
fn handle_tertiary_tap_up(self: Handle<Self>, app: &mut App, details: TapUpDetails) {
if let Some(current_splash) = app.get_mut(self).current_splash.take() {
current_splash.confirm(app);
}
self.update_highlight(app, HighlightType::Pressed, false, true);
if let Some(on_tertiary_tap_up) = self.widget(app).response.on_tertiary_tap_up.clone() {
on_tertiary_tap_up(app, details);
}
}
fn handle_tertiary_tap_cancel(self: Handle<Self>, app: &mut App) {
if let Some(current_splash) = app.get_mut(self).current_splash.take() {
current_splash.cancel(app);
}
if let Some(on_tertiary_tap_cancel) =
self.widget(app).response.on_tertiary_tap_cancel.clone()
{
on_tertiary_tap_cancel.call(app);
}
self.update_highlight(app, HighlightType::Pressed, false, true);
}
fn start_new_splash(
self: Handle<Self>,
app: &mut App,
details: Option<&TapDownDetails>,
context: Option<BuildContext>,
) {
debug_assert!(details.is_some() || context.is_some());
let global_position = match context {
Some(context) => {
let reference_box = context
.find_render_object(app)
.and_then(|render_object| render_object.as_box())
.expect("an ink response has a render box");
debug_assert!(
reference_box.has_size(app),
"InkResponse must be done with layout before starting a splash."
);
let center = reference_box.as_object().paint_bounds(app).center();
reference_box.local_to_global(app, center, None)
}
None => details.expect("asserted above").global_position,
};
self.states_controller(app)
.update(app, WidgetState::Pressed, true); let splash = self.create_splash(app, global_position);
app.get_mut(self)
.splashes
.get_or_insert_with(HashSet::new)
.insert(splash.clone());
if let Some(current_splash) = app.get(self).current_splash.handle() {
current_splash.cancel(app);
}
app.get_mut(self).current_splash = Some(splash);
self.update_keep_alive(app);
self.update_highlight(app, HighlightType::Pressed, true, true);
}
fn handle_tap(self: Handle<Self>, app: &mut App) {
if let Some(current_splash) = app.get_mut(self).current_splash.take() {
current_splash.confirm(app);
}
self.update_highlight(app, HighlightType::Pressed, false, true);
if let Some(on_tap) = self.widget(app).response.on_tap.clone() {
if self.widget(app).response.enable_feedback {
let context = self.context(app);
Feedback::for_tap(app, context);
}
on_tap.call(app);
}
}
fn handle_tap_cancel(self: Handle<Self>, app: &mut App) {
if let Some(current_splash) = app.get_mut(self).current_splash.take() {
current_splash.cancel(app);
}
if let Some(on_tap_cancel) = self.widget(app).response.on_tap_cancel.clone() {
on_tap_cancel.call(app);
}
self.update_highlight(app, HighlightType::Pressed, false, true);
}
fn handle_double_tap(self: Handle<Self>, app: &mut App) {
if let Some(current_splash) = app.get_mut(self).current_splash.take() {
current_splash.confirm(app);
}
self.update_highlight(app, HighlightType::Pressed, false, true);
if let Some(on_double_tap) = self.widget(app).response.on_double_tap.clone() {
on_double_tap.call(app);
}
}
fn handle_long_press(self: Handle<Self>, app: &mut App) {
if let Some(current_splash) = app.get_mut(self).current_splash.take() {
current_splash.confirm(app);
}
if let Some(on_long_press) = self.widget(app).response.on_long_press.clone() {
if self.widget(app).response.enable_feedback {
let context = self.context(app);
Feedback::for_long_press(app, context);
}
on_long_press.call(app);
}
}
fn handle_long_press_up(self: Handle<Self>, app: &mut App) {
if let Some(current_splash) = app.get_mut(self).current_splash.take() {
current_splash.confirm(app);
}
if let Some(on_long_press_up) = self.widget(app).response.on_long_press_up.clone() {
on_long_press_up.call(app);
}
}
fn handle_secondary_tap(self: Handle<Self>, app: &mut App) {
if let Some(current_splash) = app.get_mut(self).current_splash.take() {
current_splash.confirm(app);
}
self.update_highlight(app, HighlightType::Pressed, false, true);
if let Some(on_secondary_tap) = self.widget(app).response.on_secondary_tap.clone() {
on_secondary_tap.call(app);
}
}
fn handle_secondary_tap_cancel(self: Handle<Self>, app: &mut App) {
if let Some(current_splash) = app.get_mut(self).current_splash.take() {
current_splash.cancel(app);
}
if let Some(on_secondary_tap_cancel) =
self.widget(app).response.on_secondary_tap_cancel.clone()
{
on_secondary_tap_cancel.call(app);
}
self.update_highlight(app, HighlightType::Pressed, false, true);
}
fn is_widget_enabled(widget: &InkResponseStateWidget) -> bool {
Self::primary_button_enabled(widget)
|| Self::secondary_button_enabled(widget)
|| Self::tertiary_button_enabled(widget)
}
fn primary_button_enabled(widget: &InkResponseStateWidget) -> bool {
let response = &widget.response;
response.on_tap.is_some()
|| response.on_double_tap.is_some()
|| response.on_long_press.is_some()
|| response.on_long_press_up.is_some()
|| response.on_tap_up.is_some()
|| response.on_tap_down.is_some()
}
fn secondary_button_enabled(widget: &InkResponseStateWidget) -> bool {
let response = &widget.response;
response.on_secondary_tap.is_some()
|| response.on_secondary_tap_up.is_some()
|| response.on_secondary_tap_down.is_some()
}
fn tertiary_button_enabled(widget: &InkResponseStateWidget) -> bool {
let response = &widget.response;
response.on_tertiary_tap_up.is_some() || response.on_tertiary_tap_down.is_some()
}
fn enabled(self: Handle<Self>, app: &App) -> bool {
Self::is_widget_enabled(self.widget(app))
}
fn primary_enabled(self: Handle<Self>, app: &App) -> bool {
Self::primary_button_enabled(self.widget(app))
}
fn secondary_enabled(self: Handle<Self>, app: &App) -> bool {
Self::secondary_button_enabled(self.widget(app))
}
fn tertiary_enabled(self: Handle<Self>, app: &App) -> bool {
Self::tertiary_button_enabled(self.widget(app))
}
fn handle_mouse_enter(self: Handle<Self>, app: &mut App, _event: PointerEnterEvent) {
app.get_mut(self).hovering = true;
if self.enabled(app) {
self.handle_hover_change(app);
}
}
fn handle_mouse_exit(self: Handle<Self>, app: &mut App, _event: PointerExitEvent) {
app.get_mut(self).hovering = false;
self.handle_hover_change(app);
}
fn handle_hover_change(self: Handle<Self>, app: &mut App) {
let hovering = app.get(self).hovering;
self.update_highlight(app, HighlightType::Hover, hovering, true);
}
fn can_request_focus(self: Handle<Self>, app: &mut App) -> bool {
let context = self.context(app);
match MediaQuery::maybe_navigation_mode_of(app, context) {
Some(NavigationMode::Traditional) | None => {
self.enabled(app) && self.widget(app).response.can_request_focus
}
Some(NavigationMode::Directional) => true,
}
}
}
impl AutomaticKeepAliveClientMixin for InkResponseState {
fn automatic_keep_alive_client_data(
self: Handle<Self>,
app: &App,
) -> &AutomaticKeepAliveClientMixinData {
&app.get(self).automatic_keep_alive_client
}
fn automatic_keep_alive_client_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut AutomaticKeepAliveClientMixinData {
&mut app.get_mut(self).automatic_keep_alive_client
}
fn want_keep_alive(self: Handle<Self>, app: &App) -> bool {
self.highlights_exist(app)
|| app
.get(self)
.splashes
.as_ref()
.is_some_and(|splashes| !splashes.is_empty())
}
}
impl State for InkResponseState {
type Widget = InkResponseStateWidget;
waft_widgets::state_accessors!();
fn init_state(self: Handle<Self>, app: &mut App) {
AutomaticKeepAliveClientMixin::init_state(self, app);
let activate = CallbackAction::<ActivateIntent>::new(
app,
Rc::new(move |app: &mut App, _intent: &ActivateIntent| {
self.activate_on_intent(app);
None
}),
);
let button_activate = CallbackAction::<ButtonActivateIntent>::new(
app,
Rc::new(move |app: &mut App, _intent: &ButtonActivateIntent| {
self.activate_on_intent(app);
None
}),
);
let action_map = &mut app.get_mut(self).action_map;
action_map.insert(TypeId::of::<ActivateIntent>(), activate.into_erased());
action_map.insert(
TypeId::of::<ButtonActivateIntent>(),
button_activate.into_erased(),
);
self.init_states_controller(app);
let listener: HighlightModeListener =
Rc::new(move |app, mode| self.handle_focus_highlight_mode_change(app, mode));
app.get_mut(self).focus_highlight_mode_listener = Some(Rc::clone(&listener));
FocusManager::instance(app).add_highlight_mode_listener(app, listener);
}
fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &InkResponseStateWidget) {
let widget = &self.widget(app).response;
let old = &old_widget.response;
if widget.states_controller != old.states_controller {
if let Some(old_states_controller) = old.states_controller.clone() {
old_states_controller.remove_listener(
app,
&Listener::handle_method(self, Self::handle_states_controller_change),
);
}
if self.widget(app).response.states_controller.is_some()
&& let Some(internal) = app.get_mut(self).internal_states_controller.take()
{
internal.dispose(app);
}
self.init_states_controller(app);
}
let widget = &self.widget(app).response;
if widget.radius != old.radius
|| widget.highlight_shape != old.highlight_shape
|| widget.border_radius != old.border_radius
{
if let Some(hover_highlight) = app
.get(self)
.highlights
.get(&HighlightType::Hover)
.and_then(|highlight| highlight.handle())
{
InkFeatureLeaf::dispose(hover_highlight, app);
let hovering = app.get(self).hovering;
self.update_highlight(app, HighlightType::Hover, hovering, false);
}
if let Some(focus_highlight) = app
.get(self)
.highlights
.get(&HighlightType::Focus)
.and_then(|highlight| highlight.handle())
{
InkFeatureLeaf::dispose(focus_highlight, app);
}
}
let custom_border_changed = self.widget(app).response.custom_border != old.custom_border;
if custom_border_changed {
self.update_highlights_and_splashes(app);
}
let enabled = self.enabled(app);
if enabled != Self::is_widget_enabled(old_widget) {
let states_controller = self.states_controller(app);
states_controller.update(app, WidgetState::Disabled, !enabled);
if !enabled {
states_controller.update(app, WidgetState::Pressed, false);
if let Some(hover_highlight) = app
.get(self)
.highlights
.get(&HighlightType::Hover)
.and_then(|highlight| highlight.handle())
{
InkFeatureLeaf::dispose(hover_highlight, app);
}
}
let hovering = app.get(self).hovering;
self.update_highlight(app, HighlightType::Hover, hovering, false);
}
self.update_focus_highlights(app);
}
fn dispose(self: Handle<Self>, app: &mut App) {
if let Some(listener) = app.get_mut(self).focus_highlight_mode_listener.take() {
FocusManager::instance(app).remove_highlight_mode_listener(app, &listener);
}
self.states_controller(app).remove_listener(
app,
&Listener::handle_method(self, Self::handle_states_controller_change),
);
if let Some(internal) = app.get_mut(self).internal_states_controller.take() {
internal.dispose(app);
}
if let Some(timer) = app.get_mut(self).activation_timer.take() {
timer.cancel(app);
}
}
fn deactivate(self: Handle<Self>, app: &mut App) {
if let Some(splashes) = app.get_mut(self).splashes.take() {
for splash in splashes {
splash.dispose(app);
}
app.get_mut(self).current_splash = None;
}
debug_assert!(app.get(self).current_splash.is_none());
let highlights: Vec<HighlightType> = app.get(self).highlights.keys().copied().collect();
for highlight in highlights {
if let Some(ink) = app
.get(self)
.highlights
.get(&highlight)
.and_then(|highlight| highlight.handle())
{
InkFeatureLeaf::dispose(ink, app);
}
app.get_mut(self).highlights.insert(highlight, None);
}
if let Some(parent_state) = self.widget(app).parent_state {
parent_state.mark_child_ink_response_pressed(app, self, false);
}
AutomaticKeepAliveClientMixin::deactivate(self, app);
}
fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
let response = self.widget(app).response.clone();
debug_assert!((response.debug_check_context)(app, context));
AutomaticKeepAliveClientMixin::build(self, app);
let theme = Theme::of(app, context);
let highlightable_states = [
WidgetState::Focused,
WidgetState::Hovered,
WidgetState::Pressed,
];
let non_highlightable_states: WidgetStates = self
.states(app)
.into_iter()
.filter(|state| !highlightable_states.contains(state))
.collect();
let with = |state: WidgetState| {
let mut states = non_highlightable_states.clone();
states.insert(state);
states
};
let pressed = with(WidgetState::Pressed);
let focused = with(WidgetState::Focused);
let hovered = with(WidgetState::Hovered);
let get_highlight_color_for_type = |r#type: HighlightType| -> Color {
let overlay = |states: &WidgetStates| {
response
.overlay_color
.as_ref()
.and_then(|overlay_color| overlay_color.resolve(states))
};
match r#type {
HighlightType::Pressed => overlay(&pressed)
.or(response.highlight_color)
.unwrap_or(theme.highlight_color),
HighlightType::Focus => overlay(&focused)
.or(response.focus_color)
.unwrap_or(theme.focus_color),
HighlightType::Hover => overlay(&hovered)
.or(response.hover_color)
.unwrap_or(theme.hover_color),
}
};
let highlights: Vec<(HighlightType, Option<Handle<InkHighlight>>)> = app
.get(self)
.highlights
.iter()
.map(|(r#type, highlight)| (*r#type, highlight.handle()))
.collect();
for (r#type, highlight) in highlights {
if let Some(highlight) = highlight {
InteractiveInkFeature::set_color(
highlight,
app,
get_highlight_color_for_type(r#type),
);
}
}
if let Some(current_splash) = app.get(self).current_splash.handle() {
let states = self.states(app);
let color = response
.overlay_color
.as_ref()
.and_then(|overlay_color| overlay_color.resolve(&states))
.or(response.splash_color)
.unwrap_or(theme.splash_color);
current_splash.set_color(app, color);
}
let states = self.states(app);
let mouse_cursor: MouseCursorRef = response
.mouse_cursor
.clone()
.unwrap_or_else(|| Rc::new(WidgetStateMouseCursor::adaptive_clickable()));
let effective_mouse_cursor =
<dyn WidgetStateProperty<MouseCursorRef>>::resolve_as(&mouse_cursor, &states);
let primary_enabled = self.primary_enabled(app);
let secondary_enabled = self.secondary_enabled(app);
let tertiary_enabled = self.tertiary_enabled(app);
let mut detector = GestureDetector::new().behavior(HitTestBehavior::Opaque);
if primary_enabled {
detector = detector
.on_tap_down(Rc::new(move |app, details| {
self.handle_tap_down(app, details)
}))
.on_tap_up(Rc::new(move |app, details| {
self.handle_tap_up(app, details)
}))
.on_tap(Listener::new(move |app| self.handle_tap(app)))
.on_tap_cancel(Listener::new(move |app| self.handle_tap_cancel(app)));
}
if response.on_double_tap.is_some() {
detector =
detector.on_double_tap(Listener::new(move |app| self.handle_double_tap(app)));
}
if response.on_long_press.is_some() {
detector =
detector.on_long_press(Listener::new(move |app| self.handle_long_press(app)));
}
if response.on_long_press_up.is_some() {
detector =
detector.on_long_press_up(Listener::new(move |app| self.handle_long_press_up(app)));
}
if secondary_enabled {
detector = detector
.on_secondary_tap_down(Rc::new(move |app, details| {
self.handle_secondary_tap_down(app, details)
}))
.on_secondary_tap_up(Rc::new(move |app, details| {
self.handle_secondary_tap_up(app, details)
}))
.on_secondary_tap(Listener::new(move |app| self.handle_secondary_tap(app)))
.on_secondary_tap_cancel(Listener::new(move |app| {
self.handle_secondary_tap_cancel(app)
}));
}
if tertiary_enabled {
detector = detector
.on_tertiary_tap_down(Rc::new(move |app, details| {
self.handle_tertiary_tap_down(app, details)
}))
.on_tertiary_tap_up(Rc::new(move |app, details| {
self.handle_tertiary_tap_up(app, details)
}))
.on_tertiary_tap_cancel(Listener::new(move |app| {
self.handle_tertiary_tap_cancel(app)
}));
}
if let Some(child) = response.child.clone() {
detector = detector.child(child);
}
let can_request_focus = self.can_request_focus(app);
let mut semantics = Semantics::new();
if !(response.exclude_from_semantics || response.on_tap.is_none()) {
semantics = semantics.on_tap(Rc::new(move |app: &mut App| self.simulate_tap(app)));
}
if !(response.exclude_from_semantics || response.on_long_press.is_none()) {
semantics = semantics
.on_long_press(Rc::new(move |app: &mut App| self.simulate_long_press(app)));
}
let selection_style = DefaultSelectionStyle::merge(semantics.child(detector))
.mouse_cursor(effective_mouse_cursor.clone());
let region = MouseRegion::new()
.cursor(effective_mouse_cursor)
.on_enter(Rc::new(move |app, event| {
self.handle_mouse_enter(app, event)
}))
.on_exit(Rc::new(move |app, event| {
self.handle_mouse_exit(app, event)
}))
.child(selection_style);
let mut focus = Focus::new(region)
.can_request_focus(can_request_focus)
.on_focus_change(move |app, has_focus| self.handle_focus_update(app, has_focus))
.autofocus(response.autofocus);
if let Some(focus_node) = response.focus_node {
focus = focus.focus_node(focus_node);
}
ParentInkResponseProvider {
state: self,
child: Actions::new(app.get(self).action_map.clone(), focus).into_widget(),
}
.into_widget()
}
}
#[derive(Clone, Debug)]
pub struct InkWell {
pub ink_response: InkResponse,
}
impl Default for InkWell {
fn default() -> InkWell {
InkWell {
ink_response: InkResponse {
contained_ink_well: true,
highlight_shape: BoxShape::Rectangle,
..InkResponse::default()
},
}
}
}
impl InkWell {
pub fn new() -> InkWell {
InkWell::default()
}
fn ink_response_mut(&mut self) -> &mut InkResponse {
&mut self.ink_response
}
ink_response_shared_setters!(InkWell);
}
impl StatelessWidget for InkWell {
fn key(&self) -> Option<&KeyRef> {
self.ink_response.key.as_ref()
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
self.ink_response.build_state_widget(app, context)
}
}
#[cfg(test)]
mod tests {
use std::cell::{Cell, RefCell};
use waft_embedder::{Rect, TargetPlatform, valo};
use waft_gestures::{K_SECONDARY_BUTTON, K_TERTIARY_BUTTON};
use waft_widgets::{
Align, Builder, Center, FocusHighlightStrategy, FocusNode, GlobalKey, MediaQueryData,
Overlay, OverlayEntry, OverlayPortal, OverlayPortalController, Padding, Positioned,
RepaintBoundary, Row, SizedBox, WidgetStatesController,
};
use waft_widgets::testing::{TestPointer, WidgetTester, find, test_widgets};
use super::*;
use crate::colors::Colors;
use crate::material::RenderInkFeatures;
use crate::no_splash::NoSplash;
use crate::test_support::{PaintInspection, path_like};
use crate::theme_data::ThemeData;
use waft_rendering::testing::{CanvasMethod, RecordedCall, count_calls};
type Log = Rc<RefCell<Vec<String>>>;
fn logger(log: &Log, entry: &'static str) -> Listener {
let log = Rc::clone(log);
Listener::new(move |_app| log.borrow_mut().push(entry.to_string()))
}
fn logged<T: 'static>(log: &Log, entry: &'static str) -> ValueChanged<T> {
let log = Rc::clone(log);
Rc::new(move |_app, _details| log.borrow_mut().push(entry.to_string()))
}
fn take(log: &Log) -> Vec<String> {
std::mem::take(&mut log.borrow_mut())
}
fn ltr<K>(child: impl IntoWidget<K>) -> WidgetRef {
Directionality::new(TextDirection::Ltr, child).into_widget()
}
fn material_ltr(child: WidgetRef) -> WidgetRef {
Material::new()
.child(Directionality::new(TextDirection::Ltr, child))
.into_widget()
}
fn centered_square(child: impl IntoWidget<waft_widgets::StatelessKind>) -> WidgetRef {
Center::new()
.child(SizedBox::square(Some(100.0)).child(child))
.into_widget()
}
fn top_left_square(child: impl IntoWidget<waft_widgets::StatelessKind>) -> WidgetRef {
Align::new()
.alignment(waft_painting::AlignmentGeometry::TOP_LEFT)
.child(SizedBox::square(Some(100.0)).child(child))
.into_widget()
}
fn paints_rect(tester: &WidgetTester, color: u32, rect: Rect) -> bool {
waft_rendering::testing::paints(
tester.ink_list().ops(),
&[waft_rendering::testing::PaintCall::rect_at(
rect,
Some(Color::new(color)),
)],
)
.is_ok()
}
fn paths(drawn: &[RecordedCall]) -> Vec<(u32, Rect)> {
drawn
.iter()
.filter_map(|call| path_like(call).map(|(color, bounds)| (color.value(), bounds)))
.collect()
}
fn highlights(tester: &WidgetTester) -> Vec<(u32, Rect)> {
let render = tester.first::<RenderInkFeatures>();
let app = tester.cell.borrow();
render
.debug_ink_features(&app)
.unwrap_or_default()
.into_iter()
.filter_map(|feature| feature.downcast::<InkHighlight>(&app))
.map(|highlight| {
let reference_box = crate::material::InkFeature::reference_box(highlight, &app);
let origin = reference_box.local_to_global(&app, Offset::ZERO, None);
(
highlight.debug_paint_color(&app).value(),
origin & reference_box.size(&app),
)
})
.collect()
}
const CENTER_RECT: Rect = Rect::from_ltrb(350.0, 250.0, 450.0, 350.0);
const TOP_LEFT_RECT: Rect = Rect::from_ltrb(0.0, 0.0, 100.0, 100.0);
fn center_of_view() -> Offset {
WidgetTester::SIZE.center(Offset::ZERO)
}
fn set_highlight_strategy(tester: &WidgetTester, strategy: FocusHighlightStrategy) {
let mut app = tester.cell.borrow_mut();
FocusManager::instance(&mut app).set_highlight_strategy(&mut app, strategy);
}
fn hover(tester: &mut WidgetTester, at: Offset) -> TestPointer {
let mut mouse = TestPointer::mouse(1, 0);
tester.add_pointer(&mut mouse, Offset::ZERO);
tester.move_to(&mut mouse, at, false);
tester.pump_and_settle();
mouse
}
fn focus_node(tester: &WidgetTester) -> FocusNodeRef {
FocusNode::new(&mut tester.cell.borrow_mut()).into_erased()
}
#[test]
fn ink_well_gestures_control_test() {
test_widgets(|tester| {
let log: Log = Rc::default();
tester.pump_widget(ltr(Material::new().child(
Center::new().child(
InkWell::new()
.on_tap(logger(&log, "tap"))
.on_double_tap(logger(&log, "double-tap"))
.on_long_press(logger(&log, "long-press"))
.on_long_press_up(logger(&log, "long-press-up"))
.on_tap_down(logged(&log, "tap-down"))
.on_tap_up(logged(&log, "tap-up"))
.on_tap_cancel(logger(&log, "tap-cancel")),
),
)));
tester.tap_at(center_of_view(), 1);
tester.pump_by(Duration::from_secs(1));
assert_eq!(take(&log), ["tap-down", "tap-up", "tap"]);
tester.tap_at(center_of_view(), 2);
tester.pump_by(Duration::from_millis(100));
tester.tap_at(center_of_view(), 3);
assert_eq!(take(&log), ["double-tap"]);
tester.long_press_at(center_of_view(), 4);
assert_eq!(
take(&log),
["tap-down", "tap-cancel", "long-press", "long-press-up"]
);
let mut gesture = TestPointer::touch(5);
tester.down(&mut gesture, center_of_view());
tester.pump_by(Duration::from_millis(100));
assert_eq!(*log.borrow(), ["tap-down"]);
tester.up(&gesture);
tester.pump_by(Duration::from_secs(1));
assert_eq!(take(&log), ["tap-down", "tap-up", "tap"]);
let mut gesture = TestPointer::touch(6);
tester.down(&mut gesture, center_of_view());
tester.pump_by(Duration::from_millis(100));
tester.move_to(
&mut gesture,
center_of_view() + Offset::new(0.0, 200.0),
true,
);
tester.cancel(&gesture);
assert_eq!(take(&log), ["tap-down", "tap-cancel"]);
});
}
#[test]
fn ink_well_highlight_should_not_survive_after_on_tap_down_on_double_tap_sequence() {
test_widgets(|tester| {
let log: Log = Rc::default();
tester.pump_widget(ltr(Material::new().child(
Center::new().child(
InkWell::new()
.on_tap(logger(&log, "tap"))
.on_double_tap(logger(&log, "double-tap"))
.on_tap_down(logged(&log, "tap-down"))
.on_tap_cancel(logger(&log, "tap-cancel")),
),
)));
let location = tester.get_center(find::by_type::<InkWell>().evaluate(tester)[0]);
let mut gesture = TestPointer::touch(1);
tester.down(&mut gesture, location);
tester.pump_by(Duration::from_millis(100));
assert_eq!(*log.borrow(), ["tap-down"]);
tester.up(&gesture);
tester.pump_by(Duration::from_millis(100));
tester.tap_at(location, 2);
tester.pump_by(Duration::from_millis(100));
assert_eq!(take(&log), ["tap-down", "double-tap"]);
tester.pump_and_settle();
assert_eq!(
count_calls(tester.ink_list().ops(), CanvasMethod::DrawRect),
0
);
});
}
#[test]
fn ink_well_splash_should_not_survive_after_on_tap_down_on_tap_down_on_tap_cancel_on_double_tap_sequence()
{
test_widgets(|tester| {
let log: Log = Rc::default();
tester.pump_widget(ltr(Material::new().child(
Center::new().child(
InkWell::new()
.on_tap(logger(&log, "tap"))
.on_double_tap(logger(&log, "double-tap"))
.on_tap_down(logged(&log, "tap-down"))
.on_tap_cancel(logger(&log, "tap-cancel")),
),
)));
let location = tester.get_center(find::by_type::<InkWell>().evaluate(tester)[0]);
let mut first = TestPointer::touch(1);
tester.down(&mut first, location);
tester.pump_by(Duration::from_millis(100));
assert_eq!(*log.borrow(), ["tap-down"]);
tester.up(&first);
tester.pump_by(Duration::from_millis(100));
let mut second = TestPointer::touch(2);
tester.down(&mut second, location);
tester.pump_by(Duration::from_millis(100));
assert_eq!(*log.borrow(), ["tap-down", "tap-down"]);
tester.up(&second);
tester.pump_by(Duration::from_millis(100));
assert_eq!(
take(&log),
["tap-down", "tap-down", "tap-cancel", "double-tap"]
);
tester.pump_and_settle();
assert_eq!(
count_calls(tester.ink_list().ops(), CanvasMethod::DrawCircle),
0
);
});
}
#[test]
fn ink_well_only_on_tap_down_enables_gestures() {
test_widgets(|tester| {
let down_tapped = Rc::new(Cell::new(false));
tester.pump_widget(ltr(Material::new().child(Center::new().child(
InkWell::new().on_tap_down({
let down_tapped = Rc::clone(&down_tapped);
Rc::new(move |_app, _details| down_tapped.set(true))
}),
))));
tester.tap_at(center_of_view(), 1);
assert!(down_tapped.get());
});
}
#[test]
fn ink_well_invokes_activation_actions_when_expected() {
test_widgets(|tester| {
let log: Log = Rc::default();
let inner: Rc<Cell<Option<BuildContext>>> = Rc::default();
tester.pump_widget(ltr(Material::new().child(
Center::new().child(
InkWell::new()
.autofocus(true)
.on_tap(logger(&log, "tap"))
.child(Builder::new({
let inner = Rc::clone(&inner);
move |_app, context| {
inner.set(Some(context));
SizedBox::shrink().into_widget()
}
})),
),
)));
let context = inner.get().expect("built");
Actions::invoke(&mut tester.cell.borrow_mut(), context, &ActivateIntent);
tester.pump();
assert_eq!(take(&log), ["tap"]);
Actions::invoke(
&mut tester.cell.borrow_mut(),
context,
&ButtonActivateIntent,
);
tester.pump();
assert_eq!(take(&log), ["tap"]);
});
}
#[test]
fn ink_well_on_long_press_up_callback_is_triggered() {
test_widgets(|tester| {
let was_called = Rc::new(Cell::new(false));
tester.pump_widget(material_ltr(
InkWell::new()
.on_long_press(Listener::new(|_app| {}))
.on_long_press_up({
let was_called = Rc::clone(&was_called);
Listener::new(move |_app| was_called.set(true))
})
.child(SizedBox::new().width(100.0).height(100.0))
.into_widget(),
));
let mut gesture = TestPointer::touch(1);
tester.down(&mut gesture, Offset::new(50.0, 50.0));
tester.pump_by(Duration::from_secs(1));
tester.up(&gesture);
tester.pump_and_settle();
assert!(was_called.get());
});
}
#[test]
fn long_press_and_tap_on_disabled_should_not_throw() {
test_widgets(|tester| {
tester.pump_widget(material_ltr(
Center::new().child(InkWell::new()).into_widget(),
));
tester.tap_at(center_of_view(), 1);
tester.pump_by(Duration::from_secs(1));
tester.long_press_at(center_of_view(), 1);
tester.pump_by(Duration::from_secs(1));
});
}
fn colored_ink_well() -> InkWell {
InkWell::new()
.hover_color(Color::new(0xff00ff00))
.splash_color(Color::new(0xffff0000))
.focus_color(Color::new(0xff0000ff))
.highlight_color(Color::new(0xf00fffff))
.on_tap(Listener::new(|_app| {}))
.on_long_press(Listener::new(|_app| {}))
.on_long_press_up(Listener::new(|_app| {}))
.on_hover(Rc::new(|_app, _hover| {}))
}
fn overlay_ink_well() -> InkWell {
InkWell::new()
.overlay_color(<dyn WidgetStateProperty<Option<Color>>>::resolve_with(
|states| {
if states.contains(&WidgetState::Hovered) {
return Some(Color::new(0xff00ff00));
}
if states.contains(&WidgetState::Focused) {
return Some(Color::new(0xff0000ff));
}
if states.contains(&WidgetState::Pressed) {
return Some(Color::new(0xf00fffff));
}
Some(Color::new(0xffbadbad)) },
))
.on_tap(Listener::new(|_app| {}))
.on_long_press(Listener::new(|_app| {}))
.on_long_press_up(Listener::new(|_app| {}))
.on_hover(Rc::new(|_app, _hover| {}))
}
#[test]
fn ink_well_changes_color_on_hover() {
test_widgets(|tester| {
tester.pump_widget(material_ltr(centered_square(colored_ink_well())));
hover(tester, center_of_view());
assert!(paints_rect(tester, 0xff00ff00, CENTER_RECT));
});
}
#[test]
fn ink_well_changes_color_on_hover_with_overlay_color() {
test_widgets(|tester| {
tester.pump_widget(material_ltr(centered_square(overlay_ink_well())));
hover(tester, center_of_view());
assert!(paints_rect(tester, 0xff00ff00, CENTER_RECT));
});
}
#[test]
fn ink_response_changes_color_on_focus() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
let focus_node = focus_node(tester);
tester.pump_widget(material_ltr(centered_square(
colored_ink_well().focus_node(focus_node.clone()),
)));
tester.pump_and_settle();
assert!(count_calls(tester.ink_list().ops(), CanvasMethod::DrawRect) == 0);
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
assert!(paints_rect(tester, 0xff0000ff, CENTER_RECT));
});
}
#[test]
fn ink_response_changes_color_on_focus_with_overlay_color() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
let focus_node = focus_node(tester);
tester.pump_widget(material_ltr(centered_square(
overlay_ink_well().focus_node(focus_node.clone()),
)));
tester.pump_and_settle();
assert!(count_calls(tester.ink_list().ops(), CanvasMethod::DrawRect) == 0);
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
assert!(paints_rect(tester, 0xff0000ff, CENTER_RECT));
});
}
#[test]
fn ink_well_changes_color_on_pressed_with_overlay_color() {
test_widgets(|tester| {
let pressed_color = Color::new(0xffdd00ff);
tester.pump_widget(material_ltr(top_left_square(
InkWell::new()
.splash_factory(NoSplash::splash_factory())
.overlay_color(<dyn WidgetStateProperty<Option<Color>>>::resolve_with(
move |states| {
if states.contains(&WidgetState::Pressed) {
return Some(pressed_color);
}
Some(Color::new(0xffbadbad)) },
))
.on_tap(Listener::new(|_app| {})),
)));
tester.pump_and_settle();
let mut gesture = TestPointer::touch(1);
tester.down(&mut gesture, TOP_LEFT_RECT.center());
assert!(
highlights(tester).contains(&(pressed_color.with_alpha(0).value(), TOP_LEFT_RECT))
);
tester.pump_and_settle(); assert!(paints_rect(tester, pressed_color.value(), TOP_LEFT_RECT));
tester.up(&gesture);
});
}
const SELECTED_HOVERED_COLOR: u32 = 0xff00ff00;
const SELECTED_FOCUSED_COLOR: u32 = 0xff0000ff;
const SELECTED_PRESSED_COLOR: u32 = 0xff00ffff;
fn selected_boilerplate(tester: &WidgetTester, focus_node: Option<FocusNodeRef>) -> WidgetRef {
let states = WidgetStates::from([WidgetState::Selected]);
let states_controller =
WidgetStatesController::new(&mut tester.cell.borrow_mut(), Some(states));
let mut ink_well = InkWell::new()
.splash_factory(NoSplash::splash_factory())
.states_controller(states_controller)
.overlay_color(<dyn WidgetStateProperty<Option<Color>>>::resolve_with(
|states| {
if states.contains(&WidgetState::Selected) {
if states.contains(&WidgetState::Pressed) {
return Some(Color::new(SELECTED_PRESSED_COLOR));
}
if states.contains(&WidgetState::Hovered) {
return Some(Color::new(SELECTED_HOVERED_COLOR));
}
if states.contains(&WidgetState::Focused) {
return Some(Color::new(SELECTED_FOCUSED_COLOR));
}
Some(Color::new(0xffbadbad)) } else {
Some(Color::new(0xff000000))
}
},
))
.on_tap(Listener::new(|_app| {}));
if let Some(focus_node) = focus_node {
ink_well = ink_well.focus_node(focus_node);
}
material_ltr(top_left_square(ink_well))
}
#[test]
fn ink_well_overlay_color_resolution_respects_selected_when_focused() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
let focus_node = focus_node(tester);
let widget = selected_boilerplate(tester, Some(focus_node.clone()));
tester.pump_widget(widget);
tester.pump_and_settle();
assert!(count_calls(tester.ink_list().ops(), CanvasMethod::DrawRect) == 0);
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
assert!(paints_rect(tester, SELECTED_FOCUSED_COLOR, TOP_LEFT_RECT));
});
}
#[test]
fn ink_well_overlay_color_resolution_respects_selected_when_hovered() {
test_widgets(|tester| {
let widget = selected_boilerplate(tester, None);
tester.pump_widget(widget);
tester.pump_and_settle();
hover(tester, TOP_LEFT_RECT.center());
assert!(paints_rect(tester, SELECTED_HOVERED_COLOR, TOP_LEFT_RECT));
});
}
#[test]
fn ink_well_overlay_color_resolution_respects_selected_when_pressed() {
test_widgets(|tester| {
let widget = selected_boilerplate(tester, None);
tester.pump_widget(widget);
tester.pump_and_settle();
let mut gesture = TestPointer::touch(1);
tester.down(&mut gesture, TOP_LEFT_RECT.center());
assert!(highlights(tester).contains(&(
Color::new(SELECTED_PRESSED_COLOR).with_alpha(0).value(),
TOP_LEFT_RECT
)));
tester.pump_and_settle(); assert!(paints_rect(tester, SELECTED_PRESSED_COLOR, TOP_LEFT_RECT));
tester.up(&gesture);
});
}
fn m2(tester: &WidgetTester, child: WidgetRef) -> WidgetRef {
let data = ThemeData::builder()
.use_material3(false)
.build(&tester.cell.borrow());
Theme::new(data, child).into_widget()
}
fn assert_splash_color(tester: &mut WidgetTester, ink_well: InkWell) {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTouch);
let widget = m2(tester, material_ltr(centered_square(ink_well)));
tester.pump_widget(widget);
tester.pump_and_settle();
let mut gesture = TestPointer::touch(1);
tester.down(&mut gesture, center_of_view());
tester.pump_by(Duration::from_millis(200)); tester.pump_by(Duration::from_millis(16));
let circles = paths(&tester.ink_calls());
assert!(
circles.iter().any(|(color, bounds)| *color == 0xffff0000
&& bounds.center() == Offset::new(50.0, 50.0)),
"{circles:?}"
);
tester.up(&gesture);
}
#[test]
fn ink_response_splash_color_matches_splash_color_parameter() {
test_widgets(|tester| assert_splash_color(tester, colored_ink_well()));
}
#[test]
fn ink_response_splash_color_matches_resolved_overlay_color_for_pressed() {
test_widgets(|tester| {
assert_splash_color(
tester,
InkWell::new()
.overlay_color(<dyn WidgetStateProperty<Option<Color>>>::resolve_with(
|states| {
if states.contains(&WidgetState::Hovered) {
return Some(Color::new(0xff00ff00));
}
if states.contains(&WidgetState::Focused) {
return Some(Color::new(0xff0000ff));
}
if states.contains(&WidgetState::Pressed) {
return Some(Color::new(0xffff0000));
}
Some(Color::new(0xffbadbad)) },
))
.on_tap(Listener::new(|_app| {}))
.on_long_press(Listener::new(|_app| {}))
.on_long_press_up(Listener::new(|_app| {}))
.on_hover(Rc::new(|_app, _hover| {})),
)
});
}
#[test]
fn ink_response_uses_radius_for_focus_highlight() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
let focus_node = focus_node(tester);
tester.pump_widget(material_ltr(
Center::new()
.child(
SizedBox::square(Some(100.0)).child(
InkResponse::new()
.focus_node(focus_node.clone())
.radius(20.0)
.focus_color(Color::new(0xff0000ff))
.on_tap(Listener::new(|_app| {})),
),
)
.into_widget(),
));
tester.pump_and_settle();
assert!(paths(&tester.ink_calls()).is_empty());
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
let circles = paths(&tester.ink_calls());
assert_eq!(circles.len(), 1);
let (color, bounds) = circles[0];
assert_eq!(color, 0xff0000ff);
assert_eq!(bounds.width(), 40.0, "radius 20");
});
}
#[test]
fn ink_well_uses_border_radius_for_focus_highlight() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
let focus_node = focus_node(tester);
tester.pump_widget(material_ltr(centered_square(
InkWell::new()
.focus_node(focus_node.clone())
.border_radius(BorderRadius::all(waft_embedder::Radius::circular(10.0)))
.focus_color(Color::new(0xff0000ff))
.on_tap(Listener::new(|_app| {})),
)));
tester.pump_and_settle();
assert!(paths(&tester.ink_calls()).is_empty());
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
assert_eq!(paths(&tester.ink_calls()), [(0xff0000ff, CENTER_RECT)]);
});
}
#[test]
fn ink_well_uses_border_radius_for_hover_highlight() {
test_widgets(|tester| {
tester.pump_widget(material_ltr(centered_square(
InkWell::new()
.border_radius(BorderRadius::all(waft_embedder::Radius::circular(10.0)))
.hover_color(Color::new(0xff00ff00))
.on_tap(Listener::new(|_app| {})),
)));
tester.pump_and_settle();
assert!(paths(&tester.ink_calls()).is_empty());
let mut mouse = TestPointer::mouse(1, 0);
tester.add_pointer(&mut mouse, center_of_view());
tester.pump_and_settle();
assert_eq!(paths(&tester.ink_calls()), [(0xff00ff00, CENTER_RECT)]);
});
}
#[test]
fn ink_response_does_not_change_color_on_focus_when_on_touch_device() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTouch);
let focus_node = focus_node(tester);
tester.pump_widget(material_ltr(centered_square(
colored_ink_well().focus_node(focus_node.clone()),
)));
tester.pump_and_settle();
assert!(count_calls(tester.ink_list().ops(), CanvasMethod::DrawRect) == 0);
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
assert!(count_calls(tester.ink_list().ops(), CanvasMethod::DrawRect) == 0);
});
}
fn active_cursor(tester: &WidgetTester) -> MouseCursorRef {
let mut app = tester.cell.borrow_mut();
let tracker = waft_rendering::RendererBinding::instance(&mut app).mouse_tracker(&app);
tracker
.debug_device_active_cursor(&app, 1)
.expect("the mouse is over the view")
}
#[test]
fn ink_well_mouse_cursor_changes_cursor_on_hover() {
test_widgets(|tester| {
use waft_services::SystemMouseCursors;
let in_region = |child: WidgetRef| {
material_ltr(
MouseRegion::new()
.cursor(SystemMouseCursors::FORBIDDEN.into())
.child(child)
.into_widget(),
)
};
let expect = |tester: &mut WidgetTester, child: WidgetRef, cursor: MouseCursorRef| {
tester.pump_widget(in_region(child));
assert!(*active_cursor(tester) == *cursor);
};
let basic: MouseCursorRef = SystemMouseCursors::BASIC.into();
tester.pump_widget(in_region(SizedBox::shrink().into_widget()));
let mut mouse = TestPointer::mouse(1, 0);
tester.add_pointer(&mut mouse, Offset::new(1.0, 1.0));
expect(
tester,
InkWell::new()
.mouse_cursor(SystemMouseCursors::CELL.into())
.on_tap(Listener::new(|_app| {}))
.into_widget(),
SystemMouseCursors::CELL.into(),
);
expect(
tester,
InkWell::new()
.on_tap(Listener::new(|_app| {}))
.into_widget(),
basic.clone(),
);
expect(tester, InkWell::new().into_widget(), basic.clone());
expect(
tester,
InkResponse::new()
.on_tap(Listener::new(|_app| {}))
.into_widget(),
basic.clone(),
);
expect(tester, InkResponse::new().into_widget(), basic);
});
}
#[derive(Default)]
struct FeedbackTester {
haptics: RefCell<Vec<waft_embedder::HapticFeedbackType>>,
}
impl waft_embedder::Haptics for FeedbackTester {
fn feedback(&self, kind: waft_embedder::HapticFeedbackType) {
self.haptics.borrow_mut().push(kind);
}
}
fn feedback_case(enable_feedback: bool) -> usize {
let feedback = Rc::new(FeedbackTester::default());
let platform = waft_test::TestPlatform::new()
.on(TargetPlatform::Android)
.with_haptics(Rc::clone(&feedback) as Rc<dyn waft_embedder::Haptics>);
let mut tester = WidgetTester::with_platform(platform);
let tester = &mut tester;
tester.pump_widget(material_ltr(
Center::new()
.child(
InkWell::new()
.on_tap(Listener::new(|_app| {}))
.on_long_press(Listener::new(|_app| {}))
.on_long_press_up(Listener::new(|_app| {}))
.enable_feedback(enable_feedback),
)
.into_widget(),
));
tester.tap_at(center_of_view(), 1);
tester.pump_by(Duration::from_secs(1));
assert!(feedback.haptics.borrow().is_empty());
tester.long_press_at(center_of_view(), 1);
tester.pump_by(Duration::from_secs(1));
feedback.haptics.borrow().len()
}
#[test]
fn feedback_enabled_by_default() {
test_widgets(|_| assert_eq!(feedback_case(true), 1));
}
#[test]
fn feedback_disabled() {
test_widgets(|_| assert_eq!(feedback_case(false), 0));
}
fn focus_case(
tester: &mut WidgetTester,
navigation_mode: Option<waft_widgets::NavigationMode>,
) -> bool {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTouch);
let focus_node = focus_node(tester);
let frame = |ink_well: InkWell| {
let child = ltr(ink_well
.focus_node(focus_node.clone())
.child(SizedBox::new()));
let child = match navigation_mode {
Some(mode) => waft_widgets::MediaQuery::new(
MediaQueryData::default().navigation_mode(mode),
child,
)
.into_widget(),
None => child,
};
Material::new().child(child).into_widget()
};
tester.pump_widget(frame(
InkWell::new()
.autofocus(true)
.on_tap(Listener::new(|_app| {}))
.on_long_press(Listener::new(|_app| {}))
.on_long_press_up(Listener::new(|_app| {}))
.on_hover(Rc::new(|_app, _hover| {})),
));
tester.pump_and_settle();
assert!(focus_node.has_primary_focus(&tester.cell.borrow()));
tester.pump_widget(frame(InkWell::new()));
tester.pump_and_settle();
focus_node.has_primary_focus(&tester.cell.borrow())
}
#[test]
fn ink_response_does_not_focus_when_disabled() {
test_widgets(|tester| assert!(!focus_case(tester, None)));
}
#[test]
fn ink_response_accepts_focus_when_disabled_in_directional_navigation_mode() {
test_widgets(|tester| {
assert!(focus_case(
tester,
Some(waft_widgets::NavigationMode::Directional)
))
});
}
#[test]
fn when_ink_wells_are_nested_only_the_inner_one_is_triggered_by_tap_splash() {
test_widgets(|tester| {
let inner_key = GlobalKey::new();
let padded_ink_well = |key: Option<&GlobalKey>, child: WidgetRef| {
let mut ink_well = InkWell::new()
.on_tap(Listener::new(|_app| {}))
.child(Padding::new(waft_painting::EdgeInsets::all(50.0).into()).child(child));
if let Some(key) = key {
ink_well = ink_well.key(key.clone().into());
}
ink_well.into_widget()
};
let tree = padded_ink_well(
None,
padded_ink_well(
None,
padded_ink_well(
Some(&inner_key),
SizedBox::new().width(50.0).height(50.0).into_widget(),
),
),
);
let widget = m2(
tester,
material_ltr(Center::new().child(tree).into_widget()),
);
tester.pump_widget(widget);
let inner = tester.get_center(&inner_key);
let circles = |tester: &WidgetTester| paths(&tester.ink_calls()).len();
let mut gesture = TestPointer::touch(1);
tester.down(&mut gesture, inner);
tester.pump_by(Duration::from_millis(200));
tester.pump_by(Duration::from_millis(16)); assert_eq!(circles(tester), 1);
tester.up(&gesture);
tester.pump_and_settle();
assert!(tester.ink_calls().is_empty());
tester.down(&mut gesture, inner);
tester.pump_by(Duration::from_millis(200));
tester.pump_by(Duration::from_millis(16)); assert_eq!(circles(tester), 1);
tester.cancel(&gesture);
tester.pump_and_settle();
assert!(tester.ink_calls().is_empty());
tester.down(&mut gesture, inner);
tester.pump_by(Duration::from_millis(200));
tester.pump_by(Duration::from_millis(16)); assert_eq!(circles(tester), 1);
let mut gesture2 = TestPointer::touch(2);
tester.down(&mut gesture2, inner);
tester.pump_by(Duration::from_millis(200));
tester.pump_by(Duration::from_millis(16)); assert_eq!(circles(tester), 1);
tester.up(&gesture2);
});
}
#[test]
fn disabled_and_hovered_inkwell_responds_to_mouse_exit() {
test_widgets(|tester| {
let on_hover_count = Rc::new(Cell::new(0));
let hover_value = Rc::new(Cell::new(None));
let build_frame = |enabled: bool| {
let mut ink_well = InkWell::new().on_hover({
let on_hover_count = Rc::clone(&on_hover_count);
let hover_value = Rc::clone(&hover_value);
Rc::new(move |_app, value| {
on_hover_count.set(on_hover_count.get() + 1);
hover_value.set(Some(value));
})
});
if enabled {
ink_well = ink_well.on_tap(Listener::new(|_app| {}));
}
material_ltr(centered_square(ink_well))
};
tester.pump_widget(build_frame(true));
let mut mouse = TestPointer::mouse(1, 0);
tester.add_pointer(&mut mouse, Offset::ZERO);
tester.move_to(&mut mouse, center_of_view(), false);
tester.pump_and_settle();
assert_eq!(on_hover_count.get(), 1);
assert_eq!(hover_value.get(), Some(true));
tester.pump_widget(build_frame(false));
tester.pump_and_settle();
tester.move_to(&mut mouse, Offset::ZERO, false);
assert_eq!(on_hover_count.get(), 2);
assert_eq!(hover_value.get(), Some(false));
tester.move_to(&mut mouse, center_of_view(), false);
tester.pump_and_settle();
assert_eq!(on_hover_count.get(), 2);
assert_eq!(hover_value.get(), Some(false));
tester.pump_widget(build_frame(true));
tester.pump_and_settle();
assert_eq!(on_hover_count.get(), 2);
assert_eq!(hover_value.get(), Some(false));
tester.move_to(&mut mouse, center_of_view() - Offset::new(1.0, 1.0), false);
tester.pump_and_settle();
assert_eq!(on_hover_count.get(), 2);
assert_eq!(hover_value.get(), Some(false));
});
}
#[test]
fn hovered_ink_well_draws_a_transparent_highlight_when_disabled() {
test_widgets(|tester| {
let build_frame = |enabled: bool| {
let mut ink_well = InkWell::new()
.on_hover(Rc::new(|_app, _value| {}))
.hover_color(Color::new(0xff00ff00));
if enabled {
ink_well = ink_well.on_tap(Listener::new(|_app| {}));
}
material_ltr(centered_square(ink_well))
};
tester.pump_widget(build_frame(true));
hover(tester, center_of_view());
assert!(paints_rect(tester, 0xff00ff00, CENTER_RECT));
tester.pump_widget(build_frame(false));
tester.pump_and_settle();
assert!(highlights(tester).contains(&(0x0000ff00, CENTER_RECT)));
});
}
#[test]
fn ink_well_disposes_states_controller() {
test_widgets(|tester| {
let tap_count = Rc::new(Cell::new(0));
let build_frame = |states_controller: Option<Ref<MaterialStatesController>>| {
let mut ink_well = InkWell::new()
.on_tap({
let tap_count = Rc::clone(&tap_count);
Listener::new(move |_app| tap_count.set(tap_count.get() + 1))
})
.child(SizedBox::new().width(100.0).height(20.0));
if let Some(states_controller) = states_controller {
ink_well = ink_well.states_controller(states_controller);
}
material_ltr(Center::new().child(ink_well).into_widget())
};
let controller = MaterialStatesController::new(&mut tester.cell.borrow_mut(), None);
let pressed_count = Rc::new(Cell::new(0));
controller.add_listener(
&mut tester.cell.borrow_mut(),
Listener::new({
let pressed_count = Rc::clone(&pressed_count);
{
let controller = controller.clone();
move |app| {
if app.get(&controller).value().contains(&WidgetState::Pressed) {
pressed_count.set(pressed_count.get() + 1);
}
}
}
}),
);
tester.pump_widget(build_frame(Some(controller.clone())));
tester.tap_at(center_of_view(), 1);
tester.pump_and_settle();
assert_eq!(tap_count.get(), 1);
assert_eq!(pressed_count.get(), 1);
tester.pump_widget(build_frame(None));
tester.tap_at(center_of_view(), 1);
tester.pump_and_settle();
assert_eq!(tap_count.get(), 2);
assert_eq!(pressed_count.get(), 1);
tester.pump_widget(build_frame(Some(controller.clone())));
tester.tap_at(center_of_view(), 1);
tester.pump_and_settle();
assert_eq!(tap_count.get(), 3);
assert_eq!(pressed_count.get(), 2);
});
}
#[test]
fn ink_well_overlay_color_opacity_fades_from_0xff_when_hover_ends() {
test_widgets(|tester| {
tester.pump_widget(material_ltr(centered_square(
InkWell::new()
.overlay_color(<dyn WidgetStateProperty<Option<Color>>>::resolve_with(
|states| {
states
.contains(&WidgetState::Hovered)
.then(|| Color::new(0xff00ff00))
},
))
.on_tap(Listener::new(|_app| {}))
.on_long_press(Listener::new(|_app| {}))
.on_long_press_up(Listener::new(|_app| {}))
.on_hover(Rc::new(|_app, _hover| {})),
)));
let mut mouse = hover(tester, center_of_view());
tester.move_to(&mut mouse, Offset::new(10.0, 10.0), false); tester.pump(); assert!(paints_rect(tester, 0xff00ff00, CENTER_RECT));
tester.pump_by(Duration::from_millis(25));
assert!(
paints_rect(tester, 0x8000ff00, CENTER_RECT),
"{:?}",
tester.ink_calls()
);
});
}
#[test]
fn ink_well_secondary_tap_test() {
test_widgets(|tester| {
let log: Log = Rc::default();
tester.pump_widget(ltr(Material::new().child(
Center::new().child(
InkWell::new()
.on_secondary_tap(logger(&log, "secondary-tap"))
.on_secondary_tap_down(logged(&log, "secondary-tap-down"))
.on_secondary_tap_up(logged(&log, "secondary-tap-up"))
.on_secondary_tap_cancel(logger(&log, "secondary-tap-cancel")),
),
)));
let mut mouse = TestPointer::mouse(1, K_SECONDARY_BUTTON);
tester.down(&mut mouse, center_of_view());
tester.up(&mouse);
assert_eq!(
take(&log),
["secondary-tap-down", "secondary-tap-up", "secondary-tap"]
);
let mut mouse = TestPointer::mouse(2, K_SECONDARY_BUTTON);
tester.down(&mut mouse, center_of_view());
tester.move_to(&mut mouse, Offset::new(100.0, 100.0), true);
tester.up(&mouse);
assert_eq!(take(&log), ["secondary-tap-down", "secondary-tap-cancel"]);
});
}
#[test]
fn ink_well_secondary_tap_should_not_draw_a_splash_when_no_secondary_callbacks_are_defined() {
test_widgets(|tester| {
tester.pump_widget(ltr(Material::new().child(
Center::new().child(InkWell::new().on_tap(Listener::new(|_app| {}))),
)));
let mut mouse = TestPointer::mouse(1, K_SECONDARY_BUTTON);
tester.down(&mut mouse, center_of_view());
tester.pump_by(Duration::from_millis(200));
assert!(paths(&tester.ink_calls()).is_empty());
tester.up(&mouse);
});
}
#[test]
fn ink_well_tertiary_tap_test() {
test_widgets(|tester| {
let log: Log = Rc::default();
tester.pump_widget(ltr(Material::new().child(
Center::new().child(
InkWell::new()
.on_tertiary_tap_down(logged(&log, "tertiary-tap-down"))
.on_tertiary_tap_up(logged(&log, "tertiary-tap-up"))
.on_tertiary_tap_cancel(logger(&log, "tertiary-tap-cancel")),
),
)));
let mut mouse = TestPointer::mouse(1, K_TERTIARY_BUTTON);
tester.down(&mut mouse, center_of_view());
tester.up(&mouse);
assert_eq!(take(&log), ["tertiary-tap-down", "tertiary-tap-up"]);
let mut mouse = TestPointer::mouse(2, K_TERTIARY_BUTTON);
tester.down(&mut mouse, center_of_view());
tester.move_to(&mut mouse, Offset::new(100.0, 100.0), true);
tester.up(&mouse);
assert_eq!(take(&log), ["tertiary-tap-down", "tertiary-tap-cancel"]);
});
}
#[test]
fn ink_well_tertiary_tap_highlights_only_when_a_tertiary_callback_is_defined() {
test_widgets(|tester| {
let log: Rc<RefCell<Vec<bool>>> = Rc::default();
let build_frame = |with_tertiary_tap_down: bool, with_tertiary_tap_up: bool| {
let mut ink_well = InkWell::new()
.on_tap(Listener::new(|_app| {}))
.on_highlight_changed({
let log = Rc::clone(&log);
Rc::new(move |_app, value| log.borrow_mut().push(value))
});
if with_tertiary_tap_down {
ink_well = ink_well.on_tertiary_tap_down(Rc::new(|_app, _details| {}));
}
if with_tertiary_tap_up {
ink_well = ink_well.on_tertiary_tap_up(Rc::new(|_app, _details| {}));
}
ltr(Material::new().child(
Center::new().child(SizedBox::new().width(100.0).height(100.0).child(ink_well)),
))
};
for (with_tertiary_tap_down, with_tertiary_tap_up) in
[(false, true), (true, false), (true, true)]
{
tester.pump_widget(build_frame(with_tertiary_tap_down, with_tertiary_tap_up));
let mut mouse = TestPointer::mouse(1, K_TERTIARY_BUTTON);
tester.down(&mut mouse, center_of_view());
tester.pump_by(Duration::from_millis(200));
assert_eq!(std::mem::take(&mut *log.borrow_mut()), [true]);
tester.up(&mouse);
tester.pump_by(Duration::from_millis(200));
assert_eq!(std::mem::take(&mut *log.borrow_mut()), [false]);
}
tester.pump_widget(build_frame(false, false));
let mut mouse = TestPointer::mouse(1, K_TERTIARY_BUTTON);
tester.down(&mut mouse, center_of_view());
tester.pump_by(Duration::from_millis(200));
assert!(log.borrow().is_empty());
tester.up(&mouse);
});
}
#[test]
fn try_out_hover_duration_property() {
test_widgets(|tester| {
let log: Log = Rc::default();
tester.pump_widget(ltr(Material::new().child(
Center::new().child(
InkWell::new()
.hover_duration(Duration::from_millis(1000))
.on_tap(logger(&log, "tap")),
),
)));
tester.tap_at(center_of_view(), 1);
tester.pump_by(Duration::from_secs(1));
assert_eq!(take(&log), ["tap"]);
});
}
#[test]
fn ink_well_activation_action_does_not_end_immediately() {
test_widgets(|tester| {
let controller = MaterialStatesController::new(&mut tester.cell.borrow_mut(), None);
let inner: Rc<Cell<Option<BuildContext>>> = Rc::default();
tester.pump_widget(ltr(Material::new().child(
Center::new().child(
InkWell::new()
.autofocus(true)
.on_tap(Listener::new(|_app| {}))
.states_controller(controller.clone())
.child(Builder::new({
let inner = Rc::clone(&inner);
move |_app, context| {
inner.set(Some(context));
SizedBox::new().width(10.0).height(10.0).into_widget()
}
})),
),
)));
let context = inner.get().expect("built");
Actions::invoke(
&mut tester.cell.borrow_mut(),
context,
&ButtonActivateIntent,
);
tester.pump_by(Duration::from_millis(99));
assert!(
tester
.cell
.borrow()
.get(&controller)
.value()
.contains(&WidgetState::Pressed)
);
tester.pump_and_settle();
assert!(
!tester
.cell
.borrow()
.get(&controller)
.value()
.contains(&WidgetState::Pressed)
);
});
}
fn zero_area_size(tester: &mut WidgetTester, response: WidgetRef) -> waft_embedder::Size {
let key = GlobalKey::new();
tester.pump_widget(material_ltr(
Center::new()
.child(SizedBox::shrink().key(key.clone().into()).child(response))
.into_widget(),
));
tester
.render_object(&key)
.as_box()
.expect("a box")
.size(&tester.cell.borrow())
}
#[test]
fn ink_response_does_not_crash_in_zero_area() {
test_widgets(|tester| {
assert_eq!(
zero_area_size(tester, InkResponse::new().into_widget()),
waft_embedder::Size::ZERO
)
});
}
#[test]
fn ink_well_does_not_crash_at_zero_area() {
test_widgets(|tester| {
assert_eq!(
zero_area_size(tester, InkWell::new().into_widget()),
waft_embedder::Size::ZERO
)
});
}
fn ink_ops(tester: &WidgetTester) -> Vec<valo::Op> {
tester.ink_list().ops().to_vec()
}
fn op_argb(color: valo::Color) -> u32 {
let channel = |value: f32| (value * 255.0).round() as u32;
(channel(color.a) << 24)
| (channel(color.r) << 16)
| (channel(color.g) << 8)
| channel(color.b)
}
fn to_rect(rect: valo::Rect) -> Rect {
Rect::from_ltwh(
f64::from(rect.x),
f64::from(rect.y),
f64::from(rect.width),
f64::from(rect.height),
)
}
fn circles(ops: &[valo::Op]) -> Vec<(u32, f64)> {
ops.iter()
.filter_map(|op| match op {
valo::Op::DrawPath { path, paint, .. } => path
.as_oval()
.map(|oval| (op_argb(paint.color), f64::from(oval.width) / 2.0)),
_ => None,
})
.collect()
}
fn rrects(ops: &[valo::Op]) -> Vec<(u32, Rect, [[f32; 2]; 4])> {
ops.iter()
.filter_map(|op| match op {
valo::Op::DrawPath { path, paint, .. } => path
.as_round_rect()
.map(|(rect, radii)| (op_argb(paint.color), to_rect(rect), radii)),
_ => None,
})
.collect()
}
fn clip_paths(ops: &[valo::Op]) -> Vec<std::sync::Arc<valo::Path>> {
ops.iter()
.filter_map(|op| match op {
valo::Op::ClipPath {
path,
is_axis_aligned_rect: false,
..
} => Some(std::sync::Arc::clone(path)),
_ => None,
})
.collect()
}
fn rrect_path(rect: Rect, radius: f64) -> std::sync::Arc<valo::Path> {
let mut builder = valo::PathBuilder::new();
builder.rrect_radii(
valo::Rect::new(
rect.left as f32,
rect.top as f32,
rect.width() as f32,
rect.height() as f32,
),
[radius as f32; 4],
);
builder.build()
}
fn covers_same_area_as(
actual: &valo::Path,
expected: &valo::Path,
area: Rect,
sample_size: usize,
) -> bool {
(0..sample_size).all(|i| {
(0..sample_size).all(|j| {
let x = area.left + area.width() * i as f64 / sample_size as f64;
let y = area.top + area.height() * j as f64 / sample_size as f64;
let point = valo::Point::new(x as f32, y as f32);
actual.contains(point, valo::FillRule::NonZero)
== expected.contains(point, valo::FillRule::NonZero)
})
})
}
fn clips_covering_rrect(
clips: &[std::sync::Arc<valo::Path>],
rect: Rect,
radius: f64,
) -> usize {
let expected = rrect_path(rect, radius);
clips
.iter()
.filter(|clip| covers_same_area_as(clip, &expected, rect.inflate(20.0), 100))
.count()
}
fn mouse_region<K>(child: impl IntoWidget<K>) -> WidgetRef {
MouseRegion::new().child(child).into_widget()
}
fn top_left_mouse_square<K>(child: impl IntoWidget<K>) -> WidgetRef {
Align::new()
.alignment(waft_painting::AlignmentGeometry::TOP_LEFT)
.child(SizedBox::square(Some(100.0)).child(mouse_region(child)))
.into_widget()
}
fn circle_border() -> waft_painting::CircleBorder {
waft_painting::CircleBorder::default()
}
fn rounded(radius: f64) -> waft_painting::RoundedRectangleBorder {
waft_painting::RoundedRectangleBorder::new(
waft_painting::BorderSide::NONE,
BorderRadius::all(waft_embedder::Radius::circular(radius)).into(),
)
}
#[test]
fn ink_well_custom_border_clips_for_focus_highlight() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
let focus_node = focus_node(tester);
tester.pump_widget(material_ltr(top_left_mouse_square(
InkWell::new()
.focus_node(focus_node.clone())
.border_radius(BorderRadius::all(waft_embedder::Radius::circular(10.0)))
.custom_border(circle_border())
.hover_color(Color::new(0xff00ff00))
.on_tap(Listener::new(|_app| {})),
)));
tester.pump_and_settle();
let ops = ink_ops(tester);
assert_eq!(clip_paths(&ops).len(), 0);
assert_eq!(rrects(&ops).len(), 0);
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
let ops = ink_ops(tester);
assert_eq!(clip_paths(&ops).len(), 1);
assert_eq!(rrects(&ops).len(), 1);
assert_eq!(
clips_covering_rrect(&clip_paths(&ops), TOP_LEFT_RECT, 50.0),
1
);
assert_eq!(
clips_covering_rrect(&clip_paths(&ops), TOP_LEFT_RECT, 20.0),
0
);
});
}
#[test]
fn ink_well_custom_border_clips_for_hover_highlight() {
test_widgets(|tester| {
tester.pump_widget(material_ltr(top_left_mouse_square(
InkWell::new()
.border_radius(BorderRadius::all(waft_embedder::Radius::circular(10.0)))
.custom_border(circle_border())
.hover_color(Color::new(0xff00ff00))
.on_tap(Listener::new(|_app| {})),
)));
tester.pump_and_settle();
let ops = ink_ops(tester);
assert_eq!(clip_paths(&ops).len(), 0);
assert_eq!(rrects(&ops).len(), 0);
let mut mouse = TestPointer::mouse(1, 0);
tester.add_pointer(&mut mouse, TOP_LEFT_RECT.center());
tester.pump_and_settle();
let ops = ink_ops(tester);
assert_eq!(clip_paths(&ops).len(), 1);
assert_eq!(rrects(&ops).len(), 1);
assert_eq!(
clips_covering_rrect(&clip_paths(&ops), TOP_LEFT_RECT, 50.0),
1
);
});
}
#[test]
fn ink_response_radius_can_be_updated() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
let focus_node = focus_node(tester);
let boilerplate = |radius: f64| {
material_ltr(centered_square(
InkResponse::new()
.focus_node(focus_node.clone())
.radius(radius)
.focus_color(Color::new(0xff0000ff))
.on_tap(Listener::new(|_app| {})),
))
};
tester.pump_widget(boilerplate(10.0));
tester.pump_and_settle();
assert_eq!(circles(&ink_ops(tester)).len(), 0);
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
assert_eq!(circles(&ink_ops(tester)), [(0xff0000ff, 10.0)]);
tester.pump_widget(boilerplate(20.0));
tester.pump_and_settle();
assert_eq!(circles(&ink_ops(tester)), [(0xff0000ff, 20.0)]);
});
}
#[test]
fn ink_response_highlight_shape_can_be_updated() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
let focus_node = focus_node(tester);
let boilerplate = |shape: BoxShape| {
material_ltr(centered_square(
InkResponse::new()
.focus_node(focus_node.clone())
.highlight_shape(shape)
.border_radius(BorderRadius::all(waft_embedder::Radius::circular(10.0)))
.focus_color(Color::new(0xff0000ff))
.on_tap(Listener::new(|_app| {})),
))
};
tester.pump_widget(boilerplate(BoxShape::Circle));
tester.pump_and_settle();
let ops = ink_ops(tester);
assert_eq!(circles(&ops).len(), 0);
assert_eq!(rrects(&ops).len(), 0);
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
let ops = ink_ops(tester);
assert_eq!(circles(&ops).len(), 1);
assert_eq!(rrects(&ops).len(), 0);
tester.pump_widget(boilerplate(BoxShape::Rectangle));
tester.pump_and_settle();
let ops = ink_ops(tester);
assert_eq!(circles(&ops).len(), 0);
assert_eq!(rrects(&ops).len(), 1);
});
}
#[test]
fn ink_well_border_radius_can_be_updated() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
let focus_node = focus_node(tester);
let boilerplate = |radius: f64| {
material_ltr(centered_square(
InkWell::new()
.focus_node(focus_node.clone())
.border_radius(BorderRadius::all(waft_embedder::Radius::circular(radius)))
.focus_color(Color::new(0xff0000ff))
.on_tap(Listener::new(|_app| {})),
))
};
tester.pump_widget(boilerplate(10.0));
tester.pump_and_settle();
assert_eq!(rrects(&ink_ops(tester)).len(), 0);
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
assert_eq!(
rrects(&ink_ops(tester)),
[(0xff0000ff, CENTER_RECT, [[10.0; 2]; 4])]
);
tester.pump_widget(boilerplate(30.0));
tester.pump_and_settle();
assert_eq!(
rrects(&ink_ops(tester)),
[(0xff0000ff, CENTER_RECT, [[30.0; 2]; 4])]
);
});
}
#[test]
fn ink_well_custom_border_can_be_updated() {
test_widgets(|tester| {
set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
let focus_node = focus_node(tester);
let boilerplate = |radius: f64| {
material_ltr(top_left_mouse_square(
InkWell::new()
.focus_node(focus_node.clone())
.custom_border(rounded(radius))
.hover_color(Color::new(0xff00ff00))
.on_tap(Listener::new(|_app| {})),
))
};
tester.pump_widget(boilerplate(20.0));
tester.pump_and_settle();
assert_eq!(clip_paths(&ink_ops(tester)).len(), 0);
focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
tester.pump_and_settle();
let clips = clip_paths(&ink_ops(tester));
assert_eq!(clips.len(), 1);
assert_eq!(clips_covering_rrect(&clips, TOP_LEFT_RECT, 20.0), 1);
tester.pump_widget(boilerplate(40.0));
tester.pump_and_settle();
let clips = clip_paths(&ink_ops(tester));
assert!(clips_covering_rrect(&clips, TOP_LEFT_RECT, 40.0) >= 1);
});
}
#[test]
fn ink_well_splash_custom_border_can_be_updated() {
test_widgets(|tester| {
let focus_node = focus_node(tester);
let boilerplate = |tester: &WidgetTester, radius: f64| {
m2(
tester,
material_ltr(top_left_mouse_square(
InkWell::new()
.focus_node(focus_node.clone())
.custom_border(rounded(radius))
.on_tap(Listener::new(|_app| {})),
)),
)
};
let widget = boilerplate(tester, 20.0);
tester.pump_widget(widget);
tester.pump_and_settle();
assert_eq!(clip_paths(&ink_ops(tester)).len(), 0);
let mut gesture = TestPointer::touch(1);
tester.down(&mut gesture, TOP_LEFT_RECT.center());
tester.pump_by(Duration::from_millis(200)); let clips = clip_paths(&ink_ops(tester));
assert_eq!(clips.len(), 2);
assert!(clips_covering_rrect(&clips, TOP_LEFT_RECT, 20.0) >= 2);
let widget = boilerplate(tester, 40.0);
tester.pump_widget(widget);
tester.pump_and_settle();
let clips = clip_paths(&ink_ops(tester));
assert!(clips_covering_rrect(&clips, TOP_LEFT_RECT, 40.0) >= 2);
tester.up(&gesture);
});
}
fn padded_ink_well(key: Option<&GlobalKey>, child: Option<WidgetRef>) -> WidgetRef {
let mut padding = Padding::new(waft_painting::EdgeInsets::all(50.0).into());
if let Some(child) = child {
padding = padding.child(child);
}
let mut ink_well = InkWell::new()
.on_tap(Listener::new(|_app| {}))
.child(padding);
if let Some(key) = key {
ink_well = ink_well.key(key.clone().into());
}
ink_well.into_widget()
}
fn pump_splash(tester: &mut WidgetTester, duration: Duration) {
tester.pump_by(duration);
tester.pump_by(Duration::from_millis(16)); }
fn circle_count(tester: &WidgetTester) -> usize {
circles(&ink_ops(tester)).len()
}
fn top_left_of(tester: &WidgetTester, key: &GlobalKey) -> Offset {
let render = tester.render_object(key).as_box().expect("a render box");
let app = tester.cell.borrow();
render.local_to_global(&app, Offset::ZERO, None)
}
fn m2_top_left(tester: &WidgetTester, width: f64, height: f64, child: WidgetRef) -> WidgetRef {
m2(
tester,
material_ltr(
Align::new()
.alignment(waft_painting::AlignmentGeometry::TOP_LEFT)
.child(SizedBox::new().width(width).height(height).child(child))
.into_widget(),
),
)
}
#[test]
fn reparenting_parent_should_allow_both_inkwells_to_show_splash_afterwards() {
test_widgets(|tester| {
let middle_key = GlobalKey::new();
let inner_key = GlobalKey::new();
let widget = m2_top_left(
tester,
200.0,
100.0,
Row::new()
.children([
padded_ink_well(
Some(&middle_key),
Some(padded_ink_well(Some(&inner_key), None)),
),
SizedBox::new().into_widget(),
])
.into_widget(),
);
tester.pump_widget(widget);
let mut gesture1 = TestPointer::touch(1);
let inner = tester.get_center(&inner_key);
tester.down(&mut gesture1, inner);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
let widget = m2_top_left(
tester,
200.0,
100.0,
Row::new()
.children([
padded_ink_well(Some(&inner_key), None),
padded_ink_well(Some(&middle_key), None),
])
.into_widget(),
);
tester.pump_widget(widget);
tester.up(&gesture1);
tester.pump_and_settle();
assert!(ink_ops(tester).is_empty(), "{:?}", ink_ops(tester));
let middle = tester.get_center(&middle_key);
tester.down(&mut gesture1, middle);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
let mut gesture2 = TestPointer::touch(2);
let inner = tester.get_center(&inner_key);
tester.down(&mut gesture2, inner);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 2);
tester.up(&gesture1);
tester.up(&gesture2);
tester.pump_and_settle();
});
}
#[test]
fn parent_inkwell_does_not_block_child_inkwells_from_splashes() {
test_widgets(|tester| {
let middle_key = GlobalKey::new();
let inner_key = GlobalKey::new();
let tree = padded_ink_well(
None,
Some(padded_ink_well(
Some(&middle_key),
Some(padded_ink_well(
Some(&inner_key),
Some(SizedBox::new().width(50.0).height(50.0).into_widget()),
)),
)),
);
let widget = m2(
tester,
material_ltr(Center::new().child(tree).into_widget()),
);
tester.pump_widget(widget);
let mut gesture1 = TestPointer::touch(1);
let middle = top_left_of(tester, &middle_key) + Offset::new(1.0, 1.0);
tester.down(&mut gesture1, middle);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
let mut gesture2 = TestPointer::touch(2);
let inner = tester.get_center(&inner_key);
tester.down(&mut gesture2, inner);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 2);
tester.up(&gesture1);
tester.up(&gesture2);
tester.pump_and_settle();
});
}
#[test]
fn parent_inkwell_can_count_the_number_of_pressed_children_to_prevent_splash() {
test_widgets(|tester| {
let parent_key = GlobalKey::new();
let left_key = GlobalKey::new();
let right_key = GlobalKey::new();
let child_well = |key: &GlobalKey| {
SizedBox::square(Some(50.0))
.child(
InkWell::new()
.key(key.clone().into())
.on_tap(Listener::new(|_app| {})),
)
.into_widget()
};
let parent = InkWell::new()
.key(parent_key.clone().into())
.on_tap(Listener::new(|_app| {}))
.child(Center::new().child(
SizedBox::new().width(100.0).height(50.0).child(
Row::new().children([child_well(&left_key), child_well(&right_key)]),
),
));
let widget = m2(tester, material_ltr(centered_square(parent)));
tester.pump_widget(widget);
let parent_position = top_left_of(tester, &parent_key) + Offset::new(1.0, 1.0);
let mut gesture1 = TestPointer::touch(1);
let left = tester.get_center(&left_key);
tester.down(&mut gesture1, left);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
let mut gesture2 = TestPointer::touch(2);
let right = tester.get_center(&right_key);
tester.down(&mut gesture2, right);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 2);
let mut gesture3 = TestPointer::touch(3);
tester.down(&mut gesture3, parent_position);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 2);
tester.up(&gesture3);
tester.up(&gesture1);
tester.pump_and_settle();
assert_eq!(circle_count(tester), 1);
tester.down(&mut gesture3, parent_position);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
tester.up(&gesture3);
tester.up(&gesture2);
tester.pump_and_settle();
assert_eq!(circle_count(tester), 0);
tester.down(&mut gesture3, parent_position);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
tester.up(&gesture3);
});
}
#[test]
fn when_ink_wells_are_reparented_the_old_parent_can_display_splash_while_the_new_parent_can_not()
{
test_widgets(|tester| {
let inner_key = GlobalKey::new();
let left_key = GlobalKey::new();
let right_key = GlobalKey::new();
let inner = || {
InkWell::new()
.key(inner_key.clone().into())
.on_tap(Listener::new(|_app| {}))
.into_widget()
};
let double_ink_well_row =
|tester: &WidgetTester,
left_width: f64,
right_width: f64,
left_child: Option<WidgetRef>,
right_child: Option<WidgetRef>| {
let side = |key: &GlobalKey, width: f64, child: Option<WidgetRef>| {
let mut inner_box = SizedBox::new().width(left_width).height(50.0);
if let Some(child) = child {
inner_box = inner_box.child(child);
}
SizedBox::new()
.width(width)
.height(100.0)
.child(
InkWell::new()
.key(key.clone().into())
.on_tap(Listener::new(|_app| {}))
.child(Center::new().child(inner_box)),
)
.into_widget()
};
m2_top_left(
tester,
left_width + right_width,
100.0,
Row::new()
.children([
side(&left_key, left_width, left_child),
side(&right_key, right_width, right_child),
])
.into_widget(),
)
};
let widget = double_ink_well_row(tester, 110.0, 90.0, Some(inner()), None);
tester.pump_widget(widget);
let mut gesture = TestPointer::touch(1);
tester.down(&mut gesture, Offset::new(100.0, 50.0));
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
let widget = double_ink_well_row(tester, 90.0, 110.0, None, Some(inner()));
tester.pump_widget(widget);
assert_eq!(circle_count(tester), 0);
let mut gesture2 = TestPointer::touch(2);
tester.down(&mut gesture2, Offset::new(100.0, 50.0));
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
tester.up(&gesture);
tester.up(&gesture2);
tester.pump_and_settle();
tester.down(&mut gesture, Offset::new(100.0, 50.0));
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
tester.down(&mut gesture2, Offset::new(50.0, 50.0));
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 2);
tester.up(&gesture);
tester.up(&gesture2);
});
}
#[test]
fn ink_wells_splash_starts_before_tap_is_confirmed_and_disappears_after_tap_is_canceled() {
test_widgets(|tester| {
let inner_key = GlobalKey::new();
let tree = GestureDetector::new()
.on_horizontal_drag_start(Rc::new(|_app, _details| {}))
.child(centered_square(
InkWell::new().on_tap(Listener::new(|_app| {})).child(
Center::new().child(
SizedBox::square(Some(50.0)).child(
InkWell::new()
.key(inner_key.clone().into())
.on_tap(Listener::new(|_app| {})),
),
),
),
));
let widget = m2(tester, material_ltr(tree.into_widget()));
tester.pump_widget(widget);
let mut gesture = TestPointer::touch(1);
let inner = tester.get_center(&inner_key);
tester.down(&mut gesture, inner);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
let moved = inner + Offset::new(0.0, -100.0);
tester.move_to(&mut gesture, moved, true);
tester.pump_and_settle();
assert!(ink_ops(tester).is_empty(), "{:?}", ink_ops(tester));
tester.up(&gesture);
tester.pump_and_settle();
assert!(ink_ops(tester).is_empty(), "{:?}", ink_ops(tester));
tester.down(&mut gesture, inner);
pump_splash(tester, Duration::from_millis(200));
assert_eq!(circle_count(tester), 1);
});
}
#[test]
fn the_ink_well_widget_on_overlay_portal_does_not_throw() {
test_widgets(|tester| {
let controller = {
let mut app = tester.cell.borrow_mut();
let controller = OverlayPortalController::new(&mut app, None);
controller.show(&mut app);
controller
};
let overlay_entry = OverlayEntry::new(
&mut tester.cell.borrow_mut(),
Rc::new(move |_app: &mut App, _context| {
Center::new()
.child(
SizedBox::square(Some(100.0)).child(
Material::new()
.color(Colors::BLACK)
.child(OverlayPortal::new(
controller.clone(),
|_app, _context| {
Positioned::new(
InkWell::new()
.splash_color(Colors::RED.primary())
.on_tap(Listener::new(|_app| {}))
.child(SizedBox::square(Some(100.0))),
)
.right(0.0)
.bottom(0.0)
.into_widget()
},
)),
),
)
.into_widget()
}),
false,
false,
false,
);
tester.pump_widget(
Center::new()
.child(
RepaintBoundary::new().child(
SizedBox::square(Some(200.0)).child(ltr(
Overlay::new().initial_entries([overlay_entry.clone()])
)),
),
)
.into_widget(),
);
let ink_well = find::by_type::<InkWell>().evaluate(tester)[0];
let center = { tester.get_center(ink_well) };
let mut gesture = TestPointer::touch(1);
tester.down(&mut gesture, center);
tester.pump(); tester.pump_by(Duration::from_secs(2));
tester.up(&gesture);
overlay_entry.remove(&mut tester.cell.borrow_mut());
overlay_entry.dispose(&mut tester.cell.borrow_mut());
});
}
}