app_windows.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //go:build !ci && !js && !android && !ios && !wasm && !test_web_driver
  2. // +build !ci,!js,!android,!ios,!wasm,!test_web_driver
  3. package app
  4. import (
  5. "fmt"
  6. "io/ioutil"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "syscall"
  12. "golang.org/x/sys/windows/registry"
  13. "fyne.io/fyne/v2"
  14. "fyne.io/fyne/v2/theme"
  15. "golang.org/x/sys/execabs"
  16. )
  17. const notificationTemplate = `$title = "%s"
  18. $content = "%s"
  19. [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
  20. $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
  21. $toastXml = [xml] $template.GetXml()
  22. $toastXml.GetElementsByTagName("text")[0].AppendChild($toastXml.CreateTextNode($title)) > $null
  23. $toastXml.GetElementsByTagName("text")[1].AppendChild($toastXml.CreateTextNode($content)) > $null
  24. $xml = New-Object Windows.Data.Xml.Dom.XmlDocument
  25. $xml.LoadXml($toastXml.OuterXml)
  26. $toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
  27. [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("%s").Show($toast);`
  28. func isDark() bool {
  29. k, err := registry.OpenKey(registry.CURRENT_USER, `SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize`, registry.QUERY_VALUE)
  30. if err != nil { // older version of Windows will not have this key
  31. return false
  32. }
  33. defer k.Close()
  34. useLight, _, err := k.GetIntegerValue("AppsUseLightTheme")
  35. if err != nil { // older version of Windows will not have this value
  36. return false
  37. }
  38. return useLight == 0
  39. }
  40. func defaultVariant() fyne.ThemeVariant {
  41. if isDark() {
  42. return theme.VariantDark
  43. }
  44. return theme.VariantLight
  45. }
  46. func rootConfigDir() string {
  47. homeDir, _ := os.UserHomeDir()
  48. desktopConfig := filepath.Join(filepath.Join(homeDir, "AppData"), "Roaming")
  49. return filepath.Join(desktopConfig, "fyne")
  50. }
  51. func (a *fyneApp) OpenURL(url *url.URL) error {
  52. cmd := a.exec("rundll32", "url.dll,FileProtocolHandler", url.String())
  53. cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  54. return cmd.Run()
  55. }
  56. var scriptNum = 0
  57. func (a *fyneApp) SendNotification(n *fyne.Notification) {
  58. title := escapeNotificationString(n.Title)
  59. content := escapeNotificationString(n.Content)
  60. appID := a.UniqueID()
  61. if appID == "" || strings.Index(appID, "missing-id") == 0 {
  62. appID = a.Metadata().Name
  63. }
  64. script := fmt.Sprintf(notificationTemplate, title, content, appID)
  65. go runScript("notify", script)
  66. }
  67. // SetSystemTrayMenu creates a system tray item and attaches the specified menu.
  68. // By default this will use the application icon.
  69. func (a *fyneApp) SetSystemTrayMenu(menu *fyne.Menu) {
  70. a.Driver().(systrayDriver).SetSystemTrayMenu(menu)
  71. }
  72. // SetSystemTrayIcon sets a custom image for the system tray icon.
  73. // You should have previously called `SetSystemTrayMenu` to initialise the menu icon.
  74. func (a *fyneApp) SetSystemTrayIcon(icon fyne.Resource) {
  75. a.Driver().(systrayDriver).SetSystemTrayIcon(icon)
  76. }
  77. func escapeNotificationString(in string) string {
  78. noSlash := strings.ReplaceAll(in, "`", "``")
  79. return strings.ReplaceAll(noSlash, "\"", "`\"")
  80. }
  81. func runScript(name, script string) {
  82. scriptNum++
  83. appID := fyne.CurrentApp().UniqueID()
  84. fileName := fmt.Sprintf("fyne-%s-%s-%d.ps1", appID, name, scriptNum)
  85. tmpFilePath := filepath.Join(os.TempDir(), fileName)
  86. err := ioutil.WriteFile(tmpFilePath, []byte(script), 0600)
  87. if err != nil {
  88. fyne.LogError("Could not write script to show notification", err)
  89. return
  90. }
  91. defer os.Remove(tmpFilePath)
  92. launch := "(Get-Content -Encoding UTF8 -Path " + tmpFilePath + " -Raw) | Invoke-Expression"
  93. cmd := execabs.Command("PowerShell", "-ExecutionPolicy", "Bypass", launch)
  94. cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
  95. err = cmd.Run()
  96. if err != nil {
  97. fyne.LogError("Failed to launch windows notify script", err)
  98. }
  99. }
  100. func watchTheme() {
  101. // TODO monitor the Windows theme
  102. }