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

Commit 1218dd5

Browse files
committed
ui: new OCTOPRINT_TFT_RESOLUTION variable
Signed-off-by: Máximo Cuadros <[email protected]>
1 parent 8079bd2 commit 1218dd5

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ the `.deb` package you can configure it at `/etc/octoprint-tft-environment`.
107107
- `OCTOPRINT_HOST` - OctoPrint HTTP address, default `http://localhost`
108108
- `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”.
109109
- `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.
110-
- `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.
110+
- `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.
111+
- `OCTOPRINT_TFT_RESOLUTION` - Resolution of the application, should be configured to the resolution of your screen, for example `800x480`. By default `480x320`.
112+
111113

112114
### Custom controls and commands
113115

debian/local/octoprint-tft-environment

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ OCTOPRINT_CONFIG_FILE=
1313

1414
# Location of the application theme.
1515
OCTOPRINT_TFT_STYLE_PATH=/opt/octoprint-tft/styles/default/
16+
17+
# Resolution of the application, should be configured to the resolution of your
18+
# screen, for example 800x480. By default 480x320.
19+
OCTOPRINT_TFT_RESOLUTION=

main.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"os"
66
"os/user"
77
"path/filepath"
8+
"strconv"
9+
"strings"
810

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

1416
const (
1517
EnvStylePath = "OCTOPRINT_TFT_STYLE_PATH"
18+
EnvResolution = "OCTOPRINT_TFT_RESOLUTION"
1619
EnvBaseURL = "OCTOPRINT_HOST"
1720
EnvAPIKey = "OCTOPRINT_APIKEY"
1821
EnvConfigFile = "OCTOPRINT_CONFIG_FILE"
@@ -24,11 +27,13 @@ var (
2427
BaseURL string
2528
APIKey string
2629
ConfigFile string
30+
Resolution string
2731
)
2832

2933
func init() {
3034
ui.StylePath = os.Getenv(EnvStylePath)
3135
APIKey = os.Getenv(EnvAPIKey)
36+
Resolution = os.Getenv(EnvResolution)
3237

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

55-
ui.New(BaseURL, APIKey)
60+
width, height := getSize()
61+
_ = ui.New(BaseURL, APIKey, width, height)
62+
5663
gtk.Main()
5764
}
5865

@@ -100,3 +107,32 @@ func doFindConfigFile(home string) string {
100107

101108
return ""
102109
}
110+
111+
func getSize() (width, height int) {
112+
if Resolution == "" {
113+
return
114+
}
115+
116+
parts := strings.SplitN(Resolution, "x", 2)
117+
if len(parts) != 2 {
118+
ui.Logger.Fatalf("Malformed %s variable: %q", EnvResolution, Resolution)
119+
return
120+
}
121+
122+
var err error
123+
width, err = strconv.Atoi(parts[0])
124+
if err != nil {
125+
ui.Logger.Fatalf("Malformed %s variable: %q, %s",
126+
EnvResolution, Resolution, err)
127+
return
128+
}
129+
130+
height, err = strconv.Atoi(parts[0])
131+
if err != nil {
132+
ui.Logger.Fatalf("Malformed %s variable: %q, %s",
133+
EnvResolution, Resolution, err)
134+
return
135+
}
136+
137+
return
138+
}

ui/ui.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,25 @@ type UI struct {
3636
w *gtk.Window
3737
t time.Time
3838

39+
width, height int
3940
sync.Mutex
4041
}
4142

42-
func New(endpoint, key string) *UI {
43+
func New(endpoint, key string, width, height int) *UI {
44+
if width == 0 || height == 0 {
45+
width = WindowWidth
46+
height = WindowHeight
47+
}
48+
4349
ui := &UI{
4450
Printer: octoprint.NewClient(endpoint, key),
4551
Notifications: NewNotifications(),
4652

4753
w: MustWindow(gtk.WINDOW_TOPLEVEL),
4854
t: time.Now(),
55+
56+
width: width,
57+
height: height,
4958
}
5059

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

6069
ui.w.SetTitle(WindowName)
61-
ui.w.SetDefaultSize(WindowWidth, WindowHeight)
70+
ui.w.SetDefaultSize(ui.width, ui.height)
71+
6272
ui.w.Connect("show", ui.b.Start)
6373
ui.w.Connect("destroy", func() {
6474
gtk.MainQuit()

0 commit comments

Comments
 (0)