Skip to content

Commit

Permalink
Add support for step-size to SpinBox rendered by the Qt style
Browse files Browse the repository at this point in the history
  • Loading branch information
tronical committed May 31, 2024
1 parent 3523e86 commit d183498
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
26 changes: 16 additions & 10 deletions internal/backends/qt/qt_widgets/spinbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct NativeSpinBox {
pub value: Property<i32>,
pub minimum: Property<i32>,
pub maximum: Property<i32>,
pub step_size: Property<i32>,
pub cached_rendering_data: CachedRenderingData,
pub edited: Callback<IntArg>,
data: Property<NativeSpinBoxData>,
Expand Down Expand Up @@ -136,6 +137,7 @@ impl Item for NativeSpinBox {
let mut data = self.data();
let active_controls = data.active_controls;
let pressed = data.pressed;
let step_size = self.step_size();
let widget: NonNull<()> = SlintTypeErasedWidgetPtr::qwidget_ptr(&self.widget_ptr);

let pos = event
Expand Down Expand Up @@ -179,8 +181,9 @@ 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,));
let new_val = v + step_size;
self.value.set(new_val);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(new_val,));
}
}
if new_control
Expand All @@ -190,8 +193,9 @@ 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,));
let new_val = v - step_size;
self.value.set(new_val);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(new_val,));
}
}
true
Expand All @@ -201,14 +205,16 @@ impl Item for NativeSpinBox {
if delta_y > 0. {
let v = self.value();
if v < self.maximum() {
self.value.set(v + 1);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(v + 1,));
let new_val = v + step_size;
self.value.set(new_val);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(new_val,));
}
} else if delta_y < 0. {
let v = self.value();
if v > self.minimum() {
self.value.set(v - 1);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(v - 1,));
let new_val = v - step_size;
self.value.set(new_val);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(new_val,));
}
}

Expand Down Expand Up @@ -240,14 +246,14 @@ impl Item for NativeSpinBox {
if event.text.starts_with(i_slint_core::input::key_codes::UpArrow)
&& self.value() < self.maximum()
{
let new_val = self.value() + 1;
let new_val = self.value() + self.step_size();
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()
{
let new_val = self.value() - 1;
let new_val = self.value() - self.step_size();
self.value.set(new_val);
Self::FIELD_OFFSETS.edited.apply_pin(self).call(&(new_val,));
KeyEventResult::EventAccepted
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/builtins.slint
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ export component NativeSpinBox {
in-out property <int> value;
in property <int> minimum;
in property <int> maximum: 100;
in property <int> step-size: 1;
callback edited(int /* value */);
//-is_internal
//-accepts_focus
Expand Down
4 changes: 1 addition & 3 deletions internal/compiler/widgets/qt/spinbox.slint
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// 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 component SpinBox inherits NativeSpinBox {
in property <int> step-size: 1;

export component SpinBox inherits NativeSpinBox {
value: root.minimum;
accessible-role: spinbox;
accessible-value: root.value;
Expand Down

0 comments on commit d183498

Please sign in to comment.