PatternSelectorPage.qml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. import QtQuick 2.15
  2. import QtQuick.Controls 2.15
  3. import QtQuick.Layouts 1.15
  4. import DuneWeaver 1.0
  5. import "../components"
  6. import "../components" as Components
  7. Page {
  8. id: page
  9. property var backend: null
  10. property var stackView: null
  11. property string playlistName: ""
  12. property var existingPatterns: [] // Raw pattern names already in playlist
  13. // Track patterns added in this session for immediate visual feedback
  14. property var sessionAddedPatterns: []
  15. // Local pattern model for this page
  16. PatternModel {
  17. id: patternModel
  18. }
  19. // Search state
  20. property bool searchExpanded: false
  21. property int patternCount: patternModel ? patternModel.rowCount() : 0
  22. // Update pattern count when model resets
  23. Connections {
  24. target: patternModel
  25. function onModelReset() {
  26. patternCount = patternModel.rowCount()
  27. }
  28. }
  29. // Check if a pattern is already in the playlist
  30. function isPatternInPlaylist(patternName) {
  31. if (existingPatterns.indexOf(patternName) !== -1) {
  32. return true
  33. }
  34. if (sessionAddedPatterns.indexOf(patternName) !== -1) {
  35. return true
  36. }
  37. return false
  38. }
  39. // Add pattern and track it for instant visual feedback
  40. function addPatternToPlaylist(patternName) {
  41. if (!isPatternInPlaylist(patternName) && backend) {
  42. backend.addPatternToPlaylist(playlistName, patternName)
  43. var temp = sessionAddedPatterns.slice()
  44. temp.push(patternName)
  45. sessionAddedPatterns = temp
  46. }
  47. }
  48. Rectangle {
  49. anchors.fill: parent
  50. color: Components.ThemeManager.backgroundColor
  51. }
  52. ColumnLayout {
  53. anchors.fill: parent
  54. spacing: 0
  55. // Header with back button + search
  56. Rectangle {
  57. Layout.fillWidth: true
  58. Layout.preferredHeight: Components.ThemeManager.headerHeight
  59. color: Components.ThemeManager.surfaceColor
  60. Rectangle {
  61. anchors.bottom: parent.bottom
  62. width: parent.width
  63. height: 1
  64. color: Components.ThemeManager.borderColor
  65. }
  66. RowLayout {
  67. anchors.fill: parent
  68. anchors.leftMargin: Components.ThemeManager.spaceSm
  69. anchors.rightMargin: Components.ThemeManager.spaceMd
  70. spacing: Components.ThemeManager.spaceSm
  71. // Back button
  72. Rectangle {
  73. Layout.preferredWidth: 44
  74. Layout.preferredHeight: 44
  75. radius: 22
  76. color: backArea.pressed ? Components.ThemeManager.pressedColor : "transparent"
  77. visible: !searchExpanded
  78. Components.Icon {
  79. anchors.centerIn: parent
  80. name: "arrow_back"
  81. size: 20
  82. color: Components.ThemeManager.textPrimary
  83. }
  84. MouseArea {
  85. id: backArea
  86. anchors.fill: parent
  87. onClicked: stackView.pop()
  88. }
  89. }
  90. Label {
  91. text: "Add to \"" + playlistName + "\""
  92. font.family: Components.ThemeManager.fontDisplay
  93. font.pixelSize: Components.ThemeManager.fontSizeTitle
  94. color: Components.ThemeManager.textPrimary
  95. Layout.fillWidth: true
  96. elide: Text.ElideRight
  97. visible: !searchExpanded
  98. }
  99. Label {
  100. text: patternCount + " patterns"
  101. font.family: Components.ThemeManager.fontBody
  102. font.pixelSize: Components.ThemeManager.fontSizeCaption
  103. color: Components.ThemeManager.textTertiary
  104. visible: !searchExpanded
  105. }
  106. // Expandable search pill (matching ModernPatternListPage)
  107. Rectangle {
  108. Layout.fillWidth: searchExpanded
  109. Layout.preferredWidth: searchExpanded ? parent.width - 60 : 130
  110. Layout.preferredHeight: 40
  111. radius: 20
  112. color: Components.ThemeManager.backgroundColor
  113. border.color: searchExpanded || searchField.hasUnappliedSearch
  114. ? Components.ThemeManager.accent
  115. : Components.ThemeManager.borderColor
  116. border.width: 1
  117. Behavior on Layout.preferredWidth {
  118. NumberAnimation { duration: 200 }
  119. }
  120. RowLayout {
  121. anchors.fill: parent
  122. anchors.leftMargin: Components.ThemeManager.spaceMd
  123. anchors.rightMargin: Components.ThemeManager.spaceMd
  124. spacing: Components.ThemeManager.spaceSm
  125. Components.Icon {
  126. name: "search"
  127. size: 17
  128. color: searchExpanded ? Components.ThemeManager.accent
  129. : Components.ThemeManager.textSecondary
  130. }
  131. TextField {
  132. id: searchField
  133. Layout.fillWidth: true
  134. placeholderText: searchExpanded ? "Search patterns, then press Enter" : "Search"
  135. placeholderTextColor: Components.ThemeManager.textTertiary
  136. font.family: Components.ThemeManager.fontBody
  137. font.pixelSize: Components.ThemeManager.fontSizeBody
  138. color: Components.ThemeManager.textPrimary
  139. visible: searchExpanded || text.length > 0
  140. property string lastSearchText: ""
  141. property bool hasUnappliedSearch: text !== lastSearchText && text.length > 0
  142. background: Rectangle {
  143. color: "transparent"
  144. }
  145. onAccepted: {
  146. patternModel.filter(text)
  147. lastSearchText = text
  148. Qt.inputMethod.hide()
  149. focus = false
  150. }
  151. activeFocusOnPress: true
  152. selectByMouse: true
  153. inputMethodHints: Qt.ImhNoPredictiveText
  154. MouseArea {
  155. anchors.fill: parent
  156. onPressed: {
  157. searchField.forceActiveFocus()
  158. Qt.inputMethod.show()
  159. mouse.accepted = false
  160. }
  161. }
  162. onActiveFocusChanged: {
  163. if (activeFocus) {
  164. searchExpanded = true
  165. Qt.inputMethod.show()
  166. } else {
  167. if (text !== lastSearchText) {
  168. patternModel.filter(text)
  169. lastSearchText = text
  170. }
  171. }
  172. }
  173. Keys.onReturnPressed: {
  174. Qt.inputMethod.hide()
  175. focus = false
  176. }
  177. Keys.onEscapePressed: {
  178. text = ""
  179. lastSearchText = ""
  180. patternModel.filter("")
  181. Qt.inputMethod.hide()
  182. focus = false
  183. }
  184. }
  185. Text {
  186. text: "Search"
  187. font.family: Components.ThemeManager.fontBody
  188. font.pixelSize: Components.ThemeManager.fontSizeCaption
  189. color: Components.ThemeManager.textTertiary
  190. visible: !searchExpanded && searchField.text.length === 0
  191. }
  192. }
  193. MouseArea {
  194. anchors.fill: parent
  195. enabled: !searchExpanded
  196. onClicked: {
  197. searchExpanded = true
  198. searchField.forceActiveFocus()
  199. Qt.inputMethod.show()
  200. }
  201. }
  202. }
  203. // Close button when search expanded
  204. Rectangle {
  205. Layout.preferredWidth: 40
  206. Layout.preferredHeight: 40
  207. radius: 20
  208. visible: searchExpanded
  209. color: searchCloseArea.pressed ? Components.ThemeManager.pressedColor : "transparent"
  210. Components.Icon {
  211. anchors.centerIn: parent
  212. name: "close"
  213. size: 20
  214. color: Components.ThemeManager.textSecondary
  215. }
  216. MouseArea {
  217. id: searchCloseArea
  218. anchors.fill: parent
  219. onClicked: {
  220. searchExpanded = false
  221. searchField.text = ""
  222. searchField.lastSearchText = ""
  223. searchField.focus = false
  224. patternModel.filter("")
  225. }
  226. }
  227. }
  228. }
  229. }
  230. // Pattern Grid
  231. GridView {
  232. id: gridView
  233. Layout.fillWidth: true
  234. Layout.fillHeight: true
  235. cellWidth: 200
  236. cellHeight: 220
  237. model: patternModel
  238. clip: true
  239. visible: patternCount > 0
  240. reuseItems: true
  241. cacheBuffer: 440
  242. ScrollBar.vertical: ScrollBar {
  243. active: true
  244. policy: ScrollBar.AsNeeded
  245. }
  246. delegate: Item {
  247. width: gridView.cellWidth
  248. height: gridView.cellHeight
  249. // Check if pattern is already in playlist
  250. property bool isInPlaylist: isPatternInPlaylist(model.name)
  251. ModernPatternCard {
  252. id: patternCard
  253. anchors.centerIn: parent
  254. width: gridView.cellWidth - 10
  255. height: gridView.cellHeight - 10
  256. name: model.name
  257. preview: model.preview
  258. onClicked: {
  259. page.addPatternToPlaylist(model.name)
  260. }
  261. }
  262. // Selection ring + badge for patterns already in the playlist
  263. Rectangle {
  264. anchors.fill: patternCard
  265. color: "transparent"
  266. border.color: isInPlaylist ? Components.ThemeManager.accent : "transparent"
  267. border.width: isInPlaylist ? 2 : 0
  268. radius: Components.ThemeManager.radiusMd
  269. Rectangle {
  270. visible: isInPlaylist
  271. anchors.top: parent.top
  272. anchors.right: parent.right
  273. anchors.topMargin: Components.ThemeManager.spaceSm
  274. anchors.rightMargin: Components.ThemeManager.spaceSm
  275. width: 28
  276. height: 28
  277. radius: 14
  278. color: Components.ThemeManager.accent
  279. Components.Icon {
  280. anchors.centerIn: parent
  281. name: "check"
  282. size: 16
  283. color: Components.ThemeManager.onAccent
  284. }
  285. }
  286. }
  287. }
  288. add: Transition {
  289. NumberAnimation { property: "opacity"; from: 0; to: 1; duration: 300 }
  290. NumberAnimation { property: "scale"; from: 0.8; to: 1; duration: 300 }
  291. }
  292. }
  293. // Empty state when searching
  294. Item {
  295. Layout.fillWidth: true
  296. Layout.fillHeight: true
  297. visible: patternCount === 0
  298. Column {
  299. anchors.centerIn: parent
  300. spacing: Components.ThemeManager.spaceLg
  301. Components.Icon {
  302. name: "search"
  303. size: 44
  304. anchors.horizontalCenter: parent.horizontalCenter
  305. color: Components.ThemeManager.textTertiary
  306. }
  307. Label {
  308. text: "No patterns found"
  309. anchors.horizontalCenter: parent.horizontalCenter
  310. color: Components.ThemeManager.textSecondary
  311. font.family: Components.ThemeManager.fontDisplay
  312. font.pixelSize: Components.ThemeManager.fontSizeTitle
  313. }
  314. Label {
  315. text: "Try a different search term"
  316. anchors.horizontalCenter: parent.horizontalCenter
  317. color: Components.ThemeManager.textTertiary
  318. font.family: Components.ThemeManager.fontBody
  319. font.pixelSize: Components.ThemeManager.fontSizeBody
  320. }
  321. }
  322. }
  323. }
  324. }