summaryrefslogtreecommitdiff
path: root/window.go
blob: 427e32bc6fa13e82d8f17f5977cd9ebae14fb96b (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// 11 february 2014
//package ui
package main

import (
	"sync"
)

// TODO adorn errors in each stage with which stage failed?

// Window represents an on-screen window.
type Window struct {
	// If this channel is non-nil, the event loop will receive on this when the user clicks the window's close button.
	// This channel can only be set before initially opening the window.
	Closing		chan struct{}

	lock			sync.Mutex
	created		bool
	sysData		*sysData
	initTitle		string
	initWidth		int
	initHeight		int
}

// NewWindow creates a new window with the given title and size. The window is not constructed at the OS level until a call to Open().
func NewWindow(title string, width int, height int) *Window {
	return &Window{
		sysData:		mksysdata(c_window),
		initTitle:		title,
		initWidth:		width,
		initHeight:	height,
	}
}

// SetTitle sets the window's title.
func (w *Window) SetTitle(title string) (err error) {
	w.lock.Lock()
	defer w.lock.Unlock()

	if w.created {
		return w.sysData.setText(title)
	}
	w.initTitle = title
	return nil
}

// SetSize sets the window's size.
func (w *Window) SetSize(width int, height int) (err error) {
	w.lock.Lock()
	defer w.lock.Unlock()

	if w.created {
		panic("TODO")
	}
	w.initWidth = width
	w.initHeight = height
	return nil
}

// Open opens the window, setting its control to the given control, and then shows the window. This can only be called once per window, and finalizes all initialization of the control.
// TODO rename?
func (w *Window) Open(control Control) (err error) {
	w.lock.Lock()
	defer w.lock.Unlock()

	if w.created {
		// TODO return an error instead?
		panic("window already open")
	}
	w.sysData.event = w.Closing
	err = w.sysData.make(w.initTitle, w.initWidth, w.initHeight, nil)
	if err != nil {
		return err
	}
	if control != nil {
		w.sysData.resize = control.setRect
		err = control.make(w.sysData)
		if err != nil {
			return err
		}
	}
	w.created = true
	// TODO resize window to apply control sizes
	// TODO ensure the window has been shown before setting create?
	return w.sysData.show()
}

// Show shows the window.
func (w *Window) Show() (err error) {
	return w.sysData.show()
}

// Hide hides the window.
func (w *Window) Hide() (err error) {
	return w.sysData.hide()
}