Пример #1
0
func AttributeChooser(b *έντομο.Bug, attr string, imm WhenToWrite) interface {
	gui.Widget
	gui.Changeable
} {
	opts := b.Type.AttributeOptions(attr)
	b.Initialize()
	b.Comments()
	var chooser interface {
		gui.Widget
		gui.Changeable
	}
	if len(opts) > 1 {
		//fmt.Println("looking at", attr, "has value", b.Attributes[attr])
		val := 0
		for i, o := range opts {
			if o == b.Attributes[attr] {
				val = i
			}
		}
		chooser = gui.Menu(val, opts)
	} else {
		chooser = gui.EditText(b.Attributes[attr])
	}
	go func() {
		for {
			newvalue := <-chooser.Changes()
			if imm == WriteNow {
				b.WriteAttribute(attr, newvalue)
			} else {
				b.ScheduleAttribute(attr, newvalue)
			}
		}
	}()
	return chooser
}
Пример #2
0
func NewWidget() gui.Window {
	window := gui.Window{"Class survey", "", nil}

	teamname := ""
	team := gui.Menu(0,
		"", "archimedes", "boltzmann", "curie", "doppler", "euler", "feynman", "galileo",
		"hamilton", "ising", "joule", "kelvin", "lagrange", "maxwell", "newton", "onsager", "planck",
	)
	teamrow := gui.Table([][]gui.Widget{{gui.Text("Team:"), team}})

	name := ""
	namebox := gui.EditText(name)
	namerow := gui.Row(gui.Text("Name:"), namebox)
	// namebox.HandleChanged = func(old string) (modified gui.Widget, refresh bool) {
	// 	window.Title = `Survey of ` + namebox.Text.String
	// 	return
	// }
	partner := ""
	partnerbox := gui.EditText(partner)
	partnerrow := gui.Row(gui.Text("Partner:"), partnerbox)
	donetext := ""
	dotoday := gui.TextArea(donetext)
	learnedtoday := ""
	learntoday := gui.TextArea(learnedtoday)
	problems := ""
	workwell := gui.TextArea(problems)

	button := gui.Button("Submit")

	widget := gui.Table([][]gui.Widget{
		{teamrow},
		{namerow},
		{partnerrow},
		{gui.Text("What did you do today?")},
		{dotoday},
		{gui.Text("What is one thing you learned today?")},
		{learntoday},
		{gui.Text("What is one thing that didn't work well today?")},
		{workwell},
		{button},
	})
	window.Contents = widget
	go func() {
		for {
			select {
			case teamname = <-team.Changes():
				fmt.Println("Team name changed to", teamname)
			case name = <-namebox.Changes():
				//fmt.Println("Name changed to", name)
			case partner = <-partnerbox.Changes():
				//fmt.Println("Partner changed to", partner)
			case donetext = <-dotoday.Changes():
				fmt.Println("Done text is", donetext)
			case learnedtoday = <-learntoday.Changes():
				fmt.Println("Learned today is", learnedtoday)
			case problems = <-workwell.Changes():
				fmt.Println("Problems is", problems)
			case _ = <-button.Clicks():
				t := time.LocalTime()
				// First let's see if today has already been created
				if _, err := os.Stat(t.Format("2006-01-02")); err != nil {
					surveyfile.WriteString(t.Format("\\thisday{Monday}{2006-01-02}\n\n"))
				} else {
					fmt.Println("Day already exists.")
				}

				dir := t.Format("2006-01-02/15.04.05")
				err := os.MkdirAll(dir, 0777)
				if err != nil {
					fmt.Println("ERROR CREATING DIRECTORY", dir, "!")
					return
				}
				f, err := os.Create(filepath.Join(dir, name))
				if err != nil {
					fmt.Println("ERROR CREATING FILE", filepath.Join(dir, name), "!", err)
					return
				}
				defer f.Close()
				_, err = fmt.Fprintf(f, "\\daily{%s}{%s}{%s}{%s}{\n%s\n}{\n%s\n}{\n%s\n}\n",
					t.Format("3:04PM"),
					name, partner, teamname,
					IndentWrapText("  ", CleanLatex(donetext)),
					IndentWrapText("  ", CleanLatex(learnedtoday)),
					IndentWrapText("  ", CleanLatex(problems)))
				if err == nil {
					surveyfile.WriteString(t.Format("\\input{2006-01-02/15.04.05/" +
						name + "}\n"))
				} else {
					fmt.Println("I ran into a bug!", err)
					return
				}
				window.Contents.Updater() <- gui.Text("Thank you, " + name + "!")
			}
		}
	}()
	return window
}
Пример #3
0
func main() {
	http.HandleFunc("/style.css", styleServer)
	buttonA := gui.Button("A")
	buttonB := gui.Button("B")
	buttonA.OnClick(func() gui.Refresh {
		fmt.Println("I clicked on button A")
		buttonB.SetString(buttonB.GetString() + buttonB.GetString())
		return gui.StillClean
	})
	buttonB.OnClick(func() gui.Refresh {
		fmt.Println("I clicked on button A")
		t := buttonB.GetString()
		buttonB.SetString(t[:len(t)/2+1])
		return gui.StillClean
	})
	iscool := gui.Checkbox()
	name := gui.EditText("Enter name here")
	hello := gui.Text("Hello world!")
	name.OnChange(func() gui.Refresh {
		hello.SetString("Hello " + name.GetString() + "!")
		return gui.StillClean
	})
	testing_checkbox := LabelledCheckbox("testing")
	testing_checkbox.OnChange(func() gui.Refresh {
		fmt.Println("Hello world")
		if testing_checkbox.GetBool() {
			testing_checkbox.SetString("this test is currently true")
		} else {
			testing_checkbox.SetString("this test is now false")
		}
		return gui.NeedsRefresh
	})

	// Now let's test out a set of radio buttons
	radio := RadioButtons("apples", "lemons", "oranges", "pears")
	radio_report := gui.Text("I like to eat tasty fruit")
	menu := gui.Menu("apples", "lemons", "oranges", "pears")
	radio.OnChange(func() gui.Refresh {
		menu.SetString(radio.GetString())
		radio_report.SetString("I like to eat " + radio.GetString())
		return gui.NeedsRefresh
	})

	menu.OnChange(func() gui.Refresh {
		radio.SetString(menu.GetString())
		radio_report.SetString("I like to eat " + radio.GetString())
		return radio.HandleChange()
	})

	err := gui.Run(12346,
		gui.Column(
			iscool,
			testing_checkbox,
			gui.Row(buttonA, buttonB),
			gui.Row(gui.Text("Name:"), name),
			hello,
			gui.Text("Goodbye world!"),
			radio,
			radio_report,
			menu,
		))
	if err != nil {
		panic("ListenAndServe: " + err.String())
	}
}