Skip to content

Commit

Permalink
TimePicker: use 24 hour format cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
FloVanGH committed Jun 3, 2024
1 parent fa93b2e commit 3d8f9f0
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 10 deletions.
4 changes: 4 additions & 0 deletions api/rs/slint/private_unstable_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ pub fn init_translations(domain: &str, dirname: impl Into<std::path::PathBuf>) {
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;
Expand Down
1 change: 0 additions & 1 deletion examples/gallery/ui/pages/controls_page.slint
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ export component ControlsPage inherits Page {
close-on-click: false;

TimePicker {
twenty-four-hour: true;
canceled => {
time-picker.close();
}
Expand Down
6 changes: 6 additions & 0 deletions internal/compiler/expression_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub enum BuiltinFunction {
RegisterCustomFontByMemory,
RegisterBitmapFont,
Translate,
Use24HourFormat,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -273,6 +274,9 @@ impl BuiltinFunction {
Type::Array(Type::String.into()),
],
},
BuiltinFunction::Use24HourFormat => {
Type::Function { return_type: Box::new(Type::Bool), args: vec![] }
}
}
}

Expand Down Expand Up @@ -330,6 +334,7 @@ impl BuiltinFunction {
| BuiltinFunction::RegisterCustomFontByMemory
| BuiltinFunction::RegisterBitmapFont => false,
BuiltinFunction::Translate => false,
BuiltinFunction::Use24HourFormat => false,
}
}

Expand Down Expand Up @@ -380,6 +385,7 @@ impl BuiltinFunction {
| BuiltinFunction::RegisterCustomFontByMemory
| BuiltinFunction::RegisterBitmapFont => false,
BuiltinFunction::Translate => true,
BuiltinFunction::Use24HourFormat => true,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/compiler/generator/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions internal/compiler/generator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/llr/optim_passes/inline_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
38 changes: 29 additions & 9 deletions internal/compiler/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,16 +814,36 @@ impl LookupObject for SlintInternal {
ctx: &LookupCtx,
f: &mut impl FnMut(&str, LookupResult) -> Option<R>,
) -> Option<R> {
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(),
)
})
})
}
}

Expand Down
6 changes: 6 additions & 0 deletions internal/core/date_time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// 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
}
1 change: 1 addition & 0 deletions internal/core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions internal/interpreter/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}

Expand Down

0 comments on commit 3d8f9f0

Please sign in to comment.