1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-07-20 06:28:04 +00:00

Moved UI methods to their own struct

This commit is contained in:
2023-02-21 17:47:21 +01:00
parent 5b1f410a50
commit 44c94d0d72
14 changed files with 124 additions and 74 deletions

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
}
}

6
internal/ui/open.go Normal file
View File

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

View File

@ -0,0 +1,9 @@
//go:build darwin
package ui
import "os/exec"
func reveal(fname string) {
exec.Command("open", "--reveal", fname).Run()
}

View File

@ -0,0 +1,9 @@
//go:build windows
package ui
import "os/exec"
func reveal(fname string) {
exec.Command("explorer", fname).Run()
}

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)
}