返回 DeepSeek-Reasonix
native_host.go
根目录 / desktop / native_host.go
1 package main
2
3 import "context"
4
5 // nativeHost is the only route from business code to the shell toolkit. Each
6 // method maps 1:1 onto a host/* call in docs/DESKTOP_HOST_PROTOCOL.md, served
7 // by the Electron shell through rpcNativeHost; tests substitute fakes.
8 type nativeHost interface {
9 ShowWindow(ctx context.Context)
10 ShowApplication(ctx context.Context)
11 HideWindow(ctx context.Context)
12 HideApplication(ctx context.Context)
13 MaximiseWindow(ctx context.Context)
14 UnmaximiseWindow(ctx context.Context)
15 MinimiseWindow(ctx context.Context)
16 UnminimiseWindow(ctx context.Context)
17 ToggleMaximiseWindow(ctx context.Context)
18 CenterWindow(ctx context.Context)
19 WindowIsMaximised(ctx context.Context) bool
20 WindowIsMinimised(ctx context.Context) bool
21 SetWindowPosition(ctx context.Context, x, y int)
22 SetWindowTitle(ctx context.Context, title string)
23 Screens(ctx context.Context) ([]nativeScreen, error)
24 OpenDirectoryDialog(ctx context.Context, opts nativeDialogOptions) (string, error)
25 OpenFileDialog(ctx context.Context, opts nativeDialogOptions) (string, error)
26 SaveFileDialog(ctx context.Context, opts nativeDialogOptions) (string, error)
27 MessageDialog(ctx context.Context, opts nativeMessageOptions) (string, error)
28 OpenExternal(ctx context.Context, url string)
29 Quit(ctx context.Context)
30 OpenDevTools(ctx context.Context)
31 }
32
33 type nativeScreen struct {
34 Width int
35 Height int
36 Primary bool
37 Scale float64
38 }
39
40 type nativeFileFilter struct {
41 DisplayName string
42 Pattern string // semicolon-separated globs, e.g. "*.png;*.jpg"
43 }
44
45 type nativeDialogOptions struct {
46 Title string
47 DefaultDirectory string
48 DefaultFilename string
49 Filters []nativeFileFilter
50 CanCreateDirectories bool
51 }
52
53 type nativeDialogType string
54
55 const (
56 nativeDialogInfo nativeDialogType = "info"
57 nativeDialogWarning nativeDialogType = "warning"
58 nativeDialogError nativeDialogType = "error"
59 nativeDialogQuestion nativeDialogType = "question"
60 )
61
62 type nativeMessageOptions struct {
63 Type nativeDialogType
64 Title string
65 Message string
66 Buttons []string
67 DefaultButton string
68 CancelButton string
69 }
70
71 // noopNativeHost stands in when no shell is attached (test-constructed Apps,
72 // nil receivers) so App methods never reach a toolkit that is not running.
73 type noopNativeHost struct{}
74
75 var _ nativeHost = noopNativeHost{}
76
77 func (noopNativeHost) ShowWindow(context.Context) {}
78 func (noopNativeHost) ShowApplication(context.Context) {}
79 func (noopNativeHost) HideWindow(context.Context) {}
80 func (noopNativeHost) HideApplication(context.Context) {}
81 func (noopNativeHost) MaximiseWindow(context.Context) {}
82 func (noopNativeHost) UnmaximiseWindow(context.Context) {}
83 func (noopNativeHost) MinimiseWindow(context.Context) {}
84 func (noopNativeHost) UnminimiseWindow(context.Context) {}
85 func (noopNativeHost) ToggleMaximiseWindow(context.Context) {}
86 func (noopNativeHost) CenterWindow(context.Context) {}
87 func (noopNativeHost) WindowIsMaximised(context.Context) bool { return false }
88 func (noopNativeHost) WindowIsMinimised(context.Context) bool { return false }
89 func (noopNativeHost) SetWindowPosition(context.Context, int, int) {}
90 func (noopNativeHost) SetWindowTitle(context.Context, string) {}
91 func (noopNativeHost) OpenExternal(context.Context, string) {}
92 func (noopNativeHost) Quit(context.Context) {}
93 func (noopNativeHost) OpenDevTools(context.Context) {}
94
95 func (noopNativeHost) Screens(context.Context) ([]nativeScreen, error) { return nil, nil }
96
97 func (noopNativeHost) OpenDirectoryDialog(context.Context, nativeDialogOptions) (string, error) {
98 return "", nil
99 }
100
101 func (noopNativeHost) OpenFileDialog(context.Context, nativeDialogOptions) (string, error) {
102 return "", nil
103 }
104
105 func (noopNativeHost) SaveFileDialog(context.Context, nativeDialogOptions) (string, error) {
106 return "", nil
107 }
108
109 func (noopNativeHost) MessageDialog(context.Context, nativeMessageOptions) (string, error) {
110 return "", nil
111 }
112
113 func (a *App) nativeHost() nativeHost {
114 if a == nil || a.host == nil {
115 return noopNativeHost{}
116 }
117 return a.host
118 }
119
120 func (a *App) setNativeHost(h nativeHost) {
121 a.host = h
122 }
123
123 lines GO