!457 delete toast

Merge pull request !457 from 王力量/master
This commit is contained in:
openharmony_ci
2025-07-30 10:47:11 +00:00
committed by Gitee
7 changed files with 2 additions and 304 deletions
@@ -19,7 +19,6 @@ import { GlobalContext } from '../common/utils/globalContext';
import { Configuration } from '@ohos.app.ability.Configuration';
import Want from '@ohos.app.ability.Want';
import { Property, NavigationBarRect } from '../common/model/typedef';
import { NotifyType } from '../common/model/definition';
import { Log } from '../common/utils/utils';
const BG_COLOR = '#00000000';
@@ -42,17 +41,13 @@ export default class SecurityExtensionAbility extends extension {
try {
let width = want.parameters?.['ohos.display.width'] as number ?? 0;
let height = want.parameters?.['ohos.display.height'] as number ?? 0;
let top = want.parameters?.['ohos.display.top'] as number ?? 0;
let navigationBarRect: NavigationBarRect = {
left: 0,
top: top,
top: 0,
width: width,
height: height
};
let notifyType = want.parameters?.['ohos.ability.notify.type'] ?? 0;
if (notifyType === NotifyType.Dialog) {
this.createWindow('SecurityDialog' + startId, window.WindowType.TYPE_DIALOG, navigationBarRect, want);
}
this.createWindow('SecurityDialog' + startId, window.WindowType.TYPE_DIALOG, navigationBarRect, want);
} catch (exception) {
Log.error(`Failed to obtain the default display object. Code: ${JSON.stringify(exception)}.`);
};
@@ -119,27 +114,4 @@ export default class SecurityExtensionAbility extends extension {
Log.error(`window create failed! err: ${JSON.stringify(err)}.`);
};
}
private async createToast(name: string, windowType: window.WindowType, rect: NavigationBarRect,
want: Want): Promise<void> {
try {
const win = await window.createWindow({ ctx: this.context, name, windowType });
let property: Record<string, Object> = { 'want': want, 'win': win };
let storage: LocalStorage = new LocalStorage(property);
await win.moveWindowTo(rect.left, rect.top);
await win.resize(rect.width, rect.height);
try {
await win.setSystemAvoidAreaEnabled(true);
} catch (error) {
Log.error(`setSystemAvoidAreaEnabled error: ${JSON.stringify(error)}.`);
};
await win.loadContent('pages/securityToast', storage);
win.setWindowBackgroundColor(BG_COLOR);
await win.setWindowTouchable(false);
await win.setWindowFocusable(false);
await win.showWindow();
} catch (error) {
Log.error(`window create failed! err: ${JSON.stringify(error)}.`);
};
}
};
@@ -99,8 +99,3 @@ export enum PermissionOption {
// 不操作
SKIP = 'SKIP'
}
export enum NotifyType {
Toast = 1,
Dialog = 0
}
@@ -350,27 +350,4 @@ export default class Constants {
public static CUSTOMIZE_BUTTON_WIDTH = 152;
public static CUSTOMIZE_BUTTON_TEXT_MIN_SIZE = 12;
public static CUSTOMIZE_BUTTON_TEXT_MAX_SIZE = 15;
// SecurityToast
public static TOAST_SYMBOLGLYPH_FONTSIZE = '16vp';
public static TOAST_TEXT_MIDDLE_FONT_SIZE = '12vp';
public static TOAST_POSITION_Y_UP = 15;
public static TOAST_POSITION_Y_UNDER = -15;
public static TOAST_ANIMATION_OFFSET = 80;
public static TOAST_ANIMATION_TIMEOUT_START = 100;
public static TOAST_ANIMATION_TIMEOUT_END = 3100;
public static TOAST_ANIMATION_TIMEOUT_ID = 3500;
public static TOAST_ICON_FONTSIZE_4 = 4;
public static TOAST_ICON_FONTSIZE_2 = 2;
public static TOAST_ICON_FONTSIZE_16 = 16;
public static TOAST_ICON_FONTSIZE_14 = 14;
public static TOAST_MAX_FONT_SCALE = 1;
public static TOAST_CONSTRAINT_SIZE_MAX_WIDTH = 400;
public static TOAST_CONSTRAINT_SIZE_MIN_HEIGHT = 36;
public static TOAST_COLUMN_WIDTH = '100%';
public static TOAST_COLUMN_HEIGHT = '100%';
public static TOAST_FLEX_SHADOW_RADIUS_40 = 40;
public static TOAST_FLEX_SHADOW_RADIUS_60 = 60;
public static TOAST_FLEX_SHADOW_OFFSET_X = 0;
public static TOAST_FLEX_SHADOW_OFFSET_Y = 10;
}
@@ -1,217 +0,0 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import window from '@ohos.window';
import display from '@ohos.display';
import accessibility from '@ohos.accessibility';
import { Log } from '../common/utils/utils';
import { DeviceUtil } from '../common/utils/deviceUtil';
import Constants from '../common/utils/constant';
import { WantInfo } from '../common/model/typedef';
class ToastFlexModifier implements AttributeModifier<FlexAttribute> {
applyNormalAttribute(instance: FlexAttribute): void {
if (DeviceUtil.isPC()) {
instance.padding($r('sys.float.padding_level4'))
instance.shadow({
radius: Constants.TOAST_FLEX_SHADOW_RADIUS_40,
color: $r('app.color.outerDefaultSMColor'),
offsetX: Constants.TOAST_FLEX_SHADOW_OFFSET_X,
offsetY: Constants.TOAST_FLEX_SHADOW_OFFSET_Y
})
instance.borderRadius($r('sys.float.corner_radius_level4'))
instance.backgroundBlurStyle(BlurStyle.COMPONENT_REGULAR)
instance.border({ width: '1px', color: '#33ffffff' })
instance.outline({ width: '1px', color: $r('app.color.outline'), radius: $r('sys.float.corner_radius_level4') })
} else {
instance.padding({
top: Constants.PADDING_8,
right: Constants.PADDING_16,
bottom: Constants.PADDING_8,
left: Constants.PADDING_16
})
instance.shadow({
radius: Constants.TOAST_FLEX_SHADOW_RADIUS_60,
color: $r('app.color.outerDefaultMDColor'),
offsetX: Constants.TOAST_FLEX_SHADOW_OFFSET_X,
offsetY: Constants.TOAST_FLEX_SHADOW_OFFSET_Y
})
instance.borderRadius($r('sys.float.corner_radius_level9'))
instance.backgroundBlurStyle(BlurStyle.COMPONENT_ULTRA_THICK)
}
}
}
@Entry({ useSharedStorage: true })
@Component
struct SecurityToast {
@State modifier: ToastFlexModifier = new ToastFlexModifier();
@LocalStorageLink('want') want: WantInfo = new WantInfo([]);
@LocalStorageLink('win') win: window.Window = {} as window.Window;
@State alpha: number = 0;
@State positionY: number = 0;
@State setPosition: boolean = false;
@State setOffset: number = Constants.TOAST_ANIMATION_OFFSET;
@State windowWidth: number = 0;
@State windowHeight: number = 0;
@State rotationInit: number = 0;
@State densityDPIInit: number = 0;
@State topRectHeight: number = 0;
getAnnounceForAccessibility() {
try {
let textAnnounce =
this.getUIContext().getHostContext()?.resourceManager.getStringSync($r('app.string.SecurityTitle_mediaFiles')
.id);
let eventInfo: accessibility.EventInfo = ({
type: 'announceForAccessibility',
bundleName: 'com.ohos.permissionmanager',
triggerAction: 'common',
textAnnouncedForAccessibility: textAnnounce
});
accessibility.sendAccessibilityEvent(eventInfo)
.catch((error: Error) => {
Log.error(`sendAnnounceForAccessibilityEvent fail: ${error.message}`);
return false;
});
} catch (error) {
Log.error(`sendAccessibilityEvent failed, error code: ${error.code}, message: ${error.message}.`);
};
}
getWinAvoidArea() {
let type = window.AvoidAreaType.TYPE_SYSTEM;
try {
let avoidArea = this.win?.getWindowAvoidArea(type);
this.topRectHeight = avoidArea.topRect.height;
} catch (error) {
Log.error(`Failed to obtain the area. Cause code: ${error.code}, message: ${error.message}`);
};
}
aboutToAppear(): void {
Log.info('onAboutToAppear');
try {
let dis = display.getDefaultDisplaySync();
this.rotationInit = dis.orientation;
this.densityDPIInit = dis.densityDPI;
} catch (error) {
Log.error(`Failed to get default display. Cause code: ${error.code}, message: ${error.message}`);
};
this.setPosition = this.want.parameters['ohos.toast.position'] ?? false;
this.positionY = this.setPosition ? Constants.TOAST_POSITION_Y_UP : Constants.TOAST_POSITION_Y_UNDER;
this.setOffset = this.want.parameters['ohos.toast.offset'] ?? Constants.TOAST_ANIMATION_OFFSET;
this.screenMonitor();
this.getWinAvoidArea();
let timeoutStart = setTimeout(() => {
this.alpha = 1;
this.positionY = 0;
clearTimeout(timeoutStart);
}, Constants.TOAST_ANIMATION_TIMEOUT_START);
let timeoutEnd = setTimeout(() => {
this.alpha = 0;
this.positionY = this.setPosition ? Constants.TOAST_POSITION_Y_UP : Constants.TOAST_POSITION_Y_UNDER;
clearTimeout(timeoutEnd);
}, Constants.TOAST_ANIMATION_TIMEOUT_END);
let timeoutID = setTimeout(() => {
this.win?.destroyWindow();
clearTimeout(timeoutID);
}, Constants.TOAST_ANIMATION_TIMEOUT_ID);
}
aboutToDisappear(): void {
try {
display.off('foldStatusChange');
display.off('change');
} catch (exception) {
Log.error(`Failed to unregister callback. Cause code: ${exception.code}, message: ${exception.message}`);
};
}
screenMonitor() {
try {
display.on('foldStatusChange', (data: display.FoldStatus) => {
Log.info(`Listening foldStatusChange. Data: ${JSON.stringify(data)}`);
this.win?.destroyWindow();
});
} catch (exception) {
Log.error(`Failed to register callback. Code: ${JSON.stringify(exception)}`);
};
try {
display.on('change', (data: number) => {
Log.info(`Listening change. Data: ${JSON.stringify(data)}`);
let rotation: number = display.getDefaultDisplaySync().orientation;
let densityDPI = display.getDefaultDisplaySync().densityDPI;
if (rotation !== this.rotationInit) {
this.rotationInit = rotation;
this.win?.destroyWindow();
return;
}
if (this.densityDPIInit !== densityDPI) {
this.densityDPIInit = densityDPI;
this.win?.destroyWindow();
}
});
} catch (exception) {
Log.error(`Failed to register callback. Code: ${JSON.stringify(exception)}`);
};
}
build() {
Column() {
Flex({ alignItems: ItemAlign.Center }) {
SymbolGlyph($r('sys.symbol.security_shield'))
.fontSize(Constants.TOAST_SYMBOLGLYPH_FONTSIZE)
.fontColor([$r('sys.color.icon_primary'), $r('sys.color.icon_emphasize')])
.renderingStrategy(SymbolRenderingStrategy.MULTIPLE_COLOR)
Blank()
.width('4vp')
Text($r('app.string.SecurityTitle_mediaFiles'))
.fontColor($r('sys.color.font_primary'))
.minFontSize(Constants.TOAST_TEXT_MIDDLE_FONT_SIZE)
.maxFontSize($r('sys.float.Body_M'))
.fontSize($r('sys.float.Body_M'))
.maxLines(Constants.SECURITY_HEADER_MAX_LINES)
.heightAdaptivePolicy(TextHeightAdaptivePolicy.MAX_LINES_FIRST)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.onAppear(() => {
this.getAnnounceForAccessibility();
})
}
.opacity(this.alpha)
.offset({ bottom: this.positionY })
.animation({
duration: Constants.TOAST_ANIMATION_TIMEOUT_START,
curve: 'cubic-bezier(0.2, 0, 0.1, 1)',
})
.width('auto')
.constraintSize({
maxWidth: Constants.TOAST_CONSTRAINT_SIZE_MAX_WIDTH,
minHeight: Constants.TOAST_CONSTRAINT_SIZE_MIN_HEIGHT
})
.margin(this.setPosition ? { top: this.setOffset - px2vp(this.topRectHeight) } : { bottom: this.setOffset })
.attributeModifier(this.modifier)
}
.width(Constants.TOAST_COLUMN_WIDTH)
.height(Constants.TOAST_COLUMN_HEIGHT)
.justifyContent(this.setPosition ? FlexAlign.Start : FlexAlign.End)
.padding({
left: (DeviceUtil.isPC() || DeviceUtil.isTablet()) ? Constants.PADDING_24 : Constants.PADDING_16,
right: (DeviceUtil.isPC() || DeviceUtil.isTablet()) ? Constants.PADDING_24 : Constants.PADDING_16
})
}
}
@@ -27,18 +27,6 @@
{
"name": "local_background_color",
"value": "#3194F7"
},
{
"name": "outline",
"value": "#1A000000"
},
{
"name": "outerDefaultMDColor",
"value": "#33000000"
},
{
"name": "outerDefaultSMColor",
"value": "#26000000"
}
]
}
@@ -10,7 +10,6 @@
"pages/dialogPlus",
"pages/globalSwitch",
"pages/securityDialog",
"pages/securityToast",
"pages/transition",
"PermissionSheet/PermissionStateSheetDialog",
"PermissionSheet/GlobalSwitchSheetDialog"
@@ -1,16 +0,0 @@
{
"color": [
{
"name": "outline",
"value": "#66000000"
},
{
"name": "outerDefaultMDColor",
"value": "#7B050505"
},
{
"name": "outerDefaultSMColor",
"value": "#52050505"
}
]
}