| 1 |
package ui |
| 2 |
|
| 3 |
import "go.rockorager.dev/vaxis" |
| 4 |
|
| 5 |
type Popover struct { |
| 6 |
x, y, width, height int |
| 7 |
content Drawable |
| 8 |
} |
| 9 |
|
| 10 |
func (p *Popover) Draw(ctx *Context) { |
| 11 |
var subcontext *Context |
| 12 |
|
| 13 |
// trim desired width to fit |
| 14 |
width := p.width |
| 15 |
if p.x+p.width > ctx.Width() { |
| 16 |
width = ctx.Width() - p.x |
| 17 |
} |
| 18 |
|
| 19 |
switch { |
| 20 |
case p.y+p.height+1 < ctx.Height(): |
| 21 |
// draw below |
| 22 |
subcontext = ctx.Subcontext(p.x, p.y+1, width, p.height) |
| 23 |
case p.y-p.height >= 0: |
| 24 |
// draw above |
| 25 |
subcontext = ctx.Subcontext(p.x, p.y-p.height, width, p.height) |
| 26 |
default: |
| 27 |
// can't fit entirely above or below, so find the largest available |
| 28 |
// vertical space and shrink to fit |
| 29 |
if p.y > ctx.Height()-p.y { |
| 30 |
// there is more space above than below |
| 31 |
height := p.y |
| 32 |
subcontext = ctx.Subcontext(p.x, 0, width, height) |
| 33 |
} else { |
| 34 |
// there is more space below than above |
| 35 |
height := ctx.Height() - p.y |
| 36 |
subcontext = ctx.Subcontext(p.x, p.y+1, width, height-1) |
| 37 |
} |
| 38 |
} |
| 39 |
p.content.Draw(subcontext) |
| 40 |
} |
| 41 |
|
| 42 |
func (p *Popover) Event(e vaxis.Event) bool { |
| 43 |
if di, ok := p.content.(DrawableInteractive); ok { |
| 44 |
return di.Event(e) |
| 45 |
} |
| 46 |
return false |
| 47 |
} |
| 48 |
|
| 49 |
func (p *Popover) Focus(f bool) { |
| 50 |
if di, ok := p.content.(DrawableInteractive); ok { |
| 51 |
di.Focus(f) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func (p *Popover) Invalidate() { |
| 56 |
Invalidate() |
| 57 |
} |