Example #1
0
//gui界面,用的 github.com/andlabs/ui 的ui库
func gui() {
	input = ui.NewTextField()
	input.OnChanged(func() {
		go feedback() // 主要是这里在不同的刷新(table)内容
	})
	button := ui.NewButton("查词")
	button.OnClicked(guiTranslate)
	inputBox := ui.NewHorizontalStack(input, button)
	inputBox.SetStretchy(0)
	tab := ui.NewTab()
	table = ui.NewTable(reflect.TypeOf(str{}))
	table.OnSelected(tableSelected)
	tab.Append("单词", table)
	si = ui.NewLabel("Simple")
	tab.Append("简约", si)
	al = ui.NewLabel("All")
	tab.Append("全部", al)
	stack := ui.NewVerticalStack(inputBox, tab)
	stack.SetStretchy(1)
	w = ui.NewWindow("Window", 280, 350, stack)
	w.OnClosing(func() bool {
		ui.Stop()
		return true
	})
	w.Show()
}
Example #2
0
func gui() {
	commands := spotify.Commands()
	leftButton := ui.NewButton("<<")
	playButton := ui.NewButton("||")
	rightButton := ui.NewButton(">>")

	leftButton.OnClicked(func() {
		spotify.Execute(commands["previousTrack"])
	})

	playButton.OnClicked(func() {
		spotify.Execute(commands["playpause"])
	})

	rightButton.OnClicked(func() {
		spotify.Execute(commands["nextTrack"])
	})

	stack := ui.NewHorizontalStack(
		leftButton,
		playButton,
		rightButton)

	w := ui.NewWindow("Spotify Client", 90, 25, stack)
	w.OnClosing(func() bool {
		ui.Stop()
		return true
	})
	w.Show()
}
Example #3
0
func main() {
	go ui.Do(func() {
		name := ui.NewTextField()
		button := ui.NewButton("Greet")
		greeting := ui.NewLabel("")
		stack := ui.NewHorizontalStack(
			ui.NewLabel("Enter your name:"), name,
			button, greeting)
		window = ui.NewWindow("Hello", 200, 100, stack)
		button.OnClicked(func() {
			greeting.SetText("Hello, " + name.Text() + "!")
		})
		window.OnClosing(func() bool {
			ui.Stop()
			return true
		})
		window.Show()
	})
	err := ui.Go()
	if err != nil {
		panic(err)
	}
}
Example #4
0
func gui() {
	connect()
	var c Container

	//Stack for the control
	l := ui.NewLabel("Image to start")
	imageName := ui.NewTextField()
	imageName.SetText("ipython/scipystack")
	startBtn := ui.NewButton("Launch")
	controlStack := ui.NewVerticalStack(l, imageName, startBtn)
	controlGrp := ui.NewGroup("Launch Image", controlStack)
	controlGrp.SetMargined(true)

	// Table of running containers
	table := ui.NewTable(reflect.TypeOf(c))
	openBrowserBtn := ui.NewButton("Open in browser")
	killBtn := ui.NewButton("Kill")
	manageRunningContainerGrp := ui.NewHorizontalStack(openBrowserBtn, killBtn)
	containerControlGrp := ui.NewVerticalStack(table, manageRunningContainerGrp)
	containerListGrp := ui.NewGroup("Running containers", containerControlGrp)
	containerListGrp.SetMargined(true)

	//Container info area
	selectedContainerInfo := ui.NewTextField()

	// Now make a new 2 column stack
	topStack := ui.NewHorizontalStack(controlGrp, containerListGrp)
	topStack.SetStretchy(0)
	topStack.SetStretchy(1)

	mainStack := ui.NewVerticalStack(topStack, selectedContainerInfo)
	mainStack.SetStretchy(0)
	mainStack.SetStretchy(1)

	startBtn.OnClicked(func() {
		go Start(imageName.Text())
	})

	table.OnSelected(func() {
		c := table.Selected()
		table.Lock()
		d := table.Data().(*[]Container)
		//this makes a shallow copy of the structure so that we can access elements per
		//   http://giantmachines.tumblr.com/post/51007535999/golang-struct-shallow-copy
		newC := *d
		table.Unlock()
		if c > -1 {
			fmt.Println("Getting info for container ", newC[c].Name)
			selectedContainerInfo.SetText(Info(newC[c].Name))
		}
	})

	openBrowserBtn.OnClicked(func() {
		c := table.Selected()
		table.Lock()
		d := table.Data().(*[]Container)
		//this makes a shallow copy of the structure so that we can access elements per
		//   http://giantmachines.tumblr.com/post/51007535999/golang-struct-shallow-copy
		newC := *d
		table.Unlock()
		url := fmt.Sprintf("%s.%s", newC[c].Name, os.Getenv("DOMAIN_NAME"))
		webbrowser.Open(url)
	})

	killBtn.OnClicked(func() {
		c := table.Selected()
		table.Lock()
		d := table.Data().(*[]Container)
		//this makes a shallow copy of the structure so that we can access elements per
		//   http://giantmachines.tumblr.com/post/51007535999/golang-struct-shallow-copy
		newC := *d
		table.Unlock()
		go Kill(newC[c].Name)
	})

	w = ui.NewWindow("Manage Containers on RCS", 600, 450, mainStack)
	w.SetMargined(true)

	w.OnClosing(func() bool {
		ui.Stop()
		return true
	})
	go updateTable(table)
	w.Show()

}
Example #5
0
func myMain() {
	var cmd *exec.Cmd
	var timer *time.Timer
	var timerChan <-chan time.Time
	var w *ui.Window

	status := ui.NewLabel("")

	stop := func() {
		if cmd != nil { // stop the command if it's running
			err := cmd.Process.Kill()
			if err != nil {
				ui.MsgBoxError(w,
					fmt.Sprintf("Error killing process: %v", err),
					"You may need to kill it manually.")
			}
			err = cmd.Process.Release()
			if err != nil {
				ui.MsgBoxError(w,
					fmt.Sprintf("Error releasing process: %v", err),
					"")
			}
			cmd = nil
		}
		if timer != nil { // stop the timer if we started it
			timer.Stop()
			timer = nil
			timerChan = nil
		}
		status.SetText("")
	}

	w = ui.NewWindow("wakeup", 400, 100)
	ui.AppQuit = w.Closing // treat application close as main window close
	cmdbox := ui.NewLineEdit(defCmdLine)
	timebox := ui.NewLineEdit(defTime)
	bStart := ui.NewButton("Start")
	bStop := ui.NewButton("Stop")

	// a Stack to keep both buttons at the same size
	btnbox := ui.NewHorizontalStack(bStart, bStop)
	btnbox.SetStretchy(0)
	btnbox.SetStretchy(1)
	// and a Stack around that Stack to keep them at a reasonable size, with space to their right
	btnbox = ui.NewHorizontalStack(btnbox, status)

	// the main layout
	grid := ui.NewGrid(2,
		ui.NewLabel("Command"), cmdbox,
		ui.NewLabel("Time"), timebox,
		ui.Space(), ui.Space(), // the Space on the right will consume the window blank space
		ui.Space(), btnbox)
	grid.SetStretchy(2, 1) // make the Space noted above consume
	grid.SetFilling(0, 1)  // make the two textboxes grow horizontally
	grid.SetFilling(1, 1)

	w.Open(grid)

mainloop:
	for {
		select {
		case <-w.Closing:
			break mainloop
		case <-bStart.Clicked:
			stop() // only one alarm at a time
			alarmTime, err := time.Parse(timeFmt, timebox.Text())
			if err != nil {
				ui.MsgBoxError(w,
					fmt.Sprintf("Error parsing time %q: %v", timebox.Text(), err),
					fmt.Sprintf("Make sure your time is in the form %q (without quotes).", timeFmt))
				continue
			}
			now := time.Now()
			later := bestTime(now, alarmTime)
			timer = time.NewTimer(later.Sub(now))
			timerChan = timer.C
			status.SetText("Started")
		case <-timerChan:
			cmd = exec.Command("/bin/sh", "-c", "exec "+cmdbox.Text())
			// keep stdin /dev/null in case user wants to run multiple alarms on one instance (TODO should I allow this program to act as a pipe?)
			// keep stdout /dev/null to avoid stty mucking
			cmd.Stderr = os.Stderr
			err := cmd.Start()
			status.SetText("Firing")
			if err != nil {
				ui.MsgBoxError(w,
					fmt.Sprintf("Error running program: %v", err),
					"")
				cmd = nil
				status.SetText("")
			}
			timer = nil
			timerChan = nil
		case <-bStop.Clicked:
			stop()
		}
	}

	// clean up
	stop()
}