func (p parseEle) Element() Element { switch p.Type { case "Text": f := &ui.FontDescriptor{ Family: p.FontFamily, Size: p.FontSize, } switch p.FontWeight { case "Bold": f.Weight = ui.TextWeightBold case "Normal", "": f.Weight = ui.TextWeightNormal } base := NewText(p.Text, ui.LoadClosestFont(f)) base.ElementBase = parseElementBase(p) return base case "Group": children := make([]Element, len(p.Children)) for i, child := range p.Children { children[i] = child.Element() } base := NewGroup(children) base.ElementBase = parseElementBase(p) return base default: panic(fmt.Errorf("Unknown Type %v", p.Type)) } }
func (a HistogramAreaHandler) Draw(area *ui.Area, dp *ui.AreaDrawParams) { fntNum := ui.LoadClosestFont(&ui.FontDescriptor{Family: "Droid Sans", Size: 10, Weight: ui.TextWeightBold}) fntVolume := ui.LoadClosestFont(&ui.FontDescriptor{Family: "Droid Sans", Size: 12, Italic: ui.TextItalicItalic}) brush := &ui.Brush{Type: ui.Solid, R: 0.1, G: 0.5, B: 0.7, A: 1} columnWidth := dp.AreaWidth / float64(len(a.Data)) barWidth := columnWidth * 0.8 for i, volume := range a.Data { x1 := (float64(i) + 0.1) * columnWidth x2 := x1 + barWidth y1 := dp.AreaHeight - 16 y2 := y1 - (dp.AreaHeight-32.0)*float64(volume)/100.0 layoutNum := ui.NewTextLayout(strconv.Itoa(i), fntNum, columnWidth) w1, _ := layoutNum.Extents() dp.Context.Text(x1+(barWidth-w1)/2, y1, layoutNum) layoutVolume := ui.NewTextLayout(strconv.Itoa(volume), fntVolume, columnWidth) w2, h2 := layoutVolume.Extents() dp.Context.Text(x1+(barWidth-w2)/2, y2-h2, layoutVolume) dp.Context.Save() p := ui.NewPath(ui.Winding) p.NewFigure(x1, y1) p.LineTo(x2, y1) p.LineTo(x2, y2) p.LineTo(x1, y2) p.CloseFigure() p.End() dp.Context.Fill(p, brush) dp.Context.Restore() } }
func main() { file, err := os.Open("document.json") if err != nil { panic(err) } defer file.Close() buffer := new(bytes.Buffer) _, err = buffer.ReadFrom(file) if err != nil { panic(err) } var document uidoc.Element err = ui.Main(func() { document, err = uidoc.Parse(buffer.Bytes()) if err != nil { panic(err) } font := ui.LoadClosestFont(&ui.FontDescriptor{ Family: "Deja Vu", Size: 12, }) name := ui.NewEntry() button := ui.NewButton("Greet") doc := uidoc.New() doc.SetDocument(document) box := ui.NewVerticalBox() box.Append(ui.NewLabel("Enter your name:"), false) box.Append(name, false) box.Append(button, false) box.Append(doc, true) window := ui.NewWindow("Hello", 400, 700, false) window.SetChild(box) button.OnClicked(func(*ui.Button) { element := uidoc.NewText("Hello, "+name.Text()+"!", font) document.(*uidoc.Group).Append(element) doc.Layout() }) window.OnClosing(func(*ui.Window) bool { ui.Quit() return true }) window.Show() }) if err != nil { panic(err) } }