Skip to content

Commit b373456

Browse files
committed
feat: launch at login
1 parent 340c29b commit b373456

File tree

9 files changed

+66
-19
lines changed

9 files changed

+66
-19
lines changed

FyneApp.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ Website = "https://fujinet.online"
44
Icon = "assets/Icon.png"
55
Name = "TNFS Server Manager"
66
ID = "org.fujinet.tnfs-ui"
7-
Version = "0.0.2"
8-
Build = 23
7+
Version = "0.0.3"
8+
Build = 33

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/BurntSushi/toml v1.4.0 // indirect
1414
github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf // indirect
1515
github.com/davecgh/go-spew v1.1.1 // indirect
16+
github.com/emersion/go-autostart v0.0.0-20210130080809-00ed301c8e9a // indirect
1617
github.com/fredbi/uri v1.1.0 // indirect
1718
github.com/fsnotify/fsnotify v1.7.0 // indirect
1819
github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
6767
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6868
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6969
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
70+
github.com/emersion/go-autostart v0.0.0-20210130080809-00ed301c8e9a h1:M88ob4TyDnEqNuL3PgsE/p3bDujfspnulR+0dQWNYZs=
71+
github.com/emersion/go-autostart v0.0.0-20210130080809-00ed301c8e9a/go.mod h1:buzQsO8HHkZX2Q45fdfGH1xejPjuDQaXH8btcYMFzPM=
7072
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
7173
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
7274
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=

internal/config/config.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"runtime"
1010

1111
"fyne.io/fyne/v2"
12+
"github.com/emersion/go-autostart"
1213
)
1314

1415
const (
1516
TNFS_ROOT_PATH_KEY = "tnfsRootPath"
1617
ALLOW_BACKGROUND_KEY = "allowBackground"
17-
START_AT_LOGIN_KEY = "startAtLogin"
1818
)
1919

2020
type Config struct {
@@ -23,6 +23,7 @@ type Config struct {
2323
Hostname string
2424
AllowBackground bool
2525
StartAtLogin bool
26+
autostartApp *autostart.App
2627
}
2728

2829
func (c *Config) SetRootPath(newPath string) {
@@ -35,9 +36,16 @@ func (c *Config) SetAllowBackground(newVal bool) {
3536
fyne.CurrentApp().Preferences().SetBool(ALLOW_BACKGROUND_KEY, newVal)
3637
}
3738

38-
func (c *Config) SetStartAtLogin(newVal bool) {
39-
c.StartAtLogin = newVal
40-
fyne.CurrentApp().Preferences().SetBool(START_AT_LOGIN_KEY, newVal)
39+
func (c *Config) SetStartAtLogin(enable bool) {
40+
var err error
41+
if enable {
42+
err = c.autostartApp.Enable()
43+
} else {
44+
err = c.autostartApp.Disable()
45+
}
46+
if err == nil {
47+
c.StartAtLogin = enable
48+
}
4149
}
4250

4351
func LoadConfig() (*Config, error) {
@@ -47,12 +55,16 @@ func LoadConfig() (*Config, error) {
4755
}
4856

4957
prefs := fyne.CurrentApp().Preferences()
58+
autostartApp, _ := makeAutostartApp()
59+
5060
cfg := &Config{
5161
ExePath: exePath,
5262
Hostname: getHostnameOrIP(),
5363
TnfsRootPath: getRootPath(prefs),
5464
AllowBackground: prefs.BoolWithFallback(ALLOW_BACKGROUND_KEY, false),
55-
StartAtLogin: prefs.BoolWithFallback(START_AT_LOGIN_KEY, false),
65+
StartAtLogin: autostartApp.IsEnabled(),
66+
67+
autostartApp: autostartApp,
5668
}
5769
return cfg, nil
5870
}
@@ -89,7 +101,6 @@ func locateTnfsdExecutable() (string, error) {
89101
}
90102

91103
exePath := filepath.Join(dir, exeName)
92-
exePath = "bin/tnfsd-bsd"
93104

94105
fmt.Println(currentExePath)
95106
fmt.Println(exePath)
@@ -109,3 +120,17 @@ func getRootPath(prefs fyne.Preferences) string {
109120
}
110121
return prefs.StringWithFallback(TNFS_ROOT_PATH_KEY, dirname)
111122
}
123+
124+
func makeAutostartApp() (*autostart.App, error) {
125+
a := fyne.CurrentApp()
126+
currentExePath, err := os.Executable()
127+
if err != nil {
128+
return nil, err
129+
}
130+
aa := &autostart.App{
131+
Name: a.Metadata().ID,
132+
DisplayName: a.Metadata().Name,
133+
Exec: []string{currentExePath, "autorun"},
134+
}
135+
return aa, nil
136+
}

internal/ui/system_tray.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ func makeSystemMenu(ui *UI, server *tnfs.Server) *fyne.Menu {
2929
})
3030

3131
m := fyne.NewMenu("TNFS Server Manager",
32-
fyne.NewMenuItem("Show TNFS Server Manager", func() {
32+
fyne.NewMenuItem("Open TNFS Server Manager", func() {
3333
ui.MainWindow.Show()
34+
if ui.cfg.AllowBackground {
35+
ShowInDock()
36+
}
3437
}),
3538
fyne.NewMenuItemSeparator(),
3639
startStop,

internal/ui/tab_options.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ import (
99
func makeOptionsTab(ui *UI) *fyne.Container {
1010
allowBackgroundCheck := widget.NewCheck("Run in background", func(checked bool) {
1111
ui.cfg.SetAllowBackground(checked)
12-
if checked {
13-
HideFromDock()
14-
} else {
15-
ShowInDock()
16-
}
1712
})
1813
startAtLoginCheck := widget.NewCheck("Start automatically at login", func(checked bool) {
1914
ui.cfg.SetStartAtLogin(checked)

internal/ui/tab_server.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func makeServerTab(ui *UI, server *tnfs.Server) *fyne.Container {
4141
})
4242
stopButton.Hide()
4343

44-
ui.On(tnfs.StatusChange, func(e tnfs.Event) {
44+
updateStatus := func() {
4545
var msg string
4646
var icon fyne.Resource
4747

@@ -53,6 +53,7 @@ func makeServerTab(ui *UI, server *tnfs.Server) *fyne.Container {
5353
stopButton.Hide()
5454
startButton.Show()
5555
startButton.Enable()
56+
dirPickerButton.Enable()
5657
case tnfs.FAILED:
5758
msg = "Error"
5859
icon = theme.NewColoredResource(theme.WarningIcon(), theme.ColorNameWarning)
@@ -63,27 +64,36 @@ func makeServerTab(ui *UI, server *tnfs.Server) *fyne.Container {
6364
stopButton.Hide()
6465
startButton.Show()
6566
startButton.Enable()
67+
dirPickerButton.Enable()
6668
case tnfs.STOPPING:
6769
msg = "Stopping..."
6870
icon = theme.NewColoredResource(theme.MediaRecordIcon(), theme.ColorNameWarning)
6971

7072
stopButton.Disable()
73+
dirPickerButton.Disable()
7174
case tnfs.STARTING:
7275
msg = "Starting..."
7376
icon = theme.NewColoredResource(theme.MediaRecordIcon(), theme.ColorNameWarning)
7477

7578
startButton.Disable()
79+
dirPickerButton.Disable()
7680
case tnfs.STARTED:
7781
msg = "Running on " + ui.cfg.Hostname
7882
icon = theme.NewColoredResource(theme.MediaRecordIcon(), theme.ColorNameSuccess)
7983

8084
startButton.Hide()
8185
stopButton.Show()
8286
stopButton.Enable()
87+
dirPickerButton.Disable()
8388
}
8489
statusText.SetText(msg)
8590
statusIcon.SetResource(icon)
91+
}
92+
93+
ui.On(tnfs.StatusChange, func(_ tnfs.Event) {
94+
updateStatus()
8695
})
96+
updateStatus()
8797

8898
return container.NewVBox(
8999
dirPickerLabel,

internal/ui/ui.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ func (ui *UI) Listen(eventch chan tnfs.Event) {
3636
}
3737

3838
func (ui *UI) ShowMain() {
39-
if ui.cfg.AllowBackground {
40-
HideFromDock()
41-
}
4239
ui.MainWindow.ShowAndRun()
4340
ui.MainWindow.SetMaster()
4441
}
@@ -81,6 +78,7 @@ func makeMainWindow(ui *UI) fyne.Window {
8178
w.SetCloseIntercept(func() {
8279
if ui.cfg.AllowBackground {
8380
w.Hide()
81+
HideFromDock()
8482
} else {
8583
w.Close()
8684
a.Quit()

main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"os"
5+
46
"fyne.io/fyne/v2"
57
"fyne.io/fyne/v2/app"
68
"github.com/fujiNetWIFI/tnfs-gui/internal/config"
@@ -22,5 +24,16 @@ func main() {
2224
server := tnfs.NewServer(cfg, events)
2325

2426
u := ui.NewUI(cfg, server, events)
25-
u.ShowMain()
27+
28+
if len(os.Args) > 1 && os.Args[1] == "autorun" {
29+
go server.Start()
30+
if cfg.AllowBackground {
31+
ui.HideFromDock()
32+
a.Run()
33+
} else {
34+
u.ShowMain()
35+
}
36+
} else {
37+
u.ShowMain()
38+
}
2639
}

0 commit comments

Comments
 (0)