Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add edited callback for SpinBox #3096

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions internal/backends/qt/qt_widgets/spinbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct NativeSpinBoxData {
pressed: bool,
}

type IntArg = (i32,);

#[repr(C)]
#[derive(FieldOffsets, Default, SlintElement)]
#[pin]
Expand All @@ -26,6 +28,7 @@ pub struct NativeSpinBox {
pub minimum: Property<i32>,
pub maximum: Property<i32>,
pub cached_rendering_data: CachedRenderingData,
pub edited: Callback<IntArg>,
data: Property<NativeSpinBoxData>,
widget_ptr: std::cell::Cell<SlintTypeErasedWidgetPtr>,
animation_tracker: Property<i32>,
Expand Down Expand Up @@ -179,6 +182,7 @@ impl Item for NativeSpinBox {
let v = self.value();
if v < self.maximum() {
self.value.set(v + 1);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(v + 1,));
}
}
if new_control
Expand All @@ -188,6 +192,7 @@ impl Item for NativeSpinBox {
let v = self.value();
if v > self.minimum() {
self.value.set(v - 1);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(v - 1,));
}
}
true
Expand Down Expand Up @@ -220,12 +225,16 @@ impl Item for NativeSpinBox {
if event.text.starts_with(i_slint_core::input::key_codes::UpArrow)
&& self.value() < self.maximum()
{
self.value.set(self.value() + 1);
let new_val = self.value() + 1;
self.value.set(new_val);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(new_val,));
KeyEventResult::EventAccepted
} else if event.text.starts_with(i_slint_core::input::key_codes::DownArrow)
&& self.value() > self.minimum()
{
self.value.set(self.value() - 1);
let new_val = self.value() - 1;
self.value.set(new_val);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(new_val,));
KeyEventResult::EventAccepted
} else {
KeyEventResult::EventIgnored
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/builtins.slint
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ export component NativeSpinBox {
in-out property <int> value;
in property <int> minimum;
in property <int> maximum: 100;
callback edited(int /* value */);
//-is_internal
}

Expand Down
3 changes: 3 additions & 0 deletions internal/compiler/widgets/fluent-base/spinbox.slint
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ component SpinBoxButton {
}

export component SpinBox {
callback edited(int /* value */);

in property <int> minimum;
in property <int> maximum: 100;
in property <bool> enabled <=> i-text-input.enabled;
Expand Down Expand Up @@ -137,6 +139,7 @@ export component SpinBox {
}

root.value = value;
root.edited(value);
}

function increment() {
Expand Down
21 changes: 15 additions & 6 deletions internal/compiler/widgets/material-base/spinbox.slint
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ component SpinBoxButton inherits Rectangle {

// Increment and decrement a value in the given range.
export component SpinBox {
callback edited(int /* value */);

in property <int> minimum;
in property <int> maximum: 100;
in property <bool> enabled <=> i-focus-scope.enabled;
Expand All @@ -80,9 +82,11 @@ export component SpinBox {
key-pressed(event) => {
if (root.enabled && event.text == Key.UpArrow && root.value < root.maximum) {
root.value += 1;
root.edited(value);
accept
} else if (root.enabled && event.text == Key.DownArrow && root.value > root.minimum) {
root.value -= 1;
root.edited(value);
accept
} else {
reject
Expand Down Expand Up @@ -131,9 +135,7 @@ export component SpinBox {
}

clicked => {
if (root.value < root.maximum) {
root.value += 1;
}
root.update-value(root.value + 1);

root.focus();
}
Expand All @@ -151,16 +153,23 @@ export component SpinBox {
}

clicked => {
if (root.value > root.minimum) {
root.value -= 1;
}
root.update-value(root.value - 1);

root.focus();
}
}
}
}

function update-value(value: int) {
if (value < root.minimum || value > root.maximum) {
return;
}

root.value = value;
root.edited(value);
}

states [
disabled when !root.enabled : {
i-background.border-color: Palette.on-surface;
Expand Down