mirror of
https://gitcode.com/shufeng2012/ohos-xournalpp.git
synced 2026-11-04 14:51:12 +08:00
86 lines
2.8 KiB
Plaintext
86 lines
2.8 KiB
Plaintext
import { UIComponent, analysisXML } from "../utils/XMLHandler";
|
|
import { common } from '@kit.AbilityKit';
|
|
import { util } from '@kit.ArkTS';
|
|
import { BusinessError } from '@kit.BasicServicesKit';
|
|
|
|
|
|
|
|
@ComponentV2
|
|
|
|
|
|
export struct Menubar {
|
|
private content: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
|
|
@Local ui_components: UIComponent[] = [];
|
|
|
|
aboutToAppear() {
|
|
const xml_string = this.getXML();
|
|
this.ui_components = analysisXML(xml_string);
|
|
}
|
|
|
|
getXML(): string {
|
|
try {
|
|
let data: Uint8Array = this.content.resourceManager.getRawFileContentSync("mainmenubar.xml");
|
|
let text_decoder: util.TextDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true });
|
|
let xml_string: string = text_decoder.decodeToString(data, { stream: false });
|
|
return xml_string;
|
|
} catch (err) {
|
|
let code: number = (err as BusinessError).code;
|
|
let message: string = (err as BusinessError).message;
|
|
console.log(`[Error](${code}): ${message}`);
|
|
return "";
|
|
}
|
|
}
|
|
|
|
@Builder
|
|
buildMenu(items: UIComponent[]) {
|
|
Menu() {
|
|
ForEach(items, (item: UIComponent) => {
|
|
if (item.isSection === true) {
|
|
MenuItemGroup() {
|
|
ForEach(item.children || [], (sub: UIComponent) => {
|
|
this.buildMenuItem(sub);
|
|
}, (sub: UIComponent, idx) => idx.toString())
|
|
}
|
|
} else {
|
|
this.buildMenuItem(item);
|
|
}
|
|
}, (item: UIComponent, idx) => idx.toString());
|
|
}
|
|
}
|
|
|
|
@Builder
|
|
buildMenuItem(item: UIComponent) {
|
|
if (item.children && item.children?.length > 0) {
|
|
MenuItem({ content: item.label, labelInfo: item.accel }) {
|
|
this.buildSubMenu(item.children || []);
|
|
}
|
|
} else {
|
|
MenuItem({ content: item.label, labelInfo: item.accel });
|
|
}
|
|
}
|
|
|
|
@Builder
|
|
buildSubMenu(items: UIComponent[]) {
|
|
Menu() {
|
|
ForEach(items, (sub: UIComponent) => {
|
|
if (sub.children && sub.children?.length > 0) {
|
|
MenuItem({ content: sub.label, labelInfo: sub.accel }) {
|
|
this.buildSubMenu(sub.children || []);
|
|
}
|
|
} else {
|
|
MenuItem({ content: sub.label, labelInfo: sub.accel });
|
|
}
|
|
}, (sub: UIComponent, idx) => idx.toString())
|
|
}
|
|
}
|
|
|
|
build() {
|
|
Row() {
|
|
ForEach(this.ui_components, (menu: UIComponent) => {
|
|
Text(menu.label)
|
|
.bindMenu(this.buildMenu(menu.children || []))
|
|
.padding('10vp')
|
|
}, (menu: UIComponent, idx) => idx.toString())
|
|
}
|
|
}
|
|
} |