Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-message

Source of release 0.5.0

0.5.0 / src / message_list.luc

## The message list: one folder in the classic Mail layout, newest first by date, with
## columns for the unread dot, the flag, the attachment clip, the sender, the subject,
## a conversation's message count and the date. Organized by conversation (the default, as in Mail), a row is a whole
## thread: its newest message's sender, subject and date, the number of messages, and
## a dot or flag when any message has one. Unread rows are bold. Typing narrows the rows
## to those whose sender or subject holds the text; a server search (Enter) narrows them
## to the messages whose text holds it.
from luce_ui.ui import TableView, TableColumn, TableCell, CellAlign, Font
from mail_index import MessageIndex
import clock
import mail_index
import theme
from sources import seen_flag, flagged_flag

pub class MessageList:
    pub let table: TableView
    var index: MessageIndex? = none
    ## What each row shows, in display order: a conversation's number when threaded,
    ## a message's index position otherwise.
    var rows: list[int] = []
    ## Whether rows are conversations.
    pub var threaded: bool = true
    var filter: str = ""
    ## The UIDs a server search matched, when one narrows the list.
    var found: set[int]? = none
    var shown_uid: int = -1
    var shown_count: int = 0
    ## Called with the UID of the (newest) message picked, or -1 when none is.
    pub var on_pick: func(int) -> unit!? = none

    pub func init(self, font: Font) -> unit!:
        let columns = [TableColumn(title = "", width = 28.0, minimum = 28.0, align = CellAlign.center),
                       TableColumn(title = "", width = 28.0, minimum = 28.0, align = CellAlign.center),
                       TableColumn(title = "", width = 28.0, minimum = 28.0, align = CellAlign.center),
                       TableColumn(title = "From", width = 150.0),
                       TableColumn(title = "Subject", weight = 1.0),
                       TableColumn(title = "", width = 36.0, minimum = 28.0, align = CellAlign.right),
                       TableColumn(title = "Date", width = 96.0, align = CellAlign.right)]
        self.table = TableView(columns, [], font = font, multiple = true)

    ## Show `index`'s messages, keeping the picked message when it is still there.
    pub func show(self, index: MessageIndex) -> unit!:
        self.index = index
        self.rebuild()

    ## Show no folder.
    pub func clear(self) -> unit!:
        self.index = none
        self.rebuild()

    ## Narrow the rows to those whose sender or subject holds `text` (any case).
    pub func set_filter(self, text: str) -> unit!:
        self.filter = text.trim().lower()
        self.rebuild()

    ## Narrow the rows to the messages with `uids` (a server search's answer); a
    ## conversation stays when any of its messages matched. `none` shows all again.
    pub func set_matches(self, uids: set[int]?) -> unit!:
        self.found = uids
        self.rebuild()

    ## Show conversations, or every message on its own.
    pub func set_threaded(self, threaded: bool) -> unit!:
        self.threaded = threaded
        self.rebuild()

    ## The index position of the message a row stands for: a conversation's newest.
    func lead(self, index: MessageIndex, row: int) -> int:
        return index.thread_member(row, 0) if self.threaded else row

    func shows(self, index: MessageIndex, row: int) -> bool:
        if let wanted = self.found:
            if self.threaded:
                var any = false
                for member in 0..<index.thread_size(row):
                    if index.uid(index.thread_member(row, member)) in wanted:
                        any = true
                if not any:
                    return false
            elif not (index.uid(row) in wanted):
                return false
            return true
        let at = self.lead(index, row)
        return self.filter == "" or self.filter in index.sender(at).lower() or self.filter in index.subject(at).lower()

    func rebuild(self) -> unit!:
        var cells: list[TableCell] = []
        var rows: list[int] = []
        var keep = -1
        # The other selected rows, by their lead UID, to select again after.
        let marked = self.marked_uids()
        var again: list[int] = []
        if let index = self.index:
            let count = index.thread_count() if self.threaded else index.count()
            for nth in 0..<count:
                let row = nth if self.threaded else index.newest(nth)
                if not self.shows(index, row):
                    continue
                let at = self.lead(index, row)
                var unread = not mail_index.has(index.flags(at), seen_flag)
                var flagged = mail_index.has(index.flags(at), flagged_flag)
                var attachment = index.attachment(at)
                var size = 1
                if self.threaded:
                    unread = index.thread_any(row, seen_flag, false)
                    flagged = index.thread_any(row, flagged_flag, true)
                    attachment = index.thread_attachment(row)
                    size = index.thread_size(row)
                if index.uid(at) == self.shown_uid:
                    keep = rows.length
                elif index.uid(at) in marked:
                    again.append(rows.length)
                rows.append(row)
                cells.append(TableCell(text = "●" if unread else "", tint = theme.unread()))
                cells.append(TableCell(text = "⚑" if flagged else "", tint = theme.flag()))
                cells.append(TableCell(text = "@" if attachment else "", muted = true))
                let sender = index.sender(at)
                cells.append(TableCell(text = sender if sender != "" else "(no sender)", emphasis = unread))
                let subject = index.subject(at)
                let title = subject if subject != "" else "(no subject)"
                cells.append(TableCell(text = title, emphasis = unread))
                # A conversation's message count, beside the date as Mail shows it.
                cells.append(TableCell(text = f"{size}" if size > 1 else "", muted = true))
                cells.append(TableCell(text = clock.list_date(index.date(at)), muted = true))
        self.rows = rows
        self.table.set_rows(cells)
        if keep >= 0:
            self.table.select(keep)
            for row in again:
                self.table.mark(row, true)
        else:
            self.shown_uid = -1
            if let pick = self.on_pick:
                pick(-1)

    ## The picked row (the lead of a selection), if any.
    func picked_row(self) -> int?:
        let at = self.table.selected_index()
        if at < 0 or at >= self.rows.length:
            return none
        return self.rows[at]

    ## Every selected row, top to bottom.
    func picked_rows(self) -> list[int]:
        var picked: list[int] = []
        for (at, row) in self.rows.indexed():
            if self.table.is_selected(at):
                picked.append(row)
        return picked

    ## The UID each selected row stands for: a conversation's newest message.
    pub func picked_leads(self) -> list[int]:
        var uids: list[int] = []
        let index = self.index else return uids
        for row in self.picked_rows():
            uids.append(index.uid(self.lead(index, row)))
        return uids

    ## The same, as a set.
    func marked_uids(self) -> set[int]:
        var uids: set[int] = {}
        for uid in self.picked_leads():
            uids.insert(uid)
        return uids

    ## How many rows (conversations, or messages) are selected.
    pub func picked_count(self) -> int:
        return self.table.selected_count()

    ## The UIDs of `row`'s messages: a whole conversation, newest first.
    func row_uids(self, index: MessageIndex, row: int) -> list[int]:
        if not self.threaded:
            return [index.uid(row)]
        var uids: list[int] = []
        for member in 0..<index.thread_size(row):
            uids.append(index.uid(index.thread_member(row, member)))
        return uids

    ## The UID of the picked message (a conversation's newest), or -1.
    pub func picked_uid(self) -> int:
        let index = self.index else return -1
        let row = self.picked_row() else return -1
        return index.uid(self.lead(index, row))

    ## The UIDs of the picked conversation's messages, newest first, as the reader shows them.
    pub func shown_uids(self) -> list[int]:
        let index = self.index else return []
        let row = self.picked_row() else return []
        return self.row_uids(index, row)

    ## The UIDs of every message the selection stands for: whole conversations.
    pub func picked_uids(self) -> list[int]:
        var uids: list[int] = []
        let index = self.index else return uids
        for row in self.picked_rows():
            for uid in self.row_uids(index, row):
                uids.append(uid)
        return uids

    ## The picked message's flags (a conversation's newest), or 0.
    pub func picked_flags(self) -> int:
        let index = self.index else return 0
        let row = self.picked_row() else return 0
        return index.flags(self.lead(index, row))

    ## Whether any message of the picked conversation is unread.
    pub func picked_unread(self) -> bool:
        let index = self.index else return false
        let row = self.picked_row() else return false
        if self.threaded:
            return index.thread_any(row, seen_flag, false)
        return not mail_index.has(index.flags(row), seen_flag)

    ## Pick the row after the selection when its messages leave the list (deleted, moved).
    pub func pick_next(self) -> unit!:
        var last = -1
        for at in 0..<self.rows.length:
            if self.table.is_selected(at):
                last = at
        if last >= 0 and last + 1 < self.rows.length:
            self.table.select(last + 1)

    ## Tell `on_pick` when the picked message changed; called every frame.
    pub func poll(self) -> unit!:
        let uid = self.picked_uid()
        let count = self.picked_count()
        if uid == self.shown_uid and count == self.shown_count:
            return
        self.shown_uid = uid
        self.shown_count = count
        if let pick = self.on_pick:
            pick(uid)