From d36ccdac74b1b9657a517dc1ec8e6e9081f7ed21 Mon Sep 17 00:00:00 2001 From: Florian Blasius Date: Mon, 3 Jun 2024 08:41:29 +0200 Subject: [PATCH] TimePicker: use 24 hour format cpp --- api/rs/slint/private_unstable_api.rs | 4 ++ examples/gallery/ui/pages/controls_page.slint | 1 - internal/compiler/expression_tree.rs | 6 +++ internal/compiler/generator/cpp.rs | 3 ++ internal/compiler/generator/rust.rs | 3 ++ .../llr/optim_passes/inline_expressions.rs | 1 + internal/compiler/lookup.rs | 38 ++++++++++++++----- internal/core/date_time.rs | 6 +++ internal/core/lib.rs | 1 + internal/interpreter/eval.rs | 1 + 10 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 internal/core/date_time.rs diff --git a/api/rs/slint/private_unstable_api.rs b/api/rs/slint/private_unstable_api.rs index 33d37933e48..5c9250faa81 100644 --- a/api/rs/slint/private_unstable_api.rs +++ b/api/rs/slint/private_unstable_api.rs @@ -165,6 +165,10 @@ pub fn init_translations(domain: &str, dirname: impl Into) { i_slint_core::translations::gettext_bindtextdomain(domain, dirname.into()).unwrap() } +pub fn use_24_hour_format() -> bool { + i_slint_core::date_time::use_24_hour_format() +} + /// internal re_exports used by the macro generated pub mod re_exports { pub use alloc::boxed::Box; diff --git a/examples/gallery/ui/pages/controls_page.slint b/examples/gallery/ui/pages/controls_page.slint index 18ea988f483..09d1e53dc18 100644 --- a/examples/gallery/ui/pages/controls_page.slint +++ b/examples/gallery/ui/pages/controls_page.slint @@ -229,7 +229,6 @@ export component ControlsPage inherits Page { close-on-click: false; TimePicker { - twenty-four-hour: true; canceled => { time-picker.close(); } diff --git a/internal/compiler/expression_tree.rs b/internal/compiler/expression_tree.rs index d8c60153480..d15f1504d69 100644 --- a/internal/compiler/expression_tree.rs +++ b/internal/compiler/expression_tree.rs @@ -68,6 +68,7 @@ pub enum BuiltinFunction { RegisterCustomFontByMemory, RegisterBitmapFont, Translate, + Use24HourFormat, } #[derive(Debug, Clone)] @@ -273,6 +274,9 @@ impl BuiltinFunction { Type::Array(Type::String.into()), ], }, + BuiltinFunction::Use24HourFormat => { + Type::Function { return_type: Box::new(Type::Bool), args: vec![] } + } } } @@ -330,6 +334,7 @@ impl BuiltinFunction { | BuiltinFunction::RegisterCustomFontByMemory | BuiltinFunction::RegisterBitmapFont => false, BuiltinFunction::Translate => false, + BuiltinFunction::Use24HourFormat => false, } } @@ -380,6 +385,7 @@ impl BuiltinFunction { | BuiltinFunction::RegisterCustomFontByMemory | BuiltinFunction::RegisterBitmapFont => false, BuiltinFunction::Translate => true, + BuiltinFunction::Use24HourFormat => true, } } } diff --git a/internal/compiler/generator/cpp.rs b/internal/compiler/generator/cpp.rs index 3e10e5f57e6..20060037343 100644 --- a/internal/compiler/generator/cpp.rs +++ b/internal/compiler/generator/cpp.rs @@ -3321,6 +3321,9 @@ fn compile_builtin_function_call( BuiltinFunction::Translate => { format!("slint::private_api::translate({})", a.join(",")) } + BuiltinFunction::Use24HourFormat => { + format!("slint::private_api::use_24_hour_format()") + } } } diff --git a/internal/compiler/generator/rust.rs b/internal/compiler/generator/rust.rs index 44b7c974866..71506847ebd 100644 --- a/internal/compiler/generator/rust.rs +++ b/internal/compiler/generator/rust.rs @@ -2666,6 +2666,9 @@ fn compile_builtin_function_call( BuiltinFunction::Translate => { quote!(slint::private_unstable_api::translate(#((#a) as _),*)) } + BuiltinFunction::Use24HourFormat => { + quote!(slint::private_unstable_api::use_24_hour_format()) + } BuiltinFunction::ItemAbsolutePosition => { if let [Expression::PropertyReference(pr)] = arguments { let item_rc = access_item_rc(pr, ctx); diff --git a/internal/compiler/llr/optim_passes/inline_expressions.rs b/internal/compiler/llr/optim_passes/inline_expressions.rs index 52d7fe7876b..d282f1d7e9b 100644 --- a/internal/compiler/llr/optim_passes/inline_expressions.rs +++ b/internal/compiler/llr/optim_passes/inline_expressions.rs @@ -107,6 +107,7 @@ fn builtin_function_cost(function: &BuiltinFunction) -> isize { BuiltinFunction::SetTextInputFocused => PROPERTY_ACCESS_COST, BuiltinFunction::TextInputFocused => PROPERTY_ACCESS_COST, BuiltinFunction::Translate => 2 * ALLOC_COST + PROPERTY_ACCESS_COST, + BuiltinFunction::Use24HourFormat => 2 * ALLOC_COST + PROPERTY_ACCESS_COST, } } diff --git a/internal/compiler/lookup.rs b/internal/compiler/lookup.rs index 229f2c11736..7d713b74f65 100644 --- a/internal/compiler/lookup.rs +++ b/internal/compiler/lookup.rs @@ -814,16 +814,36 @@ impl LookupObject for SlintInternal { ctx: &LookupCtx, f: &mut impl FnMut(&str, LookupResult) -> Option, ) -> Option { - f( - "color-scheme", - Expression::FunctionCall { - function: Expression::BuiltinFunctionReference(BuiltinFunction::ColorScheme, None) + None.or_else(|| { + f( + "color-scheme", + Expression::FunctionCall { + function: Expression::BuiltinFunctionReference( + BuiltinFunction::ColorScheme, + None, + ) .into(), - arguments: vec![], - source_location: ctx.current_token.as_ref().map(|t| t.to_source_location()), - } - .into(), - ) + arguments: vec![], + source_location: ctx.current_token.as_ref().map(|t| t.to_source_location()), + } + .into(), + ) + .or_else(|| { + f( + "use-24-hour-format", + Expression::FunctionCall { + function: Expression::BuiltinFunctionReference( + BuiltinFunction::Use24HourFormat, + None, + ) + .into(), + arguments: vec![], + source_location: ctx.current_token.as_ref().map(|t| t.to_source_location()), + } + .into(), + ) + }) + }) } } diff --git a/internal/core/date_time.rs b/internal/core/date_time.rs new file mode 100644 index 00000000000..3bf6cec1a2a --- /dev/null +++ b/internal/core/date_time.rs @@ -0,0 +1,6 @@ +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-commercial + +pub fn use_24_hour_format() -> bool { + true +} diff --git a/internal/core/lib.rs b/internal/core/lib.rs index 33ee224cee4..abdef29c0a7 100644 --- a/internal/core/lib.rs +++ b/internal/core/lib.rs @@ -24,6 +24,7 @@ pub mod api; pub mod callbacks; pub mod component_factory; pub mod context; +pub mod date_time; pub mod future; pub mod graphics; pub mod input; diff --git a/internal/interpreter/eval.rs b/internal/interpreter/eval.rs index 707a29cfa40..17907d205c5 100644 --- a/internal/interpreter/eval.rs +++ b/internal/interpreter/eval.rs @@ -1092,6 +1092,7 @@ fn call_builtin_function( &SharedString::try_from(eval_expression(&arguments[5], local_context)).unwrap(), )) } + BuiltinFunction::Use24HourFormat => Value::Bool(corelib::date_time::use_24_hour_format()), } }