Created
June 29, 2022 00:28
-
-
Save ALANVF/f9ef9485d8f01f83df8f5ea040a08d35 to your computer and use it in GitHub Desktop.
Image viewer demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
Title: "Image viewer" | |
Author: "ALANVF" | |
Date: 28-Jun-2022 | |
File: %image-viewer.red | |
Needs: 'View | |
Purpose: { | |
Basic image viewer | |
} | |
] | |
join-with: func [ | |
series [series!] | |
sep | |
/local res value | |
][ | |
if empty? series [return copy series] | |
res: copy series/1 | |
foreach value next series [ | |
append res sep | |
append res value | |
] | |
res | |
] | |
image-exts: [] | |
foreach [name codec] system/codecs [ | |
if find form codec/mime-type "image/" [ | |
foreach suffix codec/suffixes [ | |
append image-exts rejoin ["*" suffix] | |
] | |
] | |
] | |
image-filters: reduce ["Image files" join-with image-exts ";"] | |
open-file: has [image-path image][ | |
if image-path: request-file/title/filter "Choose image" image-filters [ | |
either image? image: load image-path [ | |
image-face/image: image | |
image-face/size: image/size | |
image-face/parent/size: image/size + 20x20 | |
][ | |
alert "Not an image!" | |
] | |
] | |
] | |
close-file: does [ | |
image-face/image: none | |
] | |
view/flags [ | |
size 500x500 | |
title "Image viewer" | |
on-resizing [ | |
either image: image-face/image [ | |
either image/size/x > image/size/y [ | |
size': as-pair | |
face/size/y * image/size/x / image/size/y | |
face/size/y | |
if size'/x > face/size/x [ | |
size': as-pair | |
face/size/x | |
face/size/x * image/size/y / image/size/x | |
] | |
image-face/size: size' - 20 | |
][ | |
size': as-pair | |
face/size/x | |
face/size/x * image/size/y / image/size/x | |
if size'/y > face/size/y [ | |
size': as-pair | |
face/size/y * image/size/x / image/size/y | |
face/size/y | |
] | |
image-face/size: size' - 20 | |
] | |
][ | |
image-face/size: face/size - 20 | |
] | |
] | |
on-menu [ | |
switch event/picked [ | |
open [ open-file ] | |
close [ close-file ] | |
] | |
] | |
on-key [ | |
if event/ctrl? [ | |
switch event/key [ | |
#"^O" [ open-file ] | |
#"^W" [ close-file ] | |
] | |
] | |
] | |
do [ | |
menu: [ | |
"File" [ | |
"Open file Ctrl+O" open | |
"Close file Ctrl+W" close | |
] | |
] | |
] | |
image-face: base 480x480 black | |
] [resize] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment