FluentUI/src/controls/FluAutoSuggestBox.qml

278 lines
9.0 KiB
QML
Raw Normal View History

2023-03-24 20:44:38 +08:00
import QtQuick
import QtQuick.Controls
import FluentUI
2023-03-09 15:49:37 +08:00
TextField{
2023-03-09 23:11:59 +08:00
property var values:[]
property int fontStyle: FluText.Body
property int pixelSize : FluTheme.textSize
2023-03-13 18:23:46 +08:00
property int iconSource: 0
2023-03-20 18:22:32 +08:00
property bool disabled: false
2023-03-11 20:33:29 +08:00
signal itemClicked(string data)
2023-03-09 23:11:59 +08:00
2023-03-09 15:49:37 +08:00
id:input
width: 300
2023-03-20 18:22:32 +08:00
enabled: !disabled
color: {
if(disabled){
return FluTheme.isDark ? Qt.rgba(131/255,131/255,131/255,1) : Qt.rgba(160/255,160/255,160/255,1)
}
return FluTheme.isDark ? Qt.rgba(255/255,255/255,255/255,1) : Qt.rgba(27/255,27/255,27/255,1)
}
2023-03-09 15:49:37 +08:00
selectionColor: {
if(FluTheme.isDark){
return FluTheme.primaryColor.lighter
}else{
return FluTheme.primaryColor.dark
}
}
2023-03-11 21:15:36 +08:00
renderType: FluTheme.isNativeText ? Text.NativeRendering : Text.QtRendering
2023-03-09 23:11:59 +08:00
placeholderTextColor: {
2023-03-20 18:22:32 +08:00
if(disabled){
return FluTheme.isDark ? Qt.rgba(131/255,131/255,131/255,1) : Qt.rgba(160/255,160/255,160/255,1)
}
2023-03-09 23:11:59 +08:00
if(focus){
return FluTheme.isDark ? Qt.rgba(152/255,152/255,152/255,1) : Qt.rgba(141/255,141/255,141/255,1)
}
return FluTheme.isDark ? Qt.rgba(210/255,210/255,210/255,1) : Qt.rgba(96/255,96/255,96/255,1)
}
2023-03-11 20:33:29 +08:00
rightPadding: icon_right.visible ? 50 : 30
2023-03-09 15:49:37 +08:00
selectByMouse: true
2023-03-12 14:26:03 +08:00
Keys.onUpPressed: {
list_view.currentIndex = Math.max(list_view.currentIndex-1,0)
}
Keys.onDownPressed: {
list_view.currentIndex = Math.min(list_view.currentIndex+1,list_view.count-1)
}
signal handleClicked
Keys.onEnterPressed:handleClicked()
Keys.onReturnPressed:handleClicked()
2023-03-09 23:11:59 +08:00
font.bold: {
switch (fontStyle) {
case FluText.Display:
return true
case FluText.TitleLarge:
return true
case FluText.Title:
return true
case FluText.Subtitle:
return true
case FluText.BodyLarge:
return false
case FluText.BodyStrong:
return true
case FluText.Body:
return false
case FluText.Caption:
return false
default:
return false
}
}
font.pixelSize: {
switch (fontStyle) {
case FluText.Display:
return input.pixelSize * 4
case FluText.TitleLarge:
return input.pixelSize * 2
case FluText.Title:
return input.pixelSize * 1.5
case FluText.Subtitle:
return input.pixelSize * 0.9
case FluText.BodyLarge:
return input.pixelSize * 1.1
case FluText.BodyStrong:
return input.pixelSize * 1.0
case FluText.Body:
return input.pixelSize * 1.0
case FluText.Caption:
return input.pixelSize * 0.8
default:
return input.pixelSize * 1.0
}
}
2023-03-09 15:49:37 +08:00
background: FluTextBoxBackground{
inputItem: input
FluIconButton{
2023-03-12 21:49:11 +08:00
iconSource:FluentIcons.ChromeClose
2023-03-11 14:43:07 +08:00
iconSize: 10
2023-03-09 15:49:37 +08:00
width: 20
height: 20
opacity: 0.5
visible: input.text !== ""
anchors{
verticalCenter: parent.verticalCenter
right: parent.right
2023-03-11 20:33:29 +08:00
rightMargin: icon_right.visible ? 25 : 5
2023-03-09 15:49:37 +08:00
}
onClicked:{
input.text = ""
}
}
2023-03-11 20:33:29 +08:00
FluIcon{
id:icon_right
2023-03-12 21:49:11 +08:00
iconSource: input.iconSource
2023-03-11 20:33:29 +08:00
iconSize: 15
opacity: 0.5
2023-03-13 18:23:46 +08:00
visible: input.iconSource != 0
2023-03-11 20:33:29 +08:00
anchors{
verticalCenter: parent.verticalCenter
right: parent.right
rightMargin: 5
}
}
2023-03-09 15:49:37 +08:00
}
Component.onCompleted: {
searchData()
}
Popup{
id:input_popup
visible: input.focus
y:input.height
onClosed: {
input.focus = false
}
2023-03-12 14:26:03 +08:00
onVisibleChanged: {
if(visible){
list_view.currentIndex = -1
}
}
2023-03-09 15:49:37 +08:00
background: Rectangle{
width: input.width
radius: 4
FluShadow{
radius: 4
}
2023-03-11 14:43:07 +08:00
color: FluTheme.isDark ? Qt.rgba(51/255,48/255,48/255,1) : Qt.rgba(248/255,250/255,253/255,1)
2023-03-09 15:49:37 +08:00
height: 38*Math.min(Math.max(list_view.count,1),8)
ListView{
id:list_view
signal closePopup
anchors.fill: parent
boundsBehavior: ListView.StopAtBounds
clip: true
2023-03-12 14:26:03 +08:00
currentIndex: -1
2023-03-14 18:23:12 +08:00
ScrollBar.vertical: FluScrollBar {}
2023-03-09 15:49:37 +08:00
header: Item{
width: input.width
height: visible ? 38 : 0
visible: list_view.count === 0
FluText{
text:"没有找到结果"
anchors{
verticalCenter: parent.verticalCenter
left: parent.left
2023-03-09 23:11:59 +08:00
leftMargin: 10
2023-03-09 15:49:37 +08:00
}
}
}
2023-03-12 14:26:03 +08:00
delegate:Control{
2023-03-09 15:49:37 +08:00
width: input.width
2023-03-12 14:26:03 +08:00
padding:10
background: Rectangle{
2023-03-09 15:49:37 +08:00
color: {
2023-03-12 14:26:03 +08:00
if(list_view.currentIndex === index){
return FluTheme.isDark ? Qt.rgba(63/255,60/255,61/255,1) : Qt.rgba(237/255,237/255,242/255,1)
}
if(hovered){
2023-03-11 14:43:07 +08:00
return FluTheme.isDark ? Qt.rgba(63/255,60/255,61/255,1) : Qt.rgba(237/255,237/255,242/255,1)
2023-03-09 15:49:37 +08:00
}
2023-03-11 14:43:07 +08:00
return FluTheme.isDark ? Qt.rgba(51/255,48/255,48/255,1) : Qt.rgba(0,0,0,0)
2023-03-09 15:49:37 +08:00
}
MouseArea{
2023-03-12 14:26:03 +08:00
id:mouse_area
2023-03-09 15:49:37 +08:00
anchors.fill: parent
2023-03-12 14:26:03 +08:00
Connections{
target: input
function onHandleClicked(){
if((list_view.currentIndex === index)){
mouse_area.handleClick()
}
}
}
onClicked: handleClick()
function handleClick(){
2023-03-09 15:49:37 +08:00
input_popup.close()
2023-03-11 20:33:29 +08:00
input.itemClicked(modelData)
2023-03-09 15:49:37 +08:00
input.text = modelData
}
}
2023-03-12 14:26:03 +08:00
Rectangle{
width: 3
color:FluTheme.primaryColor.dark
visible: list_view.currentIndex === index
radius: 3
height: 20
2023-03-09 15:49:37 +08:00
anchors{
left: parent.left
2023-03-12 14:26:03 +08:00
verticalCenter: parent.verticalCenter
2023-03-09 15:49:37 +08:00
}
}
}
2023-03-12 14:26:03 +08:00
contentItem: FluText{
text:modelData
anchors{
verticalCenter: parent.verticalCenter
}
}
2023-03-09 15:49:37 +08:00
}
2023-03-12 14:26:03 +08:00
// Item{
// height: 38
// width: input.width
// Rectangle{
// anchors.fill: parent
// anchors.topMargin: 2
// anchors.bottomMargin: 2
// anchors.leftMargin: 5
// anchors.rightMargin: 5
// radius: 3
// MouseArea{
// id:item_mouse
// anchors.fill: parent
// hoverEnabled: true
// onClicked: {
// input_popup.close()
// input.itemClicked(modelData)
// input.text = modelData
// }
// }
// }
// }
2023-03-09 15:49:37 +08:00
}
}
}
onTextChanged: {
searchData()
}
function searchData(){
var result = []
2023-03-11 20:33:29 +08:00
if(values==null){
list_view.model = result
return
}
2023-03-09 15:49:37 +08:00
values.map(function(item){
if(item.indexOf(input.text)!==-1){
result.push(item)
}
})
list_view.model = result
}
}