1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: MIT
3
4/*!
5 * \brief The StackView is a component that can be used to simulate a stack view.
6 *
7 * \note: Due to the language limitation only the partial implementation is possible.
8 * The component is meant to be a helper.
9 *
10 * \par Usage:
11 * The component can be used in two ways:
12 * - for smaller pages, where all pages are loaded at once,
13 * - for more complex pages, where pages are loaded dynamically.
14 *
15 * Static pages:
16 * \code{*.slint}
17 * stack := StackView {
18 * current-index: 0;
19 * min-index: 0;
20 *
21 * for color in [ Colors.red, Colors.green, Colors.blue ]:
22 * StackPage {
23 * is-current: self.check-is-current(stack.current-index);
24 * init => { self.page-index = stack.insert-page(); } // StackPage.count increased with insert-page function
25 *
26 * TestPage {
27 * background: color;
28 * push => { stack.push(); }
29 * pop => { stack.pop(); }
30 * }
31 * }
32 * }
33 * \endcode
34 *
35 * Dynamic pages:
36 * \code{*.slint}
37 * stack := StackView {
38 * count: 2; // StackPage.count provided manually
39 * current-index: 0;
40 * min-index: 0;
41 *
42 * if (stack.current-index == 0): StackPage {
43 * page-index: 0; is-current: true;
44 *
45 * TestPage {
46 * background: Colors.red;
47 * push => { stack.push(); }
48 * pop => { stack.pop(); }
49 * }
50 * }
51 * if (stack.current-index == 1): StackPage {
52 * page-index: 1; is-current: true;
53 *
54 * TestPage {
55 * background: Colors.green;
56 * push => { stack.push(); }
57 * pop => { stack.pop(); }
58 * }
59 * }
60 * }
61 * \endcode
62 *
63 * \sa StackPage
64 */
65export component StackView inherits Rectangle {
66 /// \brief This property states the number of items in the stack
67 in-out property<int> count: 0;
68
69 /// \brief This property states the index of the currently visible item
70 in-out property<int> current-index: -1;
71
72 /// \brief This property configures the minimum index the pop function can set (-1 by default)
73 in property<int> min-index: -1;
74
75 /// \brief This property configures the minimum index the push function can set (#count -1 by default)
76 in property<int> max-index: self.count - 1;
77
78 /// \brief This function increases the pages #count by one and returns new page index
79 public function insert-page() -> int {
80 self.count += 1;
81 return self.count - 1;
82 }
83
84 /// \brief This function increases the #current-index if possible
85 public function push() {
86 if (self.current-index < Math.min(self.max-index, self.count - 1)) {
87 self.current-index += 1;
88 }
89 }
90
91 /// \brief This function decreased the #current-index if possible
92 public function pop() {
93 if (self.current-index > Math.max(self.min-index, -1)) {
94 self.current-index -= 1;
95 }
96 }
97}
98
99/*!
100 * \brief The StackPage is a component to use in the StackView.
101 *
102 * The real content can either derive from the StackPage (which is Rectangle based) or can be contained
103 * as the page children.
104 *
105 * Inherits:
106 * \code{*.slint}
107 * export TestPage inherits StackPage {
108 * background: color;
109 * }
110 * \endcode
111 *
112 * Contains:
113 * \code{*.slint}
114 * StackPage {
115 * TestPage {
116 * background: Colors.red;
117 * }
118 * }
119 * \endcode
120 *
121 * \sa StackView
122 */
123export component StackPage inherits TouchArea {
124 /// \brief This property configures the page index
125 in property<int> page-index: -1;
126
127 /// \brief This property configures whether the page is a current page (if not, it is hidden)
128 in property<bool> is-current: false;
129
130 /// \brief This function is a helper function to use when setting the #is-current property
131 public pure function check-is-current(current-index: int) -> bool {
132 return current-index == self.page-index;
133 }
134
135 visible: self.is-current;
136}
137