BreakpointSystem.ets 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2024 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. */
  15. import { mediaquery } from '@kit.ArkUI';
  16. declare interface BreakpointTypeOption<T> {
  17. xs?: T
  18. sm?: T
  19. md?: T
  20. lg?: T
  21. xl?: T
  22. xxl?: T
  23. }
  24. export class BreakpointType<T> {
  25. options: BreakpointTypeOption<T>;
  26. constructor(option: BreakpointTypeOption<T>) {
  27. this.options = option
  28. }
  29. getValue(currentBreakPoint: string) {
  30. if (currentBreakPoint === 'xs') {
  31. return this.options.xs;
  32. } else if (currentBreakPoint === 'sm') {
  33. return this.options.sm;
  34. } else if (currentBreakPoint === 'md') {
  35. return this.options.md;
  36. } else if (currentBreakPoint === 'lg') {
  37. return this.options.lg;
  38. } else if (currentBreakPoint === 'xl') {
  39. return this.options.xl;
  40. } else if (currentBreakPoint === 'xxl') {
  41. return this.options.xxl;
  42. } else {
  43. return undefined;
  44. }
  45. }
  46. }
  47. interface Breakpoint {
  48. name: string,
  49. size: number,
  50. mediaQueryListener?: mediaquery.MediaQueryListener
  51. }
  52. export enum BreakpointTypeEnum {
  53. SM = 'sm',
  54. MD = 'md',
  55. LG = 'lg',
  56. XL = 'xl'
  57. }
  58. export class BreakpointSystem {
  59. private currentBreakpoint: string = "md";
  60. private breakpoints: Breakpoint[] = [
  61. { name: 'sm', size: 320 },
  62. { name: 'md', size: 600 },
  63. { name: 'lg', size: 840 },
  64. { name: 'xl', size: 1500 }
  65. ];
  66. private updateCurrentBreakpoint(breakpoint: string) {
  67. if (this.currentBreakpoint !== breakpoint) {
  68. this.currentBreakpoint = breakpoint;
  69. AppStorage.setOrCreate<string>('currentBreakpoint', this.currentBreakpoint);
  70. console.log('on current breakpoint: ' + this.currentBreakpoint);
  71. }
  72. }
  73. public register() {
  74. this.breakpoints.forEach((breakpoint: Breakpoint, index) => {
  75. let condition: string;
  76. if (index === this.breakpoints.length - 1) {
  77. condition = '(' + breakpoint.size + 'vp<=width' + ')';
  78. } else {
  79. condition = '(' + breakpoint.size + 'vp<=width<' + this.breakpoints[index + 1].size + 'vp)';
  80. }
  81. breakpoint.mediaQueryListener = mediaquery.matchMediaSync(condition);
  82. breakpoint.mediaQueryListener.on('change', (mediaQueryResult) => {
  83. if (mediaQueryResult.matches) {
  84. this.updateCurrentBreakpoint(breakpoint.name);
  85. }
  86. })
  87. })
  88. }
  89. public unregister() {
  90. this.breakpoints.forEach((breakpoint: Breakpoint) => {
  91. if (breakpoint.mediaQueryListener) {
  92. breakpoint.mediaQueryListener.off('change');
  93. }
  94. })
  95. }
  96. }