aboutsummaryrefslogtreecommitdiff
path: root/app/dialog.go
blob: fba0fd7b8711d3c48276bfa9ab950de019eb1fb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package app

import (
	"git.sr.ht/~rjarry/aerc/lib/ui"
)

type Dialog interface {
	ui.DrawableInteractive
	ContextWidth() (func(int) int, func(int) int)
	ContextHeight() (func(int) int, func(int) int)
}

type dialog struct {
	ui.DrawableInteractive
	x func(int) int
	y func(int) int
	w func(int) int
	h func(int) int
}

func (d *dialog) ContextWidth() (func(int) int, func(int) int) {
	return d.x, d.w
}

func (d *dialog) ContextHeight() (func(int) int, func(int) int) {
	return d.y, d.h
}

func NewDialog(
	d ui.DrawableInteractive,
	x func(int) int, y func(int) int,
	w func(int) int, h func(int) int,
) Dialog {
	return &dialog{DrawableInteractive: d, x: x, y: y, w: w, h: h}
}

// DefaultDialog creates a dialog window spanning half of the screen
func DefaultDialog(d ui.DrawableInteractive) Dialog {
	position := SelectedAccountUiConfig().DialogPosition
	width := SelectedAccountUiConfig().DialogWidth
	height := SelectedAccountUiConfig().DialogHeight
	return NewDialog(d,
		// horizontal starting position in columns from the left
		func(w int) int {
			return (w * (100 - width)) / 200
		},
		// vertical starting position in lines from the top
		func(h int) int {
			switch position {
			case "center":
				return (h * (100 - height)) / 200
			case "bottom":
				return h - (h * height / 100)
			default:
				return 1
			}
		},
		// dialog width from the starting column
		func(w int) int {
			return w * width / 100
		},
		// dialog height from the starting line
		func(h int) int {
			if position == "bottom" {
				return h*height/100 - 1
			}
			return h * height / 100
		},
	)
}