Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Get and set saved window size
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed May 8, 2020
1 parent 34f1e58 commit ca2c3a5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
63 changes: 63 additions & 0 deletions app/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"errors"
"fmt"
"sync"
"time"

"github.com/safing/portbase/api/client"
"github.com/safing/portbase/formats/dsd"
)

var (
apiClient = client.NewClient("127.0.0.1:817")
initAPI sync.Once
)

type WindowConfig struct {
Height int
Width int
}

func getWindowConfig() (*WindowConfig, error) {
window := &WindowConfig{}
return window, getRecord(window)
}

func getRecord(dataStruct interface{}) error {
initAPI.Do(connectToAPI)

errs := make(chan error)
apiClient.Get("core:ui/app/window", parseDataFn(dataStruct, errs))

select {
case err := <-errs:
if err != nil {
return err
}
return nil
case <-time.After(5 * time.Second):
return errors.New("request timed out")
}
}

func connectToAPI() {
go apiClient.StayConnected()
}

func parseDataFn(dataStruct interface{}, errs chan error) func(*client.Message) {
return func(m *client.Message) {
switch m.Type {
case client.MsgOk, client.MsgUpdate, client.MsgNew:
_, err := dsd.Load(m.RawValue, dataStruct)
if err != nil {
errs <- fmt.Errorf("failed to parse message: %s", err)
} else {
close(errs)
}
default:
errs <- fmt.Errorf("received unexpected reply: %s %s", m.Type, m.Value)
}
}
}
15 changes: 14 additions & 1 deletion app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ func main() {
// handle invokes
ExternalInvokeCallback: handleExternalInvokeCallback,
}

wCfg, err := getWindowConfig()
if err != nil {
log.Warningf("failed to get window config: %s", err)
} else if wCfg != nil {
if wCfg.Height > 0 {
settings.Height = wCfg.Height
}
if wCfg.Width > 0 {
settings.Width = wCfg.Width
}
}

wv := webview.New(settings)

// register helper to open links in default browser
Expand All @@ -127,7 +140,7 @@ func main() {
go shutdownHandler(wv)

// render
wv.SetColor(68, 68, 68, 1)
// wv.SetColor(68, 68, 68, 1)
wv.Run()
}

Expand Down

0 comments on commit ca2c3a5

Please sign in to comment.