mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-19 08:50:58 +01:00
fix(ui): mirror the macOS title bar when the traffic lights are on the right (RTL system languages)
## What this changes On macOS with a **right-to-left system language** (Arabic, Hebrew, Persian, …) AppKit lays the traffic lights out from the **right** end of the title bar. That happens even though PdfCraft has no right-to-left localization, and whatever PdfCraft's own interface language is (I checked 0.8.0 in English on an Arabic system: same result). The integrated tab strip (#789 / #1055) always kept its clearance on the left. So: - the lights covered the **theme** and **keyboard-shortcuts** buttons and crowded **Discord**, so they couldn't be clicked; - Home sat beside an empty 70 pt gap where the lights would have been. Before (0.8.0, Arabic system language): | | Strip | |---|---| | Expected left-to-right design | `[● ● ●] [Home] [tabs…] [+ Open] ……… [Discord] [?] [theme]` | | 0.8.0, RTL system | `[ 70 pt gap ] [Home] [+ Open] ……… [Discord] [?][theme] under [● ● ●]` | | This PR, RTL system | `[theme] [?] [Discord] ……… [Open +] [tabs…] [Home] [● ● ●]` | The fix: 1. **Ask the window which side the lights are on** instead of guessing from a language: `NSWindow.windowTitlebarLayoutDirection`, which Apple documents for apps that use `titlebarAppearsTransparent` and put controls under the title bar ("the layout direction may be RTL even in applications that do not have a RTL language localization"). Safe `objc2-app-kit` calls only (same approach as `drag_pointer.rs`), on the main thread, cached in `PdfCraftApp::titlebar_lights_right` once a window has answered. The new features (`NSApplication`, `NSResponder`, `NSWindow`, `NSUserInterfaceLayout`) are already in the tree through winit/rfd, except the empty `NSUserInterfaceLayout` feature. 2. **Mirror the strip** when they are on the right, so the existing design holds with the lights at the other end: Home beside the lights, the tabs reading from the right (the first tab beside Home), then Open; Discord, shortcuts and theme at the far end, in mirrored order. The scroll arrows keep their visual sides. - egui's `ScrollArea` only scrolls content that grows to the right, so mirrored, the tabs keep a left-to-right scroll area inside a box exactly their width (`tabs_shown_width`), set beside Home. - Tab dragging (#1537) reads the slots in reading order with x flipped (`reading_order`), so `drop_index` and `drop_marker` serve both directions unchanged. - The Combine files tab block moved into `combine_tab()` (unchanged behaviour) so it can be drawn first when mirrored and still read last. Left-to-right systems, Windows, Linux (including the GNOME header bar) and the web are unchanged: everything new hangs off `mirrored`, which is only true on macOS with the integrated title bar and lights on the right. ## How you verified it - [x] `cargo fmt --check` - [x] `cargo clippy -p pdfcraft-ui-egui --all-targets -- -D warnings`: clean. With Rust 1.95, `main` itself trips `clippy::collapsible_match` in `crates/edit/src/images.rs` and `clippy::nonminimal_bool` in `crates/ui-egui/src/compare_ui.rs`, files this PR doesn't touch, so the run allowed those two lints; a separate pass with them as warnings finds them only there. - [x] `cargo test -p pdfcraft-ui-egui --no-fail-fast`: 2,911 passed, 0 failed, after the rebase on `main` (`d749fd80`, 2026-11-12) on Windows 11; `pdfcraft-cli` + `pdfcraft`: 198 passed. The rebase moved the mirrored drag into `main`'s new `tab_drag`, which split view's strips share: the main strip passes `mirrored`, the second pane's strip does not. The macOS tests below run only on macOS; on Windows I ran them with the macOS gate lifted locally (not committed): they pass, and with the scrolling fix switched off its test fails. - [x] `cargo xtask layers` and `cargo xtask assets` New tests (macOS, `crates/ui-egui/tests/ui.rs`): - `the_integrated_title_bar_mirrors_with_traffic_lights_at_the_right`: everything stays in the 28 pt band and clear of the lights; Home is beside the lights, the tabs read from the right, then Open; theme, shortcuts and Discord are at the far left in mirrored order. - `dragging_a_tab_in_the_mirrored_title_bar_follows_its_reading_order`: dragging left moves a tab later in the order, and dragging right moves it earlier. Unit tests in `chrome.rs`: `mirrored_tabs_drop_in_their_reading_order`, `shown_tabs_take_their_capped_widths_and_the_gaps`. **Seen failing before the fix:** with mirroring switched off, both UI tests fail. With only the x-flip removed, the drag test fails and the layout test passes, so each test pins its own part. Checked by eye on macOS with an Arabic system language: the real app (dev build, portable mode, three sample PDFs) shows `[theme][?][Discord] … [فتح +][third][second][first][Home][● ● ●]`, with nothing under the lights. ## Never-crash - [x] No new `unwrap`/`expect`/`panic!`/`unreachable!`/`todo!` outside tests - [x] No indexing with input-derived positions (the tab loop indexes `app.views` with indices from `0..len`, as before) - [x] No `unsafe`: the AppKit calls are safe methods in `objc2-app-kit` 0.3; `MainThreadMarker::new()` returns `None` off the main thread, and a missing window returns `None` (then the strip stays as it was and asks again next frame) - [x] No crash fixed here ## Provenance - [x] No asset from an Adobe product - [x] No font files - [x] No GPL/AGPL code - [x] Behaviour from Apple's public AppKit documentation and black-box observation of PdfCraft itself - [x] No new assets ## Security - [x] `--root` confinement untouched - [x] MCP and the control channel untouched - [x] No network access ## Parity and reach - [ ] `parity/`: not applicable (a layout fix, not a feature) - [ ] `automation` tool: not applicable (window chrome) - [x] Docs: no documented behaviour changes ## Anything a reviewer should look at first - **Mirroring follows the lights, not the interface language.** The rest of the app isn't mirrored for RTL languages, so mirroring only the title bar could look like a partial RTL mode. But the title bar's order is anchored to the lights (Home beside them is the #789 design), and AppKit picks their side from the system language, not ours. Tying it to `windowTitlebarLayoutDirection` also covers an English UI on an Arabic system. - **Overflowing tabs when mirrored:** the scroll area still starts at offset 0, its left end, which holds the last tabs. The active tab is scrolled into view on every change (as before), so it's always visible, but at startup with many tabs the first ones (beside Home) may start out of view. Starting it scrolled to the right end would need the content width before the first frame; I left that out to keep this small. Happy to add it. - **A flaky test I saw locally:** `files_and_quit_from_the_operating_system` failed 3 times in ~40 full `--test ui` runs while the machine was also building and running the app, then 0/20 on a quiet machine, both with and without this change. It opens a file on the open worker and waits a fixed 4 frames, so it looks timing-sensitive under load rather than related to this change. - Also adds my own entry to `contributors/people.toml` (separate commit, `sync_github_name = true`).
This commit is contained in:
1 parent
9db6c9d95c
commit
4448ee940b
6 files changed
+325
-51
No files matched your search
@@ -194,3 +194,6 @@ sync_github_name = false
|
||||
|
||||
[people.enriquemelzer-bit]
|
||||
sync_github_name = true
|
||||
|
||||
[people.Yousef-Dev921]
|
||||
sync_github_name = true
|
||||
@@ -68,10 +68,11 @@ pollster = "1.0"
|
||||
fontconfig-parser = "0.5.8"
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
# Safe AppKit calls for drag coordinates and repairing an existing per-file PDFCraft binding.
|
||||
# The Objective-C wrappers and block support are already in the tree through arboard and rfd.
|
||||
# Safe AppKit calls for drag coordinates, which end of the title bar the traffic lights are at, and
|
||||
# repairing an existing per-file PDFCraft binding. The Objective-C wrappers and block support are
|
||||
# already in the tree through arboard and rfd.
|
||||
objc2 = "0.6"
|
||||
objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSEvent", "NSScreen", "NSWorkspace", "block2"] }
|
||||
objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSApplication", "NSEvent", "NSResponder", "NSScreen", "NSUserInterfaceLayout", "NSWindow", "NSWorkspace", "block2"] }
|
||||
objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSArray", "NSGeometry", "NSBundle", "NSURL", "NSString", "NSError"] }
|
||||
block2 = "0.6"
|
||||
plist = "1.10"
|
||||
|
||||
@@ -188,6 +188,7 @@ impl PdfCraftApp {
|
||||
script_messages_dropped: 0,
|
||||
toast_dropped: 0,
|
||||
integrated_titlebar: false,
|
||||
titlebar_lights_right: None,
|
||||
window_controls: None,
|
||||
system_title_bar: false,
|
||||
app_frame_shown: None,
|
||||
|
||||
+207
-48
@@ -22,13 +22,33 @@ const MAC_LIGHT: f32 = 12.0;
|
||||
const MAC_LIGHT_PITCH: f32 = 20.0;
|
||||
const MAC_LIGHTS_LEFT: f32 = 10.0;
|
||||
/// Where the strip's content starts to the right of the traffic lights: the end of the three
|
||||
/// lights plus the same gap the lights keep from the top of the window.
|
||||
/// lights plus the same gap the lights keep from the top of the window. Mirrored, it is where the
|
||||
/// content ends when the lights are at the right.
|
||||
pub const MAC_CONTENT_LEFT: f32 = MAC_LIGHTS_LEFT + 2.0 * MAC_LIGHT_PITCH + MAC_LIGHT + (MAC_TITLE_BAR - MAC_LIGHT) / 2.0;
|
||||
|
||||
/// The strip and its controls everywhere else.
|
||||
const TAB_STRIP: f32 = 38.0;
|
||||
const TAB_HEIGHT: f32 = 30.0;
|
||||
const STRIP_BUTTON: f32 = 28.0;
|
||||
|
||||
/// Whether AppKit lays the traffic lights out from the right end of the title bar, or `None` when
|
||||
/// no window can say yet. It does when the system language is right-to-left, whatever PdfCraft's
|
||||
/// own interface language is, so the window is asked rather than the interface language
|
||||
/// (Apple: `NSWindow.windowTitlebarLayoutDirection`, for apps with controls under the title bar).
|
||||
#[cfg(target_os = "macos")]
|
||||
fn mac_lights_on_right() -> Option<bool> {
|
||||
use objc2_app_kit::{NSApplication, NSUserInterfaceLayoutDirection};
|
||||
// AppKit's window list is only for the main thread (where egui runs).
|
||||
let mtm = objc2::MainThreadMarker::new()?;
|
||||
let window = NSApplication::sharedApplication(mtm).windows().firstObject()?;
|
||||
Some(window.windowTitlebarLayoutDirection() == NSUserInterfaceLayoutDirection::RightToLeft)
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
fn mac_lights_on_right() -> Option<bool> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Draw the window's tab strip: home and open controls, shrinkable and scrollable document tabs with drag
|
||||
/// reordering, and platform window controls.
|
||||
pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
@@ -36,11 +56,27 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
// macOS: the strip is the 28 pt title bar, so the traffic lights are centred in it; tabs and
|
||||
// buttons shrink to keep a margin above and below.
|
||||
let mac = cfg!(target_os = "macos") && app.integrated_titlebar;
|
||||
let left = if mac { MAC_CONTENT_LEFT as i8 } else { 8 };
|
||||
if mac && app.titlebar_lights_right.is_none() {
|
||||
app.titlebar_lights_right = mac_lights_on_right();
|
||||
}
|
||||
// With the lights at the right (a right-to-left system language), the strip is the mirror
|
||||
// image of the usual one: Home beside the lights, the tabs and Open after it, and the
|
||||
// Discord, shortcuts and theme buttons at the far end.
|
||||
let mirrored = mac && app.titlebar_lights_right == Some(true);
|
||||
let (left, right) = match (mac, mirrored) {
|
||||
(true, false) => (MAC_CONTENT_LEFT as i8, 10),
|
||||
(true, true) => (10, MAC_CONTENT_LEFT as i8),
|
||||
(false, _) => (8, 10),
|
||||
};
|
||||
let (row, far_end) = if mirrored {
|
||||
(Layout::right_to_left(Align::Center), Layout::left_to_right(Align::Center))
|
||||
} else {
|
||||
(Layout::left_to_right(Align::Center), Layout::right_to_left(Align::Center))
|
||||
};
|
||||
let (tab_h, button) = if mac { (MAC_TITLE_BAR - 2.0, 24.0) } else { (TAB_HEIGHT, STRIP_BUTTON) };
|
||||
egui::Panel::top("tab_strip")
|
||||
.exact_size(if mac { MAC_TITLE_BAR } else { TAB_STRIP })
|
||||
.frame(egui::Frame::NONE.fill(t.titlebar).inner_margin(egui::Margin { left, right: 10, top: 0, bottom: 0 }))
|
||||
.frame(egui::Frame::NONE.fill(t.titlebar).inner_margin(egui::Margin { left, right, top: 0, bottom: 0 }))
|
||||
.show(ui, |ui| {
|
||||
let full = ui.max_rect();
|
||||
app.panes.main_strip = Some(full);
|
||||
@@ -59,7 +95,7 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
.clone()
|
||||
.filter(|_| app.draws_window_frame() && !fullscreen)
|
||||
.unwrap_or_else(|| window_controls::Layout { left: Vec::new(), right: Vec::new() });
|
||||
ui.horizontal_centered(|ui| {
|
||||
ui.allocate_ui_with_layout(ui.available_size_before_wrap(), row, |ui| {
|
||||
ui.spacing_mut().item_spacing.x = 4.0;
|
||||
if !controls.left.is_empty() {
|
||||
ui.scope(|ui| {
|
||||
@@ -101,9 +137,14 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
// Where the tabs were scrolled to last frame, and how far they can go.
|
||||
let scroll_id = ui.scope_id().with("tab_scroll");
|
||||
let (offset, max_offset) = ui.data(|d| d.get_temp::<(f32, f32)>(scroll_id)).unwrap_or((0.0, 0.0));
|
||||
let mut scroll_to = None;
|
||||
if overflow && arrow(ui, "chevron-left", offset > 0.5, tl!("Scroll tabs left")) {
|
||||
scroll_to = Some((offset - (TAB_MIN_WIDTH + gap)).max(0.0));
|
||||
let scroll_left = |ui: &mut egui::Ui| overflow && arrow(ui, "chevron-left", offset > 0.5, tl!("Scroll tabs left"));
|
||||
let scroll_right = |ui: &mut egui::Ui| overflow && arrow(ui, "chevron-right", offset < max_offset - 0.5, tl!("Scroll tabs right"));
|
||||
// The arrows keep their sides: the one laid out first is at the row's start.
|
||||
let (mut left_clicked, mut right_clicked) = (false, false);
|
||||
if mirrored {
|
||||
right_clicked = scroll_right(ui);
|
||||
} else {
|
||||
left_clicked = scroll_left(ui);
|
||||
}
|
||||
// Keyed by document, not index: a tab dragged past the active one moves it without
|
||||
// the strip jumping to it.
|
||||
@@ -114,14 +155,20 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
data.insert_temp(id, state);
|
||||
changed
|
||||
});
|
||||
// Mirrored, the tabs' reading start is the scroll area's far end: the strip goes
|
||||
// there once when it starts to scroll, and the active tab is brought back in view.
|
||||
let start_id = scroll_id.with("reading_start");
|
||||
let (start, started) = mirrored_start(mirrored, max_offset, ui.data(|d| d.get_temp::<bool>(start_id)).unwrap_or(false));
|
||||
ui.data_mut(|d| d.insert_temp(start_id, started));
|
||||
let changed = changed || start.is_some();
|
||||
let mut action = None;
|
||||
let mut close = None;
|
||||
// The pointer's x while a tab is dragged, for scrolling at the strip's edges.
|
||||
let mut drag_x = None;
|
||||
ui.scope(|ui| {
|
||||
let show_tabs = |ui: &mut egui::Ui| {
|
||||
ui.style_mut().always_scroll_the_only_direction = true;
|
||||
let mut area = egui::ScrollArea::horizontal().id_salt("document_tabs").max_width(tabs_width).auto_shrink([true, true]);
|
||||
if let Some(x) = ui.data_mut(|d| d.remove_temp::<f32>(scroll_id.with("to"))) {
|
||||
if let Some(x) = ui.data_mut(|d| d.remove_temp::<f32>(scroll_id.with("to"))).or(start) {
|
||||
area = area.horizontal_scroll_offset(x);
|
||||
}
|
||||
let out = area.show(ui, |ui| {
|
||||
@@ -130,7 +177,13 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
// (with whether it was just released).
|
||||
let mut slots = Vec::with_capacity(app.views.len());
|
||||
let mut dragged = None;
|
||||
for i in app.tabs_in(Place::Main) {
|
||||
// Mirrored, the tabs read from the right: the first is beside Home.
|
||||
let mut order = app.tabs_in(Place::Main);
|
||||
if mirrored {
|
||||
order.reverse();
|
||||
combine_tab(app, ui, &t, cap, tab_h, changed);
|
||||
}
|
||||
for i in order {
|
||||
let Some(doc) = app.session.get(app.views[i].id) else { continue };
|
||||
let Some(name) = crate::canvas::tab_label(app, i) else { continue };
|
||||
let dirty = doc.dirty;
|
||||
@@ -170,7 +223,7 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
close = Some(i);
|
||||
}
|
||||
}
|
||||
drag_x = tab_drag(app, ui, &slots, dragged, Place::Main, close.is_some() || action.is_some());
|
||||
drag_x = tab_drag(app, ui, &slots, dragged, Place::Main, close.is_some() || action.is_some(), mirrored);
|
||||
if let Some(i) = close
|
||||
&& let Some(view) = app.views.get(i)
|
||||
{
|
||||
@@ -179,40 +232,17 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
if let Some(action) = action {
|
||||
app.tab_action(action);
|
||||
}
|
||||
if app.combine_tab.open {
|
||||
// After the document tabs; its index can't clash with theirs.
|
||||
let mut close = None;
|
||||
let response = tab(
|
||||
ui,
|
||||
TabSpec {
|
||||
id: ui.scope_id().with("combine-tab"),
|
||||
theme: &t,
|
||||
icon: "files",
|
||||
name: tl!("Combine files"),
|
||||
file: None,
|
||||
dirty: false,
|
||||
active: app.combine_showing(),
|
||||
index: usize::MAX,
|
||||
cap,
|
||||
height: tab_h,
|
||||
draggable: false,
|
||||
},
|
||||
&mut close,
|
||||
);
|
||||
if changed && app.combine_showing() {
|
||||
response.scroll_to_me(Some(Align::Center));
|
||||
}
|
||||
if response.clicked() {
|
||||
app.open_combine_tab();
|
||||
}
|
||||
if close.is_some() || response.middle_clicked() {
|
||||
app.close_combine_tab();
|
||||
}
|
||||
if !mirrored {
|
||||
combine_tab(app, ui, &t, cap, tab_h, changed);
|
||||
}
|
||||
});
|
||||
});
|
||||
let max = (out.content_size.x - out.inner_rect.width()).max(0.0);
|
||||
ui.data_mut(|d| d.insert_temp(scroll_id, (out.state.clamped_offset().x, max)));
|
||||
// The strip just started to scroll: the next frame takes it to its reading start.
|
||||
if mirrored_start(mirrored, max, started).0.is_some() {
|
||||
ui.ctx().request_repaint();
|
||||
}
|
||||
// A tab dragged to either edge of a strip that scrolls brings the hidden tabs in.
|
||||
if let Some(x) = drag_x
|
||||
&& max > 0.0
|
||||
@@ -229,8 +259,25 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
ui.ctx().request_repaint();
|
||||
}
|
||||
}
|
||||
});
|
||||
if overflow && arrow(ui, "chevron-right", offset < max_offset - 0.5, tl!("Scroll tabs right")) {
|
||||
};
|
||||
if mirrored {
|
||||
// egui scrolls only content that runs left to right, so mirrored the tabs sit in
|
||||
// a box that runs that way, exactly as wide as they are, beside Home.
|
||||
let width = tabs_shown_width(&natural, cap, gap).min(tabs_width);
|
||||
ui.allocate_ui_with_layout(vec2(width, ui.available_height()), Layout::left_to_right(Align::Center), show_tabs);
|
||||
} else {
|
||||
ui.scope(show_tabs);
|
||||
}
|
||||
if mirrored {
|
||||
left_clicked = scroll_left(ui);
|
||||
} else {
|
||||
right_clicked = scroll_right(ui);
|
||||
}
|
||||
let mut scroll_to = None;
|
||||
if left_clicked {
|
||||
scroll_to = Some((offset - (TAB_MIN_WIDTH + gap)).max(0.0));
|
||||
}
|
||||
if right_clicked {
|
||||
scroll_to = Some((offset + TAB_MIN_WIDTH + gap).min(max_offset));
|
||||
}
|
||||
if let Some(x) = scroll_to {
|
||||
@@ -242,7 +289,7 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
if widgets::ghost_button_sized(ui, "plus", tl!("Open"), tab_h).on_hover_text(tip).clicked() {
|
||||
app.open_dialog();
|
||||
}
|
||||
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
|
||||
ui.with_layout(far_end, |ui| {
|
||||
if !controls.right.is_empty() {
|
||||
// Laid out left to right inside the right-to-left run, so they read in
|
||||
// the desktop's order.
|
||||
@@ -284,6 +331,42 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
});
|
||||
}
|
||||
|
||||
/// The Combine files tab, when it is open: after the document tabs in reading order (drawn first
|
||||
/// when they read from the right). Its index can't clash with theirs.
|
||||
fn combine_tab(app: &mut PdfCraftApp, ui: &mut egui::Ui, t: &Tokens, cap: Option<f32>, height: f32, changed: bool) {
|
||||
if !app.combine_tab.open {
|
||||
return;
|
||||
}
|
||||
let mut close = None;
|
||||
let response = tab(
|
||||
ui,
|
||||
TabSpec {
|
||||
id: ui.scope_id().with("combine-tab"),
|
||||
theme: t,
|
||||
icon: "files",
|
||||
name: tl!("Combine files"),
|
||||
file: None,
|
||||
dirty: false,
|
||||
active: app.combine_showing(),
|
||||
index: usize::MAX,
|
||||
cap,
|
||||
height,
|
||||
draggable: false,
|
||||
},
|
||||
&mut close,
|
||||
);
|
||||
if changed && app.combine_showing() {
|
||||
response.scroll_to_me(Some(Align::Center));
|
||||
}
|
||||
if response.clicked() {
|
||||
app.open_combine_tab();
|
||||
}
|
||||
// A middle click closes it too, as it does a document tab (#1853).
|
||||
if close.is_some() || response.middle_clicked() {
|
||||
app.close_combine_tab();
|
||||
}
|
||||
}
|
||||
|
||||
/// The right-click menu on the title bar's theme button.
|
||||
fn appearance_menu(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
if ui.button(tl!("Appearance settings…")).clicked() {
|
||||
@@ -329,21 +412,32 @@ fn tab_menu(app: &PdfCraftApp, ui: &mut egui::Ui, index: usize, action: &mut Opt
|
||||
/// over split view's other pane, a hint shows where it would go instead, and letting go puts it
|
||||
/// there (`crate::panes`). `busy` is a close or menu choice this frame, which wins. Returns the
|
||||
/// pointer's x while the tab moves along the strip, for scrolling at the strip's edges.
|
||||
fn tab_drag(app: &mut PdfCraftApp, ui: &egui::Ui, slots: &[(usize, Rect)], dragged: Option<(usize, bool)>, place: Place, busy: bool) -> Option<f32> {
|
||||
fn tab_drag(
|
||||
app: &mut PdfCraftApp,
|
||||
ui: &egui::Ui,
|
||||
slots: &[(usize, Rect)],
|
||||
dragged: Option<(usize, bool)>,
|
||||
place: Place,
|
||||
busy: bool,
|
||||
mirrored: bool,
|
||||
) -> Option<f32> {
|
||||
let (from, released) = dragged?;
|
||||
let pos = ui.ctx().pointer_latest_pos()?;
|
||||
match app.drop_target(pos, place) {
|
||||
TabDrop::Strip => {
|
||||
let x = pos.x;
|
||||
let to = drop_index(slots, from, x);
|
||||
// Reading order, with x flipped when the tabs read from the right.
|
||||
let slots = reading_order(slots, mirrored);
|
||||
let flip = |x: f32| if mirrored { -x } else { x };
|
||||
let to = drop_index(&slots, from, flip(x));
|
||||
if released {
|
||||
if !busy {
|
||||
app.move_tab_in_strip(from, slots, to);
|
||||
app.move_tab_in_strip(from, &slots, to);
|
||||
}
|
||||
return None;
|
||||
}
|
||||
ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);
|
||||
if let Some(marker) = drop_marker(slots, from, to, ui.spacing().item_spacing.x) {
|
||||
if let Some(marker) = drop_marker(&slots, from, to, ui.spacing().item_spacing.x).map(flip) {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let span = slots.first().map_or(ui.max_rect().y_range(), |(_, r)| r.y_range());
|
||||
// In front of the first tab the gap lies outside the scroll area's clip rect
|
||||
@@ -418,7 +512,7 @@ pub(crate) fn pane_tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui, strip: Re
|
||||
close = Some(i);
|
||||
}
|
||||
}
|
||||
tab_drag(app, &row, &slots, dragged, place, close.is_some() || action.is_some());
|
||||
tab_drag(app, &row, &slots, dragged, place, close.is_some() || action.is_some(), false);
|
||||
if let Some(i) = close
|
||||
&& let Some(view) = app.views.get(i)
|
||||
{
|
||||
@@ -465,6 +559,32 @@ fn drop_marker(slots: &[(usize, Rect)], from: usize, to: usize, gap: f32) -> Opt
|
||||
}
|
||||
}
|
||||
|
||||
/// The tabs' slots in reading order, in a space where reading runs left to right: when the tabs
|
||||
/// read from the right (`mirrored`), their order is reversed and x flipped, so `drop_index` and
|
||||
/// `drop_marker` serve both directions.
|
||||
fn reading_order(slots: &[(usize, Rect)], mirrored: bool) -> Vec<(usize, Rect)> {
|
||||
if !mirrored {
|
||||
return slots.to_vec();
|
||||
}
|
||||
slots.iter().rev().map(|(i, r)| (*i, Rect::from_x_y_ranges(-r.right()..=-r.left(), r.y_range()))).collect()
|
||||
}
|
||||
|
||||
/// Where a mirrored strip that has just started to scroll goes (`max_offset`, the far end of a
|
||||
/// scroll area that runs left to right, is where its tabs start to read), and whether it has gone
|
||||
/// there. Once only, so the user's own scrolling stands; a strip that stops scrolling starts again.
|
||||
fn mirrored_start(mirrored: bool, max_offset: f32, started: bool) -> (Option<f32>, bool) {
|
||||
if !mirrored || max_offset.is_nan() || max_offset <= 0.5 {
|
||||
return (None, false);
|
||||
}
|
||||
if started { (None, true) } else { (Some(max_offset), true) }
|
||||
}
|
||||
|
||||
/// How wide tabs of `natural` widths, each at most `cap`, are laid out `gap` apart.
|
||||
fn tabs_shown_width(natural: &[f32], cap: Option<f32>, gap: f32) -> f32 {
|
||||
let tabs: f32 = natural.iter().map(|w| cap.map_or(*w, |cap| w.min(cap))).sum();
|
||||
tabs + gap * natural.len().saturating_sub(1) as f32
|
||||
}
|
||||
|
||||
/// A scroll arrow beside the tabs; dimmed (and inert) at its end. True when clicked.
|
||||
fn arrow(ui: &mut egui::Ui, icon: &str, enabled: bool, tip: &str) -> bool {
|
||||
ui.add_enabled_ui(enabled, |ui| icons::button(ui, icon, TAB_ARROW, false, tip)).inner.clicked()
|
||||
@@ -1043,6 +1163,45 @@ mod tab_widths {
|
||||
assert_eq!(drop_marker(&[], 0, 1, 4.0), None);
|
||||
}
|
||||
|
||||
/// Mirrored, the slots come back in reading order with x flipped, so the drop helpers read
|
||||
/// the tabs from the right.
|
||||
#[test]
|
||||
fn mirrored_tabs_drop_in_their_reading_order() {
|
||||
use super::{drop_index, drop_marker, reading_order};
|
||||
// Drawn right to left: tab 0 at the right, tab 2 at the left.
|
||||
let drawn: Vec<_> = three_slots().into_iter().map(|(k, r)| (2 - k, r)).collect();
|
||||
let slots = reading_order(&drawn, true);
|
||||
assert_eq!(slots.iter().map(|(i, _)| *i).collect::<Vec<_>>(), [0, 1, 2]);
|
||||
// Tab 0 (drawn at 208..308, beside Home) dragged to the far left lands at the end.
|
||||
assert_eq!(drop_index(&slots, 0, -10.0), 2);
|
||||
// Its drop line, flipped back, sits left of the leftmost tab.
|
||||
assert_eq!(drop_marker(&slots, 0, 2, 4.0).map(|x| -x), Some(-2.0));
|
||||
// Unmirrored, nothing changes.
|
||||
assert_eq!(reading_order(&three_slots(), false), three_slots());
|
||||
assert!(reading_order(&[], true).is_empty());
|
||||
}
|
||||
|
||||
/// A mirrored strip goes to its reading start (the far end) once when it starts to scroll,
|
||||
/// then leaves the offset to the user until it no longer scrolls.
|
||||
#[test]
|
||||
fn a_mirrored_strip_starts_at_its_reading_start_once() {
|
||||
use super::mirrored_start;
|
||||
assert_eq!(mirrored_start(true, 0.0, false), (None, false), "nothing to scroll");
|
||||
assert_eq!(mirrored_start(true, 240.0, false), (Some(240.0), true), "it just started to scroll");
|
||||
assert_eq!(mirrored_start(true, 240.0, true), (None, true), "once only");
|
||||
assert_eq!(mirrored_start(true, 0.0, true), (None, false), "it stopped scrolling: next time starts again");
|
||||
assert_eq!(mirrored_start(false, 240.0, false), (None, false), "unmirrored, the start is offset 0");
|
||||
assert_eq!(mirrored_start(true, f32::NAN, false), (None, false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shown_tabs_take_their_capped_widths_and_the_gaps() {
|
||||
use super::tabs_shown_width;
|
||||
assert_eq!(tabs_shown_width(&[100.0, 200.0], None, 4.0), 304.0);
|
||||
assert_eq!(tabs_shown_width(&[100.0, 200.0], Some(150.0), 4.0), 254.0);
|
||||
assert_eq!(tabs_shown_width(&[], None, 4.0), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_mac_title_bar_centres_and_clears_the_traffic_lights() {
|
||||
// The lights are centred in the bar, and content keeps the same gap after them (#789).
|
||||
|
||||
@@ -542,6 +542,9 @@ pub struct PdfCraftApp {
|
||||
pub toast_dropped: u64,
|
||||
/// Whether the macOS title bar is drawn by us (traffic lights over our tab strip).
|
||||
pub integrated_titlebar: bool,
|
||||
/// Whether AppKit put those traffic lights at the right end of the title bar, as it does when
|
||||
/// the system language is right-to-left (`None` until a window has said).
|
||||
pub titlebar_lights_right: Option<bool>,
|
||||
/// Set when the desktop leaves the window frame to the app (GNOME on Wayland): the tab strip
|
||||
/// carries these window buttons and the window's edges resize it.
|
||||
pub window_controls: Option<window_controls::Layout>,
|
||||
|
||||
@@ -751,6 +751,113 @@ fn the_integrated_title_bar_is_the_traffic_light_band() {
|
||||
assert!(home.left() >= 62.0 + 4.0, "Home {home:?} clears the traffic lights");
|
||||
}
|
||||
|
||||
/// macOS with a right-to-left system language: AppKit mirrors the traffic lights to the right end
|
||||
/// of the title bar, and the strip mirrors with them. Home sits beside the lights, the tabs read
|
||||
/// from the right after it, then Open; Discord, shortcuts, theme and Preferences move to the far
|
||||
/// left end.
|
||||
#[cfg(target_os = "macos")]
|
||||
fn mirrored_title_bar(names: &[&'static str]) -> Harness<'static, PdfCraftApp> {
|
||||
let names = names.to_vec();
|
||||
harness(move |app| {
|
||||
app.integrated_titlebar = true;
|
||||
app.titlebar_lights_right = Some(true);
|
||||
for name in names {
|
||||
app.open_bytes(name, None, FIXTURE.to_vec()).unwrap();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn the_integrated_title_bar_mirrors_with_traffic_lights_at_the_right() {
|
||||
let (width, band, lights) = (1400.0, 28.0, 62.0);
|
||||
let h = mirrored_title_bar(&["first.pdf", "last.pdf"]);
|
||||
let rect = |label: &str| h.get_by_label(label).rect();
|
||||
let theme = h.get_by_label_contains("Display theme:").rect();
|
||||
for (label, r) in [
|
||||
("Home", rect("Home")),
|
||||
("first.pdf", rect("first.pdf")),
|
||||
("Open", rect("Open")),
|
||||
("Discord", rect("Discord")),
|
||||
("Display theme", theme),
|
||||
("Preferences", rect("Preferences")),
|
||||
] {
|
||||
assert!(r.top() >= 0.0 && r.bottom() <= band, "{label}: {r:?} inside the {band} pt title bar");
|
||||
assert!(r.right() <= width - lights - 4.0, "{label}: {r:?} clears the traffic lights");
|
||||
}
|
||||
// Home is the first thing after the lights, as it is on the left in a left-to-right system.
|
||||
let home = rect("Home");
|
||||
assert!(home.right() >= width - lights - 16.0, "Home {home:?} sits beside the lights");
|
||||
// Then the tabs, read from the right, then Open.
|
||||
assert!(rect("first.pdf").right() <= home.left(), "the first tab follows Home");
|
||||
assert!(rect("last.pdf").right() <= rect("first.pdf").left(), "the tabs read from the right");
|
||||
assert!(rect("first.pdf").right() >= home.left() - 12.0, "the tabs stay beside Home");
|
||||
assert!(rect("Open").right() <= rect("last.pdf").left(), "Open follows the tabs");
|
||||
// The other end: Preferences, theme, shortcuts and Discord, mirroring their order at the right.
|
||||
let (preferences, help, discord) = (rect("Preferences"), rect("Keyboard shortcuts"), rect("Discord"));
|
||||
assert!(preferences.left() < 20.0, "Preferences {preferences:?} at the far end");
|
||||
assert!(
|
||||
preferences.right() <= theme.left() && theme.right() <= help.left() && help.right() <= discord.left(),
|
||||
"Preferences, theme, shortcuts, Discord"
|
||||
);
|
||||
assert!(discord.right() < rect("Open").left(), "Discord stays clear of Open");
|
||||
}
|
||||
|
||||
/// Mirrored, a tab dragged to the left moves later in the order, as it reads from the right.
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn dragging_a_tab_in_the_mirrored_title_bar_follows_its_reading_order() {
|
||||
use egui::accesskit::Role;
|
||||
let mut h = mirrored_title_bar(&["first.pdf", "middle.pdf", "last.pdf"]);
|
||||
let ids: Vec<_> = h.state().views.iter().map(|view| view.id).collect();
|
||||
let tab = |h: &Harness<'static, PdfCraftApp>, name: &str| h.get_by_role_and_label(Role::Tab, name).rect();
|
||||
// first.pdf past the end, which is the left.
|
||||
let to = tab(&h, "last.pdf").left_center() - egui::vec2(20.0, 0.0);
|
||||
drag_named_tab(&mut h, "first.pdf", to, true);
|
||||
assert_eq!(h.state().views.iter().map(|view| view.id).collect::<Vec<_>>(), [ids[1], ids[2], ids[0]]);
|
||||
assert!(tab(&h, "first.pdf").right() < tab(&h, "last.pdf").left(), "the strip draws the new order");
|
||||
// last.pdf, now in the middle, to the front, which is the right.
|
||||
let to = tab(&h, "middle.pdf").right_center() + egui::vec2(10.0, 0.0);
|
||||
drag_named_tab(&mut h, "last.pdf", to, true);
|
||||
assert_eq!(h.state().views.iter().map(|view| view.id).collect::<Vec<_>>(), [ids[2], ids[1], ids[0]]);
|
||||
}
|
||||
|
||||
/// A mirrored strip that starts to scroll (here Home shows and the window narrows) shows its
|
||||
/// reading start: the first tab beside Home, as the unmirrored strip does at its left end. It goes
|
||||
/// there once, so a step the user scrolls stands.
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn a_mirrored_strip_that_starts_to_scroll_shows_the_first_tab_beside_home() {
|
||||
let mut h = mirrored_title_bar(&["first.pdf", "second.pdf", "third.pdf", "last.pdf"]);
|
||||
h.state_mut().active = None;
|
||||
h.run_steps(4);
|
||||
assert!(h.query_by_label("Scroll tabs left").is_none(), "wide, the tabs don't scroll yet");
|
||||
h.set_size(egui::vec2(700.0, 600.0));
|
||||
h.run_steps(6);
|
||||
let home = h.get_by_label("Home").rect();
|
||||
let first = h.get_by_label("first.pdf").rect();
|
||||
assert!(first.right() <= home.left() && first.right() >= home.left() - 50.0, "first.pdf {first:?} beside Home {home:?}");
|
||||
h.get_by_label("Scroll tabs left").click();
|
||||
h.run_steps(6);
|
||||
let scrolled = h.get_by_label("first.pdf").rect();
|
||||
assert!(scrolled.left() > first.left() + 1.0, "the step stands: {first:?} -> {scrolled:?}");
|
||||
}
|
||||
|
||||
/// Going to the reading start never hides the active tab: a document shown while the strip starts
|
||||
/// to scroll stays in view.
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn a_mirrored_strip_that_starts_to_scroll_keeps_the_active_tab_in_view() {
|
||||
let mut h = mirrored_title_bar(&["first.pdf", "second.pdf", "third.pdf", "last.pdf"]);
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.state().active, Some(3));
|
||||
h.set_size(egui::vec2(700.0, 600.0));
|
||||
h.run_steps(6);
|
||||
let (home, open) = (h.get_by_label("Home").rect(), h.get_by_label("Open").rect());
|
||||
let last = h.get_by_label("last.pdf").rect();
|
||||
assert!(last.left() >= open.right() && last.right() <= home.left(), "last.pdf {last:?} between Open {open:?} and Home {home:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn narrow_tab_strips_accept_vertical_wheel_scrolling() {
|
||||
for width in [600.0, 900.0] {
|
||||
|
||||
Reference in new issue
Block a user