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
2 changes: 2 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ core-foundation = "0.10"
core-graphics = { version = "0.25", features = ["highsierra"] }
libc = "0.2"
objc2 = "0.6.4"
objc2-quartz-core = { version = "0.3.2", default-features = false, features = ["CALayer", "objc2-core-foundation", "objc2-core-graphics"] }
objc2-app-kit = { version = "0.3.2", features = ["NSBitmapImageRep", "NSColor", "NSControl", "NSEvent", "NSGraphics", "NSImage", "NSImageRep", "NSImageView", "NSPanel", "NSTextField", "NSFont", "NSResponder", "NSScreen", "NSView", "NSWindow", "NSWorkspace", "NSRunningApplication", "objc2-core-foundation"] }
objc2-core-graphics = { version = "0.3.2", default-features = false, features = ["CGEventSource", "CGEventTypes"] }
objc2-foundation = { version = "0.3.2", features = ["NSArray", "NSGeometry", "NSHost", "NSNotification", "NSObject", "NSOperation", "NSString", "NSThread", "NSURL", "block2"] }
Expand Down
178 changes: 178 additions & 0 deletions src-tauri/src/scan_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,19 @@ mod platform {
pub struct Host {
panels: Vec<Retained<NSPanel>>,
last_rects: Vec<crate::scanning::PaintedRect>,
last_title: Option<(String, Rect, f64)>,
title: Option<(
Retained<objc2_app_kit::NSView>,
Retained<objc2_app_kit::NSTextField>,
)>,
}
impl Host {
pub fn new() -> Result<Self, String> {
Ok(Self {
panels: vec![],
last_rects: vec![],
title: None,
last_title: None,
})
}
pub fn render(&mut self, rects: &[crate::scanning::PaintedRect]) -> Result<(), String> {
Expand Down Expand Up @@ -180,9 +187,83 @@ mod platform {
self.last_rects = rects.to_vec();
Ok(())
}
pub fn menu_title(&mut self, text: &str, rect: Rect, scale: f64) -> Result<(), String> {
use objc2_app_kit::{
NSFont, NSFontWeightSemibold, NSTextAlignment, NSTextField, NSView,
};
use objc2_foundation::NSString;
if self
.last_title
.as_ref()
.is_some_and(|(old_text, old_rect, old_scale)| {
old_text == text && *old_rect == rect && *old_scale == scale
})
{
return Ok(());
}
let mtm = MainThreadMarker::new().ok_or("Menu title requires the main thread.")?;
self.render(&[crate::scanning::PaintedRect {
rect,
color: [30, 35, 46],
opacity: 0,
role: crate::scanning::VisualRole::Accent,
}])?;
let bounds = NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(rect.width, rect.height));
if self.title.is_none() {
let view = NSView::initWithFrame(NSView::alloc(mtm), bounds);
view.setWantsLayer(true);
let label = NSTextField::wrappingLabelWithString(&NSString::from_str(text), mtm);
label.setTextColor(Some(&NSColor::whiteColor()));
label.setAlignment(NSTextAlignment::Center);
view.addSubview(&label);
self.title = Some((view, label));
}
let (view, label) = self.title.as_ref().unwrap();
view.setFrame(bounds);
let layer = view
.layer()
.ok_or("Menu title background is unavailable.")?;
layer.setBackgroundColor(Some(
&NSColor::colorWithSRGBRed_green_blue_alpha(
30.0 / 255.0,
35.0 / 255.0,
46.0 / 255.0,
1.0,
)
.CGColor(),
));
layer.setCornerRadius(10.0 * scale);
let value = NSString::from_str(text);
if label.stringValue() != value {
label.setStringValue(&value);
}
label.setFont(Some(&NSFont::systemFontOfSize_weight(
18.0 * scale,
unsafe { NSFontWeightSemibold },
)));
let width = (rect.width - 24.0 * scale).max(1.0);
let measured = label
.cell()
.ok_or("Menu title text is unavailable.")?
.cellSizeForBounds(NSRect::new(
NSPoint::new(0.0, 0.0),
NSSize::new(width, rect.height),
));
let height = measured.height.min(rect.height);
label.setFrame(NSRect::new(
NSPoint::new(12.0 * scale, (rect.height - height) / 2.0),
NSSize::new(width, height),
));
if self.panels[0].contentView().as_deref() != Some(view.as_ref()) {
self.panels[0].setContentView(Some(view));
}
self.last_title = Some((text.to_owned(), rect, scale));
Ok(())
}
pub fn prompt(&mut self, text: &str, rect: Rect, scale: f64) -> Result<(), String> {
use objc2_app_kit::{NSFont, NSTextField};
use objc2_foundation::NSString;
self.last_title = None;
let mtm = MainThreadMarker::new().ok_or("Prompt requires the main thread.")?;
self.render(&[crate::scanning::PaintedRect {
rect,
Expand Down Expand Up @@ -243,6 +324,7 @@ mod platform {
Ok(())
}
pub fn hide(&mut self) {
self.last_title = None;
self.last_rects.clear();
for panel in &self.panels {
panel.orderOut(None);
Expand Down Expand Up @@ -360,3 +442,99 @@ pub fn close_foreground_window() -> Result<(), String> {
}
Ok(())
}

impl Host {
pub fn label(
&mut self,
label: &crate::scanning::FrameLabel,
menu_title: Option<MenuTitle>,
) -> Result<(), String> {
#[cfg(target_os = "macos")]
if let Some(MenuTitle { rect, scale }) = menu_title {
return self.menu_title(&label.text, rect, scale);
}
#[cfg(not(target_os = "macos"))]
if let Some(MenuTitle { rect, scale }) = menu_title {
let _ = (rect, scale);
}
self.prompt(&label.text, label.rect, label.scale)
}
}

#[derive(Clone, Copy)]
pub struct MenuTitle {
rect: Rect,
scale: f64,
}

pub fn menu_title_geometry(
label: &crate::scanning::FrameLabel,
tiles: &[crate::scanning::FrameTile],
) -> Option<MenuTitle> {
let first = tiles.first()?;
let left = tiles
.iter()
.map(|tile| tile.rect.x)
.fold(f64::INFINITY, f64::min);
let right = tiles
.iter()
.map(|tile| tile.rect.x + tile.rect.width)
.fold(f64::NEG_INFINITY, f64::max);
Some(MenuTitle {
rect: Rect {
x: left,
width: right - left,
..label.rect
},
scale: first.scale,
})
}

#[cfg(test)]
mod title_tests {
use super::*;
use crate::scan_menu::{Kind, Menu};

#[test]
fn titles_follow_tile_edges_without_changing_header_spacing() {
for screen in [
Rect {
x: 0.0,
y: 0.0,
width: 320.0,
height: 240.0,
},
Rect {
x: -2048.0,
y: -100.0,
width: 2048.0,
height: 1109.0,
},
] {
for units in [1.0, 2.0] {
let mut menu = Menu::new(Kind::Actions, 500);
for paused in [false, true] {
menu.suspended = paused;
let frame = menu.frame((0, 0), screen, units);
let label = frame.label.as_ref().unwrap();
let MenuTitle { rect, scale } =
menu_title_geometry(label, &frame.tiles).unwrap();
assert_eq!(rect.x, frame.tiles[0].rect.x);
let last = &frame.tiles[2].rect;
assert!((rect.x + rect.width - last.x - last.width).abs() < 0.001);
assert_eq!(rect.y, label.rect.y);
assert_eq!(rect.height, label.rect.height);
assert!(
(frame.tiles[0].rect.y - rect.y - rect.height - 8.0 * scale).abs() < 0.001
);
assert!(rect.x >= screen.x && rect.x + rect.width <= screen.x + screen.width);
assert!(rect.y >= screen.y && rect.y + rect.height <= screen.y + screen.height);
if paused {
assert_eq!(label.text, "Select to resume");
}
assert!(menu_title_geometry(label, &[]).is_none());
}
}
}
}
}
11 changes: 7 additions & 4 deletions src-tauri/src/scanning_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ fn render<A: Adapter>(
}
})?;
render_tiles(&frame.tiles)?;
render_label(frame.label_for_prompt(prompt.is_some()))
render_label(frame.label_for_prompt(prompt.is_some()), &frame.tiles)
}
fn tick<A: Adapter>(app: &AppHandle) {
let c = app.state::<Controller<A>>();
Expand Down Expand Up @@ -656,7 +656,10 @@ pub fn install<A: Adapter>(app: &AppHandle) {
});
}

fn render_label(label: Option<&crate::scanning::FrameLabel>) -> Result<(), String> {
fn render_label(
label: Option<&crate::scanning::FrameLabel>,
tiles: &[crate::scanning::FrameTile],
) -> Result<(), String> {
LABEL.with(|slot| {
let mut host = slot.borrow_mut();
if let Some(label) = label {
Expand All @@ -665,15 +668,15 @@ fn render_label(label: Option<&crate::scanning::FrameLabel>) -> Result<(), Strin
}
host.as_mut()
.unwrap()
.prompt(&label.text, label.rect, label.scale)?;
.label(label, crate::scan_host::menu_title_geometry(label, tiles))?;
} else if let Some(host) = host.as_mut() {
host.hide();
}
Ok(())
})
}
fn hide_prompt() {
let _ = render_label(None);
let _ = render_label(None, &[]);
hide_hold_prompt();
}
fn hide_hold_prompt() {
Expand Down