mirror of
https://github.com/taigrr/wails.git
synced 2026-04-02 05:08:54 -07:00
Support Tray.SetIcon in KitchenSink
This commit is contained in:
6
v2/test/kitchensink/frontend/package-lock.json
generated
6
v2/test/kitchensink/frontend/package-lock.json
generated
@@ -135,9 +135,9 @@
|
||||
}
|
||||
},
|
||||
"@wails/runtime": {
|
||||
"version": "1.2.12",
|
||||
"resolved": "https://registry.npmjs.org/@wails/runtime/-/runtime-1.2.12.tgz",
|
||||
"integrity": "sha512-QnmDZhLz58HXatXERglpR5yLelTKHnPYM7XlRTNFrzABXJ2EVTy3wMirbe5aucKKzpYmUNzVFhz3FLkFgWNn2A==",
|
||||
"version": "1.2.20",
|
||||
"resolved": "https://registry.npmjs.org/@wails/runtime/-/runtime-1.2.20.tgz",
|
||||
"integrity": "sha512-UsKFbU+q6p9pW8cddVtUxwtJT/mULtyJCti2kE2wT8kaqfo2fjQueVYHWj0BbRlHgXfkCPcJ2mAPFjqQjvET3g==",
|
||||
"dev": true
|
||||
},
|
||||
"alphanum-sort": {
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
"@rollup/plugin-commonjs": "^11.0.0",
|
||||
"@rollup/plugin-node-resolve": "^7.0.0",
|
||||
"@rollup/plugin-url": "^5.0.1",
|
||||
"@wails/runtime": "^1.2.12",
|
||||
"@wails/runtime": "^1.2.20",
|
||||
"focus-visible": "^5.2.0",
|
||||
"halfmoon": "^1.1.1",
|
||||
"postcss": "^8.1.7",
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
'Browser',
|
||||
'File System',
|
||||
'Window',
|
||||
'Tray',
|
||||
'System'
|
||||
];
|
||||
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
import Browser from './pages/Browser/Browser.svelte';
|
||||
import Dialog from './pages/Dialog/Dialog.svelte';
|
||||
import System from './pages/System/System.svelte';
|
||||
import Window from './pages/Window/Window.svelte';
|
||||
|
||||
import Window from './pages/Window/Window.svelte';
|
||||
import Tray from './pages/Tray/Tray.svelte';
|
||||
|
||||
</script>
|
||||
|
||||
<h4 class="title">{$selectedPage || "" }</h4>
|
||||
@@ -20,6 +21,7 @@
|
||||
{#if $selectedPage == "Dialog"} <Dialog></Dialog> {/if}
|
||||
{#if $selectedPage == "System"} <System></System> {/if}
|
||||
{#if $selectedPage == "Window"} <Window></Window> {/if}
|
||||
{#if $selectedPage == "Tray"} <Tray></Tray> {/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
<script>
|
||||
import { Tray } from '@wails/runtime';
|
||||
import CodeBlock from '../../../components/CodeBlock.svelte';
|
||||
import CodeSnippet from '../../../components/CodeSnippet.svelte';
|
||||
import description from './description.txt';
|
||||
import { UniqueID } from '../../../utils/utils';
|
||||
import jsCode from './code.jsx';
|
||||
import goCode from './code.go';
|
||||
import { darkMode } from '../../../Store';
|
||||
|
||||
let isJs = false;
|
||||
$: lang = isJs ? 'Javascript' : 'Go';
|
||||
|
||||
let id = UniqueID('tray');
|
||||
|
||||
var icons = ["light", "dark", "svelte"];
|
||||
let darkmode = $darkmode;
|
||||
let iconName = darkMode ? 'light' : 'dark';
|
||||
|
||||
function setIcon() {
|
||||
console.log(iconName);
|
||||
if( isJs ) {
|
||||
Tray.SetIcon(iconName);
|
||||
} else {
|
||||
backend.main.Tray.SetIcon(iconName);
|
||||
}
|
||||
}
|
||||
|
||||
$: exampleCodeJS = `import { Tray } from '@wails/runtime';\n\nTray.SetIcon('` + iconName + `');`;
|
||||
$: exampleCodeGo = `// runtime is given through WailsInit()\nruntime.Tray.SetIcon("` + iconName + `");`;
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
<CodeBlock bind:isJs={isJs} {jsCode} {goCode} {id} title="Tray.SetIcon(trayIconID)" {description}>
|
||||
<div class="logging-form">
|
||||
<form data-wails-no-drag class="mw-full">
|
||||
<!-- Radio -->
|
||||
<div class="form-group">
|
||||
<div>Select Tray Icon</div>
|
||||
{#each icons as option}
|
||||
<div class="custom-radio">
|
||||
<input type="radio" name="trayicon" bind:group="{iconName}" id="{id}-{option}" value="{option}">
|
||||
<label for="{id}-{option}">{option}</label>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<input class="btn btn-primary" type="button" on:click="{setIcon}" value="Set Icon using {lang} runtime">
|
||||
|
||||
<CodeSnippet bind:isJs={isJs} jsCode={exampleCodeJS} goCode={exampleCodeGo}></CodeSnippet>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
</CodeBlock>
|
||||
26
v2/test/kitchensink/frontend/src/pages/Tray/SetIcon/code.go
Normal file
26
v2/test/kitchensink/frontend/src/pages/Tray/SetIcon/code.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/wailsapp/wails/v2"
|
||||
)
|
||||
|
||||
type MyStruct struct {
|
||||
runtime *wails.Runtime
|
||||
}
|
||||
|
||||
func (m *MyStruct) WailsInit(runtime *wails.Runtime) error {
|
||||
|
||||
// Load notes
|
||||
data, err := ioutil.ReadFile("notes.txt")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// SetIcon an event with the loaded data
|
||||
runtime.Events.Emit("notes loaded", string(data))
|
||||
|
||||
m.runtime = runtime
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Events } from '@wails/runtime';
|
||||
|
||||
function processButtonPress(name, address) {
|
||||
Events.Emit("new user", name, address);
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<code>SetIcon(trayIconID)</code> is used to set the tray icon.
|
||||
17
v2/test/kitchensink/frontend/src/pages/Tray/Tray.svelte
Normal file
17
v2/test/kitchensink/frontend/src/pages/Tray/Tray.svelte
Normal file
@@ -0,0 +1,17 @@
|
||||
<script>
|
||||
import SetIcon from "./SetIcon/SetIcon.svelte";
|
||||
</script>
|
||||
|
||||
<div>
|
||||
Wails includes support for adding icons/menus to the system tray. Tray icons are read from the "trayicons" directory in the project directory and bundled during the compilation phase.
|
||||
The filenames follow a convention and only PNG icons are supported. The file `<projectroot>/trayicons/light.png` is given a tray icon ID of 'light'. This is used
|
||||
when setting the icon.
|
||||
|
||||
<div style="padding: 15px"></div>
|
||||
|
||||
<SetIcon></SetIcon>
|
||||
<br/>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
</style>
|
||||
@@ -29,9 +29,8 @@ func main() {
|
||||
TitleBar: mac.TitleBarHiddenInset(),
|
||||
Menu: createApplicationMenu(),
|
||||
Tray: &menu.TrayOptions{
|
||||
Label: "Hi Go BitBar!",
|
||||
Icon: "light",
|
||||
Menu: createApplicationTray(),
|
||||
Icon: "light",
|
||||
Menu: createApplicationTray(),
|
||||
},
|
||||
},
|
||||
LogLevel: logger.TRACE,
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/wailsapp/wails/v2/pkg/menu/keys"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/wailsapp/wails/v2"
|
||||
"github.com/wailsapp/wails/v2/pkg/menu"
|
||||
"github.com/wailsapp/wails/v2/pkg/menu/keys"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Tray struct
|
||||
@@ -55,9 +51,6 @@ func (t *Tray) WailsInit(runtime *wails.Runtime) error {
|
||||
t.runtime.Tray.SetIcon("dark")
|
||||
})
|
||||
|
||||
// Start ticker
|
||||
go t.startTicker()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -65,19 +58,6 @@ func (t *Tray) WailsShutdown() {
|
||||
t.done = true
|
||||
}
|
||||
|
||||
func (t *Tray) startTicker() {
|
||||
time.Sleep(1 * time.Second)
|
||||
ticker := time.NewTicker(1 * time.Second)
|
||||
for t.done == false {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
r := rand.Intn(100)
|
||||
t.runtime.Tray.SetLabel(fmt.Sprintf("CPU: %d", r))
|
||||
}
|
||||
}
|
||||
ticker.Stop()
|
||||
}
|
||||
|
||||
func (t *Tray) incrementcounter() int {
|
||||
t.dynamicMenuCounter++
|
||||
return t.dynamicMenuCounter
|
||||
@@ -150,6 +130,10 @@ func (t *Tray) removeMenu(_ *menu.MenuItem) {
|
||||
t.runtime.Tray.Update()
|
||||
}
|
||||
|
||||
func (t *Tray) SetIcon(trayIconID string) {
|
||||
t.runtime.Tray.SetIcon(trayIconID)
|
||||
}
|
||||
|
||||
func createApplicationTray() *menu.Menu {
|
||||
trayMenu := &menu.Menu{}
|
||||
trayMenu.Append(menu.Text("Show Window", "Show Window", nil))
|
||||
|
||||
BIN
v2/test/kitchensink/trayicons/svelte.png
Normal file
BIN
v2/test/kitchensink/trayicons/svelte.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.0 KiB |
Reference in New Issue
Block a user