-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.lua
More file actions
158 lines (137 loc) · 3.56 KB
/
Copy pathController.lua
File metadata and controls
158 lines (137 loc) · 3.56 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
147
148
149
150
151
152
153
154
155
156
157
158
local ns = require("AppKit")
local xml = require("ui.xml")
local Model = require("examples.mail.Model")
local VIEWS = "examples/mail/views/"
local ACTIONS = {
filter = function(self) self:toggleUnreadFilter() end,
compose = function(self) self:compose() end,
reply = function() end,
replyAll = function() end,
forward = function() end,
archive = function() end,
trash = function() end,
}
local function buildMailboxList()
return ns.List {
fixedWidth = 220,
flexGrow = 0,
style = "sourceList",
header = false,
alternatingRows = false,
columns = {
{ id = "name", title = "Mailbox" },
{ id = "count", title = "Count", width = 45, alignment = "right" },
},
}
end
local function buildMessageList()
return ns.List {
fixedWidth = 320,
minWidth = 260,
maxWidth = 420,
flexGrow = 0,
style = "plain",
header = false,
alternatingRows = false,
columns = {
{ id = "from", title = "From" },
},
}
end
local function buildDetailPane()
return ns.VStack {
flexGrow = 1,
}
end
local WindowController = {}
WindowController.__index = WindowController
function WindowController.new()
return setmetatable({
selectedMailbox = Model.mailboxes[1],
mailboxList = nil,
messageList = nil,
detailPane = nil,
unreadOnlyFilter = false,
window = nil,
}, WindowController)
end
function WindowController:toggleUnreadFilter()
self.unreadOnlyFilter = not self.unreadOnlyFilter
self:loadMessages(self.selectedMailbox.id)
end
function WindowController:showDetail(msg)
if not msg then return end
Model.markRead(msg.id)
self.detailPane:clearContainer()
local view = xml.renderFile(VIEWS .. "MessageDetail.etlua", msg)
self.detailPane:add(view)
self.detailPane:layout()
end
function WindowController:loadMessages(mailboxId)
local rows = {}
for _, msg in ipairs(Model.byMailbox(mailboxId)) do
if (not self.unreadOnlyFilter) or msg.unread then
rows[#rows + 1] = {
_id = msg.id,
from = msg.from,
subject = msg.subject,
date = msg.date,
}
end
end
self.messageList:replaceRows(rows)
end
function WindowController:createWindow()
local cfg, refs = xml.renderFile(VIEWS .. "Window.etlua")
refs = refs or {}
for _, item in ipairs(cfg.toolbar or {}) do
if item.action and ACTIONS[item.action] then
local fn = ACTIONS[item.action]
item.action = function() fn(self) end
end
end
self.mailboxList = buildMailboxList()
self.messageList = buildMessageList()
self.detailPane = buildDetailPane()
cfg.sidebar = self.mailboxList
cfg.content = self.messageList
cfg.detail = self.detailPane
local mailboxRows = {}
for _, mb in ipairs(Model.mailboxes) do
mailboxRows[#mailboxRows + 1] = {
_id = mb.id,
name = mb.name,
count = tostring(mb.count),
icon = mb.icon,
}
end
self.mailboxList:replaceRows(mailboxRows)
self.mailboxList:onRowSelect(function(_, _, row)
if not row or not row._id then return end
self.selectedMailbox = { id = row._id }
self:loadMessages(row._id)
self.detailPane:clearContainer()
self.detailPane:layout()
end)
self:loadMessages(self.selectedMailbox.id)
self.messageList:onRowSelect(function(_, _, row)
if row and row._id then self:showDetail(Model.find(row._id)) end
end)
self:showDetail(Model.find(1))
self.window = ns.Window(cfg)
return self.window
end
function WindowController:compose()
Model.addMessage {
mailbox = self.selectedMailbox.id,
from = "Me",
initials = "ME",
subject = "Untitled",
preview = "",
date = "Now",
unread = false,
body = "",
}
self:loadMessages(self.selectedMailbox.id)
end
return WindowController