From f79d9b23d7ae15c346b27e9381efcd2ff75f5402 Mon Sep 17 00:00:00 2001 From: shylinux Date: Mon, 22 Apr 2024 00:36:57 +0800 Subject: [PATCH] add windows --- base/cli/daemon.go | 17 ++++----- misc/systray/systray.go | 75 +++++++++++++++++++++++++++++++++++++++ misc/systray/systray.shy | 11 ++++++ misc/wind/wind.go | 32 +++++++++++++++++ misc/windows/installed.go | 40 +++++++++++++++++++++ misc/windows/logged.go | 27 ++++++++++++++ misc/windows/process.go | 24 +++++++++++++ misc/windows/service.go | 23 ++++++++++++ misc/windows/windows.go | 32 +++++++++++++++++ 9 files changed, 273 insertions(+), 8 deletions(-) create mode 100644 misc/systray/systray.go create mode 100644 misc/systray/systray.shy create mode 100644 misc/wind/wind.go create mode 100644 misc/windows/installed.go create mode 100644 misc/windows/logged.go create mode 100644 misc/windows/process.go create mode 100644 misc/windows/service.go create mode 100644 misc/windows/windows.go diff --git a/base/cli/daemon.go b/base/cli/daemon.go index 3ec69c2a..9cde2f14 100644 --- a/base/cli/daemon.go +++ b/base/cli/daemon.go @@ -61,12 +61,13 @@ func _daemon_exec(m *ice.Message, cmd *exec.Cmd) { } const ( - DIR = "dir" - ENV = "env" - API = "api" - MOD = "mod" - PID = "pid" - PWD = "pwd" + DIR = "dir" + ENV = "env" + API = "api" + MOD = "mod" + PWD = "pwd" + PID = "pid" + PPID = "ppid" ) const ( BUILD = "build" @@ -160,9 +161,9 @@ func init() { func Opens(m *ice.Message, arg ...string) { if !tcp.IsLocalHost(m, m.Option(ice.MSG_USERIP)) { - return + // return } else if len(arg) == 0 || arg[0] == "" { - return + // return } switch runtime.GOOS { case DARWIN: diff --git a/misc/systray/systray.go b/misc/systray/systray.go new file mode 100644 index 00000000..8cbe600a --- /dev/null +++ b/misc/systray/systray.go @@ -0,0 +1,75 @@ +package systray + +import ( + "path" + + "shylinux.com/x/ice" + "shylinux.com/x/icebergs/base/cli" + "shylinux.com/x/icebergs/base/ctx" + "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/nfs" + "shylinux.com/x/icebergs/base/web" + kit "shylinux.com/x/toolkits" + + "github.com/getlantern/systray" +) + +type Systray struct { + ice.Hash + short string `data:"name"` + field string `data:"time,type,name,text,icons,order,space,index"` + create string `name:"create type name* text icons order space index"` + list string `name:"list name auto"` +} + +func (s Systray) Init(m *ice.Message, arg ...string) { + m = m.Spawn() + s.Hash.Init(m, arg...).GoSleep("3s", func() { + opened := false + m.AdminCmd(web.SPACE).Table(func(value ice.Maps) { kit.If(value[mdb.TYPE] == web.PORTAL, func() { opened = true }) }) + opened = true + kit.If(!opened, func() { m.Spawn().Opens(m.SpideOrigin(ice.OPS)) }) + m.Go(func() { systray.Run(func() { s.Show(m, arg...) }, func() {}) }) + }) +} +func (s Systray) Show(m *ice.Message, arg ...string) { + title := kit.JoinLine(m.SpideOrigin(ice.OPS), ice.Info.Make.Module, path.Base(kit.Path(""))) + systray.SetIcon([]byte(m.Cmdx(nfs.CAT, ice.SRC_MAIN_ICO))) + systray.SetTitle(title) + systray.SetTooltip(title) + s.List(m).Table(func(value ice.Maps) { + item := systray.AddMenuItem(value[mdb.NAME], value[mdb.TEXT]) + kit.If(value[mdb.ICONS], func(p string) { kit.If(m.Cmdx(nfs.CAT, p), func(p string) { item.SetIcon([]byte(p)) }) }) + m.Go(func() { + for _ = range item.ClickedCh { + if value[ctx.INDEX] == ice.EXIT { + m.Cmd(ice.EXIT) + break + } + p := m.SpideOrigin(ice.OPS) + kit.If(value[web.SPACE], func(pod string) { p += web.S(pod) }) + kit.If(value[ctx.INDEX], func(cmd string) { p += web.C(cmd) }) + m.Opens(p) + } + }) + }) +} +func (s Systray) List(m *ice.Message, arg ...string) *ice.Message { + if s.Hash.List(m, arg...); len(arg) == 0 { + if m.Action(s.Create, s.Build).Sort(mdb.ORDER, ice.INT).Length() == 0 { + kit.For([]string{web.PORTAL, web.DESKTOP, web.DREAM, web.ADMIN, web.VIMER, ice.EXIT}, func(p string) { + m.Search(p, func(key string, cmd *ice.Command) { + m.Push(mdb.NAME, kit.Format("%s(%s)", key, cmd.Help)).Push(ctx.INDEX, p) + }) + }) + } + } + return m +} +func (s Systray) Build(m *ice.Message, arg ...string) { + defer m.ToastProcess()() + m.Cmdy(cli.SYSTEM, cli.GO, cli.BUILD, "-ldflags", "-w -s -H=windowsgui", "-o", ice.USR_PUBLISH+"ice.exe", + ice.SRC_MAIN_GO, ice.SRC_VERSION_GO, ice.SRC_BINPACK_GO, ice.SRC_BINPACK_USR_GO) +} + +func init() { ice.ChatCtxCmd(Systray{}) } diff --git a/misc/systray/systray.shy b/misc/systray/systray.shy new file mode 100644 index 00000000..71b07e01 --- /dev/null +++ b/misc/systray/systray.shy @@ -0,0 +1,11 @@ +chapter "systray" +refer ` +源码 https://github.com/getlantern/systray +文档 https://pkg.go.dev/github.com/getlantern/systray#section-readme +` +field web.chat.systray.systray +field web.code.git.source args ` +https://github.com/getlantern/systray +usr/local/systray/ +go.mod +` \ No newline at end of file diff --git a/misc/wind/wind.go b/misc/wind/wind.go new file mode 100644 index 00000000..91cc0176 --- /dev/null +++ b/misc/wind/wind.go @@ -0,0 +1,32 @@ +package wind + +import ( + "runtime" + + "shylinux.com/x/ice" + "shylinux.com/x/icebergs/base/cli" + + "github.com/rodrigocfd/windigo/ui" + "github.com/rodrigocfd/windigo/win" +) + +type wind struct { + ice.Hash + list string `name:"list hash auto"` +} + +func (s wind) List(m *ice.Message, arg ...string) { + m.Echo("hello world") +} + +func init() { ice.ChatCtxCmd(wind{}) } + +func Run(arg ...string) string { + go func() { ice.Runs(func() {}, "serve", "start") }() + runtime.LockOSThread() + wnd := ui.NewWindowMain(ui.WindowMainOpts().Title("Contexts").ClientArea(win.SIZE{Cx: 340, Cy: 80})) + btnShow := ui.NewButton(wnd, ui.ButtonOpts().Text("&Open").Position(win.POINT{X: 0, Y: 0})) + btnShow.On().BnClicked(func() { ice.Pulse.Cmd(cli.SYSTEM, "explorer", "http://localhost:9020") }) + wnd.RunAsMain() + return "" +} diff --git a/misc/windows/installed.go b/misc/windows/installed.go new file mode 100644 index 00000000..a51514a9 --- /dev/null +++ b/misc/windows/installed.go @@ -0,0 +1,40 @@ +package windows + +import ( + "shylinux.com/x/ice" + "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/nfs" + kit "shylinux.com/x/toolkits" + + wapi "github.com/iamacarpet/go-win64api" +) + +type installed struct { + list string `name:"list name auto filter"` +} + +func (s installed) List(m *ice.Message, arg ...string) { + list, err := wapi.InstalledSoftwareList() + ListPush(m, list, err, + "installDate", + "arch", "estimatedSize", + "displayName", "displayVersion", + "publisher", "HelpLink", + "InstallLocation", + "UninstallString", + ) + m.RenameAppend( + "installDate", mdb.TIME, + "estimatedSize", nfs.SIZE, + "displayName", mdb.NAME, + "displayVersion", nfs.VERSION, + ) + m.RewriteAppend(func(value, key string, index int) string { + kit.If(key == nfs.SIZE, func() { value = kit.FmtSize(kit.Int(value) * 1024) }) + kit.If(key == mdb.TIME, func() { value = ParseTime(m, value) }) + return value + }) + m.SortIntR(nfs.SIZE) +} + +func init() { ice.ChatCtxCmd(installed{}) } diff --git a/misc/windows/logged.go b/misc/windows/logged.go new file mode 100644 index 00000000..e69dcba7 --- /dev/null +++ b/misc/windows/logged.go @@ -0,0 +1,27 @@ +package windows + +import ( + "shylinux.com/x/ice" + "shylinux.com/x/icebergs/base/aaa" + "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/web" + kit "shylinux.com/x/toolkits" + + wapi "github.com/iamacarpet/go-win64api" +) + +type logged struct { + list string `name:"list username auto"` +} + +func (s logged) List(m *ice.Message, arg ...string) { + list, err := wapi.ListLoggedInUsers() + ListPush(m, list, err, "logonTime", aaa.USERNAME, web.DOMAIN, "isLocal", "isAdmin") + m.RenameAppend("logonTime", mdb.TIME) + m.RewriteAppend(func(value, key string, index int) string { + kit.If(key == mdb.TIME, func() { value = ParseTime(m, value) }) + return value + }) +} + +func init() { ice.ChatCtxCmd(logged{}) } diff --git a/misc/windows/process.go b/misc/windows/process.go new file mode 100644 index 00000000..4e091c45 --- /dev/null +++ b/misc/windows/process.go @@ -0,0 +1,24 @@ +package windows + +import ( + "shylinux.com/x/ice" + "shylinux.com/x/icebergs/base/aaa" + "shylinux.com/x/icebergs/base/cli" + "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/nfs" + + wapi "github.com/iamacarpet/go-win64api" +) + +type process struct { + list string `name:"list name auto filter"` +} + +func (s process) List(m *ice.Message, arg ...string) { + list, err := wapi.ProcessList() + ListPush(m, list, err, "parentpid", cli.PID, aaa.USERNAME, "exeName", "fullPath") + m.RenameAppend("parentpid", cli.PPID, "exeName", mdb.NAME, "fullPath", nfs.PATH) + m.Sort("username,path") +} + +func init() { ice.ChatCtxCmd(process{}) } diff --git a/misc/windows/service.go b/misc/windows/service.go new file mode 100644 index 00000000..cd1b1117 --- /dev/null +++ b/misc/windows/service.go @@ -0,0 +1,23 @@ +package windows + +import ( + "shylinux.com/x/ice" + "shylinux.com/x/icebergs/base/cli" + "shylinux.com/x/icebergs/base/mdb" + + wapi "github.com/iamacarpet/go-win64api" +) + +type service struct { + list string `name:"list name auto filter"` +} + +func (s service) List(m *ice.Message, arg ...string) { + list, err := wapi.GetServices() + ListPush(m, list, err, cli.PID, "statusText", mdb.NAME, "displayName") + m.RenameAppend("statusText", mdb.STATUS, "displayName", mdb.TEXT) + m.StatusTimeCountStats(mdb.STATUS) + m.Sort("status,name") +} + +func init() { ice.ChatCtxCmd(service{}) } diff --git a/misc/windows/windows.go b/misc/windows/windows.go new file mode 100644 index 00000000..502c3743 --- /dev/null +++ b/misc/windows/windows.go @@ -0,0 +1,32 @@ +package windows + +import ( + "strings" + "time" + + "shylinux.com/x/ice" + kit "shylinux.com/x/toolkits" +) + +type windows struct { + cmds string `data:"logged,process,service,installed"` + list string `name:"list name auto"` +} + +func (s windows) List(m *ice.Message, arg ...string) { m.DisplayStudio() } + +func init() { ice.ChatCtxCmd(windows{}) } + +func ListPush(m *ice.Message, list ice.Any, err error, arg ...string) *ice.Message { + if !m.Warn(err) { + kit.For(kit.UnMarshal(kit.Format(list)), func(value ice.Map) { m.PushRecord(value, arg...) }) + kit.If(m.IsDebug(), func() { m.Echo(kit.Formats(list)) }) + } + return m +} +func ParseTime(m *ice.Message, value string) string { + if t, e := time.Parse(time.RFC3339, value); e == nil { + value = strings.TrimSuffix(t.Local().Format(ice.MOD_TIME), ".000") + } + return value +}