Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve #94 #95

Merged
merged 3 commits into from
Sep 7, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 103 additions & 79 deletions main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,106 @@
; make sure the path provided is a proper absolute path
(define relative->absolute (compose1 simple-form-path expand-user-path))

; resize ivy-frame based on the dimensions of the image
(define (resize-ivy-frame path)
; resize ivy-frame so that the image isn't horribly squished if it's large
(when (supported-file? path)
; determine the monitor dimensions
(define-values (monitor-width monitor-height) (get-display-size))
; approximate canvas offset
(define canvas-offset 84)
(define max-width (- monitor-width canvas-offset 100))
(define max-height (- monitor-height 100))
(define pct (if (flif? path)
(let ([dimensions (flif-dimensions path)])
(rectangle (first dimensions) (second dimensions)))
(bitmap path)))
(define pct-width (pict-width pct))
(define pct-height (pict-height pct))
(cond
; all good, let's resize the frame
[(and (< pct-width max-width)
(< pct-height max-height))
(send ivy-frame resize
(inexact->exact (floor pct-width))
(+ (inexact->exact (floor pct-height)) canvas-offset))]
; image is the same size as the monitor
[(and (= pct-width max-width)
(= pct-height max-height))
(define resized (scale-to-fit pct
pct-width
(- max-height canvas-offset)))
(send ivy-frame resize
(- (inexact->exact (floor (pict-width resized))) canvas-offset)
max-height)]
; wide image
[(and (>= pct-width max-width)
(<= pct-height max-height))
(define y
(cond [(> pct-height (- max-height canvas-offset))
max-height]
[(<= pct-height (- max-height canvas-offset))
(+ pct-height canvas-offset)]
[else pct-height]))
(define resized (scale-to-fit pct max-width y))
(send ivy-frame resize
max-width
(+ (inexact->exact (floor (pict-height resized))) canvas-offset))]
; tall image
[(and (<= pct-width max-width)
(>= pct-height max-height))
(define resized (scale-to-fit pct
pct-width
(- max-height canvas-offset)))
(send ivy-frame resize
(inexact->exact (floor (pict-width resized)))
max-height)]
; both wide and tall
[(and (> pct-width max-width)
(> pct-height max-height))
(define resized (scale-to-fit pct max-width (- max-height canvas-offset)))
(send ivy-frame resize
(inexact->exact (floor (pict-width resized)))
(+ (inexact->exact (floor (pict-height resized))) canvas-offset))])))

(define (set-image-paths! paths)
; if there are directories as paths, scan them
(define absolute-paths
(remove-duplicates
(for/fold ([imgs empty])
([ap (map relative->absolute paths)])
; directory?
(if (directory-exists? ap)
(append imgs (dir-files ap))
(append imgs (list ap))))))
(cond [(> (length absolute-paths) 1)
; we want to load a collection
(pfs absolute-paths)]
[else
; we want to load the image from the directory
(define-values (base name dir?) (split-path (first absolute-paths)))
(image-dir base)
; (path-files dir) filters only supported images
(pfs (path-files base))])
(image-path (first absolute-paths)))

; application handler stuff
(application-file-handler
(λ (path)
(when (supported-file? path)
(cond [(equal? (image-path) +root-path+)
; only bother resizing ivy-frame if we're starting fresh
(resize-ivy-frame path)
; center the frame
(send ivy-frame center 'both)
; prep work
(set-image-paths! (list path))
; actually load the file
(load-image path)]
[else
; append image to the collection
(send (ivy-canvas) on-drop-file path)]))))

; accept command-line path to load image
(command-line
#:program "Ivy"
Expand Down Expand Up @@ -149,86 +249,10 @@
(verbose? #t)]
#:args args
; hijack requested-images for -M
(unless (or (not (show-frame?))
(empty? args))
(unless (or (not (show-frame?)) (empty? args))
; if there are directories as paths, scan them
(define absolute-paths
(remove-duplicates
(for/fold ([imgs empty])
([ap (map relative->absolute args)])
; directory?
(if (directory-exists? ap)
(append imgs (dir-files ap))
(append imgs (list ap))))))
(cond [(> (length absolute-paths) 1)
; we want to load a collection
(pfs absolute-paths)]
[else
; we want to load the image from the directory
(define-values (base name dir?) (split-path (first absolute-paths)))
(image-dir base)
; (path-files dir) filters only supported images
(pfs (path-files base))])
(image-path (first absolute-paths))
; resize ivy-frame so that the image isn't horribly squished if it's large
(when (supported-file? (image-path))
; determine the monitor dimensions
(define-values (monitor-width monitor-height) (get-display-size))
; approximate canvas offset
(define canvas-offset 84)
(define max-width (- monitor-width canvas-offset 100))
(define max-height (- monitor-height 100))
(define pct (if (flif? (image-path))
(let ([dimensions (flif-dimensions (image-path))])
(rectangle (first dimensions) (second dimensions)))
(bitmap (image-path))))
(define pct-width (pict-width pct))
(define pct-height (pict-height pct))
(cond
; all good, let's resize the frame
[(and (< pct-width max-width)
(< pct-height max-height))
(send ivy-frame resize
(inexact->exact (floor pct-width))
(+ (inexact->exact (floor pct-height)) canvas-offset))]
; image is the same size as the monitor
[(and (= pct-width max-width)
(= pct-height max-height))
(define resized (scale-to-fit pct
pct-width
(- max-height canvas-offset)))
(send ivy-frame resize
(- (inexact->exact (floor (pict-width resized))) canvas-offset)
max-height)]
; wide image
[(and (>= pct-width max-width)
(<= pct-height max-height))
(define y
(cond [(> pct-height (- max-height canvas-offset))
max-height]
[(<= pct-height (- max-height canvas-offset))
(+ pct-height canvas-offset)]
[else pct-height]))
(define resized (scale-to-fit pct max-width y))
(send ivy-frame resize
max-width
(+ (inexact->exact (floor (pict-height resized))) canvas-offset))]
; tall image
[(and (<= pct-width max-width)
(>= pct-height max-height))
(define resized (scale-to-fit pct
pct-width
(- max-height canvas-offset)))
(send ivy-frame resize
(inexact->exact (floor (pict-width resized)))
max-height)]
; both wide and tall
[(and (> pct-width max-width)
(> pct-height max-height))
(define resized (scale-to-fit pct max-width (- max-height canvas-offset)))
(send ivy-frame resize
(inexact->exact (floor (pict-width resized)))
(+ (inexact->exact (floor (pict-height resized))) canvas-offset))])))
(set-image-paths! args)
(resize-ivy-frame (image-path)))

(cond
; we aren't search for tags on the cmdline, open frame
Expand Down