5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 20:22:08 +08:00

[v3 windows] Move icons to own package, systray dark mode icon, window.Focus(),

This commit is contained in:
Lea Anthony 2023-05-09 19:52:33 +10:00
parent 67e9522c67
commit 19e1e8b8a6
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
19 changed files with 138 additions and 100 deletions

View File

@ -32,6 +32,7 @@ Webview Window Interface Methods
| close() | | | Y | |
| destroy() | | | Y | |
| execJS(js string) | | | Y | |
| focus() | Y | | | |
| forceReload() | | | Y | |
| fullscreen() | Y | | Y | |
| getScreen() (*Screen, error) | | | Y | |
@ -120,27 +121,28 @@ Webview Window Interface Methods
| Feature | Windows | Linux | Mac | Notes |
|---------------------|---------|-------|-----|--------------------------------------------------------------------------------------|
| SetTitle | | | Y | |
| SetSize | | | Y | |
| Size | | | Y | |
| SetPosition | | | Y | |
| Position | | | Y | |
| FullScreen | | | Y | |
| UnFullscreen | | | Y | |
| Minimise | | | Y | |
| UnMinimise | | | Y | |
| Maximise | | | Y | |
| UnMaximise | | | Y | |
| Show | | | Y | |
| Hide | | | Y | |
| Center | | | Y | |
| SetBackgroundColour | | | Y | https://github.com/MicrosoftEdge/WebView2Feedback/issues/1621#issuecomment-938234294 |
| SetAlwaysOnTop | | | Y | |
| SetResizable | | | Y | |
| SetMinSize | | | Y | |
| SetMaxSize | | | Y | |
| Width | | | Y | |
| Height | | | Y | |
| SetTitle | Y | | Y | |
| SetSize | Y | | Y | |
| Size | Y | | Y | |
| SetPosition | Y | | Y | |
| Position | Y | | Y | |
| Focus | Y | | | |
| FullScreen | Y | | Y | |
| UnFullscreen | Y | | Y | |
| Minimise | Y | | Y | |
| UnMinimise | Y | | Y | |
| Maximise | Y | | Y | |
| UnMaximise | Y | | Y | |
| Show | Y | | Y | |
| Hide | Y | | Y | |
| Center | Y | | Y | |
| SetBackgroundColour | Y | | Y | https://github.com/MicrosoftEdge/WebView2Feedback/issues/1621#issuecomment-938234294 |
| SetAlwaysOnTop | Y | | Y | |
| SetResizable | Y | | Y | |
| SetMinSize | Y | | Y | |
| SetMaxSize | Y | | Y | |
| Width | Y | | Y | |
| Height | Y | | Y | |
| ZoomIn | | | Y | Increase view scale |
| ZoomOut | | | Y | Decrease view scale |
| ZoomReset | | | Y | Reset view scale |
@ -183,6 +185,7 @@ An 'X' indicates that the option is not supported by the platform.
| Zoom | | | | |
| EnableDragAndDrop | | | | |
| Windows | | - | - | |
| Focused | Y | | | |
### Log
@ -196,12 +199,12 @@ To log or not to log? System logger vs custom logger.
## Tray Menus
| Feature | Windows | Linux | Mac | Notes |
|--------------------|---------|-------|-----|-------|
| Icon | | | Y | |
| Label | | | Y | |
| Label (ANSI Codes) | | | | |
| Menu | | | Y | |
| Feature | Windows | Linux | Mac | Notes |
|--------------------|---------|-------|-----|----------------------------------------------------------------------|
| Icon | Y | | Y | Windows has default icons for light/dark mode & supports PNG or ICO. |
| Label | - | | Y | |
| Label (ANSI Codes) | - | | | |
| Menu | | | Y | |
## Cross Platform Events
@ -231,7 +234,7 @@ TBD
## Theme
| Plugin | Windows | Linux | Mac | Notes |
| Mode | Windows | Linux | Mac | Notes |
|--------|---------|-------|-----|-------|
| Dark | Y | | | |
| Light | Y | | | |

View File

@ -2,6 +2,7 @@ package main
import (
_ "embed"
"github.com/wailsapp/wails/v3/pkg/icons"
"log"
"os"
"runtime"
@ -45,7 +46,7 @@ func main() {
dialog := app.InfoDialog()
dialog.SetTitle("Custom Icon Example")
dialog.SetMessage("Using a custom icon")
dialog.SetIcon(application.DefaultApplicationIcon)
dialog.SetIcon(icons.ApplicationDarkMode256)
dialog.Show()
})
@ -85,7 +86,7 @@ func main() {
dialog := app.QuestionDialog()
dialog.SetTitle("Custom Icon Example")
dialog.SetMessage("Using a custom icon")
dialog.SetIcon(application.WailsLogoWhiteTransparent)
dialog.SetIcon(icons.WailsLogoWhiteTransparent)
dialog.SetDefaultButton(dialog.AddButton("I like it!"))
dialog.AddButton("Not so keen...")
dialog.Show()
@ -112,7 +113,7 @@ func main() {
dialog := app.WarningDialog()
dialog.SetTitle("Custom Icon Example")
dialog.SetMessage("Using a custom icon")
dialog.SetIcon(application.DefaultApplicationIcon)
dialog.SetIcon(icons.ApplicationLightMode256)
dialog.Show()
})
@ -137,7 +138,7 @@ func main() {
dialog := app.ErrorDialog()
dialog.SetTitle("Custom Icon Example")
dialog.SetMessage("Using a custom icon")
dialog.SetIcon(application.WailsLogoWhite)
dialog.SetIcon(icons.WailsLogoWhite)
dialog.Show()
})

View File

@ -2,6 +2,7 @@ package main
import (
_ "embed"
"github.com/wailsapp/wails/v3/pkg/icons"
"log"
"runtime"
"sync"
@ -62,9 +63,9 @@ func main() {
mySystray := app.NewSystemTray()
mySystray.SetLabel("Wails")
if runtime.GOOS == "darwin" {
mySystray.SetTemplateIcon(application.DefaultMacTemplateIcon)
mySystray.SetTemplateIcon(icons.SystrayMacTemplate)
} else {
mySystray.SetIcon(application.DefaultApplicationIcon)
mySystray.SetIcon(icons.ApplicationDarkMode256)
}
myMenu := app.NewMenu()
myMenu.Add("Item 1")
@ -102,9 +103,9 @@ func main() {
mySystray := app.NewSystemTray()
mySystray.SetLabel("Wails is awesome")
if runtime.GOOS == "darwin" {
mySystray.SetTemplateIcon(application.DefaultMacTemplateIcon)
mySystray.SetTemplateIcon(icons.SystrayMacTemplate)
} else {
mySystray.SetIcon(application.DefaultApplicationIcon)
mySystray.SetIcon(icons.ApplicationDarkMode256)
}
mySystray.SetMenu(myMenu)
mySystray.SetIconPosition(application.NSImageLeading)

View File

@ -2,12 +2,21 @@ package main
import (
_ "embed"
"fmt"
"github.com/wailsapp/wails/v3/pkg/icons"
"log"
"runtime"
"github.com/wailsapp/wails/v3/pkg/application"
)
var counter int
func clickCount() int {
counter++
return counter
}
func main() {
app := application.New(application.Options{
Name: "Systray Demo",
@ -21,7 +30,7 @@ func main() {
systemTray := app.NewSystemTray()
if runtime.GOOS == "darwin" {
systemTray.SetIcon(application.DefaultMacTemplateIcon)
systemTray.SetIcon(icons.SystrayMacTemplate)
}
myMenu := app.NewMenu()
@ -40,7 +49,8 @@ func main() {
systemTray.SetMenu(myMenu)
systemTray.OnClick(func() {
window.Show()
window.SetTitle(fmt.Sprintf("Clicked %d times", clickCount()))
window.Show().Focus()
})
err := app.Run()

View File

@ -1,6 +1,7 @@
package application
import (
"github.com/wailsapp/wails/v3/pkg/icons"
"log"
"net/http"
"os"
@ -97,7 +98,7 @@ func mergeApplicationDefaults(o *Options) {
o.Description = "An application written using Wails"
}
if o.Icon == nil {
o.Icon = DefaultApplicationIcon
o.Icon = icons.ApplicationLightMode256
}
}

File diff suppressed because one or more lines are too long

View File

@ -39,6 +39,7 @@ type WebviewWindowOptions struct {
EnableDragAndDrop bool
Mac MacWindow
Windows WindowsWindow
Focused bool
}
var WebviewWindowDefaults = &WebviewWindowOptions{

View File

@ -50,7 +50,6 @@ func NewSystemTray(id uint) *SystemTray {
id: id,
label: "",
iconPosition: NSImageLeading,
icon: DefaultApplicationIcon,
}
}

View File

@ -3,6 +3,7 @@
package application
import (
"github.com/wailsapp/wails/v3/pkg/icons"
"syscall"
"unsafe"
@ -70,14 +71,14 @@ func (s *windowsSystemTray) run() {
}
if s.parent.icon != nil {
s.lightModeIcon = lo.Must(w32.CreateHIconFromPNG(s.parent.icon))
s.lightModeIcon = lo.Must(w32.CreateHIconFromImage(s.parent.icon))
} else {
s.lightModeIcon = lo.Must(w32.CreateHIconFromPNG(DefaultApplicationIcon))
s.lightModeIcon = lo.Must(w32.CreateHIconFromImage(icons.SystrayLight))
}
if s.parent.darkModeIcon != nil {
s.darkModeIcon = lo.Must(w32.CreateHIconFromPNG(s.parent.darkModeIcon))
s.darkModeIcon = lo.Must(w32.CreateHIconFromImage(s.parent.darkModeIcon))
} else {
s.darkModeIcon = s.lightModeIcon
s.darkModeIcon = lo.Must(w32.CreateHIconFromImage(icons.SystrayDark))
}
s.uid = nid.UID
@ -144,7 +145,7 @@ func (s *windowsSystemTray) newNotifyIconData() w32.NOTIFYICONDATA {
func (s *windowsSystemTray) setIcon(icon []byte) {
var err error
s.lightModeIcon, err = w32.CreateHIconFromPNG(icon)
s.lightModeIcon, err = w32.CreateHIconFromImage(icon)
if err != nil {
panic(syscall.GetLastError())
}
@ -156,7 +157,7 @@ func (s *windowsSystemTray) setIcon(icon []byte) {
}
func (s *windowsSystemTray) setDarkModeIcon(icon []byte) {
var err error
s.darkModeIcon, err = w32.CreateHIconFromPNG(icon)
s.darkModeIcon, err = w32.CreateHIconFromImage(icon)
if err != nil {
panic(syscall.GetLastError())
}

View File

@ -55,6 +55,7 @@ type (
isNormal() bool
isVisible() bool
setFullscreenButtonEnabled(enabled bool)
focus()
show()
hide()
getScreen() (*Screen, error)
@ -807,3 +808,11 @@ func (w *WebviewWindow) NativeWindowHandle() (uintptr, error) {
}
return w.impl.nativeWindowHandle(), nil
}
func (w *WebviewWindow) Focus() {
if w.impl == nil {
w.options.Focused = true
return
}
invokeSync(w.impl.focus)
}

View File

@ -188,7 +188,9 @@ func (w *windowsWebviewWindow) run() {
w.center()
}
w.setForeground()
if options.Focused {
w.Focus()
}
if !options.Hidden {
w.show()
@ -223,7 +225,7 @@ func (w *windowsWebviewWindow) size() (int, int) {
return width, height
}
func (w *windowsWebviewWindow) setForeground() {
func (w *windowsWebviewWindow) Focus() {
w32.SetForegroundWindow(w.hwnd)
}
@ -418,6 +420,10 @@ func (w *windowsWebviewWindow) setFullscreenButtonEnabled(_ bool) {
// Unused in Windows
}
func (w *windowsWebviewWindow) focus() {
w32.SetForegroundWindow(w.hwnd)
}
func (w *windowsWebviewWindow) show() {
w32.ShowWindow(w.hwnd, w32.SW_SHOW)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

30
v3/pkg/icons/icons.go Normal file
View File

@ -0,0 +1,30 @@
package icons
import _ "embed"
//go:embed DefaultMacTemplateIcon.png
var SystrayMacTemplate []byte
//go:embed systray-light.png
var SystrayLight []byte
//go:embed systray-dark.png
var SystrayDark []byte
//go:embed ApplicationDarkMode-256.png
var ApplicationDarkMode256 []byte
//go:embed ApplicationLightMode-256.png
var ApplicationLightMode256 []byte
//go:embed WailsLogoBlack.png
var WailsLogoBlack []byte
//go:embed WailsLogoBlackTransparent.png
var WailsLogoBlackTransparent []byte
//go:embed WailsLogoWhite.png
var WailsLogoWhite []byte
//go:embed WailsLogoWhiteTransparent.png
var WailsLogoWhiteTransparent []byte

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -3,6 +3,7 @@
package w32
import (
"fmt"
"unsafe"
)
@ -27,13 +28,34 @@ func CreateIconFromResourceEx(presbits uintptr, dwResSize uint32, isIcon bool, v
return r, nil
}
// CreateHIconFromPNG creates a HICON from a PNG file
func CreateHIconFromPNG(pngData []byte) (HICON, error) {
func isPNG(fileData []byte) bool {
if len(fileData) < 4 {
return false
}
return string(fileData[:4]) == "\x89PNG"
}
func isICO(fileData []byte) bool {
if len(fileData) < 4 {
return false
}
return string(fileData[:4]) == "\x00\x00\x01\x00"
}
// CreateHIconFromImage creates a HICON from a PNG or ICO file
func CreateHIconFromImage(fileData []byte) (HICON, error) {
if len(fileData) < 8 {
return 0, fmt.Errorf("invalid file format")
}
if !isPNG(fileData) && !isICO(fileData) {
return 0, fmt.Errorf("unsupported file format")
}
iconWidth := GetSystemMetrics(SM_CXSMICON)
iconHeight := GetSystemMetrics(SM_CYSMICON)
icon, err := CreateIconFromResourceEx(
uintptr(unsafe.Pointer(&pngData[0])),
uint32(len(pngData)),
uintptr(unsafe.Pointer(&fileData[0])),
uint32(len(fileData)),
true,
0x00030000,
iconWidth,

View File

@ -1,7 +1,7 @@
version: '3'
tasks:
generate:
generate:events:
dir: ./events
cmds:
- go run generate.go
- go run generate.go

View File

@ -1,38 +0,0 @@
package main
import (
"bytes"
"os"
"strconv"
)
func main() {
if len(os.Args) != 2 {
println("Please provide a filename")
os.Exit(1)
}
data, err := os.ReadFile(os.Args[1])
if err != nil {
println("Error reading file:", err.Error())
os.Exit(1)
}
var buffer bytes.Buffer
buffer.WriteString("var image = []byte{")
// Iterate over the bytes and print them out in decimal
for _, b := range data {
// convert byte to decimal
buffer.WriteString(strconv.Itoa(int(b)) + ", ")
}
buffer.WriteString("}\n")
// write to file
err = os.WriteFile(os.Args[1]+".go", buffer.Bytes(), 0644)
if err != nil {
println("Error writing file:", err.Error())
os.Exit(1)
}
}