Skip to content

misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux

A menu shut with Escape opens again at the first click on its title (#246)

Ahmed Nefzaouic24098860b67
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bd7df884..1e1642af 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -529,6 +529,12 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
### Fixed
+- **A menu shut with Escape opens again at the first click on its title**: a menu opened from
+ the menu bar and shut with Escape, the pointer still on its title, was left marked open. On
+ Linux the next click on that title only "shut" it, and a second click was needed; on Windows
+ Escape seemed to do nothing (the menu came back at once). Escape, or MDM losing the focus, now
+ shuts the menu whatever the pointer rests on; a press on a title still shuts its own menu or
+ opens another.
- **A video that needs a sign-in downloads from the button over it**
([#229](https://github.com/anefzaoui/misterdownloadmanager/issues/229)): the button's cookies
reached the Download video window's look-up but not the download's own, which then failed with
diff --git a/ROADMAP.md b/ROADMAP.md
index 3a1c8614..14660a62 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -13,6 +13,8 @@ Everything below ships together, in one release, as version 0.3.0.
### Done
+- ✅ A menu of the menu bar shut with Escape opens again at the first click on its title (on
+ Linux it took two; on Windows, Escape with the pointer on the title left the menu open)
- ✅ Cookies for video sites: yt-dlp can read a browser's cookies on this computer (Firefox,
Chrome, Edge, Brave, Opera, Vivaldi, Chromium, Whale; Safari on macOS), so a pasted YouTube
link that asks to "confirm you're not a bot" works when you're signed in there; and the button
diff --git a/crates/app/src/app.rs b/crates/app/src/app.rs
index 672d5639..c2d5c19e 100644
--- a/crates/app/src/app.rs
+++ b/crates/app/src/app.rs
@@ -12,7 +12,8 @@ use mdm_engine::{Destination, Download, Options, Request};
use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue};
use crate::chrome::{
- self, IconKey, MenuEntry, PopupResult, ShellIcons, ToolbarEvent, TreeEvent, TreeRow,
+ self, IconKey, MenuBarEvent, MenuEntry, PopupResult, ShellIcons, ToolbarEvent, TreeEvent,
+ TreeRow,
};
use crate::dialogs::{self, AddUrl, Complete, FileInfo, Outcome, Start};
use crate::icons::Icon;
@@ -5782,7 +5783,7 @@ impl App {
self.alt_down = alt;
if let Some(i) = open {
self.alt_alone = false;
- self.open_menu = Some(i);
+ self.open_menu = chrome::menubar_next(self.open_menu, MenuBarEvent::Open(i));
chrome::start_keyboard(&ctx, &format!("menu-{i}"));
}
} else {
@@ -5790,43 +5791,43 @@ impl App {
self.alt_alone = false;
}
- // Menu bar and its popups.
+ // Menu bar and its popups: which menu is open is chrome::menubar_next's to say.
if let Some(i) = chrome::menu_bar(ui, o, w, self.open_menu) {
- let clicked = ui.input(|x| x.pointer.any_released()) || chrome::menubar_collapse(&ctx);
- self.open_menu = if self.open_menu == Some(i) && clicked {
- None
- } else {
- Some(i)
- };
+ let released = ui.input(|x| x.pointer.any_released()) || chrome::menubar_collapse(&ctx);
+ self.open_menu =
+ chrome::menubar_next(self.open_menu, MenuBarEvent::Title { i, released });
}
if let Some(i) = self.open_menu {
let x = chrome::menus(ui.painter())[i].1;
let entries = self.menu_entries(i);
// Under the item's leading edge (the menu's top-right in RTL).
let at = pos2(winui::mx(o.x + x), o.y + chrome::MENU_H - 1.0);
- match chrome::popup(&ctx, &format!("menu-{i}"), at, &entries, &|c: &Cmd| {
+ let event = match chrome::popup(&ctx, &format!("menu-{i}"), at, &entries, &|c: &Cmd| {
c.hint()
}) {
- PopupResult::Open => {}
+ PopupResult::Open => None,
PopupResult::Neighbor(d) => {
let n = chrome::menus(ui.painter()).len() as i32;
let j = (i as i32 + d).rem_euclid(n) as usize;
- self.open_menu = Some(j);
chrome::start_keyboard(&ctx, &format!("menu-{j}"));
+ Some(MenuBarEvent::Open(j))
}
PopupResult::Chosen(c) => {
chosen = Some((c, None));
- self.open_menu = None;
+ Some(MenuBarEvent::Chosen)
}
PopupResult::Dismissed => {
- // Clicks on the bar itself are handled by `menu_bar`.
- let on_bar = ctx
- .pointer_interact_pos()
- .is_some_and(|p| p.y < o.y + chrome::MENU_H);
- if !on_bar {
- self.open_menu = None;
- }
+ // Shut by a press on a title: its click (on release) is `menu_bar`'s. Not
+ // by Escape with the pointer merely resting on the bar.
+ let press =
+ ui.input(|x| x.pointer.press_origin().filter(|_| x.pointer.any_pressed()));
+ let press_on_title = press
+ .is_some_and(|at| chrome::menubar_title_at(ui.painter(), o, at).is_some());
+ Some(MenuBarEvent::Dismissed { press_on_title })
}
+ };
+ if let Some(event) = event {
+ self.open_menu = chrome::menubar_next(self.open_menu, event);
}
}
diff --git a/crates/app/src/chrome.rs b/crates/app/src/chrome.rs
index 13044c47..b63a6a8e 100644
--- a/crates/app/src/chrome.rs
+++ b/crates/app/src/chrome.rs
@@ -292,6 +292,56 @@ pub fn menu_bar(ui: &mut Ui, o: Pos2, width: f32, open: Option<usize>) -> Option
hit
}
+/// The menu-bar title under `pos` (main-window coordinates), as [`menu_bar`] lays them out at
+/// `o`: the first menu at the right in RTL.
+pub fn menubar_title_at(p: &egui::Painter, o: Pos2, pos: Pos2) -> Option<usize> {
+ menus(p).iter().position(|(_, x, w, _)| {
+ winui::rect(Rect::from_min_size(o + vec2(*x, 0.0), vec2(*w, MENU_H))).contains(pos)
+ })
+}
+
+/// What happened to the menu bar this frame, for [`menubar_next`].
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum MenuBarEvent {
+ /// [`menu_bar`] returned title `i`: clicked (`released`: the click's release, or a
+ /// screen reader's Collapse), or hovered while another menu is open.
+ Title { i: usize, released: bool },
+ /// The open menu shut itself (Escape, a press outside it, the app losing the focus);
+ /// `press_on_title`: the press that shut it went down on a title of the bar.
+ Dismissed { press_on_title: bool },
+ /// An item was chosen.
+ Chosen,
+ /// Left or Right, or the keyboard (Alt, F10, an access letter), opened menu `i`.
+ Open(usize),
+}
+
+/// The menu bar's open menu after `event` (`open`: the one open before it).
+///
+/// A menu that shut itself is closed here too, unless the press that shut it went down on a
+/// title: that press's release is the title's click, which shuts the menu (its own title) or
+/// opens another one. Escape, or the app losing the focus, with the pointer resting on the bar
+/// (where the click that opened the menu left it) used to leave the menu marked open with
+/// nothing shown, so the next click on its title only "shut" it, and nothing opened.
+pub fn menubar_next(open: Option<usize>, event: MenuBarEvent) -> Option<usize> {
+ match event {
+ MenuBarEvent::Title { i, released } => {
+ if open == Some(i) && released {
+ None
+ } else {
+ Some(i)
+ }
+ }
+ MenuBarEvent::Dismissed {
+ press_on_title: true,
+ } => open,
+ MenuBarEvent::Dismissed {
+ press_on_title: false,
+ }
+ | MenuBarEvent::Chosen => None,
+ MenuBarEvent::Open(i) => Some(i),
+ }
+}
+
/// A screen reader or UI Automation asked to shut the open menu-bar menu (Collapse on its
/// title) this frame; taken once.
pub fn menubar_collapse(ctx: &egui::Context) -> bool {
@@ -2027,3 +2077,103 @@ fn chevron(p: &egui::Painter, ox: f32, top: f32, open: bool, bright: bool) {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::{MenuBarEvent, menubar_next};
+
+ /// Runs `events` from no menu open; the open menu after each.
+ fn run(events: &[MenuBarEvent]) -> Vec<Option<usize>> {
+ let mut open = None;
+ events
+ .iter()
+ .map(|e| {
+ open = menubar_next(open, *e);
+ open
+ })
+ .collect()
+ }
+
+ #[test]
+ fn a_title_clicked_after_escape_opens_its_menu_again() {
+ // Click Tasks, Escape (the pointer still on Tasks: no press), click Tasks.
+ let steps = run(&[
+ MenuBarEvent::Title {
+ i: 0,
+ released: true,
+ },
+ MenuBarEvent::Dismissed {
+ press_on_title: false,
+ },
+ MenuBarEvent::Title {
+ i: 0,
+ released: true,
+ },
+ ]);
+ assert_eq!(steps, [Some(0), None, Some(0)]);
+ }
+
+ #[test]
+ fn clicking_the_open_menus_title_shuts_it() {
+ // The press shuts the menu's window; its release, on the same title, shuts the menu.
+ let steps = run(&[
+ MenuBarEvent::Title {
+ i: 1,
+ released: true,
+ },
+ MenuBarEvent::Dismissed {
+ press_on_title: true,
+ },
+ MenuBarEvent::Title {
+ i: 1,
+ released: true,
+ },
+ MenuBarEvent::Title {
+ i: 1,
+ released: true,
+ },
+ ]);
+ assert_eq!(steps, [Some(1), Some(1), None, Some(1)]);
+ }
+
+ #[test]
+ fn another_title_opens_its_menu_from_an_open_one() {
+ let steps = run(&[
+ MenuBarEvent::Title {
+ i: 0,
+ released: true,
+ },
+ // Hovered while Tasks is open: File opens.
+ MenuBarEvent::Title {
+ i: 1,
+ released: false,
+ },
+ // Pressed and released on Downloads.
+ MenuBarEvent::Dismissed {
+ press_on_title: true,
+ },
+ MenuBarEvent::Title {
+ i: 2,
+ released: true,
+ },
+ ]);
+ assert_eq!(steps, [Some(0), Some(1), Some(1), Some(2)]);
+ }
+
+ #[test]
+ fn a_press_elsewhere_a_choice_or_the_focus_lost_closes_the_menu() {
+ for end in [
+ MenuBarEvent::Dismissed {
+ press_on_title: false,
+ },
+ MenuBarEvent::Chosen,
+ ] {
+ assert_eq!(run(&[MenuBarEvent::Open(3), end]), [Some(3), None]);
+ }
+ // Left / Right from the keyboard move to the neighbour.
+ assert_eq!(
+ run(&[MenuBarEvent::Open(0), MenuBarEvent::Open(4)]),
+ [Some(0), Some(4)]
+ );
+ }
+}