Loading files
9d2aa90beThis file is pinned to the selected release. Download the original to open it in your design tool.
import { IconButton } from "iconbutton.slint";
import { LinkText } from "linktext.slint";
import { ToolTip } from "tooltip.slint";
import {
Backend,
Constants,
Data,
Helpers,
Int64,
LengthEditData,
LengthEditParseResult,
LengthUnit,
} from "../api.slint";
export component LengthEdit inherits Rectangle {
in property <LengthEditData> data: {
minimum: Data.min-length,
};
in-out property <Int64> value: data.value;
in-out property <LengthUnit> unit: data.unit;
in property <Int64> minimum: data.minimum;
in property <image> icon;
in property <string> tooltip;
in property <string> min-width-text: "123.456";
in property <bool> can-increase: data.can-increase;
in property <bool> can-decrease: data.can-decrease;
in property <bool> can-change-unit: true;
in property <bool> auto-supported: false;
in-out property <bool> auto-checked: false;
in property <bool> enabled: true;
property <LengthEditParseResult> parse-result;
property <bool> disable-update: false;
property <bool> auto-hovered: false;
callback value-changed(Int64);
value-changed(value) => {
self.value = value;
}
callback value-accepted(Int64);
callback unit-changed(LengthUnit);
unit-changed(unit) => {
self.unit = unit;
}
callback auto-toggled(bool);
auto-toggled(checked) => {
self.auto-checked = checked;
}
callback increase-triggered;
callback decrease-triggered;
public function select-all() {
edt.select-all();
}
function update-text-from-value() {
edt.text = Backend.format-length(root.value, root.unit);
parse-result.valid = true;
}
// Accessibility
accessible-role: spinbox;
accessible-description: tooltip;
accessible-enabled: enabled;
accessible-value: edt.text + " " + Helpers.get-unit-accessibility-label(root.unit);
accessible-action-increment => {
disable-update = false;
increase-triggered();
}
accessible-action-decrement => {
disable-update = false;
decrease-triggered();
}
accessible-action-set-value(s) => {
edt.text = s;
edt.edited();
}
changed value => {
if !disable-update {
update-text-from-value();
}
}
changed unit => {
if !disable-update {
update-text-from-value();
}
}
preferred-height: 28px;
background: #202020;
border-color: {
if enabled && (!parse-result.valid) {
edt.has-focus ? #ff0000 : #900000
} else if edt.has-focus {
Constants.librepcb-color
} else if enabled {
#606060
} else {
transparent
}
}
border-width: 1px;
border-radius: 10px;
forward-focus: edt;
// A bit ugly: Ensure enough space for numbers in the typical range.
metrics-edt := TextInput {
width: 0;
height: 0;
font-size: 13px;
single-line: true;
wrap: no-wrap;
text: min-width-text;
visible: false;
accessible-role: none;
}
ta := TouchArea {
enabled: enabled;
l := HorizontalLayout {
padding-left: 8px;
padding-right: 2px;
padding-top: self.padding-right;
padding-bottom: self.padding-top;
spacing: 5px;
if icon.width > 0: Image {
width: 12px;
image-fit: contain;
colorize: edt.color;
source: icon;
accessible-role: none;
}
edt := TextInput {
min-width: max(self.preferred-width, metrics-edt.preferred-width);
font-size: 13px;
single-line: true;
horizontal-alignment: right;
vertical-alignment: center;
selection-background-color: Constants.librepcb-color;
selection-foreground-color: black;
enabled: enabled;
accessible-role: none;
changed has-focus => {
if !self.has-focus {
disable-update = false;
update-text-from-value();
}
}
init => {
update-text-from-value();
}
edited => {
disable-update = true;
parse-result = Backend.parse-length-input(self.text, unit, minimum);
if parse-result.valid {
if parse-result.evaluated-value != value {
value-changed(parse-result.evaluated-value);
}
if can-change-unit && (parse-result.evaluated-unit != unit) {
unit-changed(parse-result.evaluated-unit);
}
}
}
accepted => {
disable-update = false;
update-text-from-value();
value-accepted(parse-result.evaluated-value);
}
key-pressed(event) => {
if event.text == Key.UpArrow {
if can-increase {
disable-update = false;
increase-triggered();
}
return accept;
} else if event.text == Key.DownArrow {
if can-decrease {
disable-update = false;
decrease-triggered();
}
return accept;
} else if (event.text == Key.PageUp) && can-change-unit {
disable-update = false;
unit-changed(Helpers.get-previous-unit(unit));
return accept;
} else if (event.text == Key.PageDown) && can-change-unit {
disable-update = false;
unit-changed(Helpers.get-next-unit(unit));
return accept;
} else if (event.text == Key.Escape) && disable-update {
disable-update = false;
update-text-from-value();
return accept;
}
reject
}
}
Rectangle {
background: black;
border-top-right-radius: root.border-radius;
border-bottom-right-radius: root.border-radius;
clip: true;
HorizontalLayout {
if can-increase || can-decrease: VerticalLayout {
up-btn := IconButton {
width: 15px;
enabled: enabled && can-increase;
background-color: black;
icon: @image-url("../../../../font-awesome/svgs/solid/caret-up.svg");
icon-offset-y: 1px;
accessible-label: "increase";
clicked => {
disable-update = false;
increase-triggered();
}
}
down-btn := IconButton {
width: 15px;
enabled: enabled && can-decrease;
background-color: black;
icon: @image-url("../../../../font-awesome/svgs/solid/caret-down.svg");
icon-offset-y: -1px;
accessible-label: "decrease";
clicked => {
disable-update = false;
decrease-triggered();
}
}
}
Rectangle {
width: 1px;
Rectangle {
y: (parent.height - self.height) / 2;
height: 15px;
background: gray;
}
}
unit-btn := LinkText {
y: ((parent.height - self.height) / 2) - 1px;
width: self.preferred-width + 10px;
horizontal-alignment: center;
vertical-alignment: center;
text-color: edt.color;
text-color-disabled: edt.color;
text: Helpers.get-unit-short-str(root.unit);
enabled: can-change-unit;
// Accessibility
accessible-description: "unit";
accessible-label: Helpers.get-unit-accessibility-label(unit);
accessible-expandable: true;
accessible-action-expand => {
unit-btn.clicked();
}
clicked => {
unit-popup.show();
}
unit-popup := PopupWindow {
x: (parent.width - self.preferred-width) / 2;
y: parent.height + 2px;
Rectangle {
background: black;
border-radius: 5px;
VerticalLayout {
padding: 5px;
spacing: 5px;
for unit in Helpers.all-length-units: unit-item-btn := LinkText {
text: Helpers.get-unit-short-str(unit);
text-color: (unit == root.unit) ? Constants.librepcb-color : unit-btn.text-color;
horizontal-alignment: center;
accessible-label: Helpers.get-unit-accessibility-label(unit);
clicked => {
unit-changed(unit);
}
}
}
}
}
}
if auto-supported: HorizontalLayout {
Rectangle {
y: (parent.height - self.height) / 2;
width: 1px;
height: 15px;
background: gray;
}
auto-btn := LinkText {
width: self.preferred-width + 10px;
horizontal-alignment: center;
vertical-alignment: center;
font-size: 10.5px;
text-color: auto-checked ? white : #909090;
text: "AUTO";
clicked => {
root.auto-toggled(!auto-checked);
}
}
}
}
}
}
scroll-event(event) => {
if event.delta-y > 0 {
if can-increase {
disable-update = false;
increase-triggered();
}
return accept;
} else if event.delta-y < 0 {
if can-decrease {
disable-update = false;
decrease-triggered();
}
return accept;
}
reject
}
}
if ta.has-hover && (!ta.pressed) && (!tooltip.is-empty) && (!auto-hovered): tt := ToolTip {
x: (parent.width - self.width) / 2;
y: parent.height + 5px;
text: tooltip;
}
}