use crate::*;
use std::fmt;
use waft_embedder::{Color, FontWeight};
use waft_foundation::{App, Listener};
use waft_painting::{AlignmentGeometry, EdgeInsetsGeometry};
use waft_rendering::BoxConstraints;
use waft_widgets::*;
const BACKPLATE_MARGIN: [f64; 4] = [4.0, 2.0, 4.0, 2.0];
const SELECTION_INDICATOR_WIDTH: f64 = 3.0;
const SELECTION_INDICATOR_HEIGHT: f64 = 16.0;
const SELECTION_INDICATOR_START: f64 = 4.0;
const SELECTION_INDICATOR_VERTICAL_MARGIN: f64 = 20.0;
const SELECTION_INDICATOR_HEIGHT_SHRINKAGE: f64 = 6.0;
const CONTENT_MARGIN: [f64; 4] = [16.0, 0.0, 12.0, 0.0];
pub(super) struct SuggestionItem {
pub(super) content: WidgetRef,
pub(super) selected: bool,
pub(super) clicked: Listener,
}
impl fmt::Debug for SuggestionItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SuggestionItem")
.field("selected", &self.selected)
.finish_non_exhaustive()
}
}
impl StatelessWidget for SuggestionItem {
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
let content = self.content.clone();
let selected = self.selected;
CommonStates::new(self.clicked.clone(), move |app, context, states| {
let r = ThemeResources::of(app, context);
chrome(&r, states.common, selected, content.clone())
})
.is_tab_stop(false)
.into_widget()
}
}
struct ChromeBrushes {
backplate: Color,
foreground: Color,
indicator: Color,
}
impl ChromeBrushes {
fn of(r: &ThemeResources, common: CommonState, selected: bool) -> ChromeBrushes {
let (backplate, indicator) = match (common, selected) {
(CommonState::Disabled, true) => (
r.list_view_item_background_selected_disabled,
r.list_view_item_selection_indicator_disabled_brush,
),
(CommonState::Disabled, false) => (
Color::new(0),
r.list_view_item_selection_indicator_disabled_brush,
),
(CommonState::Pressed, true) => (
r.list_view_item_background_selected_pressed,
r.list_view_item_selection_indicator_pressed_brush,
),
(CommonState::Pressed, false) => (
r.list_view_item_background_pressed,
r.list_view_item_selection_indicator_brush,
),
(CommonState::PointerOver, true) => (
r.list_view_item_background_selected_pointer_over,
r.list_view_item_selection_indicator_pointer_over_brush,
),
(CommonState::PointerOver, false) => (
r.list_view_item_background_pointer_over,
r.list_view_item_selection_indicator_brush,
),
(CommonState::Normal, true) => (
r.list_view_item_background_selected,
r.list_view_item_selection_indicator_brush,
),
(CommonState::Normal, false) => (
r.list_view_item_background,
r.list_view_item_selection_indicator_brush,
),
};
let foreground = if selected {
r.list_view_item_foreground_selected
} else if matches!(common, CommonState::PointerOver | CommonState::Pressed) {
r.list_view_item_foreground_pointer_over
} else {
r.list_view_item_foreground
};
ChromeBrushes {
backplate,
foreground,
indicator,
}
}
}
fn chrome(
r: &ThemeResources,
common: CommonState,
selected: bool,
content: WidgetRef,
) -> WidgetRef {
let brushes = ChromeBrushes::of(r, common, selected);
let content = ConstrainedBox::new(
BoxConstraints::new()
.min_width(r.list_view_item_min_width)
.min_height(r.list_view_item_min_height),
)
.child(
Padding::new(insets(CONTENT_MARGIN)).child(
Align::new()
.alignment(AlignmentGeometry::CENTER_START)
.child(
SizedBox::new()
.width(f64::INFINITY)
.child(DefaultTextStyle::new(
control_text_style(
r,
r.control_content_theme_font_size,
FontWeight::NORMAL,
brushes.foreground,
),
content,
)),
),
),
);
let backplate = Padding::new(insets(BACKPLATE_MARGIN)).child(
ControlBorder::new(Brush::Solid(brushes.backplate), Brush::Solid(Color::new(0)))
.border_thickness(0.0)
.corner_radius_corners(r.list_view_item_corner_radius),
);
let mut layers = vec![
Positioned::fill(backplate).into_widget(),
content.into_widget(),
];
if selected {
let pressed = common == CommonState::Pressed;
let color = brushes.indicator;
let corner_radius = r.list_view_item_selection_indicator_corner_radius;
layers.push(
Positioned::fill(
IgnorePointer::new().child(
LayoutBuilder::new(move |_, _, constraints| {
let height = selection_indicator_height(constraints.max_height, pressed);
Padding::new(EdgeInsetsGeometry::directional(
SELECTION_INDICATOR_START,
0.0,
0.0,
0.0,
))
.child(
Align::new()
.alignment(AlignmentGeometry::CENTER_START)
.child(
SizedBox::new()
.width(SELECTION_INDICATOR_WIDTH)
.height(height)
.child(
ControlBorder::new(
Brush::Solid(color),
Brush::Solid(Color::new(0)),
)
.border_thickness(0.0)
.corner_radius_corners(corner_radius),
),
),
)
.into_widget()
})
.into_widget(),
),
)
.into_widget(),
);
}
Stack::new().children(layers).into_widget()
}
fn selection_indicator_height(available: f64, pressed: bool) -> f64 {
let height = if available <= SELECTION_INDICATOR_HEIGHT {
available
} else {
SELECTION_INDICATOR_HEIGHT.max(available - 2.0 * SELECTION_INDICATOR_VERTICAL_MARGIN)
};
if pressed {
(height - SELECTION_INDICATOR_HEIGHT_SHRINKAGE).max(0.0)
} else {
height
}
}
fn insets(value: [f64; 4]) -> EdgeInsetsGeometry {
EdgeInsetsGeometry::from_ltrb(value[0], value[1], value[2], value[3])
}