Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ the [releases page](https://github.com/c9dev/penguin-mail/releases).

## Unreleased

### New

- The label and folder picker has a search box that has focus when it opens. Type to narrow the list, press Up or Down to move through the matches, and press Enter to apply the first one or the one you moved to. With Gmail's shortcuts, `v` opens the picker.

### Fixed

- In Sent, and in a conversation you have replied to, the message list shows who the last message went to (To Ana Lima), with their photo, color and VIP star, in place of your own name.
Expand Down
16 changes: 16 additions & 0 deletions app/src/offered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,22 @@ impl Filing {
}
}

/// The placeholder and screen reader name of the picker's entry.
pub fn find(self) -> String {
match self {
Filing::Labels => gettext("Find a label"),
Filing::Folders => gettext("Find a folder"),
}
}

/// What the picker says when the typed text matches nothing.
pub fn no_match(self) -> String {
match self {
Filing::Labels => gettext("No label matches that."),
Filing::Folders => gettext("No folder matches that."),
}
}

pub fn menu_item(self) -> String {
match self {
Filing::Labels => gettext("Labels…"),
Expand Down
32 changes: 14 additions & 18 deletions app/src/ui/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2736,32 +2736,28 @@ impl MainWindow {
follow.as_ref(),
);
});
if searchable {
let search = gtk::SearchEntry::builder()
.placeholder_text(gettext("Search all folders"))
.margin_top(6)
.margin_start(6)
.margin_end(6)
.build();
crate::ui::name(&search, &gettext("Search all folders"));
search.connect_search_changed(move |search| fill(&search.text()));
// Enter takes the first folder found.
let first = list.clone();
search.connect_activate(move |_| {
if let Some(row) = first.row_at_index(0) {
row.emit_activate();
}
});
content.append(&search);
}
let scroller = gtk::ScrolledWindow::builder()
.child(&list)
.hscrollbar_policy(gtk::PolicyType::Never)
.propagate_natural_height(true)
.max_content_height(360)
.min_content_width(220)
.build();
let placeholder = match searchable {
true => gettext("Search all folders"),
false => filing.find(),
};
let (search, none) = organize::picker_search(
&placeholder,
&filing.no_match(),
&popover,
&list,
&scroller,
fill,
);
content.append(&search);
content.append(&scroller);
content.append(&none);
content.append(&gtk::Separator::new(gtk::Orientation::Horizontal));
content.append(&create);
popover.set_child(Some(&content));
Expand Down
178 changes: 174 additions & 4 deletions app/src/ui/window/organize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Moving mail by drag and drop, and creating, renaming, and deleting labels.

use std::collections::{HashMap, HashSet};
use std::cell::RefCell;
use std::rc::Rc;

use adw::prelude::*;
Expand Down Expand Up @@ -309,22 +310,50 @@ impl MainWindow {
crate::ui::name(&row, &super::label_row_name(name, false));
list.append(&row);
}
let names = Rc::new(names);
let (weak, pop) = (Rc::downgrade(self), popover.clone());
let picked = Rc::clone(&names);
list.connect_row_activated(move |_, row| {
let (Some(win), Some(name)) = (weak.upgrade(), names.get(row.index() as usize)) else {
let (Some(win), Some(name)) = (weak.upgrade(), picked.get(row.index() as usize)) else {
return;
};
pop.popdown();
win.label_by_name(name.clone());
});
let query = Rc::new(RefCell::new(String::new()));
let shown = Rc::clone(&query);
list.set_filter_func(move |row| {
names
.get(row.index() as usize)
.is_some_and(|name| name_matches(name, &shown.borrow()))
});
let scroller = gtk::ScrolledWindow::builder()
.child(&list)
.hscrollbar_policy(gtk::PolicyType::Never)
.propagate_natural_height(true)
.max_content_height(360)
.min_content_width(220)
.build();
Some(scroller.upcast())
let filing = Filing::Labels;
let (search, none) = picker_search(
&filing.find(),
&filing.no_match(),
popover,
&list,
&scroller,
{
let list = list.clone();
move |text| {
*query.borrow_mut() = text.to_string();
list.invalidate_filter();
}
},
);
let content = gtk::Box::new(gtk::Orientation::Vertical, 0);
content.append(&search);
content.append(&scroller);
content.append(&none);
Some(content.upcast())
}

/// Adds the label called `name` to the targets. An account without a
Expand Down Expand Up @@ -395,7 +424,7 @@ fn focus_at_end(entry: &gtk::Entry) {
});
}

fn focus_when_shown(entry: &gtk::Entry) {
fn focus_when_shown(entry: &(impl IsA<gtk::Widget> + Clone + 'static)) {
entry.connect_map(|entry| {
let entry = entry.clone();
glib::idle_add_local_once(move || {
Expand Down Expand Up @@ -427,13 +456,127 @@ pub(super) fn picked<'a>(
.collect()
}

/// The row to move to from `from` in a picker, `down` or up, skipping rows
/// that cannot take a pick (`usable` is false). With no row chosen yet the
/// first usable one comes first. `None` leaves the choice where it is.
fn step_row(usable: &[bool], from: Option<usize>, down: bool) -> Option<usize> {
match (from, down) {
(None, _) => usable.iter().position(|u| *u),
(Some(i), true) => (i + 1..usable.len()).find(|&n| usable[n]),
(Some(i), false) => usable[..i.min(usable.len())].iter().rposition(|u| *u),
}
}

/// Whether a label called `name` answers a typed `query`: any part of the
/// name, in any case, so a nested label is found by its parent too.
pub(super) fn name_matches(name: &str, query: &str) -> bool {
name.to_lowercase().contains(&query.trim().to_lowercase())
}

/// The type-to-narrow entry of a label or folder picker, and the line shown
/// when nothing matches. Typing runs `refilter`, which narrows `list`; the
/// highlight then goes to the first row that can take a pick. Up and Down
/// move it, Enter applies it as a click would, Escape closes `popover`.
pub(super) fn picker_search(
placeholder: &str,
none: &str,
popover: &gtk::Popover,
list: &gtk::ListBox,
scroller: &gtk::ScrolledWindow,
refilter: impl Fn(&str) + 'static,
) -> (gtk::SearchEntry, gtk::Label) {
list.set_selection_mode(gtk::SelectionMode::Single);
let search = gtk::SearchEntry::builder()
.placeholder_text(placeholder)
.margin_top(6)
.margin_bottom(2)
.margin_start(6)
.margin_end(6)
.build();
crate::ui::name(&search, placeholder);
let message = gtk::Label::builder()
.label(none)
.wrap(true)
.max_width_chars(28)
.margin_top(12)
.margin_bottom(12)
.margin_start(12)
.margin_end(12)
.visible(false)
.build();
let rows = {
let list = list.clone();
move || -> Vec<gtk::ListBoxRow> { (0..).map_while(|n| list.row_at_index(n)).collect() }
};
let usable = |rows: &[gtk::ListBoxRow]| -> Vec<bool> {
rows.iter()
.map(|r| r.is_sensitive() && r.is_child_visible())
.collect()
};
let settle = {
let (list, message, rows) = (list.clone(), message.clone(), rows.clone());
let scroller = scroller.clone();
move || {
let rows = rows();
let any = rows.iter().any(|r| r.is_child_visible());
message.set_visible(!any);
scroller.set_visible(any);
match step_row(&usable(&rows), None, true) {
Some(n) => list.select_row(Some(&rows[n])),
None => list.unselect_all(),
}
}
};
settle();
search.connect_search_changed(move |search| {
refilter(&search.text());
settle();
});
let pop = popover.clone();
search.connect_stop_search(move |_| pop.popdown());
let chosen = list.clone();
search.connect_activate(move |_| {
if let Some(row) = chosen.selected_row() {
row.emit_activate();
}
});
let keys = gtk::EventControllerKey::new();
keys.set_propagation_phase(gtk::PropagationPhase::Capture);
let (list, adjustment) = (list.clone(), scroller.vadjustment());
keys.connect_key_pressed(move |_, key, _, _| {
let down = match key {
gtk::gdk::Key::Down => true,
gtk::gdk::Key::Up => false,
_ => return glib::Propagation::Proceed,
};
let rows = rows();
let current = list.selected_row().map(|r| r.index() as usize);
if let Some(n) = step_row(&usable(&rows), current, down) {
list.select_row(Some(&rows[n]));
// The entry keeps focus, so the list does not scroll by itself.
if let Some(b) = rows[n].compute_bounds(&list) {
let (top, bottom) = (f64::from(b.y()), f64::from(b.y() + b.height()));
if top < adjustment.value() {
adjustment.set_value(top);
} else if bottom > adjustment.value() + adjustment.page_size() {
adjustment.set_value(bottom - adjustment.page_size());
}
}
}
glib::Propagation::Stop
});
search.add_controller(keys);
focus_when_shown(&search);
(search, message)
}

#[cfg(test)]
mod tests {
use std::collections::BTreeSet;

use mailrs_domain::{Label, LabelKind};

use super::picked;
use super::{name_matches, picked, step_row};

fn label(id: &str) -> Label {
Label {
Expand Down Expand Up @@ -467,4 +610,31 @@ mod tests {
super::MOST_FOUND
);
}

#[test]
fn a_query_matches_anywhere_in_a_name_in_any_case() {
assert!(name_matches("Work/Tax 2025", "tax"));
assert!(name_matches("Work/Tax 2025", " WORK/t"));
assert!(name_matches("Receipts", ""));
assert!(!name_matches("Receipts", "work"));
}

#[test]
fn a_nested_folder_is_found_by_its_parent_name() {
let labels = [label("Work/Tax"), label("Home"), label("Workshop")];
let found = picked(&labels, &BTreeSet::new(), "work");
let ids: Vec<_> = found.iter().map(|l| l.id.as_str()).collect();
assert_eq!(ids, ["Work/Tax", "Workshop"]);
}

#[test]
fn the_highlight_starts_on_the_first_usable_row_and_skips_the_rest() {
let usable = [false, true, false, true];
assert_eq!(step_row(&usable, None, true), Some(1));
assert_eq!(step_row(&usable, Some(1), true), Some(3));
assert_eq!(step_row(&usable, Some(3), true), None);
assert_eq!(step_row(&usable, Some(3), false), Some(1));
assert_eq!(step_row(&usable, Some(1), false), None);
assert_eq!(step_row(&[false], None, true), None);
}
}
14 changes: 13 additions & 1 deletion app/src/ui/window/shortcuts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ impl Shortcut {
/// What the dialog's line says. The label key opens the label picker,
/// so its line reads the way the picker words itself for `filing`.
pub(super) fn line(&self, filing: Filing) -> String {
match self.keys.iter().any(|k| k.action == "win.label") {
// Gmail's own `v` line keeps Gmail's name for it.
match self
.keys
.iter()
.any(|k| k.action == "win.label" && k.trigger != "v")
{
true => filing.shortcut_line(),
false => (self.description)(),
}
Expand Down Expand Up @@ -361,6 +366,11 @@ pub(super) static GMAIL_SHORTCUTS: &[Shortcut] = &[
description: || gettext("Go to All Mail"),
keys: &[sequence("g+a", "all-mail")],
},
Shortcut {
section: Section::Organizing,
description: || gettext("Move to"),
keys: &[letter("v", "win.label")],
},
Shortcut {
section: Section::Organizing,
description: || gettext("Mark as read"),
Expand Down Expand Up @@ -1724,9 +1734,11 @@ mod tests {
assert_eq!(action(gdk::Key::u, none), Some("win.back-to-list"));
assert_eq!(action(gdk::Key::o, none), Some("win.open-focused"));
assert_eq!(action(gdk::Key::x, none), Some("win.select-focused"));
assert_eq!(action(gdk::Key::v, none), Some("win.label"));
// The default set has none of them.
let default = edit::Keymap::of(ShortcutPreset::PenguinMail, &[]);
assert_eq!(letter_in(&default, gdk::Key::x, none), None);
assert_eq!(letter_in(&default, gdk::Key::v, none), None);
assert_eq!(
letter_in(&default, gdk::Key::u, none).map(|k| k.action),
Some("win.toggle-read")
Expand Down
5 changes: 5 additions & 0 deletions docs/accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ when the tag is ticked, since the tick itself is drawn without a name.
An account with no tags, or mail from several accounts, gets a line of
words instead of rows.

The label and folder picker opens with focus in a search entry named
"Find a label" or "Find a folder". Typing narrows the rows, Up and Down
move the highlighted row, Enter applies it, and Escape closes the
picker. When nothing matches, a line of words says so.

Focused and Other are the category bar's toggles, the same control as
Gmail's categories, and each is named with its unread count, as in
"Focused, 6 unread". Only one of the two bars shows over an inbox.
Expand Down
Loading
Loading