描述

  1. 支持设置背景图,可以此实现小组件透明。
  2. 固定启发升级为弹出框选择,用户体验up。

脚本

  1. // 启发语录列表
  2. const textList = [
  3. '选我所爱,爱我所选。',
  4. '让改变适合你现在的生活和需要,而不是让你的生活和需要围绕着改变去进行。',
  5. '此生理想、近期规划、今日功课。',
  6. '流水不争先,争的是滔滔不绝。',
  7. '既往不恋,当下不杂,未来不迎。',
  8. ];
  9. /*-------上面是配置区域-------*/
  10. const fmLocal = FileManager.local();
  11. const basePath = fmLocal.documentsDirectory();
  12. const bgImgPath = basePath + 'bgImg';
  13. const fixedIndexPath = basePath + 'fixedIndex';
  14. // 创建小组件
  15. const widget = new ListWidget();
  16. if (config.runsInWidget) {
  17. createWidget();
  18. return;
  19. }
  20. let selectResult = await confirm('???', ['设置', '换一句', '忽略']);
  21. if (selectResult === 0) {
  22. let selectResult = await confirm('设置什么?', ['背景图', '固定文本', '忽略']);
  23. if (selectResult === 0) {
  24. let selectResult = await confirm('请选择', ['选择背景图', '默认背景图', '忽略']);
  25. if (selectResult === 0) {
  26. let img = await Photos.fromLibrary();
  27. fmLocal.writeImage(bgImgPath, img);
  28. widget.backgroundImage = img;
  29. }
  30. if (selectResult === 1) {
  31. const isExistBg = fmLocal.fileExists(bgImgPath);
  32. isExistBg && fmLocal.remove(bgImgPath);
  33. }
  34. }
  35. if (selectResult === 1) {
  36. let index = await confirm('请选择', textList);
  37. fmLocal.writeString(fixedIndexPath, String(index));
  38. }
  39. }
  40. if (selectResult === 1) {
  41. const isExistFixedIndex = fmLocal.fileExists(fixedIndexPath);
  42. isExistFixedIndex && fmLocal.remove(fixedIndexPath);
  43. }
  44. createWidget();
  45. // widget.presentMedium();
  46. // 创建桌面小组件
  47. function createWidget() {
  48. const isExistFixedIndex = fmLocal.fileExists(fixedIndexPath);
  49. let textItem;
  50. if (isExistFixedIndex) {
  51. const index = fmLocal.readString(fixedIndexPath);
  52. textItem = textList[Number(index)];
  53. } else {
  54. const index = Math.floor(Math.random() * textList.length);
  55. textItem = textList[index];
  56. }
  57. const bgImg = fmLocal.readImage(bgImgPath);
  58. if (!bgImg) {
  59. const gradient = new LinearGradient();
  60. gradient.locations = [0, 1];
  61. gradient.colors = [new Color('#333333'), new Color('#111111')];
  62. widget.backgroundGradient = gradient;
  63. } else {
  64. const bgImg = fmLocal.readImage(bgImgPath);
  65. widget.backgroundImage = bgImg;
  66. }
  67. let text;
  68. // 设置语录样式
  69. text = widget.addText(textItem);
  70. text.textColor = new Color('#ffffff');
  71. text.font = new Font('Georgia-BoldItalic', 26);
  72. text.minimumScaleFactor = 0.5;
  73. textItem.length > 12 ? text.leftAlignText() : text.centerAlignText();
  74. Script.setWidget(widget);
  75. }
  76. // 选择框
  77. async function confirm(message, options) {
  78. let alert = new Alert();
  79. alert.message = message;
  80. for (const option of options) {
  81. alert.addAction(option);
  82. }
  83. return await alert.presentAlert();
  84. }

效果图

image.png