<commit_before>// 11 february 2014
package ui

import (
	"fmt"
	"sync"
)

// 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 {
		err = w.sysData.setText(title)
		if err != nil {
			return fmt.Errorf("error setting window title: %v", err)
		}
		return nil
	}
	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 {
		err := w.sysData.setWindowSize(width, height)
		if err != nil {
			return fmt.Errorf("error setting window size: %v", err)
		}
		return nil
	}
	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, nil)
	if err != nil {
		return fmt.Errorf("error opening window: %v", err)
	}
	if control != nil {
		w.sysData.resize = control.setRect
		err = control.make(w.sysData)
		if err != nil {
			return fmt.Errorf("error adding window's control: %v", err)
		}
	}
	err = w.sysData.setWindowSize(w.initWidth, w.initHeight)
	if err != nil {
		return fmt.Errorf("error setting window size (in Window.Open()): %v", err)
	}
	// TODO separate showing?
	err = w.sysData.show()
	if err != nil {
		return fmt.Errorf("error showing window (in Window.Open()): %v", err)
	}
	w.created = true
	return nil
}

// Show shows the window.
func (w *Window) Show() (err error) {
	err = w.sysData.show()<commit_msg>Potential fix: show the window before getting control preferred sizes; need to make setRect() locking first.<commit_after>// 11 february 2014
package ui

import (
	"fmt"
	"sync"
)

// 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 {
		err = w.sysData.setText(title)
		if err != nil {
			return fmt.Errorf("error setting window title: %v", err)
		}
		return nil
	}
	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 {
		err := w.sysData.setWindowSize(width, height)
		if err != nil {
			return fmt.Errorf("error setting window size: %v", err)
		}
		return nil
	}
	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, nil)
	if err != nil {
		return fmt.Errorf("error opening window: %v", err)
	}
	if control != nil {
		w.sysData.resize = control.setRect
		err = control.make(w.sysData)
		if err != nil {
			return fmt.Errorf("error adding window's control: %v", err)
		}
	}
	// TODO separate showing?
	err = w.sysData.show()
	if err != nil {
		return fmt.Errorf("error showing window (in Window.Open()): %v", err)
	}
	err = w.sysData.setWindowSize(w.initWidth, w.initHeight)
	if err != nil {
		return fmt.Errorf("error setting window size (in Window.Open()): %v", err)
	}
	w.created = true
	return nil
}

// Show shows the window.
func (w *Window) Show() (err error) {
	err = w.sysData.show()
{'fraction_non_alphanumeric': 0.11595033428844317, 'fraction_numerical': 0.002292263610315186, 'mean_word_length': 3.433530906011854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1391248', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1762, 'n_words': 638}