-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebview_cgo.go
More file actions
98 lines (79 loc) · 2.34 KB
/
webview_cgo.go
File metadata and controls
98 lines (79 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//go:build cgo && !windows
package main
import (
"fmt"
"log"
"sync"
"github.com/webview/webview_go"
)
var (
// Mutex to ensure only one webview can run at a time
webviewMutex sync.Mutex
webviewRunning bool
activeWebview webview.WebView
)
func init() {
// Override the default - webview is available with CGO
isWebviewAvailableDefault = true
}
// OpenNoteInWebview opens a note in a webview window or updates existing one
func OpenNoteInWebview(title, htmlContent string) error {
// Check if webview is already running (with minimal mutex usage)
webviewMutex.Lock()
if webviewRunning && activeWebview != nil {
// Update existing webview content using Dispatch to ensure thread safety
// Dispatch runs the function on the webview's event loop thread
w := activeWebview
webviewMutex.Unlock()
w.Dispatch(func() {
w.SetTitle(fmt.Sprintf("Vimango - %s", title))
w.SetHtml(htmlContent)
})
return nil
}
webviewRunning = true
webviewMutex.Unlock()
// Ensure we reset the flag when function exits
defer func() {
webviewMutex.Lock()
webviewRunning = false
activeWebview = nil
webviewMutex.Unlock()
}()
w := webview.New(false) // false = not debug mode
defer w.Destroy()
// Store reference to active webview
webviewMutex.Lock()
activeWebview = w
webviewMutex.Unlock()
w.SetTitle(fmt.Sprintf("Vimango - %s", title))
w.SetSize(1200, 800, webview.HintNone)
// Load the HTML content directly
w.SetHtml(htmlContent)
// Run the webview (this blocks until window is closed)
// Mutex is NOT held during this call, allowing other operations
w.Run()
return nil
}
// IsWebviewRunning returns true if a webview is currently running
func IsWebviewRunning() bool {
webviewMutex.Lock()
defer webviewMutex.Unlock()
return webviewRunning
}
// CloseWebview programmatically closes the active webview window
func CloseWebview() error {
webviewMutex.Lock()
defer webviewMutex.Unlock()
if !webviewRunning || activeWebview == nil {
return fmt.Errorf("no webview window is currently open")
}
// Terminate the webview - this will cause w.Run() to return
// and the defer cleanup in OpenNoteInWebview will handle state reset
activeWebview.Terminate()
return nil
}
// ShowWebviewUnavailableMessage shows a message when webview is not available
func ShowWebviewUnavailableMessage() {
log.Println("Webview is available in this build")
}