Skip to content

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

Linux: menus switch in place on compositors that animate windows, and say which menu they are (part of #277) (#306)

Ahmed Nefzaoui1612501ba9dd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 49554a5d..cf307709 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -843,6 +843,14 @@ choose. And Linux, checked on a real desktop with every change.
lists still opened in the middle of the screen, as the compositor placed them like windows. They
are now menu windows no window manager places, as X11 menus are, so they open where MDM puts
them, on every window manager.
+- **Linux: menus appear at once, as menus, on compositors that animate windows** (Hyprland,
+ picom with fading, [#277](https://github.com/anefzaoui/misterdownloadmanager/issues/277)).
+ Before the fix above, Hyprland gave MDM's menus a window's border and slid each one down from
+ the top of the screen. And moving from title to title along the menu bar closed one menu
+ window and opened another, so such a compositor faded each menu out and the next one in at
+ every switch. The menu bar's menus now share one window that moves from title to title, as a
+ submenu's window already did from item to item, and each menu window says which menu it is (a
+ drop-down menu, a right-click menu, a drop-down list), as GTK's and Qt's do.
- **Leaving a submenu no longer closes the whole menu**
([#250](https://github.com/anefzaoui/misterdownloadmanager/issues/250)): with a submenu open
(Tasks → Import, Downloads → Speed Limiter), moving the pointer back onto its menu, to another
diff --git a/ROADMAP.md b/ROADMAP.md
index 10762c16..e1e9b839 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -436,8 +436,10 @@ These ship when they're ready: in 0.5.0 when they make it, or in a later version
- 🔍 Menus opening as the pointer crosses the menu bar on a tiling window manager
([#277](https://github.com/anefzaoui/misterdownloadmanager/issues/277)): after a command the
bar waits for a click, as before the first one (now tested on Windows and Linux); once a menu is
- open, the pointer moves it from title to title, as Windows' menu bars do. Whether a tiling
- desktop should behave otherwise is being discussed with the reporter
+ open, the pointer moves it from title to title, as Windows' menu bars do. Under compositors that
+ animate windows (Hyprland), the menus now appear at once: they are menu windows the compositor
+ doesn't slide in, and going from title to title moves one window instead of closing one and
+ opening the next (each faded out and in). Waiting on the reporter's confirmation
- 🔍 A freeze of the window on start ([#57](https://github.com/anefzaoui/misterdownloadmanager/issues/57)):
the first window took over a minute while the graphics driver set up OpenGL; MDM now draws
with Direct3D 12 by default and keeps it through a slow start, so 0.5.0 should not do
diff --git a/crates/app/src/app.rs b/crates/app/src/app.rs
index 662a0aab..da8f3933 100644
--- a/crates/app/src/app.rs
+++ b/crates/app/src/app.rs
@@ -12,8 +12,8 @@ use mdm_engine::{Destination, Download, Options, Request};
use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue};
use crate::chrome::{
- self, IconKey, MenuBarEvent, MenuEntry, PopupResult, ShellIcons, ToolbarEvent, TreeEvent,
- TreeRow,
+ self, IconKey, MenuBarEvent, MenuEntry, MenuKind, PopupResult, ShellIcons, ToolbarEvent,
+ TreeEvent, TreeRow,
};
use crate::dialogs::{self, AddUrl, Complete, FileInfo, Outcome, Start};
use crate::icons::Icon;
@@ -6220,14 +6220,23 @@ impl App {
if let Some(event) = chrome::menu_bar(ui, o, w, self.open_menu) {
self.open_menu = chrome::menubar_next(self.open_menu, event);
}
- if let Some(i) = self.open_menu {
+ // A menu opened by Left or Right is drawn in the same pass as the one it replaces (a
+ // second turn): on X11 the bar's menus share one window (chrome::popup), and a pass
+ // without a menu closed it, a new window opening for the next (#277).
+ for _ in 0..2 {
+ let Some(i) = self.open_menu else { break };
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);
- let event = match chrome::popup(&ctx, &format!("menu-{i}"), at, &entries, &|c: &Cmd| {
- c.hint()
- }) {
+ let event = match chrome::popup(
+ &ctx,
+ MenuKind::Bar,
+ &format!("menu-{i}"),
+ at,
+ &entries,
+ &|c: &Cmd| c.hint(),
+ ) {
PopupResult::Open => None,
PopupResult::Neighbor(d) => {
let n = chrome::menus(ui.painter()).len() as i32;
@@ -6249,9 +6258,13 @@ impl App {
Some(MenuBarEvent::Dismissed { press_on_title })
}
};
+ let neighbor = matches!(event, Some(MenuBarEvent::Open(_)));
if let Some(event) = event {
self.open_menu = chrome::menubar_next(self.open_menu, event);
}
+ if !neighbor {
+ break;
+ }
}
// Toolbar — full labels (IDM truncates "Delete Co...", "Start Qu...": user decision).
@@ -6573,6 +6586,7 @@ impl App {
};
match chrome::popup(
&ctx,
+ MenuKind::Context,
&format!("popup-{serial}"),
pos,
&entries,
diff --git a/crates/app/src/chrome.rs b/crates/app/src/chrome.rs
index 565e08bf..c8b633bc 100644
--- a/crates/app/src/chrome.rs
+++ b/crates/app/src/chrome.rs
@@ -638,6 +638,18 @@ fn keyboard<C: Clone>(
out
}
+/// What a popup menu is: how its window says so to the window system (X11's window types) and,
+/// on X11, whether it shares its window with others ([`menu_viewport`]).
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum MenuKind {
+ /// A menu of the menu bar, and its submenus.
+ Bar,
+ /// A context menu: a right click, the Menu key, a toolbar button's drop-down.
+ Context,
+ /// A drop-down list's choices: a combo box's, a text box's history.
+ List,
+}
+
/// Show a popup menu with its top-left at `pos` (main-window client coordinates, on screen) —
/// its top-right in a right-to-left interface, where menus open to the left.
///
@@ -647,6 +659,7 @@ fn keyboard<C: Clone>(
/// widgets underneath. Call every frame while open, from the main window's `ui`.
pub fn popup<C: Clone>(
ctx: &egui::Context,
+ kind: MenuKind,
id: &str,
pos: Pos2,
entries: &[MenuEntry<C>],
@@ -691,6 +704,7 @@ pub fn popup<C: Clone>(
ctx.data_mut(|d| d.insert_temp(window_focus, false));
popup_level(
ctx,
+ kind,
id,
origin + pos.to_vec2(),
None,
@@ -1012,9 +1026,41 @@ fn place_menu(ctx: &egui::Context, at: Pos2, size: egui::Vec2, parent: Option<Re
at
}
+/// Whether the menu bar's menus share one window per level ([`menu_viewport`]): on X11 and
+/// XWayland, where MDM places its menus itself.
+fn bar_shares_window() -> bool {
+ #[cfg(target_os = "linux")]
+ {
+ !crate::unix::on_wayland()
+ }
+ // Windows and macOS: one window per menu, as before. Native Wayland (MDM_WAYLAND=1) too: a
+ // program can't move its windows there (winit 0.30 has no popup windows on Wayland), so a
+ // shared window would stay where the first menu opened.
+ #[cfg(not(target_os = "linux"))]
+ {
+ false
+ }
+}
+
+/// The window menu `id`'s level `depth` is drawn in. With `bar_shared` (X11: [`bar_shares_window`])
+/// every menu of the bar is drawn in one window per level, moved and resized as the pointer or
+/// the arrow keys go from title to title, as a submenu's window already is from item to item.
+/// A window of its own for each menu was mapped and unmapped at every switch, and a compositor
+/// that animates new windows (Hyprland, picom with fading) faded each one in, and the last one
+/// out, every time (#277). Other menus: a window of their own, as before.
+fn menu_viewport(kind: MenuKind, id: &str, depth: usize, bar_shared: bool) -> egui::ViewportId {
+ let key = if bar_shared && kind == MenuKind::Bar {
+ "menu-bar"
+ } else {
+ id
+ };
+ egui::ViewportId::from_hash_of(("menu", key, depth))
+}
+
#[allow(clippy::too_many_arguments)]
fn popup_level<C: Clone>(
ctx: &egui::Context,
+ kind: MenuKind,
id: &str,
at: Pos2,
parent: Option<Rect>,
@@ -1035,15 +1081,9 @@ fn popup_level<C: Clone>(
.with_active(false)
.with_position(screen.min)
.with_inner_size(size);
- // Linux, X11 and XWayland (MDM's default): a menu window the window manager doesn't manage
- // (override-redirect), as X11 menus are, so it opens exactly where it's placed. A tiling
- // compositor (sway) placed a managed one as it likes, centred on the screen (#267). Never
- // activated either way (the main window keeps the keyboard); its type says it's a menu.
- // Ignored on Wayland, which has no such window.
+ // Linux: a menu window as X11 menus are (unix::menu_window).
#[cfg(target_os = "linux")]
- let builder = builder
- .with_override_redirect(true)
- .with_window_type(egui::X11WindowType::PopupMenu);
+ let builder = crate::unix::menu_window(builder, kind);
let open_sub = if depth == 0 {
ctx.data(|d| d.get_temp::<usize>(sub_key))
} else {
@@ -1051,7 +1091,7 @@ fn popup_level<C: Clone>(
};
// (index, slot top) of the submenu to show beside this menu.
let mut sub_at: Option<(usize, f32)> = None;
- let vid = egui::ViewportId::from_hash_of(("menu", id, depth));
+ let vid = menu_viewport(kind, id, depth, bar_shares_window());
let parent_vid = ctx.viewport_id();
// Set when this pass made the new (hidden) window a menu window: shown below. (Elsewhere a
// menu's window is created visible.)
@@ -1343,6 +1383,7 @@ fn popup_level<C: Clone>(
let at = pos2(x, screen.min.y + slot_y - 3.0);
popup_level(
ctx,
+ kind,
id,
at,
Some(screen),
@@ -2154,7 +2195,43 @@ fn chevron(p: &egui::Painter, ox: f32, top: f32, open: bool, bright: bool) {
#[cfg(test)]
mod tests {
- use super::{FOCUS_GRACE, FocusVerdict, MenuBarEvent, MenuFocus, menubar_next};
+ use super::{
+ FOCUS_GRACE, FocusVerdict, MenuBarEvent, MenuFocus, MenuKind, menu_viewport, menubar_next,
+ };
+
+ /// #277: on X11 the menu bar's menus share one window per level, so going from title to
+ /// title moves it (a compositor has no new window to fade in); elsewhere, and for every
+ /// other menu, a window of their own as before.
+ #[test]
+ fn the_bar_menus_share_one_window_on_x11() {
+ let bar = |i: usize, depth, shared| {
+ menu_viewport(MenuKind::Bar, &format!("menu-{i}"), depth, shared)
+ };
+ // Tasks, File, Downloads, View, Help: one window (and one for their submenus).
+ for i in 1..5 {
+ assert_eq!(bar(0, 0, true), bar(i, 0, true));
+ assert_eq!(bar(0, 1, true), bar(i, 1, true));
+ }
+ assert_ne!(bar(0, 0, true), bar(0, 1, true));
+ // Without sharing (Windows, macOS, native Wayland): one per menu, as before.
+ assert_ne!(bar(0, 0, false), bar(1, 0, false));
+ assert_eq!(
+ bar(2, 0, false),
+ egui::ViewportId::from_hash_of(("menu", "menu-2", 0usize))
+ );
+ // A context menu and a drop-down list keep their own window, shared or not.
+ for shared in [true, false] {
+ let ctx = menu_viewport(MenuKind::Context, "popup-1", 0, shared);
+ let list = menu_viewport(MenuKind::List, "drop-x", 0, shared);
+ assert_ne!(ctx, bar(0, 0, shared));
+ assert_ne!(list, bar(0, 0, shared));
+ assert_ne!(ctx, list);
+ assert_eq!(
+ ctx,
+ egui::ViewportId::from_hash_of(("menu", "popup-1", 0usize))
+ );
+ }
+ }
/// Runs `events` from no menu open; the open menu after each.
fn run(events: &[MenuBarEvent]) -> Vec<Option<usize>> {
diff --git a/crates/app/src/unix.rs b/crates/app/src/unix.rs
index 50f3cc87..17e24904 100644
--- a/crates/app/src/unix.rs
+++ b/crates/app/src/unix.rs
@@ -1280,6 +1280,37 @@ pub fn on_wayland() -> bool {
window_system().wayland
}
+/// Linux, X11 and XWayland (MDM's default): a menu's window as X11 menus are (GTK's, Qt's).
+/// Override-redirect: no window manager manages it, so it opens exactly where it's placed (a
+/// tiling compositor, sway, centred a managed one, #267), and compositors treat it as a menu:
+/// Hyprland gives it no border and doesn't slide or pop it in as it does a new window (it only
+/// fades it, as any X11 menu), where a managed one came down from the top of the screen (#277).
+/// Its type says which menu it is (`menu_window_type`), for the compositors that style each.
+/// Never activated either way: the main window keeps the keyboard, and the menu's keys reach it
+/// (chrome::popup). Native Wayland (`MDM_WAYLAND=1`) ignores both: winit 0.30 has no popup
+/// windows (xdg_popup) there, so a menu is a window the compositor places and animates as any.
+#[cfg(target_os = "linux")]
+pub fn menu_window(
+ builder: egui::ViewportBuilder,
+ kind: crate::chrome::MenuKind,
+) -> egui::ViewportBuilder {
+ builder
+ .with_override_redirect(true)
+ .with_window_type(menu_window_type(kind))
+}
+
+/// The X11 window type (`_NET_WM_WINDOW_TYPE`) of a menu's window: the menu bar's are drop-down
+/// menus, context menus popup menus, a drop-down list's choices a combo's.
+#[cfg(target_os = "linux")]
+fn menu_window_type(kind: crate::chrome::MenuKind) -> egui::X11WindowType {
+ use crate::chrome::MenuKind;
+ match kind {
+ MenuKind::Bar => egui::X11WindowType::DropdownMenu,
+ MenuKind::Context => egui::X11WindowType::PopupMenu,
+ MenuKind::List => egui::X11WindowType::Combo,
+ }
+}
+
/// The family of distributions /etc/os-release (`os_release`) names: the index of its packages
/// in `WindowLib::packages`, and how it installs one.
#[cfg(target_os = "linux")]
@@ -1870,7 +1901,23 @@ fn set_xattr(path: &Path, name: &str, value: &str) {
mod tests {
use zbus::zvariant::Value;
- use super::{WindowLib, X11_LIBS, choose_window_system, install_command};
+ use super::{WindowLib, X11_LIBS, choose_window_system, install_command, menu_window_type};
+
+ /// #277: each menu's window says which menu it is, as GTK's and Qt's do.
+ #[test]
+ fn menu_windows_have_their_x11_type() {
+ use crate::chrome::MenuKind;
+ use egui::X11WindowType;
+ assert_eq!(menu_window_type(MenuKind::Bar), X11WindowType::DropdownMenu);
+ assert_eq!(
+ menu_window_type(MenuKind::Context),
+ X11WindowType::PopupMenu
+ );
+ assert_eq!(menu_window_type(MenuKind::List), X11WindowType::Combo);
+ let b = super::menu_window(egui::ViewportBuilder::default(), MenuKind::Bar);
+ assert_eq!(b.override_redirect, Some(true));
+ assert_eq!(b.window_type, Some(X11WindowType::DropdownMenu));
+ }
const X11_KEYBOARD: &str = "libxkbcommon-x11.so.0";
diff --git a/crates/app/src/winui.rs b/crates/app/src/winui.rs
index c29e1596..8c7c23f9 100644
--- a/crates/app/src/winui.rs
+++ b/crates/app/src/winui.rs
@@ -2284,9 +2284,14 @@ pub fn copy_menu(resp: &Response, text: &str) {
return;
};
let entries = [MenuEntry::Item("&Copy", (), !text.is_empty(), false)];
- match crate::chrome::popup(&ctx, &format!("copy-{:?}", resp.id), at, &entries, &|_| {
- None
- }) {
+ match crate::chrome::popup(
+ &ctx,
+ crate::chrome::MenuKind::Context,
+ &format!("copy-{:?}", resp.id),
+ at,
+ &entries,
+ &|_| None,
+ ) {
PopupResult::Open | PopupResult::Neighbor(_) => {}
PopupResult::Chosen(()) => {
crate::copy_text(&ctx, text.to_owned());
@@ -2411,7 +2416,14 @@ pub fn edit_menu(resp: &Response, value: &mut String, password: bool, multiline:
MenuEntry::Sep,
MenuEntry::Item("Select &all", EditCmd::SelectAll, n > 0, false),
];
- let cmd = match crate::chrome::popup(&ctx, &format!("edit-{id:?}"), at, &entries, &|_| None) {
+ let cmd = match crate::chrome::popup(
+ &ctx,
+ crate::chrome::MenuKind::Context,
+ &format!("edit-{id:?}"),
+ at,
+ &entries,
+ &|_| None,
+ ) {
PopupResult::Open | PopupResult::Neighbor(_) => return,
PopupResult::Dismissed => None,
PopupResult::Chosen(c) => Some(c),
@@ -2754,7 +2766,14 @@ pub fn drop_list(
} else {
anchor.left_bottom()
};
- let menu = crate::chrome::popup(ui.ctx(), &format!("drop-{id}"), at, &entries, &|_| None);
+ let menu = crate::chrome::popup(
+ ui.ctx(),
+ crate::chrome::MenuKind::List,
+ &format!("drop-{id}"),
+ at,
+ &entries,
+ &|_| None,
+ );
match menu {
PopupResult::Chosen(i) => {
*open = false;
@@ -5350,7 +5369,14 @@ pub fn combo_light(
} else {
rc.left_bottom()
};
- match crate::chrome::popup(ui.ctx(), &popup_id, at, &entries, &|_| None) {
+ match crate::chrome::popup(
+ ui.ctx(),
+ crate::chrome::MenuKind::List,
+ &popup_id,
+ at,
+ &entries,
+ &|_| None,
+ ) {
PopupResult::Chosen(i) => {
open = false;
chosen = Some(i).filter(|i| *i != selected);