// Copyright © SixtyFPS GmbH // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 import { ListView } from "../common/listview.slint"; import { StateLayer } from "components.slint"; import { MaterialPalette, Icons } from "styling.slint"; component TableViewColumn inherits Rectangle { in property sort-order: SortOrder.unsorted; callback clicked <=> state-layer.clicked; callback adjust-size(/* size */ length); state-layer := StateLayer { background: MaterialPalette.accent-background; checked-background: MaterialPalette.alternate-background; ripple-color: MaterialPalette.accent-ripple; has-ripple: true; } HorizontalLayout { padding-left: 16px; padding-right: 16px; spacing: 4px; HorizontalLayout { @children } Image { visible: root.sort-order != SortOrder.unsorted; width: 12px; height: 12px; y: (parent.height - self.height) / 2; source: root.sort-order == SortOrder.ascending ? Icons.arrow-upward : Icons.arrow-downward; colorize: MaterialPalette.control-foreground; accessible-role: none; } } // border Rectangle { y: parent.height - self.height; width: 100%; height: 1px; background: MaterialPalette.border; } Rectangle { x: parent.width - 1px; width: 1px; background: movable-touch-area.has-hover ? MaterialPalette.border : transparent; animate background { duration: 250ms; } movable-touch-area := TouchArea { width: 10px; moved => { if (self.pressed) { adjust_size(self.mouse-x - self.pressed-x); } } mouse-cursor: ew-resize; } } } component TableViewCell inherits Rectangle { clip: true; HorizontalLayout { padding-left: 16px; padding-right: 16px; padding-top: 12px; padding-bottom: 12px; @children } // border Rectangle { y: parent.height - self.height; width: 100%; height: 1px; background: MaterialPalette.border; } } component TableViewRow inherits Rectangle { in property selected; out property mouse-x <=> state-layer.mouse-x; out property mouse-y <=> state-layer.mouse-y; callback clicked <=> state-layer.clicked; callback pointer-event <=> state-layer.pointer-event; min-height: max(42px, layout.min-height); state-layer := StateLayer { checked: root.selected; background: MaterialPalette.accent-background; checked-background: MaterialPalette.control-background; ripple-color: MaterialPalette.accent-ripple; has-ripple: true; } layout := HorizontalLayout { @children } } export component StandardTableView { private property item-height: scroll-view.viewport-height / rows.length; private property current-item-y: scroll-view.viewport-y + current-row * item-height; private property min-header-height: 42px; in property <[[StandardListViewItem]]> rows; out property current-sort-column: -1; in-out property <[TableColumn]> columns; in-out property current-row: -1; in property enabled <=> scroll-view.enabled; out property visible-width <=> scroll-view.visible-width; out property visible-height <=> scroll-view.visible-height; in-out property has-focus <=> scroll-view.has-focus; in-out property viewport-width <=> scroll-view.viewport-width; in-out property viewport-height <=> scroll-view.viewport-height; in-out property viewport-x <=> scroll-view.viewport-x; in-out property viewport-y <=> scroll-view.viewport-y; in property vertical-scrollbar-policy <=> scroll-view.vertical-scrollbar-policy; in property horizontal-scrollbar-policy <=> scroll-view.horizontal-scrollbar-policy; callback sort-ascending(column: int); callback sort-descending(column: int); callback row-pointer-event(row: int, event: PointerEvent, position: Point); callback current-row-changed(current-row: int); public function set-current-row(index: int) { if(index < 0 || index >= rows.length) { return; } current-row = index; current-row-changed(current-row); if(current-item-y < 0) { scroll-view.viewport-y += 0 - current-item-y; } if(current-item-y + item-height > scroll-view.visible-height) { scroll-view.viewport-y -= current-item-y + item-height - scroll-view.visible-height; } } 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; } min-width: 400px; min-height: 200px; horizontal-stretch: 1; vertical-stretch: 1; forward-focus: focus-scope; accessible-role: table; VerticalLayout { Rectangle { clip: true; vertical-stretch: 0; min-height: header-layout.min-height; header-layout := HorizontalLayout { padding-right: 20px; min-height: root.min-header-height; vertical-stretch: 0; 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: 900; overflow: elide; } clicked => { root.sort(index); } adjust-size(diff) => { column.width = max(1px, self.width + diff); } } } } scroll-view := ListView { for row[idx] in root.rows : TableViewRow { selected: idx == root.current-row; clicked => { root.focus(); root.set-current-row(idx); } pointer-event(pe) => { root.row-pointer-event(idx, 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, }); } for cell[index] in row : TableViewCell { private property 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; } } } } } } focus-scope := FocusScope { x: 0; width: 0; // Do not react on clicks 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 } } }