Declare .slint enum in one place in i-slint-common

This avoid repeating the enums both in the compiler and in
the runtime library, and register them in a bunch of other places.

So it should be easier to add enums and enum values

Since cbindgen doesn't see through the macro, generate the enum
manually
This commit is contained in:
Olivier Goffart 2022-04-14 19:17:48 +02:00 committed by GitHub
parent 5a270313d2
commit e85e69fda0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 366 additions and 520 deletions

View file

@ -2184,7 +2184,11 @@ fn compile_expression(expr: &llr::Expression, ctx: &EvaluationContext) -> String
)
}
Expression::EnumerationValue(value) => {
format!("slint::cbindgen_private::{}::{}", value.enumeration.name, ident(&value.to_string()))
format!(
"slint::cbindgen_private::{}::{}",
value.enumeration.name,
ident(i_slint_common::enums::cpp_escape_keyword(&value.to_string())),
)
}
Expression::ReturnStatement(Some(expr)) => format!(
"throw slint::private_api::ReturnWrapper<{}>({})",

View file

@ -499,7 +499,7 @@ fn solve_layout(
if let (Some(button_roles), Orientation::Horizontal) = (&layout.dialog_button_roles, o)
{
let cells_ty = cells.ty(ctx);
let e = crate::typeregister::DIALOG_BUTTON_ROLE_ENUM.with(|e| e.clone());
let e = crate::typeregister::BUILTIN_ENUMS.with(|e| e.DialogButtonRole.clone());
let roles = button_roles
.iter()
.map(|r| {
@ -569,8 +569,8 @@ fn solve_layout(
("padding", padding.ty(ctx), padding),
(
"alignment",
crate::typeregister::LAYOUT_ALIGNMENT_ENUM
.with(|e| Type::Enumeration(e.clone())),
crate::typeregister::BUILTIN_ENUMS
.with(|e| Type::Enumeration(e.LayoutAlignment.clone())),
bld.alignment,
),
("cells", bld.cells.ty(ctx), bld.cells),
@ -661,7 +661,7 @@ fn box_layout_data(
let alignment = if let Some(expr) = &layout.geometry.alignment {
llr_Expression::PropertyReference(ctx.map_property_reference(expr))
} else {
let e = crate::typeregister::LAYOUT_ALIGNMENT_ENUM.with(|e| e.clone());
let e = crate::typeregister::BUILTIN_ENUMS.with(|e| e.LayoutAlignment.clone());
llr_Expression::EnumerationValue(EnumerationValue {
value: e.default_value,
enumeration: e,

View file

@ -120,7 +120,7 @@ pub fn compile_paths(
fn compile_path_from_string_literal(commands: &str) -> Option<BindingExpression> {
let path_builder = lyon_path::Path::builder().with_svg();
lyon_svg::path_utils::build_path(path_builder, commands).ok().map(|path| {
let event_enum = crate::typeregister::PATH_EVENT_ENUM.with(|e| e.clone());
let event_enum = crate::typeregister::BUILTIN_ENUMS.with(|e| e.PathEvent.clone());
let point_type = Type::Struct {
fields: IntoIterator::into_iter([
("x".to_owned(), Type::Float32),
@ -162,9 +162,9 @@ fn compile_path_from_string_literal(commands: &str) -> Option<BindingExpression>
}
lyon_path::Event::End { first: _, last: _, close } => {
if close {
event_enum.clone().try_value_from_string("end_closed").unwrap()
event_enum.clone().try_value_from_string("end-closed").unwrap()
} else {
event_enum.clone().try_value_from_string("end_open").unwrap()
event_enum.clone().try_value_from_string("end-open").unwrap()
}
}
})

View file

@ -37,40 +37,37 @@ const RESERVED_LAYOUT_PROPERTIES: &[(&str, Type)] = &[
("rowspan", Type::Int32),
];
macro_rules! declare_enums {
($( $(#[$enum_doc:meta])* enum $Name:ident { $( $(#[$value_doc:meta])* $Value:ident,)* })*) => {
pub struct BuiltinEnums {
$(pub $Name : Rc<Enumeration>),*
}
impl BuiltinEnums {
fn new() -> Self {
Self {
$($Name : Rc::new(Enumeration {
name: stringify!($Name).replace('_', "-"),
values: vec![$(stringify!($Value).trim_start_matches("r#").replace('_', "-")),*],
default_value: 0,
})),*
}
}
fn fill_register(&self, register: &mut TypeRegister) {
$(if stringify!($Name) != "PathEvent" {
register.insert_type_with_name(
Type::Enumeration(self.$Name.clone()),
stringify!($Name).replace('_', "-")
);
})*
}
}
};
}
i_slint_common::for_each_enums!(declare_enums);
thread_local! {
pub static DIALOG_BUTTON_ROLE_ENUM: Rc<Enumeration> =
Rc::new(Enumeration {
name: "DialogButtonRole".into(),
values: IntoIterator::into_iter([
"none".to_owned(),
"accept".to_owned(),
"reject".to_owned(),
"apply".to_owned(),
"reset".to_owned(),
"action".to_owned(),
"help".to_owned(),
])
.collect(),
default_value: 0,
});
pub static LAYOUT_ALIGNMENT_ENUM: Rc<Enumeration> =
Rc::new(Enumeration {
name: "LayoutAlignment".into(),
values: IntoIterator::into_iter(
["stretch", "center", "start", "end", "space-between", "space-around"]
).map(String::from).collect(),
default_value: 0,
});
pub static PATH_EVENT_ENUM: Rc<Enumeration> =
Rc::new(Enumeration {
name: "PathEvent".into(),
values: IntoIterator::into_iter(
["begin", "line", "quadratic", "cubic", "end_open", "end_closed"]
).map(String::from).collect(),
default_value: 0,
});
pub static BUILTIN_ENUMS: BuiltinEnums = BuiltinEnums::new();
}
const RESERVED_OTHER_PROPERTIES: &[(&str, Type)] = &[
@ -98,7 +95,10 @@ pub fn reserved_properties() -> impl Iterator<Item = (&'static str, Type)> {
.chain(IntoIterator::into_iter([
("forward-focus", Type::ElementReference),
("focus", BuiltinFunction::SetFocusItem.ty()),
("dialog-button-role", Type::Enumeration(DIALOG_BUTTON_ROLE_ENUM.with(|e| e.clone()))),
(
"dialog-button-role",
Type::Enumeration(BUILTIN_ENUMS.with(|e| e.DialogButtonRole.clone())),
),
]))
}
@ -183,73 +183,7 @@ impl TypeRegister {
register.insert_type(Type::Angle);
register.insert_type(Type::Brush);
let mut declare_enum = |name: &str, values: &[&str]| {
register.insert_type_with_name(
Type::Enumeration(Rc::new(Enumeration {
name: name.to_owned(),
values: values.iter().cloned().map(String::from).collect(),
default_value: 0,
})),
name.to_owned(),
);
};
declare_enum("TextHorizontalAlignment", &["left", "center", "right"]);
declare_enum("TextVerticalAlignment", &["top", "center", "bottom"]);
declare_enum("TextWrap", &["no-wrap", "word-wrap"]);
declare_enum("TextOverflow", &["clip", "elide"]);
declare_enum("ImageFit", &["fill", "contain", "cover"]);
declare_enum("ImageRendering", &["smooth", "pixelated"]);
declare_enum("EventResult", &["reject", "accept"]);
declare_enum("FillRule", &["nonzero", "evenodd"]);
declare_enum("InputType", &["text", "password"]);
declare_enum(
"MouseCursor",
&[
"default",
"none",
"help",
"pointer",
"progress",
"wait",
"crosshair",
"text",
"alias",
"copy",
"move",
"no-drop",
"not-allowed",
"grab",
"grabbing",
"col-resize",
"row-resize",
"n-resize",
"e-resize",
"s-resize",
"w-resize",
"ne-resize",
"nw-resize",
"se-resize",
"sw-resize",
"ew-resize",
"ns-resize",
"nesw-resize",
"nwse-resize",
],
);
declare_enum(
"StandardButtonKind",
&[
"ok", "cancel", "apply", "close", "reset", "help", "yes", "no", "abort", "retry",
"ignore",
],
);
declare_enum("PointerEventKind", &["cancel", "down", "up"]);
declare_enum("PointerEventButton", &["none", "left", "right", "middle"]);
DIALOG_BUTTON_ROLE_ENUM
.with(|e| register.insert_type_with_name(Type::Enumeration(e.clone()), e.name.clone()));
LAYOUT_ALIGNMENT_ENUM
.with(|e| register.insert_type_with_name(Type::Enumeration(e.clone()), e.name.clone()));
BUILTIN_ENUMS.with(|e| e.fill_register(&mut register));
register.supported_property_animation_types.insert(Type::Float32.to_string());
register.supported_property_animation_types.insert(Type::Int32.to_string());