mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
215 lines
7.7 KiB
Rust
215 lines
7.7 KiB
Rust
//! The HUD on macOS: placed on the screen with the pointer, below the menu
|
|
//! bar, in it, or out of the notch (`hud_placement`), from `NSScreen`'s
|
|
//! metrics. The main thread's pump hands it the listener's events.
|
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
use objc2::MainThreadMarker;
|
|
use objc2_app_kit::{NSEvent, NSScreen, NSStatusBar};
|
|
use objc2_foundation::{NSNumber, NSRect, NSString};
|
|
use rune::{DisplayTarget, OverlayHandle, OverlayPlacement};
|
|
|
|
use super::{Hud, HudEvent, HudSender, LIVE_TEXT_GAP, WINDOW, set_live_text};
|
|
use crate::app_settings::IndicatorPosition;
|
|
use crate::rune_hud::hud_placement;
|
|
use crate::rune_ui::live_text::Spot;
|
|
|
|
/// `NSStatusWindowLevel`: over other apps' windows, under the menu bar.
|
|
const STATUS_LEVEL: isize = 25;
|
|
|
|
/// The screen under the pointer (else the main one) as `hud_placement`
|
|
/// reads it, the primary screen's top, which Rune's global space hangs
|
|
/// from, the screen's backing scale, and its `NSScreenNumber`, which Rune's
|
|
/// `NativeDisplayId::MacOS` pins an overlay to.
|
|
pub(crate) fn pointer_screen(
|
|
mtm: MainThreadMarker,
|
|
) -> Option<(hud_placement::Screen, f64, f64, Option<u32>)> {
|
|
let pointer = NSEvent::mouseLocation();
|
|
let screens = NSScreen::screens(mtm);
|
|
let primary = screens.iter().next()?;
|
|
// The half-open rule Rune resolves `UnderPointer` with (closed at a
|
|
// screen's top, open at its bottom), so the capsule's panel and the
|
|
// notch it hangs from pick the same display where two share an edge.
|
|
// The fallback stays the main screen: Rune's "previous display" would
|
|
// need state the HUD's placement doesn't keep.
|
|
let screen = screens
|
|
.iter()
|
|
.find(|screen| {
|
|
let frame = screen.frame();
|
|
pointer.x >= frame.origin.x
|
|
&& pointer.x < frame.origin.x + frame.size.width
|
|
&& pointer.y > frame.origin.y
|
|
&& pointer.y <= frame.origin.y + frame.size.height
|
|
})
|
|
.or_else(|| NSScreen::mainScreen(mtm))?;
|
|
let rect = |r: NSRect| hud_placement::Rect {
|
|
x: r.origin.x,
|
|
y: r.origin.y,
|
|
width: r.size.width,
|
|
height: r.size.height,
|
|
};
|
|
let metrics = hud_placement::Screen {
|
|
frame: rect(screen.frame()),
|
|
visible: rect(screen.visibleFrame()),
|
|
safe_top: screen.safeAreaInsets().top,
|
|
aux_left: screen.auxiliaryTopLeftArea().size.width,
|
|
aux_right: screen.auxiliaryTopRightArea().size.width,
|
|
bar: NSStatusBar::systemStatusBar().thickness(),
|
|
};
|
|
Some((
|
|
metrics,
|
|
primary.frame().origin.y + primary.frame().size.height,
|
|
screen.backingScaleFactor(),
|
|
screen_number(&screen),
|
|
))
|
|
}
|
|
|
|
/// The screen's `NSScreenNumber` (`CGDirectDisplayID`), the id Rune's
|
|
/// `NativeDisplayId::MacOS` matches: winit reports a monitor's native id
|
|
/// the same way (`CGDisplayGetDisplayIDFromUUID`), so a measured notch can
|
|
/// pin the panel to the display it was read on.
|
|
fn screen_number(screen: &NSScreen) -> Option<u32> {
|
|
let description = screen.deviceDescription();
|
|
let key = NSString::from_str("NSScreenNumber");
|
|
let number = description.objectForKey(&key)?;
|
|
Some(number.downcast_ref::<NSNumber>()?.unsignedIntValue())
|
|
}
|
|
|
|
/// The HUD's window and widest plate, and how far its island
|
|
/// spreads past the notch.
|
|
fn content(island_spread: f32) -> hud_placement::Content {
|
|
let (width, height) = WINDOW;
|
|
hud_placement::Content {
|
|
window: (f64::from(width), f64::from(height)),
|
|
plate: (
|
|
f64::from(width) - 24.0,
|
|
f64::from(crate::rune_hud::dot_scope::PLATE_HEIGHT),
|
|
),
|
|
island_spread: f64::from(island_spread),
|
|
}
|
|
}
|
|
|
|
/// The live text's spot just past `placed`'s plate, in Rune's global space.
|
|
fn live_text_beside(
|
|
placed: &hud_placement::Placed,
|
|
screen: &hud_placement::Screen,
|
|
content: hud_placement::Content,
|
|
primary_top: f64,
|
|
) -> Spot {
|
|
let (bottom, margin) =
|
|
hud_placement::beside_plate(placed, screen, content, f64::from(LIVE_TEXT_GAP));
|
|
let y = if bottom {
|
|
screen.frame.y + margin
|
|
} else {
|
|
screen.frame.y + screen.frame.height - margin
|
|
};
|
|
Spot::Global {
|
|
x: (placed.top_left.0 + content.window.0 / 2.0 + f64::from(placed.layout.center[0])) as f32,
|
|
y: (primary_top - y) as f32,
|
|
below: !bottom,
|
|
}
|
|
}
|
|
|
|
/// The pump's handle on the HUD (main thread).
|
|
pub(crate) struct DictationIndicatorUi {
|
|
hud: HudSender,
|
|
}
|
|
|
|
impl DictationIndicatorUi {
|
|
pub(crate) fn new(hud: HudSender) -> Self {
|
|
Self { hud }
|
|
}
|
|
|
|
pub(crate) fn handle(&mut self, event: HudEvent) {
|
|
self.hud.send(event);
|
|
}
|
|
|
|
/// Whether the HUD wants the display's frames: the pump ticks every
|
|
/// frame only while it does.
|
|
pub(crate) fn animating(&self) -> bool {
|
|
self.hud.animating.load(Ordering::Acquire)
|
|
}
|
|
|
|
/// Keeps a showing HUD on the pointer's screen, and hides it when the
|
|
/// indicator is set to Hidden.
|
|
pub(crate) fn follow_pointer(&mut self) {
|
|
if !self.hud.animating.load(Ordering::Acquire) {
|
|
return;
|
|
}
|
|
self.hud.overlay.update_if(|hud, _| {
|
|
if hud.shown {
|
|
hud.show();
|
|
}
|
|
false
|
|
});
|
|
}
|
|
}
|
|
|
|
impl Hud {
|
|
/// With the HUD hidden, the live text starts where its plate would sit
|
|
/// under the menu bar.
|
|
pub(super) fn place_live_text_without_hud(&self) {
|
|
let Some((screen, primary_top, _, _)) = MainThreadMarker::new().and_then(pointer_screen)
|
|
else {
|
|
return;
|
|
};
|
|
let content = content(0.0);
|
|
let placed = hud_placement::place(Default::default(), &screen, content, false);
|
|
let (top, _) = hud_placement::plate_span(&placed, content);
|
|
set_live_text(Spot::Global {
|
|
x: (screen.frame.x + screen.frame.width / 2.0) as f32,
|
|
y: (primary_top - top) as f32,
|
|
below: true,
|
|
});
|
|
}
|
|
|
|
pub(super) fn place_on_pointer_screen(&mut self, overlay: &OverlayHandle<Hud>) {
|
|
let Some((mut metrics, primary_top, _, _)) =
|
|
MainThreadMarker::new().and_then(pointer_screen)
|
|
else {
|
|
return;
|
|
};
|
|
let tuning = self.model.tuning();
|
|
if tuning.pretend_notch {
|
|
metrics = metrics.pretending_notch();
|
|
}
|
|
let (width, height) = WINDOW;
|
|
let content = content(tuning.island_spread);
|
|
// Labs' notch indicator takes any shown position on a notched screen;
|
|
// Hidden and flat screens leave HUD Lab's placement standing.
|
|
let ears = hud_placement::notch_placement(
|
|
crate::labs::notch_indicator(),
|
|
self.position,
|
|
&metrics,
|
|
content,
|
|
);
|
|
let placed = ears.unwrap_or_else(|| {
|
|
hud_placement::place(
|
|
tuning.placement,
|
|
&metrics,
|
|
content,
|
|
self.position == IndicatorPosition::Bottom,
|
|
)
|
|
});
|
|
set_live_text(live_text_beside(&placed, &metrics, content, primary_top));
|
|
if self.placed == Some(placed) {
|
|
return;
|
|
}
|
|
if self.placed.map(|old| old.over_menu_bar) != Some(placed.over_menu_bar) {
|
|
// Above the menu bar and its status items, below their menus.
|
|
overlay.set_macos_level(STATUS_LEVEL + isize::from(placed.over_menu_bar));
|
|
}
|
|
self.placed = Some(placed);
|
|
self.layout = placed.layout;
|
|
self.model.set_island(placed.layout.island);
|
|
// Rune's global rect: points from the primary screen's top-left, y down.
|
|
let (x, top) = placed.top_left;
|
|
overlay.set_display(DisplayTarget::Primary);
|
|
overlay.set_placement(Some(OverlayPlacement::Rect {
|
|
x: x as f32,
|
|
y: (primary_top - top) as f32,
|
|
width,
|
|
height,
|
|
}));
|
|
}
|
|
}
|