Skip to content
This repository has been archived by the owner on Nov 24, 2019. It is now read-only.

Commit

Permalink
ui: new OCTOPRINT_TFT_RESOLUTION variable
Browse files Browse the repository at this point in the history
Signed-off-by: Máximo Cuadros <[email protected]>
  • Loading branch information
mcuadros committed Jan 6, 2018
1 parent 8079bd2 commit 1218dd5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ the `.deb` package you can configure it at `/etc/octoprint-tft-environment`.
- `OCTOPRINT_HOST` - OctoPrint HTTP address, default `http://localhost`
- `OCTOPRINT_APIKEY` - OctoPrint-TFT expects an [API key]( http://docs.octoprint.org/en/master/api/general.html) to be supplied. This API key can be either the globally configured one or a user specific one if “Access Control”.
- `OCTOPRINT_CONFIG_FILE` - Location of the OctoPrint's config.yaml file, if `OCTOPRINT_APIKEY` is empty a the global API key will be read from the config file. If empty the file will be searched at the `pi` home folder or the current user.
- `OCTOPRINT_TFT_STYLE_PATH` - Several themes are supported, and style configurations can be done through CSS. This variable defines the location of the application theme.
- `OCTOPRINT_TFT_STYLE_PATH` - Several themes are supported, and style configurations can be done through CSS. This variable defines the location of the application theme.
- `OCTOPRINT_TFT_RESOLUTION` - Resolution of the application, should be configured to the resolution of your screen, for example `800x480`. By default `480x320`.


### Custom controls and commands

Expand Down
4 changes: 4 additions & 0 deletions debian/local/octoprint-tft-environment
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ OCTOPRINT_CONFIG_FILE=

# Location of the application theme.
OCTOPRINT_TFT_STYLE_PATH=/opt/octoprint-tft/styles/default/

# Resolution of the application, should be configured to the resolution of your
# screen, for example 800x480. By default 480x320.
OCTOPRINT_TFT_RESOLUTION=
38 changes: 37 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"os/user"
"path/filepath"
"strconv"
"strings"

"github.com/gotk3/gotk3/gtk"
"github.com/mcuadros/OctoPrint-TFT/ui"
Expand All @@ -13,6 +15,7 @@ import (

const (
EnvStylePath = "OCTOPRINT_TFT_STYLE_PATH"
EnvResolution = "OCTOPRINT_TFT_RESOLUTION"
EnvBaseURL = "OCTOPRINT_HOST"
EnvAPIKey = "OCTOPRINT_APIKEY"
EnvConfigFile = "OCTOPRINT_CONFIG_FILE"
Expand All @@ -24,11 +27,13 @@ var (
BaseURL string
APIKey string
ConfigFile string
Resolution string
)

func init() {
ui.StylePath = os.Getenv(EnvStylePath)
APIKey = os.Getenv(EnvAPIKey)
Resolution = os.Getenv(EnvResolution)

BaseURL = os.Getenv(EnvBaseURL)
if BaseURL == "" {
Expand All @@ -52,7 +57,9 @@ func main() {
settings, _ := gtk.SettingsGetDefault()
settings.SetProperty("gtk-application-prefer-dark-theme", true)

ui.New(BaseURL, APIKey)
width, height := getSize()
_ = ui.New(BaseURL, APIKey, width, height)

gtk.Main()
}

Expand Down Expand Up @@ -100,3 +107,32 @@ func doFindConfigFile(home string) string {

return ""
}

func getSize() (width, height int) {
if Resolution == "" {
return
}

parts := strings.SplitN(Resolution, "x", 2)
if len(parts) != 2 {
ui.Logger.Fatalf("Malformed %s variable: %q", EnvResolution, Resolution)
return
}

var err error
width, err = strconv.Atoi(parts[0])
if err != nil {
ui.Logger.Fatalf("Malformed %s variable: %q, %s",
EnvResolution, Resolution, err)
return
}

height, err = strconv.Atoi(parts[0])
if err != nil {
ui.Logger.Fatalf("Malformed %s variable: %q, %s",
EnvResolution, Resolution, err)
return
}

return
}
14 changes: 12 additions & 2 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,25 @@ type UI struct {
w *gtk.Window
t time.Time

width, height int
sync.Mutex
}

func New(endpoint, key string) *UI {
func New(endpoint, key string, width, height int) *UI {
if width == 0 || height == 0 {
width = WindowWidth
height = WindowHeight
}

ui := &UI{
Printer: octoprint.NewClient(endpoint, key),
Notifications: NewNotifications(),

w: MustWindow(gtk.WINDOW_TOPLEVEL),
t: time.Now(),

width: width,
height: height,
}

ui.b = NewBackgroundTask(time.Second*5, ui.verifyConnection)
Expand All @@ -58,7 +67,8 @@ func (ui *UI) initialize() {
ui.loadStyle()

ui.w.SetTitle(WindowName)
ui.w.SetDefaultSize(WindowWidth, WindowHeight)
ui.w.SetDefaultSize(ui.width, ui.height)

ui.w.Connect("show", ui.b.Start)
ui.w.Connect("destroy", func() {
gtk.MainQuit()
Expand Down

0 comments on commit 1218dd5

Please sign in to comment.