app_windows.go 3.8 KB

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