描述
- 支持设置背景图,可以此实现小组件透明。
- 固定启发升级为弹出框选择,用户体验up。
脚本
// 启发语录列表
const textList = [
'选我所爱,爱我所选。',
'让改变适合你现在的生活和需要,而不是让你的生活和需要围绕着改变去进行。',
'此生理想、近期规划、今日功课。',
'流水不争先,争的是滔滔不绝。',
'既往不恋,当下不杂,未来不迎。',
];
/*-------上面是配置区域-------*/
const fmLocal = FileManager.local();
const basePath = fmLocal.documentsDirectory();
const bgImgPath = basePath + 'bgImg';
const fixedIndexPath = basePath + 'fixedIndex';
// 创建小组件
const widget = new ListWidget();
if (config.runsInWidget) {
createWidget();
return;
}
let selectResult = await confirm('???', ['设置', '换一句', '忽略']);
if (selectResult === 0) {
let selectResult = await confirm('设置什么?', ['背景图', '固定文本', '忽略']);
if (selectResult === 0) {
let selectResult = await confirm('请选择', ['选择背景图', '默认背景图', '忽略']);
if (selectResult === 0) {
let img = await Photos.fromLibrary();
fmLocal.writeImage(bgImgPath, img);
widget.backgroundImage = img;
}
if (selectResult === 1) {
const isExistBg = fmLocal.fileExists(bgImgPath);
isExistBg && fmLocal.remove(bgImgPath);
}
}
if (selectResult === 1) {
let index = await confirm('请选择', textList);
fmLocal.writeString(fixedIndexPath, String(index));
}
}
if (selectResult === 1) {
const isExistFixedIndex = fmLocal.fileExists(fixedIndexPath);
isExistFixedIndex && fmLocal.remove(fixedIndexPath);
}
createWidget();
// widget.presentMedium();
// 创建桌面小组件
function createWidget() {
const isExistFixedIndex = fmLocal.fileExists(fixedIndexPath);
let textItem;
if (isExistFixedIndex) {
const index = fmLocal.readString(fixedIndexPath);
textItem = textList[Number(index)];
} else {
const index = Math.floor(Math.random() * textList.length);
textItem = textList[index];
}
const bgImg = fmLocal.readImage(bgImgPath);
if (!bgImg) {
const gradient = new LinearGradient();
gradient.locations = [0, 1];
gradient.colors = [new Color('#333333'), new Color('#111111')];
widget.backgroundGradient = gradient;
} else {
const bgImg = fmLocal.readImage(bgImgPath);
widget.backgroundImage = bgImg;
}
let text;
// 设置语录样式
text = widget.addText(textItem);
text.textColor = new Color('#ffffff');
text.font = new Font('Georgia-BoldItalic', 26);
text.minimumScaleFactor = 0.5;
textItem.length > 12 ? text.leftAlignText() : text.centerAlignText();
Script.setWidget(widget);
}
// 选择框
async function confirm(message, options) {
let alert = new Alert();
alert.message = message;
for (const option of options) {
alert.addAction(option);
}
return await alert.presentAlert();
}
效果图