mirror of
https://github.com/slint-ui/slint.git
synced 2025-09-11 04:46:24 +00:00
277 lines
No EOL
8.4 KiB
Text
277 lines
No EOL
8.4 KiB
Text
// Copyright © SixtyFPS GmbH <info@slint.dev>
|
|
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.0 OR LicenseRef-Slint-commercial
|
|
|
|
import { Palette, Typography, Icons } from "styling.slint";
|
|
import { ScrollView } from "scrollview.slint";
|
|
|
|
component TableViewColumn inherits Rectangle {
|
|
callback clicked <=> i-touch-area.clicked;
|
|
callback adjust_size(length);
|
|
|
|
in property <SortOrder> sort-order: SortOrder.unsorted;
|
|
|
|
background: Palette.background;
|
|
|
|
i-touch-area := TouchArea {
|
|
width: parent.width - 11px;
|
|
}
|
|
|
|
HorizontalLayout {
|
|
padding-left: 12px;
|
|
padding-right: 12px;
|
|
spacing: 2px;
|
|
|
|
HorizontalLayout {
|
|
@children
|
|
}
|
|
|
|
i-icon := Image {
|
|
image-fit: contain;
|
|
colorize: Palette.text-secondary;
|
|
visible: root.sort-order != SortOrder.unsorted;
|
|
width: 12px;
|
|
y: (parent.height - self.height) / 2;
|
|
source: root.sort-order == SortOrder.ascending ? Icons.arrow-down : Icons.arrow-up;
|
|
|
|
animate colorize { duration: 150ms; }
|
|
}
|
|
}
|
|
|
|
// border
|
|
Rectangle {
|
|
y: parent.height - self.height;
|
|
width: 100%;
|
|
height: 1px;
|
|
background: Palette.divider;
|
|
}
|
|
|
|
Rectangle {
|
|
x: parent.width - 1px;
|
|
width: 1px;
|
|
|
|
i-movable-touch-area := TouchArea {
|
|
width: 10px;
|
|
mouse-cursor: ew-resize;
|
|
|
|
moved => {
|
|
if (self.pressed) {
|
|
adjust_size(self.mouse-x - self.pressed-x);
|
|
}
|
|
}
|
|
}
|
|
|
|
animate background { duration: 150ms; }
|
|
|
|
states [
|
|
hover when i-movable-touch-area.has-hover : {
|
|
background: Palette.control-secondary;
|
|
}
|
|
]
|
|
}
|
|
|
|
states [
|
|
pressed when i-touch-area.pressed : {
|
|
background: Palette.control-secondary;
|
|
}
|
|
hover when i-touch-area.has-hover : {
|
|
background: Palette.sub-title-tertiary;
|
|
}
|
|
]
|
|
}
|
|
|
|
component TableViewCell inherits Rectangle {
|
|
clip: true;
|
|
|
|
HorizontalLayout {
|
|
padding-left: 12px;
|
|
padding-right: 12px;
|
|
|
|
@children
|
|
}
|
|
}
|
|
|
|
component TableViewRow inherits Rectangle {
|
|
callback clicked <=> i-touch-area.clicked;
|
|
|
|
in property<bool> selected;
|
|
in property <bool> even;
|
|
|
|
min-width: i-layout.min-width;
|
|
min-height: max(34px, i-layout.min-height);
|
|
border-radius: 4px;
|
|
background: root.even ? Palette.control-default : transparent;
|
|
|
|
i-touch-area := TouchArea {}
|
|
|
|
i-layout := HorizontalLayout {
|
|
@children
|
|
}
|
|
|
|
i-selector := Rectangle {
|
|
x: 0px;
|
|
y: (parent.height - self.height) / 2;
|
|
width: 3px;
|
|
height: 0px;
|
|
background: Palette.accent-default;
|
|
border-radius: 2px;
|
|
|
|
animate height { duration: 150ms; easing: ease-out; }
|
|
}
|
|
|
|
states [
|
|
pressed when i-touch-area.pressed : {
|
|
root.background: selected ? Palette.subtle-secondary : Palette.subtle-tertiary;
|
|
}
|
|
hover when i-touch-area.has-hover : {
|
|
root.background: selected ? Palette.subtle-tertiary : Palette.subtle-secondary;
|
|
i-selector.height: root.selected ? 16px : 0;
|
|
}
|
|
selected when root.selected : {
|
|
root.background: Palette.subtle-secondary;
|
|
i-selector.height: 16px;
|
|
}
|
|
]
|
|
}
|
|
|
|
export component StandardTableView {
|
|
private property <length> min-header-height: 42px;
|
|
private property <length> item-height: i-scroll-view.viewport-height / rows.length;
|
|
private property <length> current-item-y: i-scroll-view.viewport-y + current-row * item-height;
|
|
|
|
callback sort-ascending(int /* column-index */);
|
|
callback sort-descending(int /* column-index */);
|
|
|
|
in property <[[StandardListViewItem]]> rows;
|
|
out property <int> current-sort-column: -1;
|
|
in-out property <[TableColumn]> columns;
|
|
in-out property <int> current-row: -1;
|
|
|
|
min-width: 400px;
|
|
min-height: 200px;
|
|
horizontal-stretch: 1;
|
|
vertical-stretch: 1;
|
|
|
|
VerticalLayout {
|
|
Rectangle {
|
|
clip: true;
|
|
vertical-stretch: 0;
|
|
min-height: i-header-layout.min-height;
|
|
|
|
i-header-layout := HorizontalLayout {
|
|
width: max(self.preferred-width, parent.width);
|
|
x: i-scroll-view.viewport-x;
|
|
padding-right: 6px;
|
|
min-height: root.min-header-height;
|
|
|
|
for column[index] in root.columns : TableViewColumn {
|
|
sort-order: column.sort-order;
|
|
horizontal-stretch: column.horizontal-stretch;
|
|
min-width: max(column.min-width, column.width);
|
|
preferred-width: self.min-width;
|
|
max-width: (index < columns.length && column.width >= 1px) ? max(column.min-width, column.width) : 100000px;
|
|
|
|
Text {
|
|
vertical-alignment: center;
|
|
text: column.title;
|
|
font-weight: Typography.body.font-weight;
|
|
font-size: Typography.body.font-size;
|
|
color: Palette.text-secondary;
|
|
overflow: elide;
|
|
}
|
|
|
|
clicked => {
|
|
root.sort(index);
|
|
}
|
|
|
|
adjust-size(diff) => {
|
|
column.width = max(1px, self.width + diff);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
i-scroll-view := ScrollView {
|
|
vertical-stretch: 1;
|
|
|
|
VerticalLayout {
|
|
alignment: start;
|
|
|
|
for row[idx] in root.rows : TableViewRow {
|
|
selected: idx == root.current-row;
|
|
even: mod(idx, 2) == 0;
|
|
|
|
for cell[index] in row : TableViewCell {
|
|
private property <bool> has_inner_focus;
|
|
|
|
horizontal-stretch: root.columns[index].horizontal-stretch;
|
|
min-width: max(columns[index].min-width, columns[index].width);
|
|
preferred-width: self.min-width;
|
|
max-width: (index < columns.length && columns[index].width >= 1px) ? max(columns[index].min-width, columns[index].width) : 100000px;
|
|
|
|
Rectangle {
|
|
Text {
|
|
width: 100%;
|
|
height: 100%;
|
|
overflow: elide;
|
|
vertical-alignment: center;
|
|
text: cell.text;
|
|
font-weight: Typography.body.font-weight;
|
|
font-size: Typography.body.font-size;
|
|
color: mod(idx, 2) == 0 ? Palette.text-primary : Palette.text-secondary;
|
|
}
|
|
}
|
|
}
|
|
|
|
clicked => {
|
|
root.set-current-row(idx);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
FocusScope {
|
|
key-pressed(event) => {
|
|
if (event.text == Key.UpArrow) {
|
|
root.set-current-row(root.current-row - 1);
|
|
return accept;
|
|
} else if (event.text == Key.DownArrow) {
|
|
root.set-current-row(root.current-row + 1);
|
|
return accept;
|
|
}
|
|
reject
|
|
}
|
|
}
|
|
|
|
function sort(index: int) {
|
|
if (root.current-sort-column != index) {
|
|
root.columns[root.current-sort-column].sort-order = SortOrder.unsorted;
|
|
}
|
|
|
|
if(root.columns[index].sort-order == SortOrder.ascending) {
|
|
root.columns[index].sort-order = SortOrder.descending;
|
|
root.sort-descending(index);
|
|
} else {
|
|
root.columns[index].sort-order = SortOrder.ascending;
|
|
root.sort-ascending(index);
|
|
}
|
|
|
|
root.current-sort-column = index;
|
|
}
|
|
|
|
public function set-current-row(index: int) {
|
|
if (index < 0 || index >= rows.length) {
|
|
return;
|
|
}
|
|
|
|
current-row = index;
|
|
|
|
if (current-item-y < 0) {
|
|
i-scroll-view.viewport-y += 0 - current-item-y;
|
|
}
|
|
|
|
if (current-item-y + item-height > i-scroll-view.visible-height) {
|
|
i-scroll-view.viewport-y -= current-item-y + item-height - i-scroll-view.visible-height;
|
|
}
|
|
}
|
|
} |