mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
Link: a click with a modifier held opens the URL in a new tab, as a middle click does (ctl:32)
Fixtures record the URLs the frames ask to open, for the trial.
This commit is contained in:
@@ -46,6 +46,8 @@ pub(crate) struct FrameResult {
|
||||
pub ime: Option<ImeSeen>,
|
||||
/// The texts the frame put on the clipboard.
|
||||
pub copied: Vec<String>,
|
||||
/// The URLs the frame asked the platform to open, each with whether it asked for a new tab.
|
||||
pub opened: Vec<(String, bool)>,
|
||||
/// The text runs the frame painted.
|
||||
pub texts: Vec<PaintedText>,
|
||||
/// The visual bounds of every shape the frame painted, within its clip rect. A shape with no extent (egui reports an empty galley's
|
||||
@@ -182,9 +184,16 @@ pub(crate) fn run_frame(
|
||||
.iter()
|
||||
.filter_map(|command| if let egui::OutputCommand::CopyText(text) = command { Some(text.clone()) } else { None })
|
||||
.collect();
|
||||
let opened = output
|
||||
.platform_output
|
||||
.commands
|
||||
.iter()
|
||||
.filter_map(|command| if let egui::OutputCommand::OpenUrl(open) = command { Some((open.url.clone(), open.new_tab)) } else { None })
|
||||
.collect();
|
||||
FrameResult {
|
||||
closed,
|
||||
copied,
|
||||
opened,
|
||||
texts,
|
||||
bounds,
|
||||
overshoot,
|
||||
|
||||
@@ -350,6 +350,11 @@ impl Fixture {
|
||||
self.frames.copied()
|
||||
}
|
||||
|
||||
/// Every URL the fixture's frames asked to open, in order, each with whether it asked for a new tab.
|
||||
pub fn opened(&self) -> &[(String, bool)] {
|
||||
self.frames.opened()
|
||||
}
|
||||
|
||||
/// The visual bounds of the shapes the latest frame painted.
|
||||
pub fn shape_bounds(&self) -> &[Rect] {
|
||||
self.frames.shape_bounds()
|
||||
|
||||
@@ -26,6 +26,7 @@ pub struct Frames {
|
||||
tree: Option<AccessTree>,
|
||||
ime: Option<fixture::ImeSeen>,
|
||||
copied: Vec<String>,
|
||||
opened: Vec<(String, bool)>,
|
||||
texts: Vec<fixture::PaintedText>,
|
||||
bounds: Vec<egui::Rect>,
|
||||
overshoot: Vec<f32>,
|
||||
@@ -94,6 +95,7 @@ impl Frames {
|
||||
tree: None,
|
||||
ime: None,
|
||||
copied: Vec::new(),
|
||||
opened: Vec::new(),
|
||||
texts: Vec::new(),
|
||||
bounds: Vec::new(),
|
||||
overshoot: Vec::new(),
|
||||
@@ -231,6 +233,7 @@ impl Frames {
|
||||
self.tree = result.tree;
|
||||
self.ime = result.ime;
|
||||
self.copied = result.copied;
|
||||
self.opened.extend(result.opened);
|
||||
self.texts = result.texts;
|
||||
self.bounds = result.bounds;
|
||||
self.overshoot = result.overshoot;
|
||||
@@ -247,6 +250,11 @@ impl Frames {
|
||||
&self.copied
|
||||
}
|
||||
|
||||
/// Every URL the frames so far asked to open, in order, each with whether it asked for a new tab.
|
||||
pub fn opened(&self) -> &[(String, bool)] {
|
||||
&self.opened
|
||||
}
|
||||
|
||||
/// The text runs the last frame painted.
|
||||
pub fn texts(&self) -> &[fixture::PaintedText] {
|
||||
&self.texts
|
||||
|
||||
@@ -22,7 +22,7 @@ pub struct LinkStyle {
|
||||
}
|
||||
|
||||
/// A link: drawn in the accent colour in a type role (body by default) and underlined, so colour is not its only cue.
|
||||
/// A click, Enter or Space opens the URL in the same tab and a middle click opens it in a new tab; the caller can also act on `clicked()`. AccessKit reports the role `Link`, with the URL when
|
||||
/// A click, Enter or Space opens the URL in the same tab; a middle click, or a click with a modifier held, opens it in a new tab. The caller can also act on `clicked()`. AccessKit reports the role `Link`, with the URL when
|
||||
/// it has one.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
pub struct Link {
|
||||
@@ -89,10 +89,12 @@ impl Component for Link {
|
||||
PressPhase::Release { start, click: true, .. } => start.button == PointerButton::Middle,
|
||||
_ => false,
|
||||
};
|
||||
let activation = cx.activation();
|
||||
if let Some(url) = &self.url
|
||||
&& (cx.activation().is_some() || middle_click)
|
||||
&& (activation.is_some() || middle_click)
|
||||
{
|
||||
cx.open_url(url, middle_click);
|
||||
let modified = activation.is_some_and(|activation| !activation.mods.is_none());
|
||||
cx.open_url(url, middle_click || modified);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
//! A link opens its URL in the same tab on a plain click and in a new tab on a click with a modifier held, as a
|
||||
//! middle click does.
|
||||
|
||||
use craft_ui::buttons::Link;
|
||||
use craft_ui::contract::Show as _;
|
||||
use craft_ui::testing::layout::in_region;
|
||||
use craft_ui::testing::{Frames, pointer, trial};
|
||||
use egui::{Modifiers, Rect, pos2, vec2};
|
||||
use std::cell::Cell;
|
||||
|
||||
#[trial]
|
||||
fn a_modified_click_opens_the_link_in_a_new_tab() {
|
||||
let mut frames = Frames::new();
|
||||
let shown = Cell::new(Rect::NOTHING);
|
||||
let mut show = |ui: &mut egui::Ui| {
|
||||
let region = Rect::from_min_size(pos2(0.0, 0.0), vec2(200.0, 40.0));
|
||||
shown.set(in_region(ui, region, |ui| Link::new("Shortcuts").url("https://example.com").show(ui).rect));
|
||||
};
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
let at = shown.get().center();
|
||||
pointer::click(&mut frames, at, &mut show);
|
||||
frames.hold(Modifiers::COMMAND);
|
||||
pointer::click(&mut frames, at, &mut show);
|
||||
let tabs: Vec<bool> = frames.opened().iter().map(|(_, new_tab)| *new_tab).collect();
|
||||
assert_eq!(tabs, [false, true], "a plain click opens in the same tab, a Cmd/Ctrl click in a new one");
|
||||
}
|
||||
@@ -13,6 +13,7 @@ mod keys;
|
||||
mod label;
|
||||
mod layer;
|
||||
mod layout;
|
||||
mod link;
|
||||
mod list;
|
||||
mod menu;
|
||||
mod overlay;
|
||||
|
||||
Reference in New Issue
Block a user