Repository navigation
Expand file tree
/
Copy pathLibraryFrame.fs
More file actions
146 lines (131 loc) · 5.91 KB
/
Copy pathLibraryFrame.fs
File metadata and controls
146 lines (131 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
namespace Library
open WxKit
open WxKit.FSharp
/// The library's window: a search box, a choice of order, the list of books, and buttons that add a
/// book, remove one, and mark one read. The list shows the books that match the search, in the
/// order chosen, which the model reads by row.
type LibraryFrame() as this =
inherit Frame(null, "Library", size = Size(680, 480))
let book title author year isRead =
{ Title = title
Author = author
Year = year
IsRead = isRead }
let mutable books =
[ book "Pride and Prejudice" "Jane Austen" 1813 true
book "Frankenstein" "Mary Shelley" 1818 false
book "Moby-Dick" "Herman Melville" 1851 false
book "Middlemarch" "George Eliot" 1871 true
book "The Adventures of Huckleberry Finn" "Mark Twain" 1884 false
book "Dracula" "Bram Stoker" 1897 false
book "The Time Machine" "H. G. Wells" 1895 true
book "Emma" "Jane Austen" 1815 false ]
let mutable shown: Book[] = [||]
let panel = new Panel(this)
// Each label is made just before the control it names, so a screen reader names the control
// after it, and the tab order follows.
let searchLabel = new StaticText(panel, "&Search:")
let search = new SearchCtrl(panel, DescriptiveText = "Title or author")
let orderLabel = new StaticText(panel, "&Order by:")
let order = new Choice(panel, List.map Order.name Order.all, Selection = 0)
let listLabel = new StaticText(panel, "&Books:")
let list = new DataViewCtrl(panel, style = DataViewCtrlStyle.RowLines)
let add = new Button(panel, "&Add Book...")
let remove = new Button(panel, "&Remove")
let markRead = new Button(panel, "&Mark Read or Unread")
let showCounts () =
let read = shown |> Array.filter _.IsRead |> Array.length
this.SetStatusText $"{shown.Length} of {books.Length} books shown, {read} of them read"
// Puts a changed copy of the book in a row in place of the book, in the list and the library.
let replace row changed =
let old = shown[row]
books <- books |> List.map (fun book -> if LanguagePrimitives.PhysicalEquality book old then changed else book)
shown[row] <- changed
showCounts ()
let model = BookModel.create (fun () -> shown) (fun row read -> replace row { shown[row] with IsRead = read })
// The row of the selected book, if one is.
let selectedRow () =
if list.HasSelection then Some(model.GetRow list.Selection) else None
// Shows the books that match the search, in the order chosen, and says how many there are.
let showBooks () =
let text = search.Value.Trim()
shown <-
books
|> Seq.filter (fun book -> text = "" || Book.matches text book)
|> Order.sort Order.all[max order.Selection 0]
|> Array.ofSeq
model.Reset shown.Length
showCounts ()
let addBook () =
match AddBook.ask this with
| None -> ()
| Some added ->
books <- books @ [ added ]
// The search is cleared, so the new book is shown, and selected, so a screen reader reads it.
search.ChangeValue ""
showBooks ()
let item = model.GetItem(shown |> Array.findIndex (LanguagePrimitives.PhysicalEquality added))
list.Select item
list.EnsureVisible item
list.SetFocus()
let removeSelected () =
selectedRow ()
|> Option.iter (fun row ->
let old = shown[row]
books <- books |> List.filter (fun book -> not (LanguagePrimitives.PhysicalEquality book old))
shown <- shown |> Array.removeAt row
model.NotifyRowDeleted row
showCounts ())
let toggleSelected () =
selectedRow ()
|> Option.iter (fun row ->
replace row { shown[row] with IsRead = not shown[row].IsRead }
model.NotifyRowChanged row)
do
list.Model <- model
list.AppendTextColumn("Title", Column.Title, width = 260) |> ignore
list.AppendTextColumn("Author", Column.Author, width = 160) |> ignore
list.AppendTextColumn("Year", Column.Year, width = 60) |> ignore
list.AppendToggleColumn("Read", Column.IsRead, DataViewCellMode.Activatable, width = 60) |> ignore
search.TextChanged.Add(fun _ -> showBooks ())
order.SelectionChanged.Add(fun _ -> showBooks ())
add.Click.Add(fun _ -> addBook ())
remove.Click.Add(fun _ -> removeSelected ())
markRead.Click.Add(fun _ -> toggleSelected ())
// The buttons that act on a book only apply while one is selected.
this.Bind(
EventType.UpdateUI,
fun _ (e: UpdateUIEvent) ->
if e.Id = remove.Id || e.Id = markRead.Id then
e.IsEnabled <- selectedRow () |> Option.isSome
)
panel.Sizer <-
column {
row {
item searchLabel |> center
item search |> grow |> center
item orderLabel |> center
item order |> center
}
|> fill
item listLabel |> border (Direction.Left ||| Direction.Right ||| Direction.Top)
item list |> grow
row {
add
remove
markRead
}
}
this.CreateStatusBar() |> ignore
showBooks ()
this.Center()
/// For the smoke test: searches, selects the first book shown, marks it read, and removes it.
member _.Exercise() =
search.Value <- "Austen"
if shown.Length <> 2 then
failwith $"The search showed {shown.Length} books, not 2."
list.Select(model.GetItem 0)
toggleSelected ()
removeSelected ()
if books.Length <> 7 then
failwith $"The library has {books.Length} books, not 7."