Como lidar com eventos Rebol quando o layout é construída por meio de programação?

StackOverflow https://stackoverflow.com/questions/1252561

  •  12-09-2019
  •  | 
  •  

Pergunta

Eu sei como construir uma galeria de fotos de forma dinâmica, como faço agora permitem para acionar um evento quando o usuário clica em uma imagem?

Eu não quero ter a função de envolver dentro do layout, mas do lado de fora: é possível

?

Não sei se sou claro então me diga.

Foi útil?

Solução

Um truque muito fácil é a criação de um estilo com a ação que você quer, e tê-lo olhar para um valor de dados do usuário que você definiu quando gerar o rosto, se necessário.

lay: [
    across
    style my-box box [print [face/user-data face/color]]
]
repeat i 4 [
    repeat j 4 [
        repend lay [
            'my-box 50x50 get random/only [red green blue yellow] 
            'user-data to pair! reduce [i j]
        ]
    ]
    append lay 'return
]
view layout lay

Outras dicas

Como disse, o melhor é criar um estilo para minimizar o código no layout. Assim, o estilo já está configurado para as ações que deseja.

Para uma galeria de imagens, eu iria criar um estilo polegar com minhas necessidades.

Mas você não precisa usar um 'envolver func para coisas simples! Se o que você precisa é deixado / manipulação de clique direito, em seguida, uma ou duas ações blocos são suficientes e muito mais simples.

Aqui está um exemplo completo:

Rebol [
    title: "Basic image gallery"
]

thumb-size: 100x100 ; size of the thumbnails
thumbs-per-row: 6   ; number of thumbs per row

; Here is the actions I want to use when clicking/right clicking the image face.
; It's just a block: it's the 'layout function that will make it a function
; with 'face and 'value arguments
thumb-action: [
    ; left click: show full size image
    view/new layout [
        origin 2 space 2
        vh3 form (either any [file? face/user-data url? face/user-data text? face/user-data] [face/user-data] ["An image without reference"])
        image face/image
    ]
]
thumb-alt-action: [
    ; right click: open up the folder/web site where the file is
    switch/default type?/word face/user-data [
        file! [call/shell join "explorer " to-local-file first split-path face/user-data]
        url! [browse face/user-data]
    ] [alert rejoin ["Can't do anything with " type? face/user-data " type of value ! Sorry."]]
]

; Our styles for the gallery
gallery-styles: probe stylize [
    ; Here is a style for the thumbnails, with the actions wanted
    thumb: image thumb-size effect [aspect] thumb-action thumb-alt-action
]

; Some samples images
imgs: [
    ; This paths are for a typical Windows7 installation
    %/c/windows/web/wallpaper/nature/img1.jpg
    %/c/windows/web/wallpaper/nature/img2.jpg
    %/c/windows/web/wallpaper/nature/img3.jpg
    %/c/windows/web/wallpaper/nature/img4.jpg
    ; URLs as examples
    http://www.rebol.com/graphics/reb-logo.gif
    http://www.rebol.com/graphics/ref-card.jpg
]

; Base for your gallery layout
gallery-lay: copy [
    origin 2 space 2 across
    styles gallery-styles
    vh2 "Image gallery"
]

; Builds the final layout
count: 0
foreach img imgs [
    ; This for handling only a defined number of thumbs per row
    if 0 = (count // thumbs-per-row) [append gallery-lay 'return]
    count: count + 1
    ; Here you add the layout code for the current image
    append gallery-lay compose [thumb (img) user-data (img)]
]

; Here we are: the result
view layout gallery-lay

Existem muitas maneiras de fazer isso. Você não especificar como você está construindo a galeria de imagem, mas eu estou supondo que você está construindo um layout de estilos de imagem e, em seguida, exibir esse layout.

Parece também como você quer liberdade para fazer coisas específicas com cada imagem, então eu sugiro que você construir um estilo separado, possivelmente derivado de IMAGEM. Você pode fazer isso como este:

stylize/master [
    image: image with [
        feel: make feel [
            engage: func [face act event] [
                ; do my custom engage function
            ]
        ]
    ]
]

Coloque o código antes do layout. Dessa forma, você pode manter o código complexo para o comportamento de fora imagem do bloco de layout. Quando você trabalha desta forma, o estilo é globalmente alterado.

Você também pode simplesmente criar um novo estilo, alterando o nome:

stylize/master [
    image2: image with [
        ...
    ]
]

IMAGEM será deixado intocado, enquanto você pode usar IMAGE2 em seu layout.

Por estilizar / MASTER? Eu uso estilizar / MASTER por hábito, por isso não tem que especificar uma lista estilo específico no layout e posso raspar uma linha de código para cada layout.

Vamos tentar novamente:

lay: [
    across
    style my-box box [print [face/user-data face/color]]
]
repeat i 4 [
    repeat j 4 [
        repend lay [
            'my-box 50x50 get random/only [red green blue yellow] 
            'user-data to pair! reduce [i j]
        ]
    ]
    append lay 'return
]
view layout lay
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top