Status Bar Background Color, Global Page Grayscale, Close Pop-up on Blank Click, UI Component as Parameter



This content originally appeared on DEV Community and was authored by kouwei qing

[Daily HarmonyOS Next Knowledge] Status Bar Background Color, Global Page Grayscale, Close Pop-up on Blank Click, UI Component as Parameter, Disable Default Startup Effect

1. How to get the background color and font color of the status bar in HarmonyOS?

How to get the background color and font color of the status bar?

Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowsystembarproperties9

setWindowSystemBarProperties(systemBarProperties: SystemBarProperties): Promise<void>

Sets the properties of the main window’s three-key navigation bar and status bar using a Promise asynchronous callback.

This interface does not take effect on 2in1 devices. On other devices, it will not take effect immediately in split-screen mode (window.WindowStatusType.SPLIT_SCREEN), floating window mode (window.WindowStatusType.FLOATING), or free multi-window mode (enabled by clicking the free multi-window button in the device control center), and will only take effect when entering the full-screen main window.

setWindowLayoutFullScreen sets the full-screen navigation bar color to change with the page color.

import { router, window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';

@Entry
@Component
struct SystemBarPropertiesTest {
  @State message: string = 'Hello World';
  @State barHeight: number = 0;
  private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;

  async setSystemBar() {
    // Get the current application window
    let windowClass: window.Window = await window.getLastWindow(this.context)
    // Get the status bar height
    this.barHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height
    // Precondition: Set the window to full-screen
    windowClass.setWindowLayoutFullScreen(true);
    // Immersive solution 1: Set the system bar to not display [], set ['status', 'navigation'] to display
    // windowClass.setWindowSystemBarEnable([]);
    // Immersive solution 2: Set the background color of the status bar and navigation bar to the same as the application window
    /*await windowClass.setWindowSystemBarProperties({
    // The color attribute is ARGB, set the opacity to 0% to make it transparent
    // Navigation bar color
    navigationBarColor: "#fd121de5",
    // Status bar color
    statusBarColor: "#ff0ad9c2",
    // Navigation bar text color
    navigationBarContentColor: "#fde20606",
    // Status bar text color
    statusBarContentColor: "#fff1e50a"
    })*/

  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.setSystemBar()
          })
      }
      .width('100%')
    }
    .height('100%')
    .backgroundColor(Color.Brown)
  }
}

2. How to implement global page grayscale in HarmonyOS?

Grayscale can be achieved by setting the following universal attributes for the page root component: .saturate(0) and .grayscale(1).

Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-image-effect-V5

3. For HarmonyOS custom pop-up components, how to make the pop-up disappear when clicking on a blank area?

Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-promptaction-V5#ZH-CN_TOPIC_0000001884757698__basedialogoptions11

4. In HarmonyOS, this.weightDao is null when using a UI component as a parameter. (Temporarily solved by using new WeightHIstoryDao. How to modify it so that this.weightDao is available in onRightClick?)

Refer to the documentation: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builderparam-V5, which includes detailed examples of this pointing.

When developers create a custom component and want to add specific functions to it, such as adding a click-to-navigate operation in a specified custom component, embedding an event method directly within the component will add the function to all instances of the custom component. To solve this, ArkUI introduces the @BuilderParam decorator, which decorates variables pointing to @builder methods ( @BuilderParam is used to承接 @builder functions). Developers can pass parameters to the @BuilderParam-decorated custom build function in different ways (e.g., parameter modification, trailing closure, borrowing arrow functions) when initializing the custom component, and call @BuilderParam within the custom component to add specific functions. This decorator is used to declare an element of any UI description, similar to a slot placeholder.

5. Can the default startup effect of a HarmonyOS APP be canceled?

Question 1: Can the default startup effect of an APP be canceled? Can developers customize the launch page, i.e., create their own LaunchAbility new page?

Question 2: Can the icon (startWindowIcon) in the default startup effect be customized in size?

Question 1: The default startup effect of an APP is set through the startWindowIcon and startWindowBackground fields in module.json5. A launch page is mandatory for each UIAbility, and these two fields cannot be omitted; otherwise, compilation will fail. To customize the launch page, set startWindowIcon to a transparent image to cancel the default effect. The UIAbility component sets the launch page icon and background color, but it is not a real launch page in the true sense. Developers can manually create a splash page for business initialization to achieve a custom launch page, referring to the Splash Screen Ability sample.

Question 2: The icon (startWindowIcon) in the default startup effect will be displayed as its actual size, without adaptive adjustment based on the device screen or window size. For example, to set a full-screen image, provide an image according to the device’s full-screen dimensions.


This content originally appeared on DEV Community and was authored by kouwei qing