Skip to content

Commit

Permalink
Added TimePicker widget (#5251)
Browse files Browse the repository at this point in the history
  • Loading branch information
FloVanGH committed Jun 5, 2024
1 parent ef11ccf commit 3ccee81
Show file tree
Hide file tree
Showing 44 changed files with 1,612 additions and 82 deletions.
4 changes: 4 additions & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ Files: internal/compiler/widgets/cosmic-base/_*.svg
Copyright: "Cosmic Icons" by System76 <https://github.com/pop-os/cosmic-icons>
License: CC-BY-SA-4.0

Files: internal/compiler/widgets/qt/_*.svg
Copyright: "Cosmic Icons" by System76 <https://github.com/pop-os/cosmic-icons>
License: CC-BY-SA-4.0

Files: internal/backends/linuxkms/mouse-pointer.svg examples/virtual_keyboard/ui/assets/*.svg examples/ffmpeg/pause.svg examples/ffmpeg/play.svg examples/gstreamer-player/*.svg examples/uefi-demo/resource/cursor.png
Copyright: Copyright © 2018 Dave Gandy & Fork Awesome
License: MIT
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project are documented in this file.
- Fixed set current-index of ComboBox to -1 does not reset current-value
- Fixed issue where the text of `SpinBox` is not updated after value is changed from outside
- Added `step-size` to `SpinBox`
- Added `TimePicker` widget.

## [1.6.0] - 2024-05-13

Expand Down
5 changes: 5 additions & 0 deletions api/cpp/include/slint.h
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,11 @@ inline SharedString translate(const SharedString &original, const SharedString &
return result;
}

inline bool use_24_hour_format()
{
return cbindgen_private::slint_use_24_hour_format();
}

} // namespace private_api

#ifdef SLINT_FEATURE_GETTEXT
Expand Down
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: 1 addition & 0 deletions docs/reference/src/language/widgets/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ Widgets
tabwidget.md
textedit.md
verticalbox.md
timepicker.md
63 changes: 63 additions & 0 deletions docs/reference/src/language/widgets/timepicker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!-- Copyright © SixtyFPS GmbH <info@slint.dev> ; SPDX-License-Identifier: MIT -->

## struct `Time`

Defines a time with hours, minutes, and seconds.

### Fields

- **`hour`(int)**: The hour value (range from 0 to 23).
- **`minute`(int)**: The minute value (range from 1 to 59).
- **`second`(int)**: The second value (range form 1 to 59).

## `TimePicker`

Use the timer picker to select the time, in either 24-hour or 12-hour mode (AM/PM).

### Properties

- **`use-24-hour-format`**: (_in_ _bool_): If set to `true` 24 hours are displayed otherwise it is displayed in AM/PM mode. (default: system default, if cannot be determined then `true`)
- **`title`** (_in_ _string_): The text that is displayed at the top of the picker.
- **`cancel-label`** (_in_ _string_): The text written in the cancel button.
- **`ok-label`** (_in_ _string_): The text written in the ok button.
- **`time`**: (_in_ _Time_): Set the initial displayed time.

### Callbacks

- **`canceled()`**: The cancel button was clicked.
- **`accepted(Time)`** The ok button was clicked.

### Example

```slint
import { TimePicker, Button } from "std-widgets.slint";
export component Example inherits Window {
width: 600px;
height: 600px;
time-picker-button := Button {
text: @tr("Open TimePicker");
clicked => {
time-picker.show();
}
}
time-picker := PopupWindow {
width: 340px;
height: 500px;
close-on-click: false;
TimePicker {
canceled => {
time-picker.close();
}
accepted(time) => {
debug(time);
time-picker.close();
}
}
}
}
```
31 changes: 29 additions & 2 deletions examples/gallery/ui/pages/controls_page.slint
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT

import { Button, GroupBox, SpinBox, ComboBox, CheckBox, LineEdit, TabWidget, VerticalBox, HorizontalBox,
Slider, ProgressIndicator, SpinBox, Switch, Spinner, GridBox } from "std-widgets.slint";
Slider, ProgressIndicator, SpinBox, Switch, Spinner, GridBox, TimePicker } from "std-widgets.slint";
import { GallerySettings } from "../gallery_settings.slint";
import { Page } from "page.slint";

Expand Down Expand Up @@ -87,7 +87,7 @@ export component ControlsPage inherits Page {
}

GroupBox {
title: @tr("LineEdit - SpinBox");
title: @tr("LineEdit - SpinBox - TimePicker");
vertical-stretch: 0;

HorizontalBox {
Expand All @@ -104,6 +104,14 @@ export component ControlsPage inherits Page {
value: 42;
enabled: GallerySettings.widgets-enabled;
}

time-picker-button := Button {
text: @tr("Open TimePicker");

clicked => {
time-picker.show();
}
}
}
}

Expand Down Expand Up @@ -139,6 +147,7 @@ export component ControlsPage inherits Page {
row: 0;
col: 1;
rowspan: 2;

Spinner {
progress: i-progress-indicator.progress;
indeterminate: i-progress-indicator.indeterminate;
Expand Down Expand Up @@ -211,4 +220,22 @@ export component ControlsPage inherits Page {
}
}
}

time-picker := PopupWindow {
x: (root.width - 340px) / 2;
y: (root.height - 500px) / 2;
width: 340px;
height: 500px;
close-on-click: false;

TimePicker {
canceled => {
time-picker.close();
}

accepted(time) => {
time-picker.close();
}
}
}
}
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 @@ -3355,6 +3355,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 @@ -2698,6 +2698,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 @@ -109,6 +109,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
8 changes: 8 additions & 0 deletions internal/compiler/widgets/common/internal-components.slint
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

export struct TextStyle {
font-size: length,
font-weight: int,
foreground: brush,
}

0 comments on commit 3ccee81

Please sign in to comment.