mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-08 07:21:46 +08:00
refactor
This commit is contained in:
parent
cc524d78e4
commit
87f15ced04
@ -1,31 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<link rel="icon" type="image/svg+xml" href="/wails.png"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<link rel="stylesheet" href="/style.css"/>
|
||||
<title>Wails App</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div>
|
||||
<a data-wml-openURL="https://wails.io">
|
||||
<img src="/wails.png" class="logo" alt="Wails logo"/>
|
||||
</a>
|
||||
<a data-wml-openURL="https://developer.mozilla.org/en-US/docs/Web/JavaScript">
|
||||
<img src="/javascript.svg" class="logo vanilla" alt="JavaScript logo"/>
|
||||
</a>
|
||||
</div>
|
||||
<h1>Wails + Javascript</h1>
|
||||
<div class="footer">
|
||||
<div><p>Click on the Wails logo to learn more</p></div>
|
||||
<div><p id="time">Listening for Time event...</p></div>
|
||||
<div><button class="btn" onclick="sendNotification()">Send Basic Notification</button></div>
|
||||
<div><button class="btn" onclick="sendComplexNotification()">Send Complex Notificaiton</button></div>
|
||||
<div id="notifications"></div>
|
||||
</div>
|
||||
</div>
|
||||
<script type="module" src="/main.js"></script>
|
||||
</body>
|
||||
</html>
|
@ -1,67 +0,0 @@
|
||||
import { GreetService } from "./bindings/notifications";
|
||||
import { NotificationService } from "./bindings/github.com/wailsapp/wails/v3/pkg/services/notification";
|
||||
import { Events } from "@wailsio/runtime";
|
||||
|
||||
const resultElement = document.getElementById('result');
|
||||
const timeElement = document.getElementById('time');
|
||||
const notificationsElement = document.getElementById('notifications');
|
||||
|
||||
const nofitications = new Map();
|
||||
|
||||
window.doGreet = () => {
|
||||
let name = document.getElementById('name').value;
|
||||
if (!name) {
|
||||
name = 'anonymous';
|
||||
}
|
||||
GreetService.Greet(name).then((result) => {
|
||||
resultElement.innerText = result;
|
||||
}).catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
|
||||
window.sendNotification = async () => {
|
||||
const granted = await NotificationService.CheckNotificationAuthorization();
|
||||
if (granted) {
|
||||
await NotificationService.SendNotification("some-uuid-fronted", "Frontend Notificaiton", "", "Notificaiton sent through JS!");
|
||||
}
|
||||
}
|
||||
|
||||
window.sendComplexNotification = async () => {
|
||||
const granted = await NotificationService.CheckNotificationAuthorization();
|
||||
if (granted) {
|
||||
await NotificationService.RegisterNotificationCategory({
|
||||
id: "FRONTEND_NOTIF",
|
||||
actions: [
|
||||
{ id: "VIEW_ACTION", title: "View" },
|
||||
{ id: "MARK_READ_ACTION", title: "Mark as Read" },
|
||||
{ id: "DELETE_ACTION", title: "Delete", destructive: true },
|
||||
],
|
||||
hasReplyField: true,
|
||||
replyButtonTitle: "Reply",
|
||||
replyPlaceholder: "Reply to frontend...",
|
||||
});
|
||||
|
||||
await NotificationService.SendNotificationWithActions({
|
||||
id: "some-uuid-complex",
|
||||
title: "Complex Frontend Notification",
|
||||
subtitle: "From: Jane Doe",
|
||||
body: "Is it rainging today where you are?",
|
||||
categoryId: "FRONTEND_NOTIF",
|
||||
data: {
|
||||
messageId: "msg-456",
|
||||
senderId: "user-456",
|
||||
timestamp: Date.now(),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Events.On('time', (time) => {
|
||||
timeElement.innerText = time.data;
|
||||
});
|
||||
|
||||
Events.On("notification", (data) => {
|
||||
nofitications.set(data.identifier, data);
|
||||
notificationsElement.innerText = Array.from(nofitications.values()).map(notification => JSON.stringify(notification)).join(", ");
|
||||
});
|
@ -1,16 +0,0 @@
|
||||
{
|
||||
"name": "frontend",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build:dev": "vite build --minify false --mode development",
|
||||
"build": "vite build --mode production",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"devDependencies": {
|
||||
"vite": "^5.0.0",
|
||||
"@wailsio/runtime": "latest"
|
||||
}
|
||||
}
|
Binary file not shown.
@ -1 +0,0 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="32" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path fill="#F7DF1E" d="M0 0h256v256H0V0Z"></path><path d="m67.312 213.932l19.59-11.856c3.78 6.701 7.218 12.371 15.465 12.371c7.905 0 12.89-3.092 12.89-15.12v-81.798h24.057v82.138c0 24.917-14.606 36.259-35.916 36.259c-19.245 0-30.416-9.967-36.087-21.996m85.07-2.576l19.588-11.341c5.157 8.421 11.859 14.607 23.715 14.607c9.969 0 16.325-4.984 16.325-11.858c0-8.248-6.53-11.17-17.528-15.98l-6.013-2.58c-17.357-7.387-28.87-16.667-28.87-36.257c0-18.044 13.747-31.792 35.228-31.792c15.294 0 26.292 5.328 34.196 19.247l-18.732 12.03c-4.125-7.389-8.591-10.31-15.465-10.31c-7.046 0-11.514 4.468-11.514 10.31c0 7.217 4.468 10.14 14.778 14.608l6.014 2.577c20.45 8.765 31.963 17.7 31.963 37.804c0 21.654-17.012 33.51-39.867 33.51c-22.339 0-36.774-10.654-43.819-24.574"></path></svg>
|
Before Width: | Height: | Size: 995 B |
@ -1,160 +0,0 @@
|
||||
:root {
|
||||
font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto",
|
||||
"Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
|
||||
sans-serif;
|
||||
font-size: 16px;
|
||||
line-height: 24px;
|
||||
font-weight: 400;
|
||||
color-scheme: light dark;
|
||||
color: rgba(255, 255, 255, 0.87);
|
||||
background-color: rgba(27, 38, 54, 1);
|
||||
font-synthesis: none;
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
-webkit-text-size-adjust: 100%;
|
||||
}
|
||||
|
||||
* {
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
-moz-user-select: none;
|
||||
-ms-user-select: none;
|
||||
}
|
||||
|
||||
@font-face {
|
||||
font-family: "Inter";
|
||||
font-style: normal;
|
||||
font-weight: 400;
|
||||
src: local(""),
|
||||
url("./Inter-Medium.ttf") format("truetype");
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 3em;
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
a {
|
||||
font-weight: 500;
|
||||
color: #646cff;
|
||||
text-decoration: inherit;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
color: #535bf2;
|
||||
}
|
||||
|
||||
button {
|
||||
/* width: 60px; */
|
||||
height: 30px;
|
||||
line-height: 30px;
|
||||
border-radius: 3px;
|
||||
border: none;
|
||||
margin: 0 0 0 20px;
|
||||
padding: 0 8px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.result {
|
||||
height: 20px;
|
||||
line-height: 20px;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
display: flex;
|
||||
place-items: center;
|
||||
place-content: center;
|
||||
min-width: 320px;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 3.2em;
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
#app {
|
||||
max-width: 1280px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.logo {
|
||||
height: 6em;
|
||||
padding: 1.5em;
|
||||
will-change: filter;
|
||||
}
|
||||
|
||||
.logo:hover {
|
||||
filter: drop-shadow(0 0 2em #e80000aa);
|
||||
}
|
||||
|
||||
.logo.vanilla:hover {
|
||||
filter: drop-shadow(0 0 2em #f7df1eaa);
|
||||
}
|
||||
|
||||
.result {
|
||||
height: 20px;
|
||||
line-height: 20px;
|
||||
margin: 1.5rem auto;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.footer {
|
||||
margin-top: 1rem;
|
||||
align-content: center;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: light) {
|
||||
:root {
|
||||
color: #213547;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
color: #747bff;
|
||||
}
|
||||
|
||||
button {
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
.input-box .btn:hover {
|
||||
background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
.input-box .input {
|
||||
border: none;
|
||||
border-radius: 3px;
|
||||
outline: none;
|
||||
height: 30px;
|
||||
line-height: 30px;
|
||||
padding: 0 10px;
|
||||
color: black;
|
||||
background-color: rgba(240, 240, 240, 1);
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
|
||||
.input-box .input:hover {
|
||||
border: none;
|
||||
background-color: rgba(255, 255, 255, 1);
|
||||
}
|
||||
|
||||
.input-box .input:focus {
|
||||
border: none;
|
||||
background-color: rgba(255, 255, 255, 1);
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 8.8 KiB |
@ -1,135 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
"github.com/wailsapp/wails/v3/pkg/events"
|
||||
"github.com/wailsapp/wails/v3/pkg/services/notification"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := application.New(application.Options{
|
||||
Name: "Notifications Demo",
|
||||
Description: "A test of macOS notifications",
|
||||
Assets: application.AlphaAssets,
|
||||
Services: []application.Service{
|
||||
application.NewService(¬ification.NotificationService{}),
|
||||
},
|
||||
})
|
||||
|
||||
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
|
||||
Width: 500,
|
||||
Height: 800,
|
||||
})
|
||||
|
||||
app.OnApplicationEvent(events.Mac.ApplicationDidFinishLaunching, func(event *application.ApplicationEvent) {
|
||||
// Request pemission to send notifications
|
||||
granted, err := application.RequestUserNotificationAuthorization()
|
||||
if err != nil {
|
||||
log.Default().Printf("WARNING: %s\n", err)
|
||||
}
|
||||
|
||||
if granted {
|
||||
// Send notification with no actions
|
||||
err = application.SendNotification("some-uuid", "Title", "", "body!")
|
||||
if err != nil {
|
||||
log.Default().Printf("WARNING: %s\n", err)
|
||||
}
|
||||
|
||||
err = application.RegisterNotificationCategory(application.NotificationCategory{
|
||||
ID: "MESSAGE_CATEGORY",
|
||||
Actions: []application.NotificationAction{
|
||||
{ID: "VIEW_ACTION", Title: "View"},
|
||||
{ID: "MARK_READ_ACTION", Title: "Mark as Read"},
|
||||
{ID: "DELETE_ACTION", Title: "Delete", Destructive: true},
|
||||
},
|
||||
HasReplyField: true,
|
||||
ReplyPlaceholder: "Type your reply...",
|
||||
ReplyButtonTitle: "Reply",
|
||||
})
|
||||
if err != nil {
|
||||
log.Default().Printf("WARNING: %s\n", err)
|
||||
}
|
||||
|
||||
err = application.SendNotificationWithActions(application.NotificationOptions{
|
||||
ID: "some-other-uuid",
|
||||
Title: "New Message",
|
||||
Subtitle: "From: Jane Doe",
|
||||
Body: "Is it raining today where you are?",
|
||||
CategoryID: "MESSAGE_CATEGORY",
|
||||
Data: map[string]interface{}{
|
||||
"messageId": "msg-123",
|
||||
"senderId": "user-123",
|
||||
"timestamp": time.Now().Unix(),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Default().Printf("WARNING: %s\n", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
app.OnApplicationEvent(events.Mac.DidReceiveNotificationResponse, func(event *application.ApplicationEvent) {
|
||||
data := event.Context().GetData()
|
||||
|
||||
// Parse data received
|
||||
if data != nil {
|
||||
if identifier, ok := data["identifier"].(string); ok {
|
||||
fmt.Printf("Notification identifier: %s\n", identifier)
|
||||
}
|
||||
|
||||
if actionIdentifier, ok := data["actionIdentifier"].(string); ok {
|
||||
fmt.Printf("Action Identifier: %s\n", actionIdentifier)
|
||||
}
|
||||
|
||||
if userText, ok := data["userText"].(string); ok {
|
||||
fmt.Printf("User replied: %s\n", userText)
|
||||
}
|
||||
|
||||
if userInfo, ok := data["userInfo"].(map[string]interface{}); ok {
|
||||
fmt.Printf("Custom data: %+v\n", userInfo)
|
||||
}
|
||||
|
||||
// Send notification to JS
|
||||
app.EmitEvent("notification", data)
|
||||
}
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(time.Second * 5)
|
||||
// Sometime later check if you are still authorized
|
||||
granted, err := application.CheckNotificationAuthorization()
|
||||
if err != nil {
|
||||
log.Default().Printf("WARNING: %s\n", err)
|
||||
}
|
||||
println(granted)
|
||||
|
||||
// Sometime later remove delivered notification by identifier
|
||||
err = application.RemoveDeliveredNotification("some-uuid")
|
||||
if err != nil {
|
||||
log.Default().Printf("WARNING: %s\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
time.Sleep(time.Second * 10)
|
||||
// Sometime later remove all pending and delivered notifications
|
||||
err := application.RemoveAllPendingNotifications()
|
||||
if err != nil {
|
||||
log.Default().Printf("WARNING: %s\n", err)
|
||||
}
|
||||
err = application.RemoveAllDeliveredNotifications()
|
||||
if err != nil {
|
||||
log.Default().Printf("WARNING: %s\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
err := app.Run()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// @ts-check
|
||||
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
||||
// This file is automatically generated. DO NOT EDIT
|
||||
|
||||
import * as Service from "./service.js";
|
||||
export {
|
||||
Service
|
||||
};
|
||||
|
||||
import * as $models from "./models.js";
|
||||
|
||||
/**
|
||||
* NotificationAction represents an action button for a notification
|
||||
* @typedef {$models.NotificationAction} NotificationAction
|
||||
*/
|
||||
|
||||
/**
|
||||
* NotificationCategory groups actions for notifications
|
||||
* @typedef {$models.NotificationCategory} NotificationCategory
|
||||
*/
|
||||
|
||||
/**
|
||||
* NotificationOptions contains configuration for a notification
|
||||
* @typedef {$models.NotificationOptions} NotificationOptions
|
||||
*/
|
@ -0,0 +1,37 @@
|
||||
// @ts-check
|
||||
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
||||
// This file is automatically generated. DO NOT EDIT
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore: Unused imports
|
||||
import {Create as $Create} from "@wailsio/runtime";
|
||||
|
||||
/**
|
||||
* NotificationAction represents an action button for a notification
|
||||
* @typedef {Object} NotificationAction
|
||||
* @property {string} id
|
||||
* @property {string} title
|
||||
* @property {boolean} [destructive]
|
||||
* @property {boolean} [authenticationRequired]
|
||||
*/
|
||||
|
||||
/**
|
||||
* NotificationCategory groups actions for notifications
|
||||
* @typedef {Object} NotificationCategory
|
||||
* @property {string} id
|
||||
* @property {NotificationAction[]} actions
|
||||
* @property {boolean} [hasReplyField]
|
||||
* @property {string} [replyPlaceholder]
|
||||
* @property {string} [replyButtonTitle]
|
||||
*/
|
||||
|
||||
/**
|
||||
* NotificationOptions contains configuration for a notification
|
||||
* @typedef {Object} NotificationOptions
|
||||
* @property {string} id
|
||||
* @property {string} title
|
||||
* @property {string} [subtitle]
|
||||
* @property {string} body
|
||||
* @property {string} [categoryId]
|
||||
* @property {{ [_: string]: any }} [data]
|
||||
*/
|
@ -0,0 +1,98 @@
|
||||
// @ts-check
|
||||
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
||||
// This file is automatically generated. DO NOT EDIT
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore: Unused imports
|
||||
import {Call as $Call, Create as $Create} from "@wailsio/runtime";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore: Unused imports
|
||||
import * as $models from "./models.js";
|
||||
|
||||
/**
|
||||
* CheckNotificationAuthorization checks current permission status
|
||||
* @returns {Promise<boolean> & { cancel(): void }}
|
||||
*/
|
||||
export function CheckNotificationAuthorization() {
|
||||
let $resultPromise = /** @type {any} */($Call.ByID(2789931702));
|
||||
return $resultPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* RegisterNotificationCategory registers a category with actions and optional reply field
|
||||
* @param {$models.NotificationCategory} category
|
||||
* @returns {Promise<void> & { cancel(): void }}
|
||||
*/
|
||||
export function RegisterNotificationCategory(category) {
|
||||
let $resultPromise = /** @type {any} */($Call.ByID(2679064664, category));
|
||||
return $resultPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Promise<void> & { cancel(): void }}
|
||||
*/
|
||||
export function RemoveAllDeliveredNotifications() {
|
||||
let $resultPromise = /** @type {any} */($Call.ByID(384520397));
|
||||
return $resultPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* RemoveAllPendingNotifications removes all pending notifications
|
||||
* @returns {Promise<void> & { cancel(): void }}
|
||||
*/
|
||||
export function RemoveAllPendingNotifications() {
|
||||
let $resultPromise = /** @type {any} */($Call.ByID(1423986276));
|
||||
return $resultPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} identifier
|
||||
* @returns {Promise<void> & { cancel(): void }}
|
||||
*/
|
||||
export function RemoveDeliveredNotification(identifier) {
|
||||
let $resultPromise = /** @type {any} */($Call.ByID(149440045, identifier));
|
||||
return $resultPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* RemovePendingNotification removes a specific pending notification
|
||||
* @param {string} identifier
|
||||
* @returns {Promise<void> & { cancel(): void }}
|
||||
*/
|
||||
export function RemovePendingNotification(identifier) {
|
||||
let $resultPromise = /** @type {any} */($Call.ByID(3872412470, identifier));
|
||||
return $resultPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* RequestUserNotificationAuthorization requests permission for notifications.
|
||||
* @returns {Promise<boolean> & { cancel(): void }}
|
||||
*/
|
||||
export function RequestUserNotificationAuthorization() {
|
||||
let $resultPromise = /** @type {any} */($Call.ByID(3412125712));
|
||||
return $resultPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* SendNotification sends a notification with the given identifier, title, subtitle, and body.
|
||||
* @param {string} identifier
|
||||
* @param {string} title
|
||||
* @param {string} subtitle
|
||||
* @param {string} body
|
||||
* @returns {Promise<void> & { cancel(): void }}
|
||||
*/
|
||||
export function SendNotification(identifier, title, subtitle, body) {
|
||||
let $resultPromise = /** @type {any} */($Call.ByID(2246903123, identifier, title, subtitle, body));
|
||||
return $resultPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* SendNotificationWithActions sends a notification with the specified actions
|
||||
* @param {$models.NotificationOptions} options
|
||||
* @returns {Promise<void> & { cancel(): void }}
|
||||
*/
|
||||
export function SendNotificationWithActions(options) {
|
||||
let $resultPromise = /** @type {any} */($Call.ByID(1615199806, options));
|
||||
return $resultPromise;
|
||||
}
|
@ -10,6 +10,13 @@
|
||||
import * as kvstore from './bindings/github.com/wailsapp/wails/v3/pkg/services/kvstore/keyvaluestore.js';
|
||||
import {Debug, Info, Warning, Error} from './bindings/github.com/wailsapp/wails/v3/pkg/services/log/loggerservice.js';
|
||||
import * as hash from './bindings/github.com/wailsapp/wails/v3/examples/services/hashes/service.js';
|
||||
import * as notifications from "./bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/service.js"
|
||||
|
||||
notifications.RequestUserNotificationAuthorization().then((granted) => {
|
||||
if (granted) {
|
||||
notifications.SendNotification("some-uuid-fronted", "Frontend Notificaiton", "", "Notificaiton sent through JS!");
|
||||
}
|
||||
});
|
||||
|
||||
function runHash() {
|
||||
let hashstring = document.getElementById("hashstring").value;
|
||||
|
@ -2,16 +2,18 @@ package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/wailsapp/wails/v3/examples/services/hashes"
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
"github.com/wailsapp/wails/v3/pkg/services/fileserver"
|
||||
"github.com/wailsapp/wails/v3/pkg/services/kvstore"
|
||||
"github.com/wailsapp/wails/v3/pkg/services/log"
|
||||
"github.com/wailsapp/wails/v3/pkg/services/notifications"
|
||||
"github.com/wailsapp/wails/v3/pkg/services/sqlite"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
//go:embed assets/*
|
||||
@ -48,6 +50,7 @@ func main() {
|
||||
}), application.ServiceOptions{
|
||||
Route: "/files",
|
||||
}),
|
||||
application.NewService(notifications.New()),
|
||||
},
|
||||
Assets: application.AssetOptions{
|
||||
Handler: application.BundledAssetFileServer(assets),
|
||||
|
@ -1,177 +0,0 @@
|
||||
//go:build darwin
|
||||
|
||||
package application
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -mmacosx-version-min=10.14 -x objective-c
|
||||
#cgo LDFLAGS: -framework Cocoa -mmacosx-version-min=10.14 -framework UserNotifications
|
||||
#import "notifications_darwin.h"
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// NotificationAction represents a button in a notification
|
||||
type NotificationAction struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Destructive bool `json:"destructive,omitempty"`
|
||||
AuthenticationRequired bool `json:"authenticationRequired,omitempty"`
|
||||
}
|
||||
|
||||
// NotificationCategory groups actions for notifications
|
||||
type NotificationCategory struct {
|
||||
ID string `json:"id"`
|
||||
Actions []NotificationAction `json:"actions"`
|
||||
HasReplyField bool `json:"hasReplyField,omitempty"`
|
||||
ReplyPlaceholder string `json:"replyPlaceholder,omitempty"`
|
||||
ReplyButtonTitle string `json:"replyButtonTitle,omitempty"`
|
||||
}
|
||||
|
||||
// NotificationOptions contains configuration for a notification
|
||||
type NotificationOptions struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Subtitle string `json:"subtitle,omitempty"`
|
||||
Body string `json:"body"`
|
||||
CategoryID string `json:"categoryId,omitempty"`
|
||||
Data map[string]interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// Check if the app has a valid bundle identifier
|
||||
func CheckBundleIdentifier() bool {
|
||||
return bool(C.checkBundleIdentifier())
|
||||
}
|
||||
|
||||
// RequestUserNotificationAuthorization requests permission for notifications.
|
||||
func RequestUserNotificationAuthorization() (bool, error) {
|
||||
if !CheckBundleIdentifier() {
|
||||
return false, fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
result := C.requestUserNotificationAuthorization(nil)
|
||||
return result == true, nil
|
||||
}
|
||||
|
||||
// CheckNotificationAuthorization checks current permission status
|
||||
func CheckNotificationAuthorization() (bool, error) {
|
||||
if !CheckBundleIdentifier() {
|
||||
return false, fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
return bool(C.checkNotificationAuthorization()), nil
|
||||
}
|
||||
|
||||
// SendNotification sends a notification with the given identifier, title, subtitle, and body.
|
||||
func SendNotification(identifier, title, subtitle, body string) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
cIdentifier := C.CString(identifier)
|
||||
cTitle := C.CString(title)
|
||||
cSubtitle := C.CString(subtitle)
|
||||
cBody := C.CString(body)
|
||||
defer C.free(unsafe.Pointer(cIdentifier))
|
||||
defer C.free(unsafe.Pointer(cTitle))
|
||||
defer C.free(unsafe.Pointer(cSubtitle))
|
||||
defer C.free(unsafe.Pointer(cBody))
|
||||
|
||||
C.sendNotification(cIdentifier, cTitle, cSubtitle, cBody, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendNotificationWithActions sends a notification with the specified actions
|
||||
func SendNotificationWithActions(options NotificationOptions) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
cIdentifier := C.CString(options.ID)
|
||||
cTitle := C.CString(options.Title)
|
||||
cSubtitle := C.CString(options.Subtitle)
|
||||
cBody := C.CString(options.Body)
|
||||
cCategoryID := C.CString(options.CategoryID)
|
||||
defer C.free(unsafe.Pointer(cIdentifier))
|
||||
defer C.free(unsafe.Pointer(cTitle))
|
||||
defer C.free(unsafe.Pointer(cSubtitle))
|
||||
defer C.free(unsafe.Pointer(cBody))
|
||||
defer C.free(unsafe.Pointer(cCategoryID))
|
||||
|
||||
var cActionsJSON *C.char
|
||||
if options.Data != nil {
|
||||
jsonData, err := json.Marshal(options.Data)
|
||||
if err == nil {
|
||||
cActionsJSON = C.CString(string(jsonData))
|
||||
defer C.free(unsafe.Pointer(cActionsJSON))
|
||||
}
|
||||
}
|
||||
|
||||
C.sendNotificationWithActions(cIdentifier, cTitle, cSubtitle, cBody, cCategoryID, cActionsJSON, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterNotificationCategory registers a category with actions and optional reply field
|
||||
func RegisterNotificationCategory(category NotificationCategory) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
cCategoryID := C.CString(category.ID)
|
||||
defer C.free(unsafe.Pointer(cCategoryID))
|
||||
|
||||
actionsJSON, err := json.Marshal(category.Actions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cActionsJSON := C.CString(string(actionsJSON))
|
||||
defer C.free(unsafe.Pointer(cActionsJSON))
|
||||
|
||||
var cReplyPlaceholder, cReplyButtonTitle *C.char
|
||||
if category.HasReplyField {
|
||||
cReplyPlaceholder = C.CString(category.ReplyPlaceholder)
|
||||
cReplyButtonTitle = C.CString(category.ReplyButtonTitle)
|
||||
defer C.free(unsafe.Pointer(cReplyPlaceholder))
|
||||
defer C.free(unsafe.Pointer(cReplyButtonTitle))
|
||||
}
|
||||
|
||||
C.registerNotificationCategory(cCategoryID, cActionsJSON, C.bool(category.HasReplyField),
|
||||
cReplyPlaceholder, cReplyButtonTitle)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveAllPendingNotifications removes all pending notifications
|
||||
func RemoveAllPendingNotifications() error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
C.removeAllPendingNotifications()
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemovePendingNotification removes a specific pending notification
|
||||
func RemovePendingNotification(identifier string) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
cIdentifier := C.CString(identifier)
|
||||
defer C.free(unsafe.Pointer(cIdentifier))
|
||||
C.removePendingNotification(cIdentifier)
|
||||
return nil
|
||||
}
|
||||
|
||||
func RemoveAllDeliveredNotifications() error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
C.removeAllDeliveredNotifications()
|
||||
return nil
|
||||
}
|
||||
|
||||
func RemoveDeliveredNotification(identifier string) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
cIdentifier := C.CString(identifier)
|
||||
defer C.free(unsafe.Pointer(cIdentifier))
|
||||
C.removeDeliveredNotification(cIdentifier)
|
||||
return nil
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
//go:build darwin
|
||||
|
||||
package notification
|
||||
package notifications
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -mmacosx-version-min=10.14 -x objective-c
|
||||
#cgo LDFLAGS: -framework Cocoa -mmacosx-version-min=10.14 -framework UserNotifications
|
||||
#import "../../application/notifications_darwin.h"
|
||||
#import "./notifications_darwin.h"
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
@ -17,11 +17,11 @@ import (
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
)
|
||||
|
||||
type NotificationService struct {
|
||||
type Service struct {
|
||||
}
|
||||
|
||||
// NotificationAction represents an action button for a notification
|
||||
type NotificationAction struct {
|
||||
type NotificationAction = struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Destructive bool `json:"destructive,omitempty"`
|
||||
@ -29,7 +29,7 @@ type NotificationAction struct {
|
||||
}
|
||||
|
||||
// NotificationCategory groups actions for notifications
|
||||
type NotificationCategory struct {
|
||||
type NotificationCategory = struct {
|
||||
ID string `json:"id"`
|
||||
Actions []NotificationAction `json:"actions"`
|
||||
HasReplyField bool `json:"hasReplyField,omitempty"`
|
||||
@ -38,7 +38,7 @@ type NotificationCategory struct {
|
||||
}
|
||||
|
||||
// NotificationOptions contains configuration for a notification
|
||||
type NotificationOptions struct {
|
||||
type NotificationOptions = struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Subtitle string `json:"subtitle,omitempty"`
|
||||
@ -47,17 +47,21 @@ type NotificationOptions struct {
|
||||
Data map[string]interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func New() *Service {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
func CheckBundleIdentifier() bool {
|
||||
return bool(C.checkBundleIdentifier())
|
||||
}
|
||||
|
||||
// ServiceName returns the name of the service
|
||||
func (ns *NotificationService) ServiceName() string {
|
||||
func (ns *Service) ServiceName() string {
|
||||
return "github.com/wailsapp/wails/v3/services/notifications"
|
||||
}
|
||||
|
||||
// ServiceStartup is called when the service is loaded
|
||||
func (ns *NotificationService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
|
||||
func (ns *Service) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
@ -65,12 +69,12 @@ func (ns *NotificationService) ServiceStartup(ctx context.Context, options appli
|
||||
}
|
||||
|
||||
// ServiceShutdown is called when the service is unloaded
|
||||
func (ns *NotificationService) ServiceShutdown() error {
|
||||
func (ns *Service) ServiceShutdown() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RequestUserNotificationAuthorization requests permission for notifications.
|
||||
func (ns *NotificationService) RequestUserNotificationAuthorization() (bool, error) {
|
||||
func (ns *Service) RequestUserNotificationAuthorization() (bool, error) {
|
||||
if !CheckBundleIdentifier() {
|
||||
return false, fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
@ -79,7 +83,7 @@ func (ns *NotificationService) RequestUserNotificationAuthorization() (bool, err
|
||||
}
|
||||
|
||||
// CheckNotificationAuthorization checks current permission status
|
||||
func (ns *NotificationService) CheckNotificationAuthorization() (bool, error) {
|
||||
func (ns *Service) CheckNotificationAuthorization() (bool, error) {
|
||||
if !CheckBundleIdentifier() {
|
||||
return false, fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
@ -87,7 +91,7 @@ func (ns *NotificationService) CheckNotificationAuthorization() (bool, error) {
|
||||
}
|
||||
|
||||
// SendNotification sends a notification with the given identifier, title, subtitle, and body.
|
||||
func (ns *NotificationService) SendNotification(identifier, title, subtitle, body string) error {
|
||||
func (ns *Service) SendNotification(identifier, title, subtitle, body string) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
@ -105,7 +109,7 @@ func (ns *NotificationService) SendNotification(identifier, title, subtitle, bod
|
||||
}
|
||||
|
||||
// SendNotificationWithActions sends a notification with the specified actions
|
||||
func (ns *NotificationService) SendNotificationWithActions(options NotificationOptions) error {
|
||||
func (ns *Service) SendNotificationWithActions(options NotificationOptions) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
@ -134,7 +138,7 @@ func (ns *NotificationService) SendNotificationWithActions(options NotificationO
|
||||
}
|
||||
|
||||
// RegisterNotificationCategory registers a category with actions and optional reply field
|
||||
func (ns *NotificationService) RegisterNotificationCategory(category NotificationCategory) error {
|
||||
func (ns *Service) RegisterNotificationCategory(category NotificationCategory) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
@ -162,7 +166,7 @@ func (ns *NotificationService) RegisterNotificationCategory(category Notificatio
|
||||
}
|
||||
|
||||
// RemoveAllPendingNotifications removes all pending notifications
|
||||
func (ns *NotificationService) RemoveAllPendingNotifications() error {
|
||||
func (ns *Service) RemoveAllPendingNotifications() error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
@ -171,7 +175,7 @@ func (ns *NotificationService) RemoveAllPendingNotifications() error {
|
||||
}
|
||||
|
||||
// RemovePendingNotification removes a specific pending notification
|
||||
func (ns *NotificationService) RemovePendingNotification(identifier string) error {
|
||||
func (ns *Service) RemovePendingNotification(identifier string) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
@ -181,7 +185,7 @@ func (ns *NotificationService) RemovePendingNotification(identifier string) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ns *NotificationService) RemoveAllDeliveredNotifications() error {
|
||||
func (ns *Service) RemoveAllDeliveredNotifications() error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
||||
@ -189,7 +193,7 @@ func (ns *NotificationService) RemoveAllDeliveredNotifications() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ns *NotificationService) RemoveDeliveredNotification(identifier string) error {
|
||||
func (ns *Service) RemoveDeliveredNotification(identifier string) error {
|
||||
if !CheckBundleIdentifier() {
|
||||
return fmt.Errorf("Notifications require a bundled application with a unique bundle identifier")
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
#import "notifications_darwin.h"
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import <UserNotifications/UserNotifications.h>
|
||||
#import "../events/events_darwin.h"
|
||||
#import "../../events/events_darwin.h"
|
||||
|
||||
extern bool hasListeners(unsigned int);
|
||||
extern void processApplicationEvent(unsigned int, void* data);
|
Loading…
Reference in New Issue
Block a user