Skip to content

Commit

Permalink
增加小组件
Browse files Browse the repository at this point in the history
  • Loading branch information
ipuppet committed Jun 25, 2024
1 parent 3d1daaf commit 4472b8a
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 56 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"info": {
"name": "CAIO",
"version": "2.2.8",
"version": "2.2.9",
"author": "ipuppet",
"module": false
},
Expand Down
2 changes: 1 addition & 1 deletion dist/CAIO-en.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/CAIO-zh-Hans.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions dist/CAIO.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ switch ($app.env) {
break
}

// AppInstance = require("./scripts/widget")
if (AppInstance) {
AppInstance.run()
}
6 changes: 4 additions & 2 deletions scripts/action/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class ActionEnv {
static today = 0
static editor = 1
static clipboard = 2
static action = 3
static action = 3 // 主动作页面
static keyboard = 4
static recursion = 5
static widget = 6
}
class ActionData {
env
Expand Down Expand Up @@ -187,7 +189,7 @@ class Action {
}

async runAction(category, name) {
const action = this.getAction(category, name)
const action = this.getAction(category, name, new ActionData({ env: ActionEnv.recursion }))
return await action.do()
}

Expand Down
11 changes: 11 additions & 0 deletions scripts/ui/clips/clips.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ClipsSearch = require("./search")
const ClipsViews = require("./views")
const ClipsDelegates = require("./delegates")
const WebDavSync = require("../../dao/webdav-sync")
const { ActionEnv, ActionData } = require("../../action/action")

/**
* @typedef {Clips} Clips
Expand Down Expand Up @@ -107,6 +108,16 @@ class Clips extends ClipsData {
} else {
this.kernel.tabBarController.switchPageTo("actions")
}
} else if ($context.query["runAction"]) {
$delay(0, () => {
const data = JSON.parse($text.base64Decode($context.query["runAction"]))
const action = this.kernel.actions.getAction(
data.category,
data.dir,
new ActionData({ env: ActionEnv.widget })
)
action.do()
})
}
})

Expand Down
71 changes: 41 additions & 30 deletions scripts/widget.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const { Logger, Setting } = require("./libs/easy-jsbox")
const SettingStructure = require("./setting/setting")
const { Storage } = require("./dao/storage")
const { Logger } = require("./libs/easy-jsbox")
const { AppKernelBase } = require("./app")

/**
* @typedef {AppKernel} AppKernel
*/
class AppKernel extends AppKernelBase {
constructor() {
super()

this.logger = new Logger()
this.logger.printToFile(this.fileStorage, "logs/widget.log")
this.setting.setReadonly()
}
}

class Widget {
static kernel = new AppKernel()

static widgetInstance(widget, ...data) {
if ($file.exists(`/scripts/widget/${widget}.js`)) {
const { Widget } = require(`./widget/${widget}.js`)
Expand All @@ -13,25 +26,6 @@ class Widget {
}
}

static kernel() {
const logger = new Logger()
logger.printToFile(AppKernelBase.fileStorage, "logs/widget.log")
const kernel = {
setting: new Setting({
fileStorage: AppKernelBase.fileStorage,
structure: SettingStructure
}),
fileStorage: AppKernelBase.fileStorage,
logger
}
kernel.setting.setReadonly()

const storage = new Storage(kernel)
kernel.storage = storage

return kernel
}

static renderError() {
$widget.setTimeline({
render: () => ({
Expand All @@ -43,17 +37,34 @@ class Widget {
})
}

static renderClipboard() {
const widget = Widget.widgetInstance("Clipboard", Widget.kernel())
static renderClips() {
const widget = Widget.widgetInstance("Clips", Widget.kernel)
widget.render()
}

static render(widgetName = $widget.inputValue) {
widgetName = widgetName ?? "Clipboard"
if (widgetName === "Clipboard") {
Widget.renderClipboard()
} else {
Widget.renderError()
static renderFavorite() {
const widget = Widget.widgetInstance("Favorite", Widget.kernel)
widget.render()
}

static renderActions() {
const widget = Widget.widgetInstance("Actions", Widget.kernel)
widget.render()
}

static render(widgetName = $widget.inputValue ?? "Actions") {
switch (widgetName) {
case "Clips":
Widget.renderClips()
break
case "Favorite":
Widget.renderFavorite()
break
case "Actions":
Widget.renderActions()
break
default:
Widget.renderError()
}
}
}
Expand Down
241 changes: 241 additions & 0 deletions scripts/widget/Actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
const { TodayPinActions } = require("../ui/components/today-actions")

/**
* @typedef {import("../app-main").AppKernel} AppKernel
*/
class ActionsWidget {
/**
* @param {AppKernel} kernel
*/
constructor(kernel = {}) {
this.kernel = kernel
this.baseUrlScheme = `jsbox://run?name=${$addin.current.name}`

this.todayPinActions = new TodayPinActions(this.kernel)
this.actions = this.todayPinActions.getActions()
if (this.actions.length === 0) {
this.actions = Object.values(this.kernel.actions.allActions)
}
}

get maxLength() {
// require this.ctx, this.render()
switch (this.ctx.family) {
case 0:
return 1
case 1:
return 4
case 2:
return 8
}
}

get data() {
// require this.maxLength
return this.actions.slice(0, this.maxLength)
}

getUrlScheme(action) {
return this.baseUrlScheme + `&runAction=${$text.base64Encode(JSON.stringify(action))}`
}

view2x2() {
const action = this.data[0]
return {
type: "vstack",
props: {
alignment: $widget.horizontalAlignment.leading,
spacing: 0,
padding: 10,
widgetURL: this.getUrlScheme(action)
},
views: [
{
type: "hstack",
props: {
frame: {
maxWidth: Infinity,
alignment: $widget.alignment.leading
}
},
views: [
{
type: "hstack",
modifiers: [
{
background: $color(action.color),
frame: {
width: 50,
height: 50
}
},
{
cornerRadius: {
value: 10,
style: 1 // 0: circular, 1: continuous
}
}
],
views: [
{
type: "image",
props: {
symbol: {
glyph: action.icon,
size: 32
},
color: $color("#ffffff")
}
}
]
}
]
},
{ type: "spacer" },
{
// 底部
type: "vstack",
props: {
frame: {
maxWidth: Infinity,
alignment: $widget.alignment.trailing
},
spacing: 0
},
views: [
{
type: "text",
props: {
text: action.name,
font: $font(18),
bold: true,
color: $color("primaryText")
}
}
]
}
]
}
}

view2x4() {
const actions = this.data
const length = actions.length
const views = []

const height = this.ctx.displaySize.height
const width = this.ctx.displaySize.width
const padding = 28
const itemHeight = (height - padding * (this.maxLength / 2 + 1)) / (this.maxLength / 2)
const itemWidth = (width - padding * 3) / 2
for (let i = 0; i < length; i += 2) {
const row = []
for (let j = 0; j < 2; j++) {
const action = actions[i + j]
row.push({
type: "hstack",
props: {
spacing: 0,
padding: 0,
link: this.getUrlScheme(action),
frame: {
maxWidth: itemWidth,
maxHeight: itemHeight,
alignment: $widget.alignment.leading
}
},
views: [
{
type: "hstack",
modifiers: [
{
background: $color(action.color),
frame: {
width: itemHeight - 5,
height: itemHeight - 5
}
},
{
cornerRadius: {
value: 10,
style: 1 // 0: circular, 1: continuous
}
}
],
views: [
{
type: "image",
props: {
symbol: {
glyph: action.icon,
size: (itemHeight - 5) * 0.6
},
color: $color("#ffffff")
}
}
]
},
{
type: "spacer",
props: { frame: { maxWidth: 15 } }
},
{
type: "text",
props: {
text: action.name,
font: $font(15),
bold: true,
color: $color("primaryText")
}
}
]
})
}
views.push({
type: "hstack",
props: {
spacing: padding,
padding: 0
},
views: row
})
}
return {
type: "vstack",
props: {
spacing: padding,
padding: padding
},
views: views
}
}

view4x4() {
return this.view2x4()
}

render() {
$widget.setTimeline({
render: ctx => {
this.ctx = ctx
let view
switch (this.ctx.family) {
case 0:
view = this.view2x2()
break
case 1:
view = this.view2x4()
break
case 2:
view = this.view4x4()
break
default:
view = this.view2x2()
}
return view
}
})
}
}

module.exports = { Widget: ActionsWidget }
Loading

0 comments on commit 4472b8a

Please sign in to comment.