mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-19 19:08:05 +08:00
Add the unit, display_decimal_places, and step parameter widget macro attributes (#2706)
* UI working for spacing enum. * Implementations. * UI working for spacing enum. * Undo all changes. * unit, display_decimal_places, and step macro implementation. * Fixing tests. --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
@@ -121,6 +121,9 @@ pub(crate) fn property_from_type(
|
||||
index: usize,
|
||||
ty: &Type,
|
||||
number_options: (Option<f64>, Option<f64>, Option<(f64, f64)>),
|
||||
unit: Option<&str>,
|
||||
display_decimal_places: Option<u32>,
|
||||
step: Option<f64>,
|
||||
context: &mut NodePropertiesContext,
|
||||
) -> Result<Vec<LayoutGroup>, Vec<LayoutGroup>> {
|
||||
let Some(network) = context.network_interface.nested_network(context.selection_network_path) else {
|
||||
@@ -142,6 +145,15 @@ pub(crate) fn property_from_type(
|
||||
number_max = Some(range_end);
|
||||
number_input = number_input.mode_range().min(range_start).max(range_end);
|
||||
}
|
||||
if let Some(unit) = unit {
|
||||
number_input = number_input.unit(unit);
|
||||
}
|
||||
if let Some(display_decimal_places) = display_decimal_places {
|
||||
number_input = number_input.display_decimal_places(display_decimal_places);
|
||||
}
|
||||
if let Some(step) = step {
|
||||
number_input = number_input.step(step);
|
||||
}
|
||||
|
||||
let min = |x: f64| number_min.unwrap_or(x);
|
||||
let max = |x: f64| number_max.unwrap_or(x);
|
||||
@@ -155,15 +167,15 @@ pub(crate) fn property_from_type(
|
||||
// Aliased types (ambiguous values)
|
||||
Some("Percentage") => number_widget(default_info, number_input.percentage().min(min(0.)).max(max(100.))).into(),
|
||||
Some("SignedPercentage") => number_widget(default_info, number_input.percentage().min(min(-100.)).max(max(100.))).into(),
|
||||
Some("Angle") => number_widget(default_info, number_input.mode_range().min(min(-180.)).max(max(180.)).unit("°")).into(),
|
||||
Some("Multiplier") => number_widget(default_info, number_input.unit("x")).into(),
|
||||
Some("PixelLength") => number_widget(default_info, number_input.min(min(0.)).unit(" px")).into(),
|
||||
Some("Angle") => number_widget(default_info, number_input.mode_range().min(min(-180.)).max(max(180.)).unit(unit.unwrap_or("°"))).into(),
|
||||
Some("Multiplier") => number_widget(default_info, number_input.unit(unit.unwrap_or("x"))).into(),
|
||||
Some("PixelLength") => number_widget(default_info, number_input.min(min(0.)).unit(unit.unwrap_or(" px"))).into(),
|
||||
Some("Length") => number_widget(default_info, number_input.min(min(0.))).into(),
|
||||
Some("Fraction") => number_widget(default_info, number_input.mode_range().min(min(0.)).max(max(1.))).into(),
|
||||
Some("IntegerCount") => number_widget(default_info, number_input.int().min(min(1.))).into(),
|
||||
Some("SeedValue") => number_widget(default_info, number_input.int().min(min(0.))).into(),
|
||||
Some("Resolution") => coordinate_widget(default_info, "W", "H", " px", Some(64.)),
|
||||
Some("PixelSize") => coordinate_widget(default_info, "X", "Y", " px", None),
|
||||
Some("Resolution") => coordinate_widget(default_info, "W", "H", unit.unwrap_or(" px"), Some(64.)),
|
||||
Some("PixelSize") => coordinate_widget(default_info, "X", "Y", unit.unwrap_or(" px"), None),
|
||||
|
||||
// For all other types, use TypeId-based matching
|
||||
_ => {
|
||||
@@ -249,8 +261,8 @@ pub(crate) fn property_from_type(
|
||||
}
|
||||
}
|
||||
Type::Generic(_) => vec![TextLabel::new("Generic type (not supported)").widget_holder()].into(),
|
||||
Type::Fn(_, out) => return property_from_type(node_id, index, out, number_options, context),
|
||||
Type::Future(out) => return property_from_type(node_id, index, out, number_options, context),
|
||||
Type::Fn(_, out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, context),
|
||||
Type::Future(out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, context),
|
||||
};
|
||||
|
||||
extra_widgets.push(widgets);
|
||||
@@ -1395,6 +1407,9 @@ pub(crate) fn generate_node_properties(node_id: NodeId, context: &mut NodeProper
|
||||
};
|
||||
|
||||
let mut number_options = (None, None, None);
|
||||
let mut display_decimal_places = None;
|
||||
let mut step = None;
|
||||
let mut unit_suffix = None;
|
||||
let input_type = match implementation {
|
||||
DocumentNodeImplementation::ProtoNode(proto_node_identifier) => 'early_return: {
|
||||
if let Some(field) = graphene_core::registry::NODE_METADATA
|
||||
@@ -1404,6 +1419,9 @@ pub(crate) fn generate_node_properties(node_id: NodeId, context: &mut NodeProper
|
||||
.and_then(|metadata| metadata.fields.get(input_index))
|
||||
{
|
||||
number_options = (field.number_min, field.number_max, field.number_mode_range);
|
||||
display_decimal_places = field.number_display_decimal_places;
|
||||
unit_suffix = field.unit;
|
||||
step = field.number_step;
|
||||
if let Some(ref default) = field.default_type {
|
||||
break 'early_return default.clone();
|
||||
}
|
||||
@@ -1417,7 +1435,7 @@ pub(crate) fn generate_node_properties(node_id: NodeId, context: &mut NodeProper
|
||||
let mut input_types = implementations
|
||||
.keys()
|
||||
.filter_map(|item| item.inputs.get(input_index))
|
||||
.filter(|ty| property_from_type(node_id, input_index, ty, number_options, context).is_ok())
|
||||
.filter(|ty| property_from_type(node_id, input_index, ty, number_options, unit_suffix, display_decimal_places, step, context).is_ok())
|
||||
.collect::<Vec<_>>();
|
||||
input_types.sort_by_key(|ty| ty.type_name());
|
||||
let input_type = input_types.first().cloned();
|
||||
@@ -1431,7 +1449,7 @@ pub(crate) fn generate_node_properties(node_id: NodeId, context: &mut NodeProper
|
||||
_ => context.network_interface.input_type(&InputConnector::node(node_id, input_index), context.selection_network_path).0,
|
||||
};
|
||||
|
||||
property_from_type(node_id, input_index, &input_type, number_options, context).unwrap_or_else(|value| value)
|
||||
property_from_type(node_id, input_index, &input_type, number_options, unit_suffix, display_decimal_places, step, context).unwrap_or_else(|value| value)
|
||||
});
|
||||
|
||||
layout.extend(row);
|
||||
|
||||
Reference in New Issue
Block a user