FluTabView.qml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtQuick.Layouts
  4. import FluentUI
  5. Item {
  6. enum TabWidthBehavior {
  7. Equal,
  8. SizeToContent,
  9. Compact
  10. }
  11. enum CloseButtonVisibility{
  12. Nerver,
  13. Always,
  14. OnHover
  15. }
  16. property int tabWidthBehavior : FluTabView.Equal
  17. property int closeButtonVisibility : FluTabView.Always
  18. property int itemWidth: 146
  19. property bool addButtonVisibility: true
  20. signal newPressed
  21. id:control
  22. implicitHeight: height
  23. implicitWidth: width
  24. anchors.fill: {
  25. if(parent)
  26. return parent
  27. return undefined
  28. }
  29. QtObject {
  30. id: d
  31. property int dragIndex: -1
  32. property bool dragBehavior: false
  33. property bool itemPress: false
  34. property int maxEqualWidth: 240
  35. }
  36. MouseArea{
  37. anchors.fill: parent
  38. preventStealing: true
  39. }
  40. ListModel{
  41. id:tab_model
  42. }
  43. FluIconButton{
  44. id:btn_new
  45. visible: addButtonVisibility
  46. width: 34
  47. height: 34
  48. x:Math.min(tab_nav.contentWidth,tab_nav.width)
  49. anchors.top: parent.top
  50. iconSource: FluentIcons.Add
  51. onClicked: {
  52. newPressed()
  53. }
  54. }
  55. ListView{
  56. id:tab_nav
  57. height: 34
  58. orientation: ListView.Horizontal
  59. anchors{
  60. top: parent.top
  61. left: parent.left
  62. right: parent.right
  63. rightMargin: 34
  64. }
  65. interactive: false
  66. model: tab_model
  67. move: Transition {
  68. NumberAnimation { properties: "x"; duration: 100; easing.type: Easing.OutCubic }
  69. NumberAnimation { properties: "y"; duration: 100; easing.type: Easing.OutCubic }
  70. }
  71. moveDisplaced: Transition {
  72. NumberAnimation { properties: "x"; duration: 300; easing.type: Easing.OutCubic}
  73. NumberAnimation { properties: "y"; duration: 100; easing.type: Easing.OutCubic }
  74. }
  75. clip: true
  76. ScrollBar.horizontal: ScrollBar{
  77. id: scroll_nav
  78. policy: ScrollBar.AlwaysOff
  79. }
  80. delegate: Item{
  81. width: item_layout.width
  82. height: item_container.height
  83. z: item_mouse_drag.pressed ? 1000 : 1
  84. Item{
  85. id:item_layout
  86. width: item_container.width
  87. height: item_container.height
  88. FluItem{
  89. id:item_container
  90. property real timestamp: new Date().getTime()
  91. height: tab_nav.height
  92. width: {
  93. if(tabWidthBehavior === FluTabView.Equal){
  94. return Math.max(Math.min(d.maxEqualWidth,tab_nav.width/tab_nav.count),41 + item_btn_close.width)
  95. }
  96. if(tabWidthBehavior === FluTabView.SizeToContent){
  97. return itemWidth
  98. }
  99. if(tabWidthBehavior === FluTabView.Compact){
  100. return item_mouse_hove.containsMouse || item_btn_close.hovered || tab_nav.currentIndex === index ? itemWidth : 41 + item_btn_close.width
  101. }
  102. return Math.max(Math.min(d.maxEqualWidth,tab_nav.width/tab_nav.count),41 + item_btn_close.width)
  103. }
  104. radius: [6,6,0,0]
  105. Behavior on x { enabled: d.dragBehavior; NumberAnimation { duration: 200 } }
  106. Behavior on y { enabled: d.dragBehavior; NumberAnimation { duration: 200 } }
  107. MouseArea{
  108. id:item_mouse_hove
  109. anchors.fill: parent
  110. hoverEnabled: true
  111. }
  112. MouseArea{
  113. id:item_mouse_drag
  114. anchors.fill: parent
  115. drag.target: item_container
  116. drag.axis: Drag.XAxis
  117. onWheel: (wheel)=>{
  118. if (wheel.angleDelta.y > 0) scroll_nav.decrease()
  119. else scroll_nav.increase()
  120. }
  121. onPressed: {
  122. d.itemPress = true
  123. item_container.timestamp = new Date().getTime();
  124. d.dragBehavior = false;
  125. var pos = tab_nav.mapFromItem(item_container, 0, 0)
  126. d.dragIndex = model.index
  127. item_container.parent = tab_nav
  128. item_container.x = pos.x
  129. item_container.y = pos.y
  130. }
  131. onReleased: {
  132. d.itemPress = false
  133. timer.stop()
  134. var timeDiff = new Date().getTime() - item_container.timestamp
  135. if (timeDiff < 300) {
  136. tab_nav.currentIndex = index
  137. }
  138. d.dragIndex = -1;
  139. var pos = tab_nav.mapToItem(item_layout, item_container.x, item_container.y)
  140. item_container.parent = item_layout;
  141. item_container.x = pos.x;
  142. item_container.y = pos.y;
  143. d.dragBehavior = true;
  144. item_container.x = 0;
  145. item_container.y = 0;
  146. }
  147. onPositionChanged: {
  148. var pos = tab_nav.mapFromItem(item_container, 0, 0)
  149. updatePosition(pos)
  150. if(pos.x<0){
  151. timer.isIncrease = false
  152. timer.restart()
  153. }else if(pos.x>tab_nav.width-itemWidth){
  154. timer.isIncrease = true
  155. timer.restart()
  156. }else{
  157. timer.stop()
  158. }
  159. }
  160. Timer{
  161. id:timer
  162. property bool isIncrease: true
  163. interval: 10
  164. repeat: true
  165. onTriggered: {
  166. if(isIncrease){
  167. if(tab_nav.contentX>=tab_nav.contentWidth-tab_nav.width){
  168. return
  169. }
  170. tab_nav.contentX = tab_nav.contentX+2
  171. }else{
  172. if(tab_nav.contentX<=0){
  173. return
  174. }
  175. tab_nav.contentX = tab_nav.contentX-2
  176. }
  177. item_mouse_drag.updatePosition(tab_nav.mapFromItem(item_container, 0, 0))
  178. }
  179. }
  180. function updatePosition(pos){
  181. var idx = tab_nav.indexAt(pos.x+tab_nav.contentX+1, pos.y)
  182. var firstIdx = tab_nav.indexAt(tab_nav.contentX+1, pos.y)
  183. var lastIdx = tab_nav.indexAt(tab_nav.width+tab_nav.contentX-1, pos.y)
  184. if(lastIdx === -1){
  185. lastIdx = tab_nav.count-1
  186. }
  187. if (idx!==-1 && idx >= firstIdx && idx <= lastIdx && d.dragIndex !== idx) {
  188. tab_model.move(d.dragIndex, idx, 1)
  189. d.dragIndex = idx;
  190. }
  191. }
  192. }
  193. Rectangle{
  194. anchors.fill: parent
  195. color: {
  196. if(FluTheme.dark){
  197. if(item_mouse_hove.containsMouse || item_btn_close.hovered){
  198. return Qt.rgba(1,1,1,0.03)
  199. }
  200. if(tab_nav.currentIndex === index){
  201. return Qt.rgba(1,1,1,0.06)
  202. }
  203. return Qt.rgba(0,0,0,0)
  204. }else{
  205. if(item_mouse_hove.containsMouse || item_btn_close.hovered){
  206. return Qt.rgba(0,0,0,0.03)
  207. }
  208. if(tab_nav.currentIndex === index){
  209. return Qt.rgba(0,0,0,0.06)
  210. }
  211. return Qt.rgba(0,0,0,0)
  212. }
  213. }
  214. }
  215. RowLayout{
  216. spacing: 0
  217. height: parent.height
  218. Image{
  219. source:model.icon
  220. Layout.leftMargin: 10
  221. Layout.preferredWidth: 14
  222. Layout.preferredHeight: 14
  223. Layout.alignment: Qt.AlignVCenter
  224. }
  225. FluText{
  226. id:item_text
  227. text: model.text
  228. Layout.leftMargin: 10
  229. visible: {
  230. if(tabWidthBehavior === FluTabView.Equal){
  231. return true
  232. }
  233. if(tabWidthBehavior === FluTabView.SizeToContent){
  234. return true
  235. }
  236. if(tabWidthBehavior === FluTabView.Compact){
  237. return item_mouse_hove.containsMouse || item_btn_close.hovered || tab_nav.currentIndex === index
  238. }
  239. return false
  240. }
  241. Layout.preferredWidth: visible?item_container.width - 41 - item_btn_close.width:0
  242. elide: Text.ElideRight
  243. Layout.alignment: Qt.AlignVCenter
  244. FluTooltip{
  245. visible: item_mouse_hove.containsMouse
  246. text:item_text.text
  247. delay: 1000
  248. }
  249. }
  250. }
  251. FluIconButton{
  252. id:item_btn_close
  253. iconSource: FluentIcons.ChromeClose
  254. iconSize: 10
  255. width: visible ? 24 : 0
  256. height: 24
  257. visible: {
  258. if(closeButtonVisibility === FluTabView.Nerver)
  259. return false
  260. if(closeButtonVisibility === FluTabView.OnHover)
  261. return item_mouse_hove.containsMouse || item_btn_close.hovered
  262. return true
  263. }
  264. anchors{
  265. right: parent.right
  266. rightMargin: 5
  267. verticalCenter: parent.verticalCenter
  268. }
  269. onClicked: {
  270. tab_model.remove(index)
  271. }
  272. }
  273. FluDivider{
  274. width: 1
  275. height: 16
  276. anchors{
  277. verticalCenter: parent.verticalCenter
  278. right: parent.right
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. Item{
  286. id:container
  287. anchors{
  288. top: tab_nav.bottom
  289. left: parent.left
  290. right: parent.right
  291. bottom: parent.bottom
  292. }
  293. Repeater{
  294. model:tab_model
  295. Loader{
  296. property var argument: model.argument
  297. anchors.fill: parent
  298. sourceComponent: model.page
  299. visible: tab_nav.currentIndex === index
  300. }
  301. }
  302. }
  303. function createTab(icon,text,page,argument={}){
  304. return {icon:icon,text:text,page:page,argument:argument}
  305. }
  306. function appendTab(icon,text,page,argument){
  307. tab_model.append(createTab(icon,text,page,argument))
  308. }
  309. function setTabList(list){
  310. tab_model.clear()
  311. tab_model.append(list)
  312. }
  313. function count(){
  314. return tab_nav.count
  315. }
  316. }