|
1 | 1 | package guidefs |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 | "image/color" |
| 7 | + "path/filepath" |
6 | 8 | "strconv" |
7 | 9 |
|
8 | 10 | "fyne.io/fyne/v2" |
@@ -53,6 +55,7 @@ func initGraphics() { |
53 | 55 | return []string{"canvas", "image/color"} |
54 | 56 | }, |
55 | 57 | }, |
| 58 | + "*canvas.Image": initImageGraphic(), |
56 | 59 | "*canvas.LinearGradient": { |
57 | 60 | Name: "LinearGradient", |
58 | 61 | Create: func(Context) fyne.CanvasObject { |
@@ -206,7 +209,165 @@ func initGraphics() { |
206 | 209 | GraphicsNames = extractNames(Graphics) |
207 | 210 | } |
208 | 211 |
|
209 | | -// TODO tidy the API and move to a widget package |
| 212 | +func initImageGraphic() WidgetInfo { |
| 213 | + return WidgetInfo{ |
| 214 | + Name: "Image", |
| 215 | + Create: func(Context) fyne.CanvasObject { |
| 216 | + return &canvas.Image{} |
| 217 | + }, |
| 218 | + Edit: func(obj fyne.CanvasObject, c Context, _ func([]*widget.FormItem), onchanged func()) []*widget.FormItem { |
| 219 | + i := obj.(*canvas.Image) |
| 220 | + props := c.Metadata()[obj] |
| 221 | + |
| 222 | + minWidthInput := widget.NewEntry() |
| 223 | + minWidthInput.SetText(props["minWidth"]) |
| 224 | + minHeightInput := widget.NewEntry() |
| 225 | + minHeightInput.SetText(props["minHeight"]) |
| 226 | + |
| 227 | + minWidthInput.Validator = func(s string) error { |
| 228 | + if s == "" { |
| 229 | + return nil |
| 230 | + } |
| 231 | + |
| 232 | + f, err := strconv.ParseFloat(s, 32) |
| 233 | + if err != nil { |
| 234 | + return errors.New("invalid number format") |
| 235 | + } |
| 236 | + if f < 0 { |
| 237 | + return errors.New("negative minimum size") |
| 238 | + } |
| 239 | + return nil |
| 240 | + } |
| 241 | + minHeightInput.Validator = minWidthInput.Validator |
| 242 | + |
| 243 | + updateMin := func(_ string) { |
| 244 | + w, err := strconv.ParseFloat(minWidthInput.Text, 32) |
| 245 | + if err != nil { |
| 246 | + props["minWidth"] = "" |
| 247 | + return |
| 248 | + } else { |
| 249 | + props["minWidth"] = minWidthInput.Text |
| 250 | + } |
| 251 | + h, err := strconv.ParseFloat(minHeightInput.Text, 32) |
| 252 | + if err != nil { |
| 253 | + props["minHeight"] = "" |
| 254 | + return |
| 255 | + } else { |
| 256 | + props["minHeight"] = minHeightInput.Text |
| 257 | + } |
| 258 | + |
| 259 | + s := fyne.Size{} |
| 260 | + if w > 0 || h > 0 { |
| 261 | + s = fyne.NewSize(float32(w), float32(h)) |
| 262 | + } |
| 263 | + i.SetMinSize(s) |
| 264 | + onchanged() |
| 265 | + } |
| 266 | + minWidthInput.OnChanged = updateMin |
| 267 | + minHeightInput.OnChanged = updateMin |
| 268 | + |
| 269 | + fill := widget.NewSelect([]string{"Stretch", "Contain", "Original size"}, func(s string) { |
| 270 | + mode := canvas.ImageFillStretch |
| 271 | + switch s { |
| 272 | + case "Contain": |
| 273 | + mode = canvas.ImageFillContain |
| 274 | + updateMin("") // reset possible original fill override |
| 275 | + case "Original size": |
| 276 | + mode = canvas.ImageFillOriginal |
| 277 | + default: |
| 278 | + updateMin("") // reset possible original fill override |
| 279 | + } |
| 280 | + i.FillMode = mode |
| 281 | + i.Refresh() |
| 282 | + onchanged() |
| 283 | + }) |
| 284 | + fill.SetSelectedIndex(int(i.FillMode)) |
| 285 | + |
| 286 | + // TODO move to go:embed for files! |
| 287 | + pathSelect := widget.NewButton("(No file)", nil) |
| 288 | + if i.File != "" { |
| 289 | + pathSelect.SetText(filepath.Base(i.File)) |
| 290 | + } |
| 291 | + pathSelect.OnTapped = func() { |
| 292 | + dialog.ShowFileOpen(func(r fyne.URIReadCloser, err error) { |
| 293 | + path := "" |
| 294 | + if r != nil && err == nil { |
| 295 | + _ = r.Close() |
| 296 | + path = r.URI().Path() |
| 297 | + } |
| 298 | + |
| 299 | + i.File = path |
| 300 | + if path == "" { |
| 301 | + i.Image = nil |
| 302 | + pathSelect.SetText("(No file)") |
| 303 | + } else { |
| 304 | + pathSelect.SetText(r.URI().Name()) |
| 305 | + } |
| 306 | + i.Refresh() |
| 307 | + onchanged() |
| 308 | + }, fyne.CurrentApp().Driver().AllWindows()[0]) |
| 309 | + } |
| 310 | + |
| 311 | + resSelect := newIconSelectorButton(i.Resource, func(res fyne.Resource) { |
| 312 | + i.Resource = res |
| 313 | + i.Refresh() |
| 314 | + onchanged() |
| 315 | + }, true) |
| 316 | + resSelect.SetIcon(i.Resource) |
| 317 | + |
| 318 | + return []*widget.FormItem{ |
| 319 | + widget.NewFormItem("Min Width", minWidthInput), |
| 320 | + widget.NewFormItem("Min Height", minHeightInput), |
| 321 | + widget.NewFormItem("Fill mode", fill), |
| 322 | + widget.NewFormItem("Path", pathSelect), |
| 323 | + widget.NewFormItem("Resource", resSelect), |
| 324 | + } |
| 325 | + }, |
| 326 | + Packages: func(obj fyne.CanvasObject, _ Context) []string { |
| 327 | + i := obj.(*canvas.Image) |
| 328 | + if i.Resource != nil { |
| 329 | + return []string{"canvas", "theme"} |
| 330 | + } |
| 331 | + return []string{"canvas"} |
| 332 | + }, |
| 333 | + Gostring: func(obj fyne.CanvasObject, c Context, defs map[string]string) string { |
| 334 | + i := obj.(*canvas.Image) |
| 335 | + props := c.Metadata()[obj] |
| 336 | + minWidth, _ := props["minWidth"] |
| 337 | + minHeight, _ := props["minHeight"] |
| 338 | + hasMin := (minWidth != "" && minWidth != "0") || (minHeight != "" && minHeight != "0") |
| 339 | + |
| 340 | + code := "" |
| 341 | + if i.Resource != nil { |
| 342 | + res := "theme." + IconName(i.Resource) + "()" |
| 343 | + |
| 344 | + if !hasMin && i.FillMode == canvas.ImageFillStretch { |
| 345 | + code = fmt.Sprintf("canvas.NewImageFromResource(%s)", res) |
| 346 | + } else { |
| 347 | + code = fmt.Sprintf("&canvas.Image{Resource: %s, FillMode: %s}", res, fillName(i.FillMode)) |
| 348 | + |
| 349 | + if hasMin { |
| 350 | + code = fmt.Sprintf("func() *canvas.Image {"+ |
| 351 | + "img := %s; img.SetMinSize(%#v); return img}()", code, i.MinSize()) |
| 352 | + } |
| 353 | + } |
| 354 | + } else { |
| 355 | + if !hasMin && i.FillMode == canvas.ImageFillStretch { |
| 356 | + code = fmt.Sprintf("canvas.NewImageFromFile(\"%s\")", i.File) |
| 357 | + } else { |
| 358 | + code = fmt.Sprintf("&canvas.Image{File: \"%s\", FillMode: %s}", i.File, fillName(i.FillMode)) |
| 359 | + |
| 360 | + if hasMin { |
| 361 | + code = fmt.Sprintf("func() *canvas.Image {"+ |
| 362 | + "img := %s; img.SetMinSize(%#v); return img}()", code, i.MinSize()) |
| 363 | + } |
| 364 | + } |
| 365 | + } |
| 366 | + |
| 367 | + return widgetRef(props, defs, code) |
| 368 | + }, |
| 369 | + } |
| 370 | +} |
210 | 371 |
|
211 | 372 | func newColorButton(c color.Color, fn func(color.Color)) fyne.CanvasObject { |
212 | 373 | // TODO get the window passed in somehow |
|
0 commit comments