mirror of
https://github.com/slint-ui/slint.git
synced 2025-09-28 12:54:45 +00:00
71 lines
No EOL
2.3 KiB
Text
71 lines
No EOL
2.3 KiB
Text
// Copyright © SixtyFPS GmbH <info@slint.dev>
|
|
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
|
|
|
|
import { ScrollView } from "std-widgets-impl.slint";
|
|
|
|
export component ListView inherits ScrollView {
|
|
@children
|
|
}
|
|
|
|
component StandardListViewBase inherits ListView {
|
|
in property <[StandardListViewItem]> model;
|
|
in-out property <int> current-item: -1;
|
|
|
|
callback current-item-changed(/* current-item */ int);
|
|
callback item-pointer-event(/* item-index */int, /* event */ PointerEvent, /* absolute mouse position */ Point);
|
|
|
|
public function set-current-item(index: int) {
|
|
if(index < 0 || index >= model.length) {
|
|
return;
|
|
}
|
|
|
|
root.current-item = index;
|
|
root.current-item-changed(current-item);
|
|
|
|
if(current-item-y < 0) {
|
|
self.viewport-y += 0 - current-item-y;
|
|
}
|
|
|
|
if(current-item-y + item-height > self.visible-height) {
|
|
self.viewport-y -= current-item-y + item-height - self.visible-height;
|
|
}
|
|
}
|
|
|
|
private property <length> item-height: self.viewport-height / self.model.length;
|
|
private property <length> current-item-y: self.viewport-y + current-item * item-height;
|
|
|
|
for item[i] in root.model : NativeStandardListViewItem {
|
|
item: item;
|
|
index: i;
|
|
is-selected: root.current-item == i;
|
|
has-hover: i-touch-area.has-hover;
|
|
|
|
i-touch-area := TouchArea {
|
|
clicked => {
|
|
set-current-item(i);
|
|
}
|
|
|
|
pointer-event(pe) => {
|
|
root.item-pointer-event(i, pe, {
|
|
x: self.absolute-position.x + self.mouse-x - root.absolute-position.x,
|
|
y: self.absolute-position.y + self.mouse-y - root.absolute-position.y,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export component StandardListView inherits StandardListViewBase {
|
|
FocusScope {
|
|
key-pressed(event) => {
|
|
if (event.text == Key.UpArrow) {
|
|
root.set-current-item(root.current-item - 1);
|
|
return accept;
|
|
} else if (event.text == Key.DownArrow) {
|
|
root.set-current-item(root.current-item + 1);
|
|
return accept;
|
|
}
|
|
reject
|
|
}
|
|
}
|
|
} |