mirror of https://github.com/anoshenko/rui.git
Added binding support for canvas "draw-function"
This commit is contained in:
parent
73b14ed78a
commit
e618377c11
|
@ -1,5 +1,7 @@
|
||||||
package rui
|
package rui
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
// DrawFunction is the constant for "draw-function" property tag.
|
// DrawFunction is the constant for "draw-function" property tag.
|
||||||
//
|
//
|
||||||
// Used by `CanvasView`.
|
// Used by `CanvasView`.
|
||||||
|
@ -55,7 +57,7 @@ func (canvasView *canvasViewData) removeFunc(tag PropertyName) []PropertyName {
|
||||||
if tag == DrawFunction {
|
if tag == DrawFunction {
|
||||||
if canvasView.getRaw(DrawFunction) != nil {
|
if canvasView.getRaw(DrawFunction) != nil {
|
||||||
canvasView.setRaw(DrawFunction, nil)
|
canvasView.setRaw(DrawFunction, nil)
|
||||||
canvasView.Redraw()
|
//canvasView.Redraw()
|
||||||
return []PropertyName{DrawFunction}
|
return []PropertyName{DrawFunction}
|
||||||
}
|
}
|
||||||
return []PropertyName{}
|
return []PropertyName{}
|
||||||
|
@ -66,9 +68,14 @@ func (canvasView *canvasViewData) removeFunc(tag PropertyName) []PropertyName {
|
||||||
|
|
||||||
func (canvasView *canvasViewData) setFunc(tag PropertyName, value any) []PropertyName {
|
func (canvasView *canvasViewData) setFunc(tag PropertyName, value any) []PropertyName {
|
||||||
if tag == DrawFunction {
|
if tag == DrawFunction {
|
||||||
if fn, ok := value.(func(Canvas)); ok {
|
switch value := value.(type) {
|
||||||
canvasView.setRaw(DrawFunction, fn)
|
case func(Canvas):
|
||||||
} else {
|
canvasView.setRaw(DrawFunction, value)
|
||||||
|
|
||||||
|
case string:
|
||||||
|
canvasView.setRaw(DrawFunction, value)
|
||||||
|
|
||||||
|
default:
|
||||||
notCompatibleType(tag, value)
|
notCompatibleType(tag, value)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -94,8 +101,30 @@ func (canvasView *canvasViewData) Redraw() {
|
||||||
canvas := newCanvas(canvasView)
|
canvas := newCanvas(canvasView)
|
||||||
canvas.ClearRect(0, 0, canvasView.frame.Width, canvasView.frame.Height)
|
canvas.ClearRect(0, 0, canvasView.frame.Width, canvasView.frame.Height)
|
||||||
if value := canvasView.getRaw(DrawFunction); value != nil {
|
if value := canvasView.getRaw(DrawFunction); value != nil {
|
||||||
if drawer, ok := value.(func(Canvas)); ok {
|
switch drawer := value.(type) {
|
||||||
|
case func(Canvas):
|
||||||
drawer(canvas)
|
drawer(canvas)
|
||||||
|
|
||||||
|
case string:
|
||||||
|
bind := canvasView.binding()
|
||||||
|
if bind == nil {
|
||||||
|
ErrorLogF(`There is no a binding object for call "%s"`, drawer)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
val := reflect.ValueOf(bind)
|
||||||
|
method := val.MethodByName(drawer)
|
||||||
|
if !method.IsValid() {
|
||||||
|
ErrorLogF(`The "%s" method is not valid`, drawer)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
methodType := method.Type()
|
||||||
|
if methodType.NumIn() == 1 && equalType(methodType.In(0), reflect.TypeOf(canvas)) {
|
||||||
|
method.Call([]reflect.Value{reflect.ValueOf(canvas)})
|
||||||
|
} else {
|
||||||
|
ErrorLogF(`Unsupported prototype of "%s" method`, drawer)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
canvas.finishDraw()
|
canvas.finishDraw()
|
||||||
|
|
24
image.go
24
image.go
|
@ -4,34 +4,44 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ImageLoadingStatus defines type of status of the image loading
|
||||||
|
type ImageLoadingStatus int
|
||||||
|
|
||||||
// Constants which represent return values of the LoadingStatus function of an [Image] view
|
// Constants which represent return values of the LoadingStatus function of an [Image] view
|
||||||
const (
|
const (
|
||||||
// ImageLoading is the image loading status: in the process of loading
|
// ImageLoading is the image loading status: in the process of loading
|
||||||
ImageLoading = 0
|
ImageLoading ImageLoadingStatus = 0
|
||||||
// ImageReady is the image loading status: the image is loaded successfully
|
// ImageReady is the image loading status: the image is loaded successfully
|
||||||
ImageReady = 1
|
ImageReady ImageLoadingStatus = 1
|
||||||
// ImageLoadingError is the image loading status: an error occurred while loading
|
// ImageLoadingError is the image loading status: an error occurred while loading
|
||||||
ImageLoadingError = 2
|
ImageLoadingError ImageLoadingStatus = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
// Image defines the image that is used for drawing operations on the Canvas.
|
// Image defines the image that is used for drawing operations on the Canvas.
|
||||||
type Image interface {
|
type Image interface {
|
||||||
// URL returns the url of the image
|
// URL returns the url of the image
|
||||||
URL() string
|
URL() string
|
||||||
// LoadingStatus returns the status of the image loading: ImageLoading (0), ImageReady (1), ImageLoadingError (2)
|
|
||||||
LoadingStatus() int
|
// LoadingStatus returns the status of the image loading:
|
||||||
|
// - ImageLoading (0) - in the process of loading;
|
||||||
|
// - ImageReady (1) - the image is loaded successfully;
|
||||||
|
// - ImageLoadingError (2) - an error occurred while loading.
|
||||||
|
LoadingStatus() ImageLoadingStatus
|
||||||
|
|
||||||
// LoadingError: if LoadingStatus() == ImageLoadingError then returns the error text, "" otherwise
|
// LoadingError: if LoadingStatus() == ImageLoadingError then returns the error text, "" otherwise
|
||||||
LoadingError() string
|
LoadingError() string
|
||||||
setLoadingError(err string)
|
setLoadingError(err string)
|
||||||
|
|
||||||
// Width returns the width of the image in pixels. While LoadingStatus() != ImageReady returns 0
|
// Width returns the width of the image in pixels. While LoadingStatus() != ImageReady returns 0
|
||||||
Width() float64
|
Width() float64
|
||||||
|
|
||||||
// Height returns the height of the image in pixels. While LoadingStatus() != ImageReady returns 0
|
// Height returns the height of the image in pixels. While LoadingStatus() != ImageReady returns 0
|
||||||
Height() float64
|
Height() float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type imageData struct {
|
type imageData struct {
|
||||||
url string
|
url string
|
||||||
loadingStatus int
|
loadingStatus ImageLoadingStatus
|
||||||
loadingError string
|
loadingError string
|
||||||
width, height float64
|
width, height float64
|
||||||
listener func(Image)
|
listener func(Image)
|
||||||
|
@ -45,7 +55,7 @@ func (image *imageData) URL() string {
|
||||||
return image.url
|
return image.url
|
||||||
}
|
}
|
||||||
|
|
||||||
func (image *imageData) LoadingStatus() int {
|
func (image *imageData) LoadingStatus() ImageLoadingStatus {
|
||||||
return image.loadingStatus
|
return image.loadingStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue