slint/internal/compiler/widgets/material-base/widget-scrollview.slint
Olivier Goffart 1089d29ebb Make Empty not fill the parent by default
Unless `preferred-*:100%` is set

Part of #2284
2023-03-24 11:15:05 +01:00

129 lines
No EOL
4.1 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
import { md } from "md.slint";
component ScrollBar inherits Rectangle {
in-out property <bool> horizontal;
in-out property<length> maximum;
in-out property<length> page-size;
// this is always negative and bigger than -maximum
in-out property<length> value;
in-out property<bool> enabled <=> touch.enabled;
state_layer := Rectangle {
width: 100%;
height: 100%;
background: md.sys.color.primary;
border-radius: 4px;
opacity: 0;
visible: handle.width > 0 && handle.height > 0;
animate opacity { duration: 250ms; easing: ease; }
}
handle := Rectangle {
x: !root.horizontal ? 0phx : (root.width - handle.width) * (-root.value / root.maximum);
y: root.horizontal ? 0phx : (root.height - handle.height) * (-root.value / root.maximum);
width: !root.horizontal ? parent.width : root.maximum <= 0phx ? 0phx : max(32px, parent.width * max(root.page-size / (root.maximum + root.page-size)));
height: root.horizontal ? parent.height : root.maximum <= 0phx ? 0phx : max(32px, parent.height * (root.page-size / (root.maximum + root.page-size)));
container := Rectangle {
width: 100%;
height: 100%;
border-radius: 4px;
border-color: md.sys.color.outline;
border-width: 1px;
}
}
touch := TouchArea {
property <length> pressed-value;
width: parent.width;
height: parent.height;
pointer-event(event) => {
if (event.button == PointerEventButton.left && event.kind == PointerEventKind.down) {
self.pressed-value = -root.value;
}
}
moved => {
if (self.enabled && self.pressed) {
root.value = -max(0px, min(root.maximum, self.pressed-value + (
root.horizontal ? (touch.mouse-x - touch.pressed-x) * (root.maximum / (root.width - handle.width))
: (touch.mouse-y - touch.pressed-y) * (root.maximum / (root.height - handle.height))
)));
}
}
}
states [
disabled when !touch.enabled : {
container.border-color: md.sys.color.on-surface;
handle.opacity: 0.12;
}
hover when touch.has-hover : {
state-layer.opacity: 0.08;
}
pressed when touch.has-hover : {
state-layer.opacity: 0.12;
}
]
}
// Scrollview contains a viewport that is bigger than the view and can be scrolled.
export component ScrollView {
in-out property <length> viewport-width <=> fli.viewport-width;
in-out property <length> viewport-height <=> fli.viewport-height;
in-out property <length> viewport-x <=> fli.viewport-x;
in-out property <length> viewport-y <=> fli.viewport-y;
out property <length> visible-width <=> fli.width;
out property <length> visible-height <=> fli.height;
in property <bool> enabled: true;
in-out property <bool> has-focus;
min-height: 50px;
min-width: 50px;
horizontal-stretch: 1;
vertical-stretch: 1;
preferred-height: 100%;
preferred-width: 100%;
Rectangle {
background: md.sys.color.surface;
}
fli := Flickable {
x:0;y:0;
interactive: false;
viewport-y <=> vbar.value;
viewport-x <=> hbar.value;
width: parent.width - vbar.width - 4px;
height: parent.height - hbar.height - 4px;
@children
}
vbar := ScrollBar {
width: 8px;
x: fli.width + fli.x;
y: fli.y;
height: fli.height;
horizontal: false;
maximum: fli.viewport-height - fli.height;
page-size: fli.height;
enabled: root.enabled;
}
hbar := ScrollBar {
height: 8px;
y: fli.height + fli.y;
x: fli.x;
width: fli.width;
horizontal: true;
maximum: fli.viewport-width - fli.width;
page-size: fli.width;
enabled: root.enabled;
}
}