208 lines
11 KiB
Text
208 lines
11 KiB
Text
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||
|
|
|
||
|
|
;; bjomarks-gui/src/fetch-button.bjo — the module (bjomarks-gui fetch-button):
|
||
|
|
;; the buttons that fetch a title and an archive link, with a spinner while
|
||
|
|
;; they work.
|
||
|
|
;;
|
||
|
|
;; - `fetch-button` is beside the title, and `archive-button` beside the
|
||
|
|
;; archive link. Both are made by `spinner-button`.
|
||
|
|
;; - The value of a button is true while its fetch runs. Then the label is hidden, and an
|
||
|
|
;; arc turns in its place, longer and shorter as it turns. The button
|
||
|
|
;; keeps its size.
|
||
|
|
;; - The animation is Avalonia's, in a style of the arc that applies while
|
||
|
|
;; the arc has the class "spinning". So it runs on the render loop, and
|
||
|
|
;; only while a fetch runs.
|
||
|
|
;; - A click sends `FetchPressed`, also while a fetch runs: the program
|
||
|
|
;; decides what that does.
|
||
|
|
;;
|
||
|
|
;; bjogui has no animations, so the button is an Avalonia `Button`, made
|
||
|
|
;; with (bjogui native).
|
||
|
|
|
||
|
|
(export FetchOut fetch-button archive-button)
|
||
|
|
|
||
|
|
(import (std prelude))
|
||
|
|
(import (std interop))
|
||
|
|
(import (bjogui native))
|
||
|
|
|
||
|
|
(import/class
|
||
|
|
(Object (: System.Object))
|
||
|
|
(TimeSpan (: System.TimeSpan))
|
||
|
|
(Control (: Avalonia.Controls.Control))
|
||
|
|
(Button (: Avalonia.Controls.Button (-> Button)))
|
||
|
|
(ContentControl (: Avalonia.Controls.ContentControl))
|
||
|
|
(Grid (: Avalonia.Controls.Grid (-> Grid)))
|
||
|
|
(Panel (: Avalonia.Controls.Panel))
|
||
|
|
(Controls (: Avalonia.Controls.Controls))
|
||
|
|
(TextBlock (: Avalonia.Controls.TextBlock (-> TextBlock)))
|
||
|
|
(Arc (: Avalonia.Controls.Shapes.Arc (-> Arc)))
|
||
|
|
(Shape (: Avalonia.Controls.Shapes.Shape))
|
||
|
|
(IBrush (: Avalonia.Media.IBrush))
|
||
|
|
(PenLineCap (: Avalonia.Media.PenLineCap))
|
||
|
|
(Layoutable (: Avalonia.Layout.Layoutable))
|
||
|
|
(HAlign (: Avalonia.Layout.HorizontalAlignment))
|
||
|
|
(VAlign (: Avalonia.Layout.VerticalAlignment))
|
||
|
|
(Visual (: Avalonia.Visual))
|
||
|
|
(StyledElement (: Avalonia.StyledElement))
|
||
|
|
(AvaloniaObject (: Avalonia.AvaloniaObject))
|
||
|
|
(AvaloniaProperty (: Avalonia.AvaloniaProperty))
|
||
|
|
(Classes (: Avalonia.Controls.Classes))
|
||
|
|
(Styles (: Avalonia.Styling.Styles))
|
||
|
|
(IStyle (: Avalonia.Styling.IStyle))
|
||
|
|
(Style (: Avalonia.Styling.Style (-> Style)))
|
||
|
|
(StyleBase (: Avalonia.Styling.StyleBase))
|
||
|
|
(Selector (: Avalonia.Styling.Selector))
|
||
|
|
(Setter (: Avalonia.Styling.Setter (-> AvaloniaProperty Object Setter)))
|
||
|
|
(Animation (: Avalonia.Animation.Animation (-> Animation)))
|
||
|
|
(IAnimation (: Avalonia.Animation.IAnimation))
|
||
|
|
(KeyFrame (: Avalonia.Animation.KeyFrame (-> KeyFrame)))
|
||
|
|
(KeyFrames (: Avalonia.Animation.KeyFrames))
|
||
|
|
(IAnimationSetter (: Avalonia.Animation.IAnimationSetter))
|
||
|
|
(Cue (: Avalonia.Animation.Cue (-> double Cue)))
|
||
|
|
(IterationCount (: Avalonia.Animation.IterationCount))
|
||
|
|
(Easing (: Avalonia.Animation.Easings.Easing))
|
||
|
|
(SineEaseInOut (: Avalonia.Animation.Easings.SineEaseInOut (-> SineEaseInOut)))
|
||
|
|
((ICollection %a) (: System.Collections.Generic.ICollection))
|
||
|
|
((IList %a) (: System.Collections.Generic.IList))
|
||
|
|
((AvaloniaList %a) (: Avalonia.Collections.AvaloniaList)))
|
||
|
|
|
||
|
|
(import/extern
|
||
|
|
;; Typed, so that a Native AOT build keeps the event.
|
||
|
|
(on-click! (: Avalonia.Controls.Button.Click #:event)))
|
||
|
|
|
||
|
|
(import/extern
|
||
|
|
(from-ms (: System.TimeSpan.FromMilliseconds (-> double TimeSpan)))
|
||
|
|
(content! (: Avalonia.Controls.ContentControl.Content (-> ContentControl Object void) #:set))
|
||
|
|
(children-of (: Avalonia.Controls.Panel.Children (-> Panel Controls) #:get))
|
||
|
|
(child-at (: Avalonia.Controls.Controls.get_Item (-> Controls int Control)))
|
||
|
|
(text! (: Avalonia.Controls.TextBlock.Text (-> TextBlock string void) #:set))
|
||
|
|
(width! (: Avalonia.Layout.Layoutable.Width (-> Layoutable double void) #:set))
|
||
|
|
(height! (: Avalonia.Layout.Layoutable.Height (-> Layoutable double void) #:set))
|
||
|
|
(horizontal! (: Avalonia.Layout.Layoutable.HorizontalAlignment (-> Layoutable HAlign void) #:set))
|
||
|
|
(vertical! (: Avalonia.Layout.Layoutable.VerticalAlignment (-> Layoutable VAlign void) #:set))
|
||
|
|
(visible! (: Avalonia.Visual.IsVisible (-> Visual bool void) #:set))
|
||
|
|
(opacity! (: Avalonia.Visual.Opacity (-> Visual double void) #:set))
|
||
|
|
(start-angle! (: Avalonia.Controls.Shapes.Arc.StartAngle (-> Arc double void) #:set))
|
||
|
|
(sweep-angle! (: Avalonia.Controls.Shapes.Arc.SweepAngle (-> Arc double void) #:set))
|
||
|
|
(start-angle-property (: Avalonia.Controls.Shapes.Arc.StartAngleProperty #:get))
|
||
|
|
(sweep-angle-property (: Avalonia.Controls.Shapes.Arc.SweepAngleProperty #:get))
|
||
|
|
(stroke! (: Avalonia.Controls.Shapes.Shape.Stroke (-> Shape IBrush void) #:set))
|
||
|
|
(stroke-thickness! (: Avalonia.Controls.Shapes.Shape.StrokeThickness (-> Shape double void) #:set))
|
||
|
|
(stroke-cap! (: Avalonia.Controls.Shapes.Shape.StrokeLineCap (-> Shape PenLineCap void) #:set))
|
||
|
|
(foreground-property (: Avalonia.Controls.Primitives.TemplatedControl.ForegroundProperty #:get))
|
||
|
|
(get-value (: Avalonia.AvaloniaObject.GetValue (-> AvaloniaObject AvaloniaProperty Object)))
|
||
|
|
(classes-of (: Avalonia.StyledElement.Classes (-> StyledElement Classes) #:get))
|
||
|
|
(class-set! (: Avalonia.Controls.Classes.Set (-> Classes string bool void)))
|
||
|
|
(styles-of (: Avalonia.StyledElement.Styles (-> StyledElement Styles) #:get))
|
||
|
|
(styles-add! (: Avalonia.Styling.Styles.Add (-> Styles IStyle void)))
|
||
|
|
(selector! (: Avalonia.Styling.Style.Selector (-> Style Selector void) #:set))
|
||
|
|
(class-selector (: Avalonia.Styling.Selectors.Class (-> Selector string Selector)))
|
||
|
|
(animations-of (: Avalonia.Styling.StyleBase.Animations (-> StyleBase (IList IAnimation)) #:get))
|
||
|
|
(duration! (: Avalonia.Animation.Animation.Duration (-> Animation TimeSpan void) #:set))
|
||
|
|
(iterations! (: Avalonia.Animation.Animation.IterationCount (-> Animation IterationCount void) #:set))
|
||
|
|
(easing! (: Avalonia.Animation.Animation.Easing (-> Animation Easing void) #:set))
|
||
|
|
(forever (: Avalonia.Animation.IterationCount.Infinite #:get))
|
||
|
|
(key-frames-of (: Avalonia.Animation.Animation.Children (-> Animation KeyFrames) #:get))
|
||
|
|
(setters-of (: Avalonia.Animation.KeyFrame.Setters (-> KeyFrame (AvaloniaList IAnimationSetter)) #:get))
|
||
|
|
(cue! (: Avalonia.Animation.KeyFrame.Cue (-> KeyFrame Cue void) #:set))
|
||
|
|
(add-to! (: System.Collections.Generic.ICollection.Add (-> (ICollection %a) %a void))))
|
||
|
|
|
||
|
|
;; What the button sends: it was clicked.
|
||
|
|
(type (: FetchOut (Union FetchPressed)))
|
||
|
|
|
||
|
|
;; The class that makes the arc turn.
|
||
|
|
(: string)
|
||
|
|
(def spinning "spinning")
|
||
|
|
|
||
|
|
;; An animation of `property` through `values`, evenly spaced, in `ms`
|
||
|
|
;; milliseconds, again and again.
|
||
|
|
(: (-> AvaloniaProperty (Vec double) double Animation))
|
||
|
|
(defun (looping property values ms)
|
||
|
|
(def a (Animation.))
|
||
|
|
(duration! a (from-ms ms))
|
||
|
|
(iterations! a forever)
|
||
|
|
(def n (vec-length values))
|
||
|
|
(loop (:for i (range 0 n))
|
||
|
|
(:do (def k (KeyFrame.))
|
||
|
|
(cue! k (Cue. (/ (cast double i) (cast double (- n 1)))))
|
||
|
|
(add-to! (cast (ICollection IAnimationSetter) (setters-of k))
|
||
|
|
(cast IAnimationSetter (Setter. property (cast Object (vec-ref values i)))))
|
||
|
|
(add-to! (cast (ICollection KeyFrame) (key-frames-of a)) k)))
|
||
|
|
a)
|
||
|
|
|
||
|
|
;; The style that turns an arc while it has the class "spinning": its start
|
||
|
|
;; goes round once in 0.9 s, and its length grows and shrinks in 1.6 s, so
|
||
|
|
;; that the two do not keep step.
|
||
|
|
(: (-> Style))
|
||
|
|
(defun (spin-style)
|
||
|
|
(def s (Style.))
|
||
|
|
(selector! s (class-selector (clr-default) spinning))
|
||
|
|
(def turn (looping (cast AvaloniaProperty start-angle-property) [0.0 360.0] 900.0))
|
||
|
|
(def breathe (looping (cast AvaloniaProperty sweep-angle-property) [60.0 270.0 60.0] 1600.0))
|
||
|
|
(easing! breathe (cast Easing (SineEaseInOut.)))
|
||
|
|
(def animations (cast (ICollection IAnimation) (animations-of s)))
|
||
|
|
(add-to! animations (cast IAnimation turn))
|
||
|
|
(add-to! animations (cast IAnimation breathe))
|
||
|
|
s)
|
||
|
|
|
||
|
|
(: (-> Control Grid))
|
||
|
|
(defun (face-of button) (cast Grid (.-Content (cast ContentControl button))))
|
||
|
|
|
||
|
|
;; The label and the arc, in the button.
|
||
|
|
(: (-> Control (Tuple TextBlock Arc)))
|
||
|
|
(defun (parts-of button)
|
||
|
|
(def kids (children-of (cast Panel (face-of button))))
|
||
|
|
(Tuple (cast TextBlock (child-at kids 0)) (cast Arc (child-at kids 1))))
|
||
|
|
|
||
|
|
;; The arc is drawn in the colour of the button's text, when the button has
|
||
|
|
;; one.
|
||
|
|
(: (-> Control Arc void))
|
||
|
|
(defun (colour-arc! button arc)
|
||
|
|
(match (get-value (cast AvaloniaObject button) (cast AvaloniaProperty foreground-property))
|
||
|
|
((:is Avalonia.Media.IBrush brush) (stroke! (cast Shape arc) brush))
|
||
|
|
(_ unit)))
|
||
|
|
|
||
|
|
;; A button labelled `label`, with a spinner while its value is true.
|
||
|
|
(: (-> string (Native bool FetchOut)))
|
||
|
|
(defun (spinner-button label)
|
||
|
|
(native (fun (send)
|
||
|
|
(def text (TextBlock.))
|
||
|
|
(text! text label)
|
||
|
|
(horizontal! (cast Layoutable text) HAlign.Center)
|
||
|
|
(vertical! (cast Layoutable text) VAlign.Center)
|
||
|
|
(def arc (Arc.))
|
||
|
|
(width! (cast Layoutable arc) 16.0)
|
||
|
|
(height! (cast Layoutable arc) 16.0)
|
||
|
|
(horizontal! (cast Layoutable arc) HAlign.Center)
|
||
|
|
(vertical! (cast Layoutable arc) VAlign.Center)
|
||
|
|
(start-angle! arc 0.0)
|
||
|
|
(sweep-angle! arc 270.0)
|
||
|
|
(stroke-thickness! (cast Shape arc) 2.0)
|
||
|
|
(stroke-cap! (cast Shape arc) PenLineCap.Round)
|
||
|
|
(visible! (cast Visual arc) #f)
|
||
|
|
(styles-add! (styles-of (cast StyledElement arc)) (cast IStyle (spin-style)))
|
||
|
|
;; One cell: the arc is over the label, and the label gives the
|
||
|
|
;; button its size.
|
||
|
|
(def face (Grid.))
|
||
|
|
(.Add (children-of (cast Panel face)) (cast Control text))
|
||
|
|
(.Add (children-of (cast Panel face)) (cast Control arc))
|
||
|
|
(def button (Button.))
|
||
|
|
(content! (cast ContentControl button) (cast Object face))
|
||
|
|
(ignore (on-click! button (fun (s e) (send FetchPressed))))
|
||
|
|
(cast Control button))
|
||
|
|
(fun (control busy)
|
||
|
|
(def (Tuple text arc) (parts-of control))
|
||
|
|
(when busy (colour-arc! control arc))
|
||
|
|
(opacity! (cast Visual text) (if busy 0.0 1.0))
|
||
|
|
(visible! (cast Visual arc) busy)
|
||
|
|
(class-set! (classes-of (cast StyledElement arc)) spinning busy))))
|
||
|
|
|
||
|
|
;; The button beside the title of the dialog.
|
||
|
|
(: fetch-button (Native bool FetchOut))
|
||
|
|
(def fetch-button (spinner-button "Fetch title"))
|
||
|
|
|
||
|
|
;; The button beside the archive link of the dialog.
|
||
|
|
(: archive-button (Native bool FetchOut))
|
||
|
|
(def archive-button (spinner-button "Fetch archive"))
|