• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import router from '@system.router';
16import promptAction from '@ohos.promptAction';
17
18@Entry
19@Component
20struct ScrollExample {
21  scroller: Scroller = new Scroller()
22  private arr: number[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
23  @State hoverText : string = 'jump'
24  @State pressure : number = 0.0
25  build() {
26    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
27      Text('MainPage')
28        .fontSize(50)
29        .fontWeight(FontWeight.Bold)
30      Stack({ alignContent: Alignment.TopStart }) {
31        Scroll(this.scroller) {
32          Column() {
33            Button('toast').fontSize(25).fontWeight(FontWeight.Bold)
34              .onClick(() => {
35                try {
36                  console.info('toastShow start')
37                  promptAction.showToast({
38                    message: 'toastShow',
39                    duration: 60000
40                  });
41                  console.info('toastShow over')
42                } catch (error) {
43                  console.error(`showToast args error code is ${error.code}, message is ${error.message}`);
44                };
45              })
46
47            Button('dialog').fontSize(25).fontWeight(FontWeight.Bold)
48              .onClick(() => {
49                console.info('dialog start')
50                AlertDialog.show(
51                  {
52                    title: '',
53                    message: 'dialogShow',
54                    alignment: DialogAlignment.Bottom,
55                    confirm: {
56                      value: 'close',
57                      action: () => {
58                        console.info('close dialog')
59                      }
60                    }
61                  }
62                )
63                console.info('dialog over')
64              })
65            Button() {
66              Text('next page')
67                .fontSize(25)
68                .fontWeight(FontWeight.Bold)
69            }
70            .key('my-key')
71            .type(ButtonType.Capsule)
72            .margin({ top: 20 })
73            .onClick(() => {
74              console.info('jump to second')
75              router.push({ uri: 'pages/second' })
76              console.info('jump to second over')
77            })
78            .gesture(
79              LongPressGesture({ repeat: false })
80                .onAction((event: GestureEvent) => {
81                  console.info('jump to fourth')
82                  router.push({ uri: 'pages/fourth' })
83                  console.info('jump to fourth over')
84                })
85            )
86            .gesture(
87              LongPressGesture({ repeat: false, duration: 1800 })
88                .onAction((event: GestureEvent) => {
89                  console.info('duration 1800 jump to fourth')
90                  router.push({ uri: 'pages/fourth' })
91                  console.info('duration 1800 jump to fourth over')
92                })
93            )
94            Button() {
95              Text('Click twice')
96                .fontSize(25)
97                .fontWeight(FontWeight.Bold)
98            }
99            .type(ButtonType.Capsule)
100            .margin({ top: 20 })
101            .gesture(
102              TapGesture({ count: 1 })
103                .onAction(() => {
104                  console.info('jump to third')
105                  router.push({ uri: 'pages/third' })
106                  console.info('jump to third over')
107                })
108            )
109            .gesture(
110              LongPressGesture({ repeat: false })
111                .onAction((event: GestureEvent) => {
112                  console.info('jump to wearList')
113                  router.push({ uri: 'pages/wearList' })
114                  console.info('jump to wearList over')
115                })
116            )
117            Button() {
118              Text(this.hoverText)
119                .fontSize(25)
120                .fontWeight(FontWeight.Bold)
121            }
122            .type(ButtonType.Capsule)
123            .key('jump')
124            .margin({ top: 20 })
125            .onHover((isHover: boolean) => {
126              console.info('hover start')
127              if (isHover) {
128                this.hoverText = 'hover'
129              }
130              console.info('hover over')
131            })
132            .onMouse((event: MouseEvent) => {
133              console.info('Mouse operation events start')
134              switch (event.button) {
135                case MouseButton.Left :
136                  this.hoverText = 'left'
137                  break
138                case MouseButton.Right :
139                  this.hoverText = 'right'
140                  break
141                case MouseButton.Middle :
142                  this.hoverText = 'middle'
143                  break
144              }
145              console.info('Mouse operation events over')
146            })
147            .onClick(() => {
148              console.info('jump to pinch')
149              router.push({ uri: 'pages/pinch' })
150              console.info('jump to pinch over')
151            })
152            .gesture(
153              LongPressGesture({ repeat: false })
154                .onAction((event: GestureEvent) => {
155                  console.info('jump to drag')
156                  router.push({ uri: 'pages/drag' })
157                  console.info('jump to drag over')
158                })
159            )
160            Button() {
161              Text('scroll')
162                .fontSize(25)
163                .fontWeight(FontWeight.Bold)
164            }
165            .type(ButtonType.Capsule)
166            .margin({ top: 20 })
167            .onClick(() => {
168              console.info('jump to scroll')
169              router.push({ uri: 'pages/scroll' })
170              console.info('jump to scroll over')
171            })
172            Button() {
173              Text('wearList')
174                .fontSize(25)
175                .fontWeight(FontWeight.Bold)
176            }
177            .type(ButtonType.Capsule)
178            .margin({ top: 20 })
179            .onClick(() => {
180              console.info('jump to wearList')
181              router.push({ uri: 'pages/wearList' })
182              console.info('jump to wearList over')
183            })
184            Checkbox({ name: 'hi' })
185              .size({ width: 30, height: 30 })
186            TextInput({ placeholder: 'welcome', text: 'Hello World' })
187              .type(InputType.Normal)
188              .width(300)
189              .height(50)
190              .fontSize(40)
191              .enabled(true)
192              .margin({ top: 20 })
193            ForEach(this.arr, (item:number) => {
194              Text(item.toString())
195                .width('100%')
196                .height('30%')
197                .backgroundColor(0xFFFFFF)
198                .borderRadius(75)
199                .fontSize(80)
200                .textAlign(TextAlign.Center)
201                .margin({ top: 10 })
202            }, (item:string) => item)
203            Button() {
204              Text('bottom')
205                .fontSize(25)
206                .fontWeight(FontWeight.Bold)
207            }.type(ButtonType.Capsule)
208            .margin({
209              top: 20, left: 150
210            })
211            .onClick(() => {
212              console.info('jump to second')
213              router.push({ uri: 'pages/second' })
214              console.info('jump to second over')
215            })
216          }.width('100%')
217        }
218        .scrollable(ScrollDirection.Vertical)
219        .scrollBar(BarState.On)
220        .scrollBarColor(Color.Gray)
221        .scrollBarWidth(30)
222        .onScroll((xOffset: number, yOffset: number) => {
223          console.info(xOffset + ' ' + yOffset)
224        })
225      }.width('100%').height('100%').backgroundColor(0xDCDCDC)
226      .onTouch((event) => {
227        console.log(`onTouch action 11111`);
228        this.pressure = event.pressure;
229      })
230      Text(this.pressure.toFixed(1))
231        .id('pressure')
232        .fontSize(20)
233      Text('MainPage_End')
234        .fontSize(50)
235        .fontWeight(FontWeight.Bold)
236    }
237  }
238}
239
240