1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 13:07:58 +00:00

Moved UI methods to their own struct

This commit is contained in:
Romein van Buren 2023-02-21 17:47:21 +01:00
parent 5b1f410a50
commit 44c94d0d72
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
14 changed files with 124 additions and 74 deletions

View File

@ -1,5 +1,5 @@
<script>
import { OpenDirectory } from '$wails/go/app/App';
import { OpenDirectory } from '$wails/go/ui/UI';
export let value = '';
export let id = '';

View File

@ -3,7 +3,7 @@
</script>
<script>
import { Beep } from '$wails/go/app/App';
import { Beep } from '$wails/go/ui/UI';
import { fade, fly } from 'svelte/transition';
import Icon from './icon.svelte';

View File

@ -1,7 +1,8 @@
<script>
import busy from '$lib/stores/busy';
import { connections } from '$lib/stores/connections';
import { EnterText, Hosts, RenameCollection } from '$wails/go/app/App';
import { Hosts, RenameCollection } from '$wails/go/app/App';
import { EnterText } from '$wails/go/ui/UI';
import { EventsOn } from '$wails/runtime/runtime';
import { onMount } from 'svelte';
import CollectionDetail from './collection/index.svelte';

View File

@ -4,13 +4,13 @@ import {app} from '../models';
import {primitive} from '../models';
import {map[string]app} from '../models';
import {menu} from '../models';
import {context} from '../models';
import {ui} from '../models';
export function AddHost(arg1:string):Promise<void>;
export function Aggregate(arg1:string,arg2:string,arg3:string,arg4:string,arg5:string):Promise<void>;
export function Beep():Promise<void>;
export function CreateIndex(arg1:string,arg2:string,arg3:string,arg4:string):Promise<string>;
export function DropCollection(arg1:string,arg2:string,arg3:string):Promise<boolean>;
@ -19,8 +19,6 @@ export function DropDatabase(arg1:string,arg2:string):Promise<boolean>;
export function DropIndex(arg1:string,arg2:string,arg3:string,arg4:string):Promise<boolean>;
export function EnterText(arg1:string,arg2:string,arg3:string):Promise<string>;
export function Environment():Promise<app.EnvironmentInfo>;
export function FindItems(arg1:string,arg2:string,arg3:string,arg4:string):Promise<app.QueryResult>;
@ -39,8 +37,6 @@ export function OpenConnection(arg1:string):Promise<Array<string>>;
export function OpenDatabase(arg1:string,arg2:string):Promise<Array<string>>;
export function OpenDirectory(arg1:string,arg2:string):Promise<string>;
export function PerformExport(arg1:string):Promise<boolean>;
export function PurgeLogDirectory():Promise<void>;
@ -63,6 +59,8 @@ export function SavedQueries():Promise<map[string]app.SavedQuery>;
export function Settings():Promise<app.Settings>;
export function Startup(arg1:context.Context,arg2:ui.UI):Promise<void>;
export function TruncateCollection(arg1:string,arg2:string,arg3:string):Promise<boolean>;
export function UpdateHost(arg1:string,arg2:string):Promise<void>;

View File

@ -10,10 +10,6 @@ export function Aggregate(arg1, arg2, arg3, arg4, arg5) {
return window['go']['app']['App']['Aggregate'](arg1, arg2, arg3, arg4, arg5);
}
export function Beep() {
return window['go']['app']['App']['Beep']();
}
export function CreateIndex(arg1, arg2, arg3, arg4) {
return window['go']['app']['App']['CreateIndex'](arg1, arg2, arg3, arg4);
}
@ -30,10 +26,6 @@ export function DropIndex(arg1, arg2, arg3, arg4) {
return window['go']['app']['App']['DropIndex'](arg1, arg2, arg3, arg4);
}
export function EnterText(arg1, arg2, arg3) {
return window['go']['app']['App']['EnterText'](arg1, arg2, arg3);
}
export function Environment() {
return window['go']['app']['App']['Environment']();
}
@ -70,10 +62,6 @@ export function OpenDatabase(arg1, arg2) {
return window['go']['app']['App']['OpenDatabase'](arg1, arg2);
}
export function OpenDirectory(arg1, arg2) {
return window['go']['app']['App']['OpenDirectory'](arg1, arg2);
}
export function PerformExport(arg1) {
return window['go']['app']['App']['PerformExport'](arg1);
}
@ -118,6 +106,10 @@ export function Settings() {
return window['go']['app']['App']['Settings']();
}
export function Startup(arg1, arg2) {
return window['go']['app']['App']['Startup'](arg1, arg2);
}
export function TruncateCollection(arg1, arg2, arg3) {
return window['go']['app']['App']['TruncateCollection'](arg1, arg2, arg3);
}

13
frontend/wailsjs/go/ui/UI.d.ts vendored Executable file
View File

@ -0,0 +1,13 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {context} from '../models';
export function Beep():Promise<void>;
export function EnterText(arg1:string,arg2:string,arg3:string):Promise<string>;
export function OpenDirectory(arg1:string,arg2:string):Promise<string>;
export function Reveal(arg1:string):Promise<void>;
export function Startup(arg1:context.Context):Promise<void>;

23
frontend/wailsjs/go/ui/UI.js Executable file
View File

@ -0,0 +1,23 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function Beep() {
return window['go']['ui']['UI']['Beep']();
}
export function EnterText(arg1, arg2, arg3) {
return window['go']['ui']['UI']['EnterText'](arg1, arg2, arg3);
}
export function OpenDirectory(arg1, arg2) {
return window['go']['ui']['UI']['OpenDirectory'](arg1, arg2);
}
export function Reveal(arg1) {
return window['go']['ui']['UI']['Reveal'](arg1);
}
export function Startup(arg1) {
return window['go']['ui']['UI']['Startup'](arg1);
}

View File

@ -3,14 +3,12 @@ package app
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/garraflavatra/rolens/internal/open_file"
"github.com/gen2brain/beeep"
"github.com/garraflavatra/rolens/internal/ui"
"github.com/ncruces/zenity"
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/wailsapp/wails/v2/pkg/menu/keys"
@ -32,6 +30,7 @@ type EnvironmentInfo struct {
type App struct {
ctx context.Context
ui *ui.UI
Env EnvironmentInfo
}
@ -69,8 +68,9 @@ func NewApp() *App {
return a
}
func (a *App) Startup(ctx context.Context) {
func (a *App) Startup(ctx context.Context, ui *ui.UI) {
a.ctx = ctx
a.ui = ui
wailsRuntime.LogInfo(a.ctx, "Startup")
wailsEnv := wailsRuntime.Environment(a.ctx)
@ -113,8 +113,9 @@ func (a *App) Menu() *menu.Menu {
aboutMenu := appMenu.AddSubmenu("About")
aboutMenu.AddText("About…", nil, menuEventEmitter(a, "OpenAboutModal"))
aboutMenu.AddText("Prefrences…", keys.CmdOrCtrl(","), menuEventEmitter(a, "OpenPrefrences"))
aboutMenu.AddText("Open data directory…", nil, func(cd *menu.CallbackData) { a.ui.Reveal(a.Env.DataDirectory) })
aboutMenu.AddSeparator()
aboutMenu.AddText("Open log directory…", nil, func(cd *menu.CallbackData) { open_file.Reveal(a.Env.LogDirectory) })
aboutMenu.AddText("Open log directory…", nil, func(cd *menu.CallbackData) { a.ui.Reveal(a.Env.LogDirectory) })
aboutMenu.AddText("Purge logs", nil, func(cd *menu.CallbackData) { a.PurgeLogDirectory() })
aboutMenu.AddSeparator()
aboutMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(cd *menu.CallbackData) { wailsRuntime.Quit(a.ctx) })
@ -144,45 +145,3 @@ func (a *App) Menu() *menu.Menu {
return appMenu
}
func (a *App) OpenDirectory(id, title string) string {
if title == "" {
title = "Choose a directory"
}
wailsRuntime.LogInfo(a.ctx, fmt.Sprintf("Opening directory ('%v')", title))
dir, err := zenity.SelectFile(zenity.Title(title), zenity.Directory())
if err != nil && err != zenity.ErrCanceled {
wailsRuntime.LogWarning(a.ctx, "Encountered an error while opening directory:")
wailsRuntime.LogWarning(a.ctx, err.Error())
wailsRuntime.MessageDialog(a.ctx, wailsRuntime.MessageDialogOptions{
Type: wailsRuntime.ErrorDialog,
Title: "Encountered an error while opening directory",
Message: err.Error(),
})
}
wailsRuntime.LogInfo(a.ctx, "Chosen directory: "+dir)
return dir
}
func (a *App) EnterText(title, info, defaultEntry string) string {
input, err := zenity.Entry(info, zenity.Title(title), zenity.EntryText(defaultEntry))
if err == zenity.ErrCanceled {
return ""
} else if err != nil {
zenity.Error(err.Error(), zenity.Title("Encountered an error!"), zenity.ErrorIcon)
return ""
} else {
return input
}
}
func (a *App) Beep() {
if runtime.GOOS == "windows" {
return
}
beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
}

30
internal/ui/dialogs.go Normal file
View File

@ -0,0 +1,30 @@
package ui
import "github.com/ncruces/zenity"
func (u *UI) OpenDirectory(id, title string) string {
if title == "" {
title = "Choose a directory"
}
dir, err := zenity.SelectFile(zenity.Title(title), zenity.Directory())
if err != nil && err != zenity.ErrCanceled {
zenity.Error("Error while opening directory", zenity.ErrorIcon)
}
return dir
}
func (u *UI) EnterText(title, info, defaultEntry string) string {
input, err := zenity.Entry(info, zenity.Title(title), zenity.EntryText(defaultEntry))
if err == zenity.ErrCanceled {
return ""
} else if err != nil {
zenity.Error(err.Error(), zenity.Title("Encountered an error!"), zenity.ErrorIcon)
return ""
} else {
return input
}
}

View File

@ -1,6 +1,6 @@
package open_file
package ui
// Reveal reveals the specified file in the Finder.
func Reveal(fname string) {
func (u *UI) Reveal(fname string) {
reveal(fname)
}

View File

@ -1,6 +1,6 @@
//go:build darwin
package open_file
package ui
import "os/exec"

View File

@ -1,6 +1,6 @@
//go:build windows
package open_file
package ui
import "os/exec"

27
internal/ui/ui.go Normal file
View File

@ -0,0 +1,27 @@
package ui
import (
"context"
"runtime"
"github.com/gen2brain/beeep"
)
type UI struct {
ctx context.Context
}
func New() *UI {
return &UI{}
}
func (u *UI) Startup(ctx context.Context) {
u.ctx = ctx
}
func (u *UI) Beep() {
if runtime.GOOS == "windows" {
return
}
beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
}

11
main.go
View File

@ -1,10 +1,12 @@
package main
import (
"context"
"embed"
"path"
"github.com/garraflavatra/rolens/internal/app"
uictrl "github.com/garraflavatra/rolens/internal/ui"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/options"
@ -22,6 +24,8 @@ var (
func main() {
app := app.NewApp()
ui := uictrl.New()
err := wails.Run(&options.App{
Title: "Rolens",
@ -32,10 +36,13 @@ func main() {
BackgroundColour: &options.RGBA{R: 0, G: 0, B: 139, A: 1},
Menu: app.Menu(),
Bind: []interface{}{app},
Bind: []interface{}{app, ui},
AssetServer: &assetserver.Options{Assets: assets},
OnStartup: app.Startup,
OnStartup: func(ctx context.Context) {
app.Startup(ctx, ui)
ui.Startup(ctx)
},
OnShutdown: app.Shutdown,
Logger: logger.NewFileLogger(path.Join(app.Env.LogDirectory, "rolens.log")),